-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.js
68 lines (53 loc) · 1.99 KB
/
bridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Create a Prolog Session
const session = pl.create();
// Load the prolog script
session.consult("knowledge-base.pl", {
success: () => {
console.log("Yay");
},
error: (err) => {
console.log("Something Happened:");
console.log(err);
}
});
// Functions to evaluate data
// Function to construct the prolog command
const makeQuery = (income, period, year) => `payment(${income},${period}, Whithold, Deposit,${year}).`;
// Get the fields to be used later
const incomeField = document.getElementById('income');
const periodField = document.getElementById('frequency');
const yearField = document.getElementById('year');
const resultsArea = document.getElementById('res');
const queryFailMessage = "Error: There is no information available in the Knowledge Base to answer this quey. This is likely due to missing US Income Tax data for previous years";
// Function to grab the data and pass it to prolog
const calc = () => {
// Get the data
const income = incomeField.value;
const period = periodField.value;
const year = yearField.value;
// Validate not empty
if(!income || income == null || income.length == 0) {
alert("Invalid input on Income");
return;
}
// Make the query
const q = makeQuery(income,period, year);
// Execute the query
console.log('Executing: ',q);
session.query(q);
// Tau Prolog requires this to be asyncronous
session.answer((answer) => {
console.log(answer);
if(!answer) {
// Query failed
alert(queryFailMessage);
return;
}
// Get the data from the answer
const deposit = answer.links.Deposit.value;
const tax = answer.links.Whithold.value;
resultsArea.innerText = `Amount to deposit: $${deposit}, taxes to withold: $${tax}`;
});
}
// Assign the calc lambda to button
document.getElementById('calcButton').onclick = calc;