Requirements

Build

Open a terminal and go into the extracted folder. Then, execute the following command:

javac -cp rewriting-example-1.3.1.jar fr/lirmm/graphik/graal/examples/rewriting/Rewriting.java

Run the example

Execute the following command:

java -cp rewriting-example-1.3.1.jar fr/lirmm/graphik/graal/examples/rewriting/Rewriting ./data/

What does this example do?

  1. Create a DLGP writer to display the rewritings:
    
        DlgpWriter writer = new DlgpWriter();
        
  2. Define some prefixes:
    
        writer.write(new Prefix("", "http://ksg.meraka.co.za/adolena.owl#"));
        writer.write(new Prefix("NAP", "file:///home/aurona/0AlleWerk/Navorsing/Ontologies/NAP/NAP#"));
        
  3. Load rules from a DLGP File:
    
        File f = new File(rootDir, "A.dlp");
        Ontology onto = new DefaultOntology(new DlgpParser(f));
        
  4. Create a query from a Java string:
    
        ConjunctiveQuery query = DlgpParser.parseQuery("@prefix :  "
        		+ "@prefix NAP: <file:///home/aurona/0AlleWerk/Navorsing/Ontologies/NAP/NAP#> "
        		+ "?(X0) :- NAP:Device(X0), :assistsWith(X0, X1).");
        
  5. Print the query:
    
        writer.write("\n= Query =\n");
        writer.write(query);
        
  6. Initialize the rewriter:
    
        QueryRewriter rewriter = new PureRewriter();
        CloseableIteratorWithoutException it = rewriter.execute(query, onto);
        
  7. Rewrite the query and display the rewritings:
    
        writer.write("\n= Rewritings =\n");
        while (it.hasNext()) {
        	writer.write(it.next());
        	writer.flush();
        }
        it.close();
        
  8. We will now show how compile a part of your ontology to produce "pivotal" rewritings according to this compilation. Then, we will unfold these pivotal rewritings to obtain classical rewritings (note that pivotal rewritings could also be evaluated without generating classical rewritings).

  9. Compile a part of the ontology (so-called compilable rules):
    
        RulesCompilation compilation = new IDCompilation();
        compilation.compile(onto.iterator());
        
  10. Initialize the rewriter with unfolding disabled (ie we don't want classical rewritings but rather pivotal rewritings):
    
        PureRewriter pure = new PureRewriter(false);
        
  11. Rewrite the query according to the specified ontology and compilation:
    
        it = pure.execute(query, onto, compilation);
        
  12. Save the (pivotal) rewritings in a UnionOfConjunctiveQueries object:
    
        UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(query.getAnswerVariables(), it);
        
  13. Print this (pivotal) UCQ:
    
        writer.write("\n= Pivotal Rewritings =\n");
        writer.write(ucq);
        
  14. Unfold the pivotal rewritings to get classical rewritings:
    
        writer.write("\n= Unfolded Rewritings =\n");
        it = PureRewriter.unfold(ucq, compilation);
        while (it.hasNext()) {
        	writer.write(it.next());
        	writer.flush();
        }
        it.close();
        
  15. Close resources:
    
        writer.close();