1

I am trying to add a Jena rule submitted as string to the below method. However the rule is not being added as I can verify no new change on the ontology written to E://1_1_1 Could someone help me how to do this. Other questions on SO that may be related are 26292160, 349652; both of which are similar to my case.

public String ValidateAndConfigureRule(String string) {     

    try{
        GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.parseRules(string));
        Model oModel = m.getOntology();

        reasoner.setDerivationLogging(true);            
        reasoner.setOWLTranslation(true);
        reasoner.setTraceOn(true);
        reasoner.setTransitiveClosureCaching(true);

        InfModel inf = ModelFactory.createInfModel(reasoner, oModel);

        inf.write(new FileWriter("E://1_1_1"));

        Model baseModel = ModelFactory.createDefaultModel();            
        baseModel.add(inf);

        final OntModel model  = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, baseModel);          

        ValidityReport validity = model.validate();
    }
}

The rule itself is in the format [ r1: (?x :objProp1 :ind_x) -> (?x :objProp2 :ind_y) ] where objProp's are object properties and ind_x, ind_yare individuals along with the necessary prefix for ":" in the rule string.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
user3747396
  • 141
  • 12

2 Answers2

1

Andy's answer captures the most important parts here: when you define a rule-based inference model, the contents of the model are the asserted and inferred triples; the rules aren't written into the model. The rules are part of the reasoner, not the data.

This is actually in contrast to SWRL rules, which have defined serialization formats so that SWRL rules can be written alongside OWL data. You might consider using SWRL rules and a reasoner that supports them (e.g., Pellet, HermiT) instead of Jena rules, since the rules could be written along with the data.

As a workaround, you could also store the text of your Jena rules as the value of an annotation property on your OWL ontology and read them in again when you load the ontology. That is, you might end up with something like:

@prefix : <http://example.org/my-ontology/>

<http://example.org/my-ontology>
        a                owl:Ontology ;
        rdfs:comment     "My ontology with some Jena rules"@en ;
        :hasJenaRules    "...rule content here..." .

Then, when you load the ontology, you can check whether there are triples with the property :hasJenaRules, and if there is, you can take their objects, parse them as a Jena rules, and create a reasoner with the rules. I think that would be the easiest way to store your rules alongside your data with Jena.

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
0

When you write an InfModel it writes the base data - which has not changed. Rules don't get written.

AndyS
  • 16,345
  • 17
  • 21
  • Then, how can I add a jena rule to ontology model since m.getOntology gives ontology model to which I need to add the rule to – user3747396 Jan 05 '15 at 14:43