SWI in C: Getting String from a Query

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

SWI in C: Getting String from a Query

by Airo Miranda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello All,
Maybe it a very silly doubt, but i can not figure out how to do it.
I have created an interface between SWI and C, precisely, I am calling SWI logic engine from a C application.
To execute some querries to the logic engine, I made the following function:

void call_pl(char * goal)
{
     PlFrame pl_frame;
     PlCall(goal);
     pl_frame.rewind();
}

My question is: how can I put the result of the PlCall(goal) into a string ? (By now, the result is showed in console).

Cheers!

Bruno

Re: SWI in C: Getting String from a Query

by Alan Baljeu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
You need to use PlTermv and PlQuery.
 
For example:

    try
    {
        PlForeignFrame frame(PlForeignFrame::Discard);
        PlTermv args(3);
        args[1] = PlCompound("hello");

        PlQuery q("validate_k", args);
        q.next_solution();
        result = (char*) args[2];
    }
    catch (PlException)
    {
      return;
    }

This is roughly equivalant to the prolog expression validate_k(hello, A2, A3).  (I think.  I may have this slightly wrong.)

Alan Baljeu


From: Airo Miranda <airo.miranda@...>
To: swi-prolog@...
Sent: Sat, November 14, 2009 7:05:19 AM
Subject: [SWIPL] SWI in C: Getting String from a Query

Hello All,
Maybe it a very silly doubt, but i can not figure out how to do it.
I have created an interface between SWI and C, precisely, I am calling SWI logic engine from a C application.
To execute some querries to the logic engine, I made the following function:

void call_pl(char * goal)
{
     PlFrame pl_frame;
     PlCall(goal);
     pl_frame.rewind();
}

My question is: how can I put the result of the PlCall(goal) into a string ? (By now, the result is showed in console).

Cheers!

Bruno


Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail

Re: SWI in C: Getting String from a Query

by Airo Miranda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Alan, 
first, thanks for your answer. I' am a novice in using SWI in C, so, I hope you don't my
with my silly doubts. ;-)
May be you can help me to understand your code.
First, when I use  PlForeignFrame frame(PlForeignFrame::Discard) I get a compile error: 
'PlForeignFrame' : undeclared identifier
May I use the PlFrame as a substitution? Just like in my previous post?
Second, I do not understand what this code makes. Let's go to a simple example:
Suppose that I want to use the command ls. in the SWI and get in a string the name of the returned files and directories in the directory where the pl was consulted. ls. is accpeted by SWI shell. How can I do this from a C interface?

Thanks for your attention!

Best,

Bruno

2009/11/14 Alan Baljeu <alanbaljeu@...>
You need to use PlTermv and PlQuery.
 
For example:

    try
    {
        PlForeignFrame frame(PlForeignFrame::Discard);
        PlTermv args(3);
        args[1] = PlCompound("hello");

        PlQuery q("validate_k", args);
        q.next_solution();
        result = (char*) args[2];
    }
    catch (PlException)
    {
      return;
    }

This is roughly equivalant to the prolog expression validate_k(hello, A2, A3).  (I think.  I may have this slightly wrong.)

Alan Baljeu


From: Airo Miranda <airo.miranda@...>
To: swi-prolog@...
Sent: Sat, November 14, 2009 7:05:19 AM
Subject: [SWIPL] SWI in C: Getting String from a Query

Hello All,
Maybe it a very silly doubt, but i can not figure out how to do it.
I have created an interface between SWI and C, precisely, I am calling SWI logic engine from a C application.
To execute some querries to the logic engine, I made the following function:

void call_pl(char * goal)
{
     PlFrame pl_frame;
     PlCall(goal);
     pl_frame.rewind();
}

My question is: how can I put the result of the PlCall(goal) into a string ? (By now, the result is showed in console).

Cheers!

Bruno


Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail


Parent Message unknown Re: SWI in C: Getting String from a Query

by Alan Baljeu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi Bruno,

Please don't send questions only to me.  I don't always have time, and I don't always know the answer.  I'm sending the question to the mailing list.

Regarding PlFrame, I don't know. 

About the second question, PlTermv is essentially an array of terms, which are the function arguments, and so:

    PlTermv t(3); makes an array of 3 terms, and
    t[2] is a PlTerm. 

Look in the api docs for how to extract a list from a term, but it will be in two stages: First get the elements as individual terms, then convert each individual term into a C string.
 
Alan Baljeu



From: Airo Miranda <airo.miranda@...>
To: Alan Baljeu <alanbaljeu@...>
Sent: Sat, November 14, 2009 11:41:59 AM
Subject: Re: [SWIPL] SWI in C: Getting String from a Query

Hello Alan, 
first, thanks for your answer. I' am a novice in using SWI in C, so, I hope you don't my
with my silly doubts. ;-)
May be you can help me to understand your code.
First, when I use  PlForeignFrame frame(PlForeignFrame::Discard) I get a compile error: 
'PlForeignFrame' : undeclared identifier
May I use the PlFrame as a substitution? Just like in my previous post?
Second, I do not understand what this code makes. Let's go to a simple example:
Suppose that I want to use the command ls. in the SWI and get in a string the name of the returned files and directories in the directory where the pl was consulted. ls. is accpeted by SWI shell. How can I do this from a C interface?

Thanks for your attention!

Best,

Bruno

2009/11/14 Alan Baljeu <alanbaljeu@...>
You need to use PlTermv and PlQuery.
 
