Pellet reasoning in SWRL rules in the Jena Framework

I am trying to use the Jena framework to edit an existing ontology built using Protoge 4.2. those. change property values ​​or add individuals or classes, and then do reasoning. Suppose that in the ontology we have such a rule that: hasAge (? P,? Age) ^ swrlb: moreThan (? Age, 18) → Adult (? P). I would like to be able to change the hasAge property on the side of Yen and see if someone is an adult or not. Could you provide me some sample code? Any help is appreciated.

+3
source share
1 answer

Assuming that:

  • You know how to populate your model by reading in the ontology that you built.
  • Pellet
  • IRI ,

x-test://individual , , SWIRL, .

// create an empty ontology model using Pellet spec
final OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );   

// read the file
model.read( ont );

// Grab a resource and and property, and then set the property on that individual
final Resource Adult = ResourceFactory.createResource("x-domain://Adult");
final Property hasAge = ResourceFactory.createProperty("x-domain://hasAge");
final Resource res = model.createResource("x-test://individual");
res.addLiteral(hasAge, 19);

// Test that the swirl rule has executed
assert( res.hasProperty(RDF.type, Adult) );
0

All Articles