Requirements

Build

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

javac -cp relational-database-example-1.3.1.jar fr/lirmm/graphik/graal/examples/relational_db/NaturalRdbmsExample.java

Run the example

Execute the following command:

java -cp relational-database-example-1.3.1.jar fr/lirmm/graphik/graal/examples/relational_db/NaturalRdbmsExample ./data/

What does this example do?

  1. Create a KBBuilder:
    
      KBBuilder kbb = new KBBuilder();
        
  2. Connect it to an SQLite database:
    
      kbb.setStore(new NaturalRDBMSStore(new SqliteDriver(new File(rootDir, dbFilepath))));
        
  3. Load an ontology from lubm-ex-20.dlp file:
    
      kbb.addRules(new DlgpParser(new File(rootDir, ontoFilepath)));
        
  4. Specify the approach to use to query answering (here priority to query rewriting):
    
      kbb.setApproach(Approach.REWRITING_FIRST);
        
  5. Generate a knowledge base with this database and ontology:
    
      KnowledgeBase kb = kbb.build();
        
  6. Create a DlgpWriter to print the results:
    
      writer = new DlgpWriter();
        
  7. Parse and print a query:
    
      ConjunctiveQuery query = DlgpParser.parseQuery("?(X, Y1, Y2) :- "
          + " <Professor>(X),                                         "
          + " worksFor(X, <http://www.Department0.University0.edu>),  "
          + " name(X, Y1),                                            "
          + " emailAddress(X, Y2).");
      writer.write("\n= Query =\n");
      writer.write(query);
        
  8. Query the KB and print the answers:
    
      writer.write("\n= Answers =\n");
      CloseableIterator results = kb.query(query);
      if (results.hasNext()) {
      	do {
      		writer.write(results.next());
      	} while (results.hasNext());
      } else {
      	writer.write("No answers.\n");
      }
        
  9. Close resources:
    
      results.close();
      writer.close();
      kb.close();