Using the PDDL Parser

The library has a parser of the PDDL language. The language respects the 3.1 standard. To get the exact BNF (Backus–Naur form) of the PDDL language implemented in the library you can type the command:

./gradlew jjdoc

To access to the documentation generated open the file ./build/docs/PDDL4J_BNF/lexer.html or simply by clicking on this link

Pre-requisite Installations

For this tutorial you need:

In the following, we will give the commands line so that the tutorial can be done independently of any IDE.

Step 1. Create a simple Java project with PDDL4J

First, open a terminal and create your development directory

mkdir PDDLParserExample

Then, create the sub-directories of your project

cd PDDLParserExample
mkdir -p src/fr/uga/pddl4j/examples
mkdir classes
mkdir lib

Finally, get the last binary of PDDL4J and save it in the lib directory

wget http://pddl4j.imag.fr/repository/pddl4j/binaries/pddl4j-4.0.0.jar
mv pddl4j-4.0.0.jar lib/pddl4j-4.0.0.jar

Step 2. Create the main class of our example

Create and edit a file called PDDLParserExample.java in the directory src/fr/uga/pddl4j/examples/. The skeleton of this class is given bellow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package fr.uga.pddl4j.examples;

import fr.uga.pddl4j.parser.DefaultParsedProblem;
import fr.uga.pddl4j.parser.ErrorManager;
import fr.uga.pddl4j.parser.Message;
import fr.uga.pddl4j.parser.Parser;

/**
 * The class is an example class. It shows how to use the library to create a PDDL parser and use it to parse PDDL
 * planning problem description.
 *
 * @author D. Pellier
 * @version 4.0 - 06.12.2021
 */
public class PDDLParserExample {

    /**
     * The main method the class. The first argument must be the path to the PDDL domain description and the second
     * argument the path to the PDDL problem description.
     *
     * @param args the command line arguments.
     */
    public static void main(final String[] args) {

        // Checks the number of arguments from the command line
        if (args.length != 2) {
            System.out.println("Invalid command line");
            return;
        }

        try {
            // Creates an instance of the PDDL parser
            final Parser parser = new Parser();
            // Parses the domain and the problem files.
            final DefaultParsedProblem parsedProblem = parser.parse(args[0], args[1]);
            // Gets the error manager of the parser
            final ErrorManager errorManager = parser.getErrorManager();
            // Checks if the error manager contains errors
            if (!errorManager.isEmpty()) {
                // Prints the errors
                for (Message m : errorManager.getMessages()) {
                    System.out.println(m.toString());
                }
            } else {
                // Prints that the domain and the problem were successfully parsed
                System.out.print("\nparsing domain file \"" + args[0] + "\" done successfully");
                System.out.print("\nparsing problem file \"" + args[1] + "\" done successfully\n\n");
                // Print domain and the problem parsed
                System.out.println(parsedProblem.toString());
            }
            // This exception could happen if the domain or the problem does not exist
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

Step 3. Compile and Run the example

To test the above code use the following command line to compile the example:

javac -d classes -cp classes:lib/pddl4j-4.0.0.jar src/fr/uga/pddl4j/examples/PDDLParserExample.java

and then the following command line to run the example:

java -cp classes:lib/pddl4j-4.0.0.jar fr.uga.pddl4j.examples.PDDLParserExample \\
    src/test/resources/benchmarks/pddl/ipc2000/logistics/strips-typed/domain.pddl \\
    src/test/resources/benchmarks/pddl/ipc2000/logistics/strips-typed/p01.pddl