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.
Dave
so you can't use the OntModel methods (that would
otherwise do exactly what you want):
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.ProfileException;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class MaxCardinality {
public static void main(String[] args) {
// Create an Ont Model
String ns = "http://www.example.com/ontology#";
OntModel model = ModelFactory.createOntologyModel(
OntModelSpec.OWL_DL_MEM );
// Assert that a game has at most 4 players
OntClass game = model.createClass( ns+"Game" );
OntClass player = model.createClass( ns+"Player" );
OntProperty hasPlayer = model.createOntProperty( ns+"hasPlayer"
);
OntClass restriction;
try {
restriction = model.createMaxCardinalityQRestriction(
null,
hasPlayer, 4, player );
}
catch ( ProfileException e ) {
System.err.println( "Couldn't create the restriction."
);
e.printStackTrace();
return;
}
game.addSuperClass( restriction );
model.write( System.out, "RDF-XML/ABBREV" );
}
}
If you try that code, you'll get the output:
Couldn't create the restriction.
com.hp.hpl.jena.ontology.ProfileException: Attempted to use language
construct MAX_CARDINALITY_Q that is not supported in the current
language profile: OWL DL
at
com.hp.hpl.jena.ontology.impl.OntModelImpl.checkProfileEntry(OntModelImpl.java:3044)
at
com.hp.hpl.jena.ontology.impl.OntModelImpl.createMaxCardinalityQRestriction(OntModelImpl.java:1748)
at MaxCardinality.main(MaxCardinality.java:19)
However, if you've already got the ontology that you need in RDF/XML,
and can load it into Jena, you can get the restriction object from the
serialization. (However, given that OWL can be encoded into RDF in
various ways, this might not work in all cases). Here's code that
does just that. (The output includes a serialization of the model, so
you can see what the data looked like:
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.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"
);
model.write( System.out, "RDF/XML-ABBREV" );
// 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.
for ( ResIterator it = model.listSubjectsWithProperty(
OWL2.maxQualifiedCardinality ); it.hasNext() ; ) {
// Making r a restriction lets us use
Restriction#getOnProperty as
in (i), but
// we could also just make r a resource and use
Resource#getPropertyValue(),
// as in (2,3,4).
Restriction r = it.next().as( Restriction.class );
System.out.println( "(1) on property: " +
r.getOnProperty() );
System.out.println( "(2) on property: " +
r.getPropertyValue(
OWL2.onProperty ));
System.out.println( "(3) on class: " +
r.getPropertyValue(
OWL2.onClass ));
System.out.println( "(4) max cardinality: " +
r.getPropertyValue(
OWL2.maxQualifiedCardinality ));
}
}
}
<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>
</rdf:RDF>
(1) on property: http://www.example.com/ontology#hasPlayer
(2) on property: http://www.example.com/ontology#hasPlayer
(3) on class: http://www.example.com/ontology#Player
(4) max cardinality: 4^^http://www.w3.org/2001/XMLSchema#nonNegativeInteger
Hope this helps!
//JT