info-gnuprologjava
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Info-gnuprologjava] Problems understanding the usage of the GNU Pro


From: Daniel Thomas
Subject: Re: [Info-gnuprologjava] Problems understanding the usage of the GNU Prolog API
Date: Thu, 17 Feb 2011 17:23:36 +0000

Hello,

The html version of the manual does appear to be a bit broken. The info
version should work better
http://www.gnu.org/software/gnuprologjava/manual.info

You can load facts programatically if you define the predicate to be
dynamic beforehand in the standard manner for prolog.
:-dynamic(predicate/n).
However that declaration needs to be in a file which you load using
ensureLoaded and after which you then have called runInitialisation

You should be able to define both rules and facts programatically at
runtime if you have previously declared the relevant predicates to be
dynamic in a file. (In a future version we will support loading text
straight from a reader or similar without having to use a file at all
but at the moment at least one file is necessary to define the existence
of the predicates).

I will continue answering inline.

On Thu, 2011-02-17 at 17:36 +0100, Daniel Warzecha wrote:
> Hi,
> 
> 
> 
> yes, I found the manual, but it really didn't help me, I'm someone who
> learns best by example. The formatting on the manual is a bit off,
> also. 
> 
> 
> So, by loading facts you mean load them from a prolog file? I wanted
> to use the library without resorting to loading prolog files at first,
> to see if I can programmatically build both knowledge base and rules.
> The second step will probably be building a prolog file to easily
> create the more complex rules.
> 
> 
> ensureLoaded seems to load a prolog file and probably incorporate the
> facts etc. into the environment, am I right?

Yep.

> So if I have some Java strings which represent some atoms, e.g.
> "key1", is the following correct?
> 
> String keyAtom = "key1";
> Environment prologEnv = new Environment();
> AtomTerm.get(keyAtom); // creates the AtomTerm "key1" in the
> environment?
Nope. AtomTerm.get(keyAtom) returns an AtomTerm (see the return type).
If by in the environment you mean such that
:- key1.
would succeed then that won't work. There isn't a neat way of making
that work at present (ensureLoading from a string or a term would be
neater) the difficulty being in managing a new kind of unique identifier
as we wouldn't be able to use filenames any more and Prolog cares about
where things came from.
Since you probably can't predefine key1 etc. to be dynamic that might
cause problems but you can easily define key/1 to be dynamic and then
declare at runtime key(key1) etc. using assert[az].

> Or is there another way to put atoms in the environment?
> Something like Environment.put(Term)? Environment.createFact(Term)?

No. The problem is that a declared term to exist in the Environment
needs to have a declaring file (or something similar) and we don't have
the something similar working yet (I haven't given much thought as to
how to go about doing that yet).

> Does stringToTerm(String,Environment) create a Term from the String
> and put it into the Environment?

Nope stringToTerm creates the Term object from the string - essentially
it simply causes it to be parsed into a Term object - which you could
then execute. It doesn't cause it to go through the guts of
PrologTextLoader as that needs a file in order to know where things came
from.

> And what about predicates (e.g. key/1) ? Is there a mechanism to let
> the environment know a predicate with an arity exists?

Yes :- dynamic(key/1) as in the prolog standard.

> Is the Predicate class used to do this?
Nope. You shouldn't be using the Predicate class for anything except
perhaps if you were extending GNU Prolog for Java by defining new built
in predicates (and then only if they were doing odd things with the
internal representation of Prolog terms).

> Or rather, is the following correct?
> String functor = "key";
> int arity = 1;
> Module mod = new Module();
> Predicate keyPred = new Predicate(mod, CompoundTermTag.get(functor,
> arity));

No that is very wrong. Don't do that.
I should probably create and change the permissions on the constructor
for Module so that that isn't even possible.

As a user of the library you want to be using Environment and
Interpreter along with some of the classes in gnu.prolog.term These
classes have better documentation than the rest (though that is still
not saying as much as I would like - I haven't yet got around to going
through the whole library and adding documentation everywhere as when I
took over maintenance there was essentially none.)

