Pengine outputs just object object

Hey its me again, started working with pengines and overall it works. Now i tried to put my simple prolog code into pengine and changed those writes and reads with the equals of pengine, but i just get “Object object” as output or if i use stringify just [()]. Where my problem?

<script type="text/x-prolog">
car(vw,'SUV',tiguan,2010,benzin).

read_car:-
pengine_input("Which car brand do you prefer?", Carbrand),
pengine_input("Which type do you prefer?(SUV,Coupe)",Type),
pengine_input("Which fuel do you prefer?",Fuel),
car(Carbrand,Type,Output1,Output2,Fuel),
pengine_output(Output1),
pengine_output(Output2).





</script>

<script type="text/javascript">
/*
Create a Prolog engine running the code   from  the above script and the
query specified in `ask`. Get the results in chunks of max 1,000 entries
and ask for more results if there are more available.
*/

 var pengine = new Pengine({ server: "https://swish.swi-prolog.org/pengine",
		ask: "read_car",
		application: "swish",
        onprompt: handlePrompt,
        onsuccess: handleOutput,
        onerror: handleOutput
            });
            function handlePrompt() {
                pengine.respond(prompt(this.data));
		   }
            function handleOutput() {
			var solution = JSON.stringify(this.data);

              $('#out').append(this.data + "<br/>");
		   }
</script>

<div id="out"></div>

also another question how can i implement basic if then logic? for example if the user choose “Tesla” as carbrand, i dont want prolog to ask for the type of fuel, because obviously tesla is just electro.

Thank you again guys!

Use conditional ->/2.

In imperative pseudo code

If <condition> then
  <true action>
else
  <false action>

In Prolog using conditional

(
   <condition>
->
   <true action>
;
  <false action>
)

If you use ->/2 I would strongly suggest that both actions always have something, true/0 can be used and is often used, e.g.

(
   <condition>
->
   <true action>
;
   true
)

That says that if the condition succeeds then do the <true action> otherwise when the condition fails do the <false action> which is true and just succeeds doing nothing.


A very common mistake made with the condition is to use = (unification) thinking it is a comparison; in Prolog == is a comparison. See: Comparison and Unification of Terms

This is typically bad in a condition,

A = 1

This is probably what you want

A == 1

If you know your code is deterministic (has only one possible answer) then use det/1, e.g.

:- det(<predicate indicator>). 

or use single sided unification =>/2.


Also pay very close attention to the characters used for the operators
-> is for conditional
=> is for single sided unification.


As far the pengine questions I can not help.

Here is a small sample that shows you how to use pengine_input/2 and pengine_output/1.

It is a slight modification of packages-pengines/input_output.html at 0ab89f53ed1698e332e88860a396b3bc1350d677 · SWI-Prolog/packages-pengines · GitHub to use SWISH.

<html lang="en">
    <head>
        <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="https://swish.swi-prolog.org/pengine/pengines.js"></script>
        <script type="text/x-prolog">

	   read_names :-
        pengine_input('Please enter a name, stop with "stop"', X),
		(   X == stop
		->  !
		;   pengine_output(X),
		   read_names
		).

        </script>
        <script>
	   var pengine = new Pengine({
   	            server: "https://swish.swi-prolog.org/pengine",
   	            chunk: 1000,
   	            application: "swish",
                oncreate: handleCreate,
                onprompt: handlePrompt,
                onoutput: handleOutput
            });
            function handleCreate() {
                pengine.ask('read_names');
	   }
            function handlePrompt() {
                pengine.respond(prompt(this.data));
	   }
            function handleOutput() {
                $('#out').append("<div>"+this.data+"</div>");
	   }
        </script>
    </head>
    <body>
        <div id="out"></div>
    </body>
</html>

Sorry, didn’t notice your actual problem before, either:

  1. You are entring data that doesn’t match your car/5 predicate.
    or
  2. You have invalid prolog code (usually a syntax error).

Use the following code and it should work:

 car(vw,'SUV',tiguan,2010,benzin).
 read_car :-
	 pengine_input("Which car brand do you prefer?", Carbrand),
	 pengine_input("Which type do you prefer?(SUV,Coupe)",Type),
	 pengine_input("Which fuel do you prefer?",Fuel),
	 ( car(Carbrand,Type,Output1,Output2,Fuel)
	 -> pengine_output(Output1),
		pengine_output(Output2)
	 ;  pengine_output("Unknown car")
	 ).

EDIT: I suggest you write and test your prolog code in the local machine first before you put it in your html file.

Thanks, i used “onsuccess” and it still gave me [Object object]. I changed it to onoutput and now it works. Dont know why it dont work with onsuccess.