I'm fairly new to Tau-Prolog and Javascript. At the beginning I would like to show some results computed by the Tau-Prolog engine in a Browser.
So far, all I want to show is some information contained in my Prolog-database:
All robots in the database and the interface to connect with a gripper/sensor etc.
All grippers/sensors in the database and the interface to connect to the robot.
The prolog-output would be something like: "Robot A fits with Gripper C (Same interface Z)." I've programmed this in SWI-Prolog and it works fine so far.
Now I would like to show the results in a HTML-Element:
<div class="example-result" id="show_result1"></div>
This is what I've come up so far:
function queryRG() {
var session = pl.create(1000);
var parsed = session.consult("code.pl");
//if (parsed !== true) { console.log(pl.format_answer(parsed)) }
var query1 = session.query("queryRG.");
var callback = function(answer){
var result1 = document.getElementById("show_result1");
console.log( pl.format_answer( answer ));
if (pl.type.is_substitution(answer)){
var ab = answer.toString();
result.innerHTML = result.innerHTML + ab;
}
}
session.answer(callback);
}
I can see all the (correct) results in the console, but I fail to make them visible on the website. Thanks for your help!
Edited:
After the the help of @CapelliC i came up with this version now. Still I have the problem that the expected result show up in the Console, but not on the site:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playground</title>
</head>
<body>
<script type="text/javascript" src="tau-prolog.js"></script>
<h1>Playground4</h1>
<script>
function queryRG() {
var session = pl.create(1000)
var code_pl = `
robot('Robot1','A', 1, 550).
robot('Robot2','C', 2, 340).
robot('Robot3','A', 2, 200).
robot('Robot4','B', 3, 260).
gripper('Gripper1', 'B', 50).
gripper('Gripper2', 'A', 60).
gripper('Gripper3', 'C', 30).
gripper('Gripper4', 'C', 80).
gripper('Gripper5', 'A', 20).
gripper('Gripper6', 'B', 30).
gripper('Gripper7', 'C', 90).
query_robots :-robot(Name,Interface,Size,Price), write('The database contains: '),write(Name), write(', with Interface: '), write(Interface), write(', Size: '), write(Size) , write(', Price: '), write(Price), nl, fail.
`
var parsed = session.consult(code_pl)
var query = session.query('query_robots.')
function inform(msg) {
show_result1.innerHTML += '<div>' + msg + '</div>'
}
var count_answers = 0
var callback = function(answer) {
if (answer === false) {
inform('DONE, #answers='+count_answers)
return
}
if (answer === null) {
inform('TIMEOUT, #answers='+count_answers)
return
}
// loop
++count_answers
inform(pl.format_answer(answer))
session.answer(callback)
}
// start the query loop
session.answer(callback)
}
</script>
<div id=show_result1>
<h3>results show here...</h3>
</div>
<button onclick="queryRG()">Click to run</button>
</body>
</html>