你好,我的项目有问题!
我们在序言中有一个知识库,我们必须创建一个Web应用程序才能从KB添加,编辑或删除规则。我的问题是,我使用以下代码以KB格式添加规则(使用html):
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>'
]).
当我按下ADD按钮时,会出现以下情况:
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.
之后,我进入我的知识库,我看到了:
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'.
规则必须如下所示:
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'.
我想删除引号,但不能删除。
当前使用term_to_atom/2删除引号,但这在我的知识库中为我提供
rule(rid21, [_, _, _, _, _, _, _, _, _], B) :-
true,
atom_number(_, A),
A>100,
B='Evales poli megalo koresmo'.
答案 0 :(得分:1)
最后我找到了解决方案!!!
%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>'
]).
当我从页面中获取数据时,我使用addNewRule
并在此处进行处理。 max_ruleId/1
和rules/1
来自我的知识库,第一个包含规则之和,另一个是包含所有ID的list
。
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.
连接字符串的谓词:
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.
基本上,我从 HTML 中获取输入(使用http_parametres/2),我使用string_list_concat
将所有输入合并到一个字符串列表中
最后使用term_string/2将规则声明到我的知识库中
将字符串转换为术语,而引号消失了。也许我的问题应该有所不同。