1

Here is my code:

public class FunctionalityCheckTest1 {

    InfModel infModel;
    Model model = ModelFactory.createDefaultModel();
    String NS = "http://myweb.com/vocab#";

    @Test
    public void playingWithJenaReasoner()
    {
        Resource alex = this.model.createResource(NS+"Alex");
        Resource bob = this.model.createResource(NS+"Bob");
        Resource alice = this.model.createResource(NS+"Alice");
        Property isFriendOf = this.model.createProperty(NS,"isFriendOf");
        alex.addProperty(isFriendOf,bob);
        bob.addProperty(isFriendOf,alice);
        StmtIterator stmtIterator1 = this.model.listStatements();
        while (stmtIterator1.hasNext())
        {
            System.out.println(stmtIterator1.next());
        }

        String customRule = "@prefix vocab: <http://myweb.com/vocab#>. " +
                "[rule1: (?a vocab:isFriendOf ?b) (?b vocab:isFriendOf ?c) -> (?a vocab:isFriendOf ?c) ]";

        List<Rule> rules = new ArrayList<>();
        rules.add(Rule.parseRule(customRule));

        GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
        reasoner.setDerivationLogging(false);
        this.infModel = ModelFactory.createInfModel(reasoner, this.model);
        StmtIterator stmtIterator2 = this.infModel.listStatements();
        while (stmtIterator2.hasNext())
        {
            System.out.println(stmtIterator2.next());
        }
    }

}

On executing playingWithJenaReasoner() function it throws error:
com.hp.hpl.jena.reasoner.rulesys.Rule$ParserException: Expected '(' at start of clause, found vocab:
from line rules.add(Rule.parseRule(customRule));

While everything works fine if I add these changes to above code

PrintUtil.registerPrefix("vocab",NS);
String customRule = "[rule1: (?a vocab:isFriendOf ?b) (?b vocab:isFriendOf ?c) -> (?a vocab:isFriendOf ?c) ]";

So whats wrong with this

String customRule = "@prefix vocab: <http://myweb.com/vocab#>. " +
                    "[rule1: (?a vocab:isFriendOf ?b) (?b vocab:isFriendOf ?c) -> (?a vocab:isFriendOf ?c) ]";

In this Jena Documentation,they have mentioned @prefix with rule . Where am I doing wrong?

Badman
  • 407
  • 5
  • 17

1 Answers1

2

I ran into the same problem you had today and it seems like the method

public static List<Rule> parseRules(String source)

does not allows for prefixes in the string. I'm not sure if this is a bug or a feature of this method.

However if you state your rules in a rule file and load it via the

public static List<Rule> rulesFromURL(String uri)

You should be able to load rules that include prefixes.

Here is a small example to test if this works. It assumes that you have your ontology stored somewhere on the file system and a rule file with the name jena.rule in your classpath:

public class JenaRuleTest {

    public static void main(String[] args) throws UnsupportedEncodingException {

        OntModel model = ModelFactory.createOntologyModel();        
        model.read("C:\\path\\to\\ontology\\ontology.ttl");     

        String ruleResourceStr = JenaRuleTest.class.getResource("/jena.rule").toString();

        Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(ruleResourceStr));
        reasoner.setDerivationLogging(true);

        InfModel inf = ModelFactory.createInfModel(reasoner, model);        
        inf.write(System.out, "TURTLE");        
    }
}

A more elaborate example how to do it can be found here: http://tutorial-academy.com/jena-reasoning-with-rules/

Documentation for the Rule class can be found here: https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/reasoner/rulesys/Rule.html

Hope that helps if someone faces this issue.

Greetings

Scyla101
  • 230
  • 3
  • 18