Next: Constructing Terms, Previous: Using the API, Up: Using the API

### 2.1 Setup

First construct a gnu.prolog.vm.Environment which is the core of the VM using `new Environment()`.

Then load any prolog files you want to which define additional predicates etc using `environment.ensureLoaded(AtomTerm.get(``URL of file''))` for each file. Though if you already have an gnu.prolog.vm.AtomTerm representation of the file then you can use that.

Instances of Terms should be got using their relevant `get(Type)` methods as we ensure only one gnu.prolog.term.Term instance for each distinct object they represent.

Then you will need to create at least one gnu.prolog.vm.Interpreter which is used to execute goals. You do this using `environment.createInterpreter()`. You might want multiple Interpreters if you want to run goals in multiple threads.

Having created it you need to use it to run the initialization of any files you have loaded by using `env.runInitialization(interpreter);`. This ensures the goals contained in any “:- initialization(goal).” or “:- goal.”s in the files you have loaded are run.

At this point the setup is completed and you can start running goals. This is done using `interpreter.runOnce(Term);` where Term is the goal you want to execute. This does one execution of the goal and then stops. The construction of Terms is explained in the next section. If you need something more complex than that then you will need to use `goal = interpreter.prepareGoal(goalTerm); int rc = interpreter.execute(goal)` and then depending on the value of rc possibly run `execute(goal)` again. If you stop running `execute(goal)` while rc is still SUCCESS then you need to run `interpreter.stop(goal)`.