How call RAW text with Prolog predicate from C?

View: New views
6 Messages — Rating Filter:   Alert me  

How call RAW text with Prolog predicate from C?

by pva-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello. I'am sorry for my bad English(russian).

I want call from C application Prolog predicates.
I try that:

PlTerm goal;
....
Pl_Start_Prolog(0, 0);

char str[]="asserta(parent(bob,mary))"; //or ANYthing else in string
format.

goal = Pl_Mk_Atom(Pl_Create_Allocate_Atom(str));

args = Pl_Rd_Callable_Check(goal, &functor, &arity);

Pl_Query_Begin(PL_TRUE);

Pl_Query_Call(functor, arity, args);

Pl_Query_End(PL_KEEP_FOR_PROLOG);



Pl_Stop_Prolog();

But that not work.
Please help me.



_______________________________________________
Users-prolog mailing list
Users-prolog@...
http://lists.gnu.org/mailman/listinfo/users-prolog

Re: How call RAW text with Prolog predicate from C?

by Cesar Rabak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

pva@... escreveu:

> Hello. I'am sorry for my bad English(russian).
>
> I want call from C application Prolog predicates.
> I try that:
>
> PlTerm goal;
> ....
> Pl_Start_Prolog(0, 0);
>
> char str[]="asserta(parent(bob,mary))"; //or ANYthing else in string
> format.
>
> goal = Pl_Mk_Atom(Pl_Create_Allocate_Atom(str));
>
> args = Pl_Rd_Callable_Check(goal, &functor, &arity);
>
> Pl_Query_Begin(PL_TRUE);
>
> Pl_Query_Call(functor, arity, args);

Where are you checking the return of Pl_Query_Call?


--
Cesar Rabak
GNU/Linux User 52247.
Get counted: http://counter.li.org/


_______________________________________________
Users-prolog mailing list
Users-prolog@...
http://lists.gnu.org/mailman/listinfo/users-prolog

Re: How call RAW text with Prolog predicate from C?

by pva-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is the listing C code:

#include <stdio.h>
#include <string.h>
#define __GPROLOG_FOREIGN_STRICT__
#include "gprolog.h"
int Main_Wrapper(int argc, char *argv[])
{
  PlTerm *args;
  int functor, arity;
  int iarity;
        PlBool res;
        PlTerm except;

  PlTerm goal;
  char str[]="asserta(parent(bob,mary))";
  char *exceptstr;

  printf("0)\n");
        Pl_Start_Prolog(0, 0);

       
  printf("1)\n");
        goal = Pl_Mk_Atom(Pl_Create_Allocate_Atom(str));

  printf("2)\n");
  fflush(stdout);

        args = Pl_Rd_Callable_Check(goal, &functor, &arity);

  printf("3) functor: %d, arity: %d, args: %p\n", functor, arity, args);
  fflush(stdout);

  Pl_Query_Begin(PL_TRUE);
  res = (PlBool)Pl_Query_Call(functor, arity, args);
  printf("4) res: %d\n", res);
  fflush(stdout);
  Pl_Query_End(PL_KEEP_FOR_PROLOG);

  Pl_Stop_Prolog();
  return 0;
}
int
main(int argc, char *argv[])
{
  return Main_Wrapper(argc, argv);
}
--

Is the output:
0)
1)
2)
3) functor: 781435, arity: 0, args: 00000000
4) res: 2
--

res==2 - meaning PL_EXCEPTION.

If str=="listing" then res == 1, and is OK.

I think that the problem is that "asserta(parent(bob,mary))" not a Atom, is
Term. But "listing" is Atom.
The problem is that I do not know how to create(programatically) the
correct Term from string.
Or how to get gnu-prolog implement any str(programatically).

Sorry for my bad English(russian).
On Thu, 23 Apr 2009 15:04:35 -0300, Cesar Rabak <cesar.rabak@...>
wrote:

> pva@... escreveu:
>> Hello. I'am sorry for my bad English(russian).
>>
>> I want call from C application Prolog predicates.
>> I try that:
>>
>> PlTerm goal;
>> ....
>> Pl_Start_Prolog(0, 0);
>>
>> char str[]="asserta(parent(bob,mary))"; //or ANYthing else in string
>> format.
>>
>> goal = Pl_Mk_Atom(Pl_Create_Allocate_Atom(str));
>>
>> args = Pl_Rd_Callable_Check(goal, &functor, &arity);
>>
>> Pl_Query_Begin(PL_TRUE);
>>
>> Pl_Query_Call(functor, arity, args);
>
> Where are you checking the return of Pl_Query_Call?
>
>
>



_______________________________________________
Users-prolog mailing list
Users-prolog@...
http://lists.gnu.org/mailman/listinfo/users-prolog

Parent Message unknown Re: How call RAW text with Prolog predicate from C?

by pva-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I Try.
It not work. res == 2.

On Sun, 26 Apr 2009 11:33:09 -0700 (PDT), Dave Sworin
<sworin88dave@...> wrote:
> asserta/1 requires an extra set of parentheses.
> So try the following ...
>    char str[]="asserta((parent(bob,mary)))";
> ... and see if this works.
>
>  
>
>
>



_______________________________________________
Users-prolog mailing list
Users-prolog@...
http://lists.gnu.org/mailman/listinfo/users-prolog

