如何从通过"C ++到SWI-Prolog的接口"给出的序言中得到结果(两个序言列表). ,并将它们存储在c ++中的变量中 [英] how to get the result (two prolog lists ) from prolog, given via "c++ Interface to SWI-Prolog" , and store them in a variable in c++

查看:66
本文介绍了如何从通过"C ++到SWI-Prolog的接口"给出的序言中得到结果(两个序言列表). ,并将它们存储在c ++中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:)
我遇到的问题是如何从Prolog(通过swi-prolog的界面)获取(变量X和Y)并将其存储到Visual Studio中的变量(c ++)

Hello to everyone :)
The Problem i have is how to get and store(Variable X and Y) from Prolog (via interface from swi-prolog) into a variable (c++) in visual studio

简而言之:我将带有三角形拼图部件的拼图转换为Prolog表示形式.这个难题由序言解决.到目前为止,该方法有效.每个拼图块的视觉表示都是在视觉工作室中完成的.这也正在工作.

In short: I transfered a Puzzle with triangular puzzle parts into Prolog representation. This puzzle is solved by prolog. This works so far. The visual representation with each puzzle tile is done in visual studion. This is working also.

现在我想通过Interface调用prolog引擎,这将返回解决方案和过程中的每个步骤(打开拼图块/哪个拼图块/在哪里)(由prolog中的write()完成).到目前为止,我可以实例化prolog调用该函数并在控制台窗口中获取返回值.我也显示在控制台中的每个write().但是只能在控制台中.

Now i want to call via Interface the prolog engine and this will return the Solution and each step(turn puzzle tile/which puzzle tile/ and where it is) during the process(done by write() in prolog). So far i can instantiate prolog call the function and get return value in console window. Also i get displayed each write() in console. BUT only in console.

序言部分:
X代表具有拼图零件ID的列表.
Y表示由于prolog而导致的拼图零件旋转状态列表.
内部拼图部分表示为:

teil(ID,side_a,side_b,side_c).

Prolog Part:
X represent a List with IDs of puzzle parts
Y represent a List of rotatestates from the puzzle parts as a result of prolog
internally puzzle parts represented as:

teil(ID,side_a, side_b, side_c).

teil(8,lsh,hbsh,gsv).

prolog call:
?- puFlaeche(X,Y).
Result:
X = [2, 7, 3, 5, 9, 1, 4, 6, 8],
Y = [rechts, rechts, unten, rechts, rechts, unten, rechts, unten, rechts].

Visual Studio:

Visual Studio:


if( ! PL_initialise(1,av)){
    Console::Write("eeor with PL_initialise(1,av)");
     }else {
        Console::Write("no error with PL_initialise(1,av)");
     }

PlCall("consult('puzzle.pl')");
char rval2;
predicate_t pred2 = PL_predicate("puFlaeche",2,"user");
term_t h2 = PL_new_term_refs(2);
char * variable1 = "X";
char * variable2 = "Y";
//add some Variable to prolog
PL_put_variable(h2);
PL_put_variable(h2+1);
//produce console output
rval2=PL_call_predicate(NULL,PL_Q_NORMAL,pred2,h2);

如果我打电话:

if (PL_call(h2, NULL))
{
    PL_get_list_chars(h2, &fact2,CVT_ATOM|REP_UTF8);
}

我从brakpoint的VS调试中看到事实2以某种方式存储了结果:

I see in debug from VS at brakpoint that fact2 somehow store the result:

fact2   0x05699460 "\x2\a\x3\x5\t\x1\x4\x6\b"   char *

它也显示在控制台中...但带有示例输出:

And its also displayed in console...but with example output:

ERROR: source_sink 2 does not exist
ERROR: source_sink 7 does not exist
ERROR: source_sink 3 does not exist
...

如果我打电话:

term_t tail = PL_copy_term_ref(h2);
term_t head = PL_new_term_ref();
int x;
while(PL_get_list(tail, head, tail))
{
    PL_get_integer(head, &x);
    Console::WriteLine("Ergebnis 2:---"+x+"---| ");
}

我确实在某种程度上从控制台获得了解决方案(从X):

i get indeed the solution (from X)somehow without error in the console:

"some debug text": 2 
"some debug text": 7
"some debug text": 3
...

