# Operate Knowledge Base Dynamically with JPL

**URL:** <https://swi-prolog.discourse.group/t/operate-knowledge-base-dynamically-with-jpl/3405>\
**Category:** Data Structure\
**Tags:** how-to\
**Created:** [December 21, 2020, 12:04pm UTC](https://swi-prolog.discourse.group/t/operate-knowledge-base-dynamically-with-jpl/3405 "2020-12-21T12:04:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TramsWang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/tramswang/32/3147_2.png) [@TramsWang](https://swi-prolog.discourse.group/u/TramsWang)\
**Post date:** [December 21, 2020, 12:04pm UTC](https://swi-prolog.discourse.group/t/operate-knowledge-base-dynamically-with-jpl/3405/1 "2020-12-21T12:04:59Z")

</div>

Hi. I’m using JPL to operate a knowledge base dynamically. However I cannot find any resources that tells me how to do that.

For example, I can use the following code to create a new fact and append it to KB:

```java
Atom a = new Atom("a");
Atom b = new Atom("b");
Compound p = new Compound("parent", new Term[]{a, b});
Query q = new Query(new Compound("assertz", new Term[]{p}));
q.hasSolution();q1.close();

```

which is the Java version of the following command in swipl:

```prolog
assertz(parent(a, b)).

```

But I cannot find a way to add the following rules to KB:

```prolog
assertz((ancestor(X, Y) :- parent(X, Y))).
assertz((ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y))).

```

Moreover, I cannot create the following query:

```prolog
?- parent(X, Y), parent(Y, Z).

```

which is simply answered in the terminal version of swipl.

Is there any way to achieve these purposes?

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [December 21, 2020, 12:32pm UTC](https://swi-prolog.discourse.group/t/operate-knowledge-base-dynamically-with-jpl/3405/2 "2020-12-21T12:32:48Z")

</div>

This is just a quick reply that may help. I have never used JPL with SWI-Prolog.

The source code for JPL is open source and has [test cases](https://github.com/SWI-Prolog/packages-jpl/tree/master/src/test/java/org/jpl7).

[Family.java](https://github.com/SWI-Prolog/packages-jpl/blob/a6a3617d17cde587ee0946c108d92cae8b1627d0/src/test/java/org/jpl7/standalone/Family.java) contains a query example.
