Getting Started with Graal without IDE

  1. Download and install Maven
  2. Create a new Maven Project
    1. Open a terminal
    2. Copy/Paste and execute the following command:
      
           mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-graal-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
           
  3. Configure your project
    1. Go to the my-graal-app folder
    2. Edit the pom.xml file
    3. Insert the following dependencies in the "dependencies" tag (above the "junit" dependency):
      
          <dependency>
            <groupId>fr.lirmm.graphik</groupId>
            <artifactId>graal-core</artifactId>
            <version>1.3.1</version>
          </dependency>
          <dependency>
            <groupId>fr.lirmm.graphik</groupId>
            <artifactId>graal-io-dlgp</artifactId>
            <version>1.3.1</version>
          </dependency>
          <dependency>
            <groupId>fr.lirmm.graphik</groupId>
            <artifactId>graal-kb</artifactId>
            <version>1.3.1</version>
          </dependency>
          
    4. Save the file
  4. Create your first Graal program
    1. Open the src/main/java/com/mycompany/app/App.java file
    2. Replace the main method by the following one:
      
      	public static void main(String[] args) throws Exception {
      		// 0 - Create a KBBuilder
      		KBBuilder kbb = new KBBuilder();
      		// 1 - Add a rule
      		kbb.add(DlgpParser.parseRule("mortal(X) :- human(X)."));
      		// 2 - Add a fact
      		kbb.add(DlgpParser.parseAtom("human(socrate)."));
      		// 3 - Generate the KB
      		KnowledgeBase kb = kbb.build();
      		// 4 - Create a DLGP writer to print data
      		DlgpWriter writer = new DlgpWriter();
      		// 5 - Parse a query from a Java String
      		ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- mortal(X).");
      		// 6 - Query the KB
      		CloseableIterator resultIterator = kb.query(query);
      		// 7 - Iterate and print results
      		writer.write("\n= Answers =\n");
      		if (resultIterator.hasNext()) {
      			do {
      				writer.write(resultIterator.next());
      				writer.write("\n");
      			} while (resultIterator.hasNext());
      		} else {
      			writer.write("No answers.\n");
      		}
      		// 8 - Close resources
      		kb.close();
      		writer.close();
      	}
          
    3. Insert all needed imports:
      
          import fr.lirmm.graphik.graal.api.core.ConjunctiveQuery;
          import fr.lirmm.graphik.graal.api.kb.KnowledgeBase;
          import fr.lirmm.graphik.graal.io.dlp.DlgpParser;
          import fr.lirmm.graphik.graal.io.dlp.DlgpWriter;
          import fr.lirmm.graphik.graal.kb.KBBuilder;
          import fr.lirmm.graphik.util.stream.CloseableIterator;
       
  5. Run App:
    1. Build App by executing the following command:
      
          mvn package
        
    2. Run it by executing the following command:
      
          mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
        
  6. Do not care about following SLF4J warning (see SLF4J page to disable this warning):
             SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
             SLF4J: Defaulting to no-operation (NOP) logger implementation
             SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
        

    You are ready to work with Graal