我尝试了其他几种解决方案,但这是迄今为止我迄今为止获得的最好结果...
经过很长时间的文字问题后:
A)如何从Prolog中获取Y变量的内容解决

I tryed several other solutions but this is so far the best result i have so far...
After long Text questions again:
A) how to get Y variable content from Prolog SOLVED Below

----编辑问题------

----EDIT QUESTION------

B)如何从控制台或其他方式显示的prolog中获取write(),以获取prolog中"trace"之类的逐步输出

B) how to get the write() from prolog which is displayed in console or anny other way to get a step by step output like "trace" in prolog

C)如何将A)和write()语句存储到c ++中的变量或列表中

C) how to store A) and the write() statements into a variable or list in c++

我可能会遇到错误的方式...所以您可以帮助我:D 在此先感谢:)

May i run into wrong way...so you can help me out :D Thanks in advance :)

编辑


A)我通过调用函数"PL_get_list"来获得X和Y列表的内容 对于表示拼图部分ID的X,//也要感谢Jan

EDIT


A) i get the content of the X and Y list by calling the function "PL_get_list" For X which represents the IDs of the puzzle parts //thanks also to Jan

    term_t tail = PL_copy_term_ref(h2);
    term_t head = PL_new_term_ref();
    int x;
    while(PL_get_list(tail, head, tail))
    {
        PL_get_integer(head, &x);
        PL_get_integer(head+1, &x);
        Console::WriteLine("Ergebnis X :---"+x+"---| ");
    }

对于代表旋转状态的Y,

And for Y which represent the rotate state

    term_t tail1 = PL_copy_term_ref(h2+1);
    term_t head1 = PL_new_term_ref();
    std::string extracted_state_array[10];
    char* state_array[10];int count=0;

    while(PL_get_list(tail1, head1, tail1)){
        std::string st; 
        PL_get_chars(head1,&state_array[count],CVT_ALL | CVT_WRITE | BUF_RING);
        st.assign(state_array[count],strlen(state_array[count]));
        extracted_state_array[count]=st;

        std::cout<<"Ergebnis Y :---"+extracted_state_array[count]+"---| "<< std::endl;
        count++;
        }

推荐答案

直到PL_call_predicate(),一切都很好.现在,h2是两个术语引用的数组,h2+0绑定到[2, 7, 3, 5, 9, 1, 4, 6, 8]h2+1绑定到[rechts, rechts, unten, rechts, rechts, unten, rechts, unten, rechts].然后您会做一些奇怪的事情:您致电 h2+0,基本上是致电

Up to PL_call_predicate(), all is fine. Now, h2 is an array of two term references, h2+0 bound to [2, 7, 3, 5, 9, 1, 4, 6, 8] and h2+1 to [rechts, rechts, unten, rechts, rechts, unten, rechts, unten, rechts]. Then you do something strange: you call h2+0, basically calling

call([2, 7, 3, 5, 9, 1, 4, 6, 8])

该列表是Consult/1的缩写,因此您尝试查阅(加载)名为1的文件,等等.因此,显示消息.

The list is an abbreviation for consult/1, so you try to consult (load) files named 1, etc. Hence the messages.

您需要使用PlList类中的列表迭代方法来枚举数组中的元素.然后,您可以将它们转换为数字或字符串(第二个).

You need to use the list iteration methods from PlList class to enumerate the elements in the array. Then you can translate each to a number or string (for the second).

(在C ++中)嵌入时,通常不会使用Prolog的I/O.您可以使用PL_get_chars()和变体将单个术语转换为字符串.如果您确实要重新绑定I/O,则有标头SWI-Stream.h.虽然它的用法大部分没有记录.您可以查看源代码,以获取重新绑定Prolog的I/O以读取/写入到swipl-win.exe控制台的示例,等等.尽管如此,我认为这不是您想要的.

When embedding (in C++) you will normally not use Prolog's I/O. You can translate individual terms to a string using PL_get_chars() and variations. If you really want to rebind I/O, there is the header SWI-Stream.h. Its usage is mostly undocumented though. You can look through the source code for examples re-binding Prolog's I/O to read/write to the swipl-win.exe console, etc. I do not think that is what you are after though.

这篇关于如何从通过"C ++到SWI-Prolog的接口"给出的序言中得到结果(两个序言列表). ,并将它们存储在c ++中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