For example:

    try
    {
        PlForeignFrame frame(PlForeignFrame::Discard);
        PlTermv args(3);
        args[1] = PlCompound("hello");

        PlQuery q("validate_k", args);
        q.next_solution();
        result = (char*) args[2];
    }
    catch (PlException)
    {
      return;
    }

This is roughly equivalant to the prolog expression validate_k(hello, A2, A3).  (I think.  I may have this slightly wrong.)

Alan Baljeu


From: Airo Miranda <airo.miranda@...>
To: swi-prolog@...
Sent: Sat, November 14, 2009 7:05:19 AM
Subject: [SWIPL] SWI in C: Getting String from a Query

Hello All,
Maybe it a very silly doubt, but i can not figure out how to do it.
I have created an interface between SWI and C, precisely, I am calling SWI logic engine from a C application.
To execute some querries to the logic engine, I made the following function:

void call_pl(char * goal)
{
     PlFrame pl_frame;
     PlCall(goal);
     pl_frame.rewind();
}

My question is: how can I put the result of the PlCall(goal) into a string ? (By now, the result is showed in console).

Cheers!

Bruno


Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail



The new Internet Explorer® 8 - Faster, safer, easier. Optimized for Yahoo! Get it Now for Free!

Re: SWI in C: Getting String from a Query

by Jan Wielemaker-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday 16 November 2009 02:34:21 pm Alan Baljeu wrote:
> Hi Bruno,
>
> Please don't send questions only to me.  I don't always have time, and I
> don't always know the answer.  I'm sending the question to the mailing
> list.
>
> Regarding PlFrame, I don't know.

<snip>

> >    try
> >    {
> >        PlForeignFrame frame(PlForeignFrame::Discard);

There is no PlForeignFrame in SWI-cpp.h (at least, not in mine). I think
Alan has added a class. In a private version? Might be a good idea to
add something along these lines.  Can you send the source for that?

But yes, this can be (the subtle difference doesn't matter to much right
now).

        try
        {
            PlFrame();

            ...


        Cheers --- Jan

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: SWI in C: Getting String from a Query

by Airo Miranda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Jan,
I am using PlFrame and PlQuery, although, the result that I want to put into a string is not a part of the query, since the query has no variables. For instance, I think that is easy to get the answers to a query such mother(X,Y), but I cannot figure out how to get the answer from a query such ls (that lists the files and directories in the current directory). since ls is not defined as ls(X). Do you have any idea how can I put such answer in a C string?

Cheers!


2009/11/16 Jan Wielemaker <J.Wielemaker@...>
On Monday 16 November 2009 02:34:21 pm Alan Baljeu wrote:
> Hi Bruno,
>
> Please don't send questions only to me.  I don't always have time, and I
> don't always know the answer.  I'm sending the question to the mailing
> list.
>
> Regarding PlFrame, I don't know.

<snip>

> >    try
> >    {
> >        PlForeignFrame frame(PlForeignFrame::Discard);

There is no PlForeignFrame in SWI-cpp.h (at least, not in mine). I think
Alan has added a class. In a private version? Might be a good idea to
add something along these lines.  Can you send the source for that?

But yes, this can be (the subtle difference doesn't matter to much right
now).

       try
       {
           PlFrame();

           ...


       Cheers --- Jan



Re: SWI in C: Getting String from a Query

by Jan Wielemaker-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday 16 November 2009 04:58:58 pm Airo Miranda wrote:
> Thanks Jan,
> I am using PlFrame and PlQuery, although, the result that I want to put
> into a string is not a part of the query, since the query has no variables.
> For instance, I think that is easy to get the answers to a query such
> mother(X,Y), but I cannot figure out how to get the answer from a query
> such ls (that lists the files and directories in the current directory).
> since ls is not defined as ls(X). Do you have any idea how can I put such
> answer in a C string?

Call with_output_to(string(X), Goal) and get the output from X. But,
except for predicates aiming at debugging and toplevel interaction,
there are no predicates that give textual output.

        ---- Jan

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Fw: SWI in C: Getting String from a Query

by Alan Baljeu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here you go Jan.

 Alan Baljeu




----- Forwarded Message ----
From: Alan Baljeu <alanbaljeu@...>
To: Jan Wielemaker <J.Wielemaker@...>
Sent: Mon, November 16, 2009 1:47:16 PM
Subject: Re: [SWIPL] SWI in C: Getting String from a Query

>There is no PlForeignFrame in SWI-cpp.h (at least, not in mine). I think

>Alan has added a class. In a private version? Might be a good idea to
>add something along these lines.  Can you send the source for that?

You're right.  I forgot about this:

/** class PlForeignFrame
  * PURPOSE:
  * Automatic creation and cleanup of a prolog foreign
  * stack.
  */
class PlForeignFrame
{
public:
  enum DestroyAction
  {
    Close,
    Discard
  };

  PlForeignFrame(DestroyAction _action = Close) : fid(0), action(_action)
  {
    fid = PL_open_foreign_frame();
  }

  ~PlForeignFrame()
  {
    if (action == Discard)
      PL_discard_foreign_frame(fid);
    else
      PL_close_foreign_frame(fid);
  }

  operator fid_t& ()
  { return fid; }

  operator const fid_t& () const
  { return fid; }

private:
  fid_t fid;
  DestroyAction action;
};

#endif /* !defined(PLUTILS_H) */


      __________________________________________________________________
Make your browsing faster, safer, and easier with the new Internet Explorer® 8. Optimized for Yahoo! Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/



      __________________________________________________________________
Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail.  Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog