Hello I have problem in my project !
We have a Knowledge Base in prolog and we must make a web app to add,edit or remove rules from KB. My problem is when I add the rule (using html) in KB using this code:
addRule(_Request):-
format('Content-type: text/html~n~n'),
print_html([
'<html>
<head>
<link rel="stylesheet" type="text/css" href="/materialize/css/materialize.css">
<script src="/materialize/js/materialize.js"></script>
<title>Rules</title>
</head>
<body>
<h4>Εισαγωγή κανόνα</h4>
<form action="/addition" method="POST">
Δώσε τα δεδομένα :
<div class="input-field inline">
<input type="text" name="newRuleData" class="validate">
</div>
Δώσε τις προυποθέσεις :
<div class="input-field inline">
<input type="text" name="newRuleCon" class="validate">
</div>
Δώσε το αποτέλεσμα :
<div class="input-field inline">
<input type="text" name="newRuleResult" class="validate">
</div>
<button class="black-text btn waves-effect waves-light green" type="submit" name="action">ADD</button>
<br><br><br>
</form>
</body>
</html>'
]).
When i press ADD button goes here:
addition(Request):-
http_parameters(Request,[
newRuleData(RuleData,[default('NULL')]),
newRuleCon(RuleCon,[default('NULL')]),
newRuleResult(RuleResult,[default('NULL')])
]),
%προσθήκη max_ruleId
max_ruleId(MaxID),
NewMaxID is MaxID+1,
atom_concat(rid,NewMaxID,NewRuleId),
retract( max_ruleId(MaxID) ),
asserta( max_ruleId(NewMaxID) ),
%προσθήκη στα Rules
rules(List),
append(List,[NewRuleId],NewList),
retract(rules(List)),
asserta(rules(NewList)),
assertz((rule(NewRuleId,RuleData,Answer):- RuleCon,Answer=RuleResult)),
saveRules.
After, I go in my KB and i see this:
rule(rid15, '[LabValues,Saturation,Nitrate,Oligochaetes,Sediments,Hydrothio,Methanio,Substrates,SmellWater]', A) :-
'LabValues = nai, atom_number(Saturation,Sat), Sat>100',
A='Evales poli megalo koresmo'.
The rule must look like this:
rule(rid15, [LabValues,Saturation,Nitrate,Oligochaetes,Sediments,Hydrothio,Methanio,Substrates,SmellWater], A) :-
LabValues = nai, atom_number(Saturation,Sat), Sat>100,
A='Evales poli megalo koresmo'.
I want to remove the quotes but I can't.
Currently using term_to_atom/2 to remove the quotes but it gives me this in my KB
rule(rid21, [_, _, _, _, _, _, _, _, _], B) :-
true,
atom_number(_, A),
A>100,
B='Evales poli megalo koresmo'.
Finally, I found solution !!!
%We take the request from Form
addition(Request):-
member(method(post),Request),!,
http_parameters(Request,
[
newRuleData(RuleData,[length>0, string]),
newRuleCon(RuleCon,[length>0, string]),
newRuleResult(RuleResult,[length>0, string])
]),
addNewRule(RuleData,"Answer",RuleCon,RuleResult),
format('Content-type: text/html~n~n'),
print_html([
'<html>
<head>
<link rel="stylesheet" type="text/css" href="/materialize/css/materialize.css">
<script src="/materialize/js/materialize.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>Επιτυχία</title>
</head>
<body>
<div class="navbar-fixed">
<nav>
<div class="nav-wrapper teal lighten-2">
<a href="#!" class="brand-logo center">WebApp της εργασίας 2</a>
</div>
</nav>
</div>
<div class="container center">
<h4>Η εισαγωγή του κανόνα πέτυχε.</h4>
<br><br>
<a class="waves-effect waves-light btn-small" href="http://localhost:8000/update"><i class="material-icons left" >arrow_back</i>Back</a>
<a class="waves-effect waves-light btn-small" href="http://localhost:8000"><i class="material-icons left" >home</i>Home</a>
<br><br><br>
<h3>Μάνος Κουτουλάκης 4002</h3>
</div>
</body>
</html>'
]).
When I took the data from my page I use the addNewRule
and here we make processes. the max_ruleId/1
and the rules/1
coming from my KB the first one have inside the sum of rules and the other one it's a list
with all id's inside.
addNewRule(RuleData,RuleResultVariableName,RuleCon,RuleResult):-
%προσθήκη max_ruleId
max_ruleId(MaxID),
NewMaxID is MaxID+1,
atom_concat(rid,NewMaxID,NewRuleId),
retract( max_ruleId(MaxID) ),
asserta( max_ruleId(NewMaxID) ),
%προσθήκη στα Rules
rules(List),
append(List,[NewRuleId],NewList),
retract(rules(List)),
asserta(rules(NewList)),
atom_string(NewRuleId, SRuleId),
string_list_concat(
[
"rule(", SRuleId, ",", RuleData, ",", RuleResultVariableName,
"):-",
RuleCon, ",", RuleResultVariableName, "='", RuleResult, "'"
],
StringRule
),
term_string(Rule, StringRule),
assertz(Rule),
saveRules.
The predicate to concat the String:
string_list_concat([], S):-
S = "".
string_list_concat([H], S):- !,
string(H),
S = H.
string_list_concat([H|T], S):-
string_list_concat([H|T], "", S).
string_list_concat([H|T], Acc, S):-
string(H),
string_concat(Acc, H, Acc1),
string_list_concat(T, Acc1, S).
string_list_concat([], Acc, S):-
S = Acc.
Essentially I take the inputs from HTML (Using http_parametres/2), I union all inputs in one String list using string_list_concat
and finally to assert the rule into my KB I use term_string/2
to converse the string to term and the Quotes has gone. maybe my question should have been different.