jenabuilt-injena-rules

Jena list statements with print builtin rule


I am trying to learn how Jena and GenericRuleReasoner works. I took this from Apache Jena tutorial.

[transitiveRule: (?A demo:p ?B), (?B demo:p ?C) -> (?A > demo:p ?C) ]
[symmetricRule: (?Y demo:p ?X) -> (?X demo:p ?Y) ]

If I want to add another statement

[testPrintRule: (?X demo:p ?Y) -> print(?Y, "for testing")] //Print only this

Is it possible for me to only list statement in print command? The print statement is printed in console. I want to display in my UI.

This is what I am currently doing in Jena to get the triples. I am sorry if I am wrong, I am a beginner.

Property p = ReadOntology.model.getProperty(ns + "demo:p");
List rules = Rule.rulesFromURL(FileLocations.getRulesLoc());
Reasoner reasoner = new GenericRuleReasoner( rules );
InfModel infModel = ModelFactory.createInfModel( reasoner, ReadOntology.model);

StmtIterator train = infModel.listStatements(null, p, (RDFNode)null);

Solution

  • Ok, if I understand you correctly you have 2 questions:

    (1) you want to know whether you can use rules to print your triples, and

    (2) you want to know whether you can write stuff from a Jena rule to you GUI.

    Answer (1)

    You can print your triple via a rule like

    [rulePrintTriples: (?s ?p ?o) -> print(?s, ?p, ?o)]
    

    though I do not think you should do that because it is likely to be very inefficient. Besides, Jena already has a way to do this easily, i.e.:

    RDFDataMgr.write(System.out, model, RDFFormat.TTL);
    

    Answer (2)

    Yes, you could have a rule to write to your GUI, but for that you will have to create your own builtin type as explained here. Assuming for your GUI you are using some sort of MVC pattern you can update the model of your GUI when the rule is triggered with your custom rule code.

    Again it may be questionable whether you want to do that since it may be brittle in the case where you GUI has not been initialized yet. A more robust approach would to add the triples to the Jena model and later, say when your GUI is initialized, run a SPARQL query against the Jena model and populate your GUI in that way.

    I have written about Jena rules here and here.