Re: How call RAW text with Prolog predicate from C?

by Daniel Diaz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You are right, the problem comes from:

   char str[]="asserta(parent(bob,mary))";

followed by:

   Pl_Mk_Atom(Pl_Create_Allocate_Atom(str))

you thus create an atom and not a compound term. You should use instead
Pl_Mk_Compound instead
(see http://gprolog.org/manual/html_node/gprolog069.html#toc291)

BTW: since you have 2 nested compound terms (asserta/1 and parent/2) you
have to first create parent(bob,mary) in a PlTerm variable 'X' and then
to create asserta(X) into a variable 'goal'. Use Pl_Mk_Compound for both.

Daniel

pva@... a écrit :

> Is the listing C code:
>
> #include <stdio.h>
> #include <string.h>
> #define __GPROLOG_FOREIGN_STRICT__
> #include "gprolog.h"
> int Main_Wrapper(int argc, char *argv[])
> {
>   PlTerm *args;
>   int functor, arity;
>   int iarity;
> PlBool res;
> PlTerm except;
>
>   PlTerm goal;
>   char str[]="asserta(parent(bob,mary))";
>   char *exceptstr;
>
>   printf("0)\n");
> Pl_Start_Prolog(0, 0);
>
>
>   printf("1)\n");
> goal = Pl_Mk_Atom(Pl_Create_Allocate_Atom(str));
>
>   printf("2)\n");
>   fflush(stdout);
>
> args = Pl_Rd_Callable_Check(goal, &functor, &arity);
>
>   printf("3) functor: %d, arity: %d, args: %p\n", functor, arity, args);
>   fflush(stdout);
>
>   Pl_Query_Begin(PL_TRUE);
>   res = (PlBool)Pl_Query_Call(functor, arity, args);
>   printf("4) res: %d\n", res);
>   fflush(stdout);
>   Pl_Query_End(PL_KEEP_FOR_PROLOG);
>
>   Pl_Stop_Prolog();
>   return 0;
> }
> int
> main(int argc, char *argv[])
> {
>   return Main_Wrapper(argc, argv);
> }
> --
>
> Is the output:
> 0)
> 1)
> 2)
> 3) functor: 781435, arity: 0, args: 00000000
> 4) res: 2
> --
>
> res==2 - meaning PL_EXCEPTION.
>
> If str=="listing" then res == 1, and is OK.
>
> I think that the problem is that "asserta(parent(bob,mary))" not a Atom, is
> Term. But "listing" is Atom.
> The problem is that I do not know how to create(programatically) the
> correct Term from string.
> Or how to get gnu-prolog implement any str(programatically).
>
> Sorry for my bad English(russian).
> On Thu, 23 Apr 2009 15:04:35 -0300, Cesar Rabak <cesar.rabak@...>
> wrote:
>> pva@... escreveu:
>>> Hello. I'am sorry for my bad English(russian).
>>>
>>> I want call from C application Prolog predicates.
>>> I try that:
>>>
>>> PlTerm goal;
>>> ....
>>> Pl_Start_Prolog(0, 0);
>>>
>>> char str[]="asserta(parent(bob,mary))"; //or ANYthing else in string
>>> format.
>>>
>>> goal = Pl_Mk_Atom(Pl_Create_Allocate_Atom(str));
>>>
>>> args = Pl_Rd_Callable_Check(goal, &functor, &arity);
>>>
>>> Pl_Query_Begin(PL_TRUE);
>>>
>>> Pl_Query_Call(functor, arity, args);
>> Where are you checking the return of Pl_Query_Call?
>>
>>
>>
>
>
>
> _______________________________________________
> Users-prolog mailing list
> Users-prolog@...
> http://lists.gnu.org/mailman/listinfo/users-prolog
>


--
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.



_______________________________________________
Users-prolog mailing list
Users-prolog@...
http://lists.gnu.org/mailman/listinfo/users-prolog

Re: How call RAW text with Prolog predicate from C?

by pva-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, this is so. But the problem is not how to implement(execute, make)
THIS predicate as well as to be able to execute any predicate stored in
string. I think that the interface of this library can not do it.
My aim is Prologue server that can send a text query (a predicate). The
problem in obtaining the correct predicate from the string.
Thank you all, I decided to go to the SWI-Prolog.

(Sorry for my bad English)



On Mon, 27 Apr 2009 15:22:07 +0200, Daniel Diaz
<Daniel.Diaz@...> wrote:

> You are right, the problem comes from:
>
>    char str[]="asserta(parent(bob,mary))";
>
> followed by:
>
>    Pl_Mk_Atom(Pl_Create_Allocate_Atom(str))
>
> you thus create an atom and not a compound term. You should use instead
> Pl_Mk_Compound instead
> (see http://gprolog.org/manual/html_node/gprolog069.html#toc291)
>
> BTW: since you have 2 nested compound terms (asserta/1 and parent/2) you
> have to first create parent(bob,mary) in a PlTerm variable 'X' and then
> to create asserta(X) into a variable 'goal'. Use Pl_Mk_Compound for both.
>
> Daniel
>




_______________________________________________
Users-prolog mailing list
Users-prolog@...
http://lists.gnu.org/mailman/listinfo/users-prolog