On Sun, Mar 24, 2013 at 3:00 PM, Dave Reynolds
<[email protected]> wrote:
> On 24/03/13 17:27, Joshua TAYLOR wrote:
>>
>> On Sat, Mar 23, 2013 at 11:41 AM, Luis Eufrasio Teixeira Neto
>> <[email protected]> wrote:
>>>
>>> does anybody could send me an example of an ontology having a max
>>> cardinality restriction over an objecct property and a sample jena code
>>> for
>>> readind the value of the max cardinality, please?
>>
>>
>> MaxCardinality Restrictions are an OWL2 feature, and Jena's OntModels
>> don't support OWL2,
>
>
> maxCardinality is OWL1 and supported by OntModels.
>
> maxQualifiedCardinality is indeed OWL2 and not supported other than by
> manually creating/parsing at the RDF level, as you say.
Yes, sorry for the imprecision (and any resulting confusion)! Here's
some updated code that shows the OntModel based way of doing this for
non-qualified max cardinality restrictions. One point to note is that
if the OntModel API is used, there's no need to convert the
cardinality value, which is a typed literal if accessed directly, to a
Java numeric type.
import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.Restriction;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.OWL2;
public class ReadMaxCardinality {
public static void main(String[] args) {
// Create an OntModel, read the contents in from disk, and
write the
// contents so that everyone can see.
OntModel model = ModelFactory.createOntologyModel();
model.read(
"file:///home/taylorj/Documents/ontologies/ontology/ontology.owl"
);
System.out.println( "== Model ==" );
model.write( System.out, "RDF/XML-ABBREV" );
// If you don't have to work with quantified max cardinality
restrictions (that is, you've only
// got, for instance, "hasPlayers max 3", and not "hasPlayers
max 3
Player"), then you can use
// the Restriction and MaxCardinality interfaces. Note
MaxCardinalityRestriction#getMaxCardinality
// returns an int (2a), but that accessing the property directly
yields a typed literal (2b).
System.out.println( "== Non-Q MaxRestrictions ==");
for ( ExtendedIterator<Restriction> rs =
model.listRestrictions();
rs.hasNext() ; ) {
Restriction r = rs.next();
if ( r.isMaxCardinalityRestriction() ) {
MaxCardinalityRestriction mcr =
r.asMaxCardinalityRestriction();
System.out.println( "(1) on property: " +
mcr.getOnProperty() );
System.out.println( "(2a) max cardinality: " +
mcr.getMaxCardinality() );
System.out.println( "(2b) max cardinality: " +
mcr.getPropertyValue( OWL.maxCardinality ));
}
}
// You'll need to find some way to identify the max cardinality
restrictions. I'm assuming that
// every max cardinality restriction will have a value for
OWL2.maxQualifiedCardinality, and that
// *only* max cardinality restrictions will have a value for
that peoperty.
System.out.println( "== Q MaxRestrictions ==" );
for ( ResIterator it = model.listSubjectsWithProperty(
OWL2.maxQualifiedCardinality ); it.hasNext() ; ) {
// Making r a restriction lets us use
Restriction#getOnProperty as
in (1a), but
// we could also just make r a resource and use
Resource#getPropertyValue(),
// as in (1b,2,3).
Restriction r = it.next().as( Restriction.class );
System.out.println( "(1a) on property: " +
r.getOnProperty() );
System.out.println( "(1b) on property: " +
r.getPropertyValue(
OWL2.onProperty ));
System.out.println( "(2) max cardinality: " +
r.getPropertyValue(
OWL2.maxQualifiedCardinality ));
System.out.println( "(3) on class: " +
r.getPropertyValue(
OWL2.onClass ));
}
}
}
Output:
== Model ==
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns="http://www.example.com/ontology#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.example.com/ontology"/>
<owl:Class rdf:about="http://www.example.com/ontology#Game">
<rdfs:subClassOf>
<owl:Restriction>
<owl:maxQualifiedCardinality
rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
>4</owl:maxQualifiedCardinality>
<owl:onClass>
<owl:Class rdf:about="http://www.example.com/ontology#Player"/>
</owl:onClass>
<owl:onProperty>
<owl:ObjectProperty
rdf:about="http://www.example.com/ontology#hasPlayer"/>
</owl:onProperty>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
<owl:Class rdf:about="http://www.example.com/ontology#Player">
<rdfs:subClassOf>
<owl:Restriction>
<owl:maxCardinality
rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
>3</owl:maxCardinality>
<owl:onProperty>
<owl:ObjectProperty
rdf:about="http://www.example.com/ontology#relatedTo"/>
</owl:onProperty>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
</rdf:RDF>
== Non-Q MaxRestrictions ==
(1) on property: http://www.example.com/ontology#relatedTo
(2a) max cardinality: 3
(2b) max cardinality: 3^^http://www.w3.org/2001/XMLSchema#nonNegativeInteger
== Q MaxRestrictions ==
(1a) on property: http://www.example.com/ontology#hasPlayer
(1b) on property: http://www.example.com/ontology#hasPlayer
(2) max cardinality: 4^^http://www.w3.org/2001/XMLSchema#nonNegativeInteger
(3) on class: http://www.example.com/ontology#Player
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/