> And last but not least, to construct a CompoundTerm, e.g. key("key1"),
> what do I do?
> Is the following correct?
> String keyAtom = "key1";
> String functor = "key";
> CompoundTerm keyComp = new CompoundTerm(functor, keyAtom);
No. There is no constructor which takes those arguments. All the
constructors are documented
http://www.gnu.org/software/gnuprologjava/api/gnu/prolog/term/CompoundTerm.html
You probably want something along the lines of 
Term[] keyAtom = {AtomTerm.get("keyAtom")};
String functor = "key";
CompoundTerm keyComp = new CompoundTerm(functor,keyAtom);
As that should create a compound term of with the correct functor and
use the arity from keyAtom to determine the arity and fill its arguments
with an atom keyAtom.

> What do I have to do to incorporate the CompoundTerms and Predicates
> into the environment? Is this happening automatically?

As above don't use Predicate. To incorporate a CompoundTerm into the
environment dynamically you will want to use Interpreter.runOnce
http://www.gnu.org/software/gnuprologjava/api/gnu/prolog/vm/Interpreter.html#runOnce(gnu.prolog.term.Term)
as per http://www.gnu.org/software/gnuprologjava/manual/Setup.html#Setup
with an argument containing an assert[az]/1 term with the CompoundTerm
as the argument.
you want assertz(key(keyAtom)) or something similar.

> Sorry for all the questions, I really don't understand the online
> manual yet.

I will try and get the manual improved in the next release.

> Thanks for your help,
> Daniel W. , Germany

I hope this helps,

Daniel

> 2011/2/17 Daniel Thomas <address@hidden>
>         Hello,
>         
>         Have you found the GNU Prolog for Java manual?
>         http://www.gnu.org/software/gnuprologjava/manual/
>         
>         You can load prolog facts and rules using the ensureLoaded see
>         http://www.gnu.org/software/gnuprologjava/manual/Setup.html#Setup and
>         
> http://www.gnu.org/software/gnuprologjava/api/gnu/prolog/vm/Environment.html#ensureLoaded(gnu.prolog.term.Term)
>         
>         If you are asserting facts at runtime then you do this as
>         normal in
>         Prolog using assertz or asserta or similar (having previously
>         declared
>         it to by dynamic as usual in a prolog file which you then
>         ensureloaded)
>         - you can find instructions on creating terms here:
>         
> http://www.gnu.org/software/gnuprologjava/manual/Constructing-Terms.html#Constructing-Terms
>         or if you want to construct a term from a string (if you
>         already have
>         the data you want as a string representation of a prolog term)
>         then you
>         might want to use
>         
> http://www.gnu.org/software/gnuprologjava/api/gnu/prolog/io/TermReader.html#stringToTerm(java.lang.String,
>  gnu.prolog.vm.Environment) though it doesn't look like that has been 
> documented properly yet. Sorry.
>         
>         Apologies that you found the API documentation irritating. I
>         will try to
>         get it improved in the next release.
>         
>         I hope this helps,
>         
>         Daniel
>         
>         On Thu, 2011-02-17 at 15:52 +0100, Daniel Warzecha wrote:
>         > Hello,
>         >
>         >
>         > I hope someone can help me. I'm trying to understand how to
>         use the
>         > API in a correct way and am a bit overwhelmed.
>         >
>         >
>         > The background: I'm trying to parse messages in UML Sequence
>         Diagrams
>         > and transform their facts, i.e. the message data to Prolog.
>         Then I
>         > want to pose questions to the Prolog interpreter in regards
>         to the
>         > knowledge base.
>         >
>         >
>         > Okay, sadly I don't understand how to do the following basic
>         things
>         > with the API:
>         >
>         >
>         > 1. Tell the environment (if that's right) that some
>         predicates exist:
>         > e.g. key/1 or knows/2 .
>         > 2. Tell Prolog that some facts exist: e.g. key("key1") or
>         > knows("attacker","key1")
>         > 3. Tell Prolog that certain rules exist: e.g.
>         > knows("attacker","message") :- encrypted("message","key1"),
>         > symmetricKey("key1").
>         >
>         >
>         > Can somebody please help me? The API docs only irritated me
>         even more.
>         >
>         >
>         > Thanks in advance,
>         >
>         >
>         > Daniel W.
>         
>         
> 

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]