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.
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.
Sorry, didn’t notice your actual problem before, either:
You are entring data that doesn’t match your car/5 predicate.
or
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.