Requirements

Build

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

javac -cp getting-started-with-kb-example-1.3.1.jar fr/lirmm/graphik/graal/examples/getting_started/GettingStarted.java

Run the example

Execute the following command:

java -cp getting-started-with-kb-example-1.3.1.jar fr/lirmm/graphik/graal/examples/getting_started/GettingStarted ./data/

What does this example do?

  1. Create a KBBuilder:
    
        KBBuilder kbb = new KBBuilder();
        
  2. Load an ontology and facts from a DLGP file:
    
        kbb.addAll(new DlgpParser(new File(rootDir, "animals.dlp")));
        
  3. Generate the KnowledgeBase, by default facts are stored in main memory:
    
        KnowledgeBase kb = kbb.build();
        
  4. Create a DLGP writer:
    
        writer = new DlgpWriter();
        
  5. Print the ontology in DLGP format:
    
        writer.write("\n= Ontology =\n");
        writer.write(kb.getOntology());
        
  6. Print the facts in DLGP format:
    
        writer.write("\n= Facts =\n");
        writer.write(kb.getFacts());
        
  7. Parse a query from a Java String and print it:
    
        ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- mammal(X).");
        writer.write("\n= Query =\n");
        writer.write(query);
        
  8. Query the KB, here the KB will use the RulesSetAnalyser module to analyze the rule base and select a query answering technique (ie saturation, rewriting or a combination of both) :
    
        CloseableIterator resultIterator = kb.query(query);
        
  9. Iterate and print the answers:
    
        if (resultIterator.hasNext()) {
        	do {
        		writer.write(resultIterator.next().toString());
        		writer.write("\n");
        	} while (resultIterator.hasNext());
        } else {
        	writer.write("No answers.\n");
        }
        
  10. Close resources:
    
        kb.close();
        writer.close();