如何更改推理规则
我正在使用 Eclipse 和 Jena 框架来开发 Web 应用程序。我的应用程序执行以下操作:
使用姓名、电子邮件、兴趣(C 语言、Java 等)、职业、用户名和密码信息注册新用户。
此信息存储在名为 user.rdf 的 rdf 文件中。
使用所需的新用户名和密码,将创建一个新用户帐户。新用户登录后,将根据用户的兴趣打开数据库中的所有相关书籍。
现在我需要向新用户推荐以下内容:
如果他/她对 C 语言感兴趣,那么将向他推荐 C++ 书籍以及列表可以填充到屏幕上。
我知道这需要一个推理引擎,它需要事实和规则。事实将位于存储用户兴趣的 rdf 文件中。规则文件将根据规则显示何时完成推荐。
我有一个包含以下内容的 user.rdf 文件。
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:kb="http://protege.stanford.edu/kb#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Suresh">
<rdfs:label>Suresh</rdfs:label>
<kb:Uname>suresh</kb:Uname>
<kb:Password>suresh</kb:Password>
<kb:Occupation>Agent</kb:Occupation>
<kb:Interest>Java</kb:Interest>
<kb:Fname>Suresh</kb:Fname>
<kb:Email>[email protected]</kb:Email>
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Raj">
<kb:Email>[email protected]</kb:Email>
<kb:Name>Raj</kb:Name>
<kb:Password>lkj</kb:Password>
<kb:Uname>raj</kb:Uname>
<kb:Interest>C Language</kb:Interest>
<kb:Occupation>Student</kb:Occupation>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Anvika">
<rdfs:label>Anvika</rdfs:label>
<kb:Uname>anu</kb:Uname>
<kb:Password>anu</kb:Password>
<kb:Occupation>Student</kb:Occupation>
<kb:Interest>C Language</kb:Interest>
<kb:Fname>Anvika</kb:Fname>
<kb:Email>[email protected]</kb:Email>
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
</rdf:Description>
</rdf:RDF>
用户 Suresh 和 Anvika 实际上是在 Protege 中创建的,然后该文件将通过应用程序使用其他用户详细信息进行更新。
test.rules 文件具有以下内容:
@prefix kb: http://protege.stanford.edu/kb#
[likec++: (?s rdf:type kb:LikeC++)
<-
(?s rdf:type kb:USER)
(?s kb:Interest ?i)
regex(?i,'C Language')
]
现在出现的推理是
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:kb="http://protege.stanford.edu/kb#"
xmlns:j.0="http://protege.stanford.edu/kb#LikeC++"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Suresh">
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
<kb:Email>[email protected]</kb:Email>
<kb:Fname>Suresh</kb:Fname>
<kb:Interest>Java</kb:Interest>
<kb:Occupation>Agent</kb:Occupation>
<kb:Password>suresh</kb:Password>
<kb:Uname>suresh</kb:Uname>
<rdfs:label>Suresh</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Raj">
<kb:Occupation>Student</kb:Occupation>
<kb:Interest>C Language</kb:Interest>
<kb:Uname>raj</kb:Uname>
<kb:Password>lkj</kb:Password>
<kb:Name>Raj</kb:Name>
<kb:Email>[email protected]</kb:Email>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Anvika">
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
<kb:Email>[email protected]</kb:Email>
<kb:Fname>Anvika</kb:Fname>
<kb:Interest>C Language</kb:Interest>
<kb:Occupation>Student</kb:Occupation>
<kb:Password>anu</kb:Password>
<kb:Uname>anu</kb:Uname>
<rdfs:label>Anvika</rdfs:label>
<rdf:type rdf:resource="http://protege.stanford.edu/kb#LikeC++"/>
</rdf:Description>
</rdf:RDF>
由于具有
<rdf:type rdf:resource="http://protege.stanford.edu/kb#LikeC++"/>
推理的行向用户 Anvika 推荐了 LikeC++。但同样对 C 语言感兴趣的用户 Raj 却缺少同样的东西。我明白了这条线 导致仅推断出用户 Anvika。但这条线是通过 Protege 自动添加的。我的程序不这样做。那么我如何通过我的应用程序添加该行。如果这是不可能的,请告诉我如何更改规则以推断正确的结果。
请帮我。对此我感触良久。
I am using Eclipse with Jena framework to develop a Web Application. My application does the following:
Register the new users with the information of name, email, interest(C language, Java, etc),Occupation, Username and password.
This information is stored in the rdf file named user.rdf.
With the new desired username and password a new user account is created.The Login of the new user opens all the relevent books in the database as per the interest of the user.
Now I require to recommend the new user the following:
If he/she is interested in C Language then the books of C++ will be recommended to him and the list can be populated onto the screen.
I know this requires an inference engine which needs facts and rules. The facts will be in the rdf file which stores the interest of the users. The rules file will show based on the rules when the recommendation will be done.
I have an user.rdf file with the following contents.
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:kb="http://protege.stanford.edu/kb#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Suresh">
<rdfs:label>Suresh</rdfs:label>
<kb:Uname>suresh</kb:Uname>
<kb:Password>suresh</kb:Password>
<kb:Occupation>Agent</kb:Occupation>
<kb:Interest>Java</kb:Interest>
<kb:Fname>Suresh</kb:Fname>
<kb:Email>[email protected]</kb:Email>
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Raj">
<kb:Email>[email protected]</kb:Email>
<kb:Name>Raj</kb:Name>
<kb:Password>lkj</kb:Password>
<kb:Uname>raj</kb:Uname>
<kb:Interest>C Language</kb:Interest>
<kb:Occupation>Student</kb:Occupation>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Anvika">
<rdfs:label>Anvika</rdfs:label>
<kb:Uname>anu</kb:Uname>
<kb:Password>anu</kb:Password>
<kb:Occupation>Student</kb:Occupation>
<kb:Interest>C Language</kb:Interest>
<kb:Fname>Anvika</kb:Fname>
<kb:Email>[email protected]</kb:Email>
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
</rdf:Description>
</rdf:RDF>
The users Suresh and Anvika were actually created in Protege and then the file will be updated with the other user details through the application.
The test.rules file has the following:
@prefix kb: http://protege.stanford.edu/kb#
[likec++: (?s rdf:type kb:LikeC++)
<-
(?s rdf:type kb:USER)
(?s kb:Interest ?i)
regex(?i,'C Language')
]
The inference which comes is
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:kb="http://protege.stanford.edu/kb#"
xmlns:j.0="http://protege.stanford.edu/kb#LikeC++"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Suresh">
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
<kb:Email>[email protected]</kb:Email>
<kb:Fname>Suresh</kb:Fname>
<kb:Interest>Java</kb:Interest>
<kb:Occupation>Agent</kb:Occupation>
<kb:Password>suresh</kb:Password>
<kb:Uname>suresh</kb:Uname>
<rdfs:label>Suresh</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Raj">
<kb:Occupation>Student</kb:Occupation>
<kb:Interest>C Language</kb:Interest>
<kb:Uname>raj</kb:Uname>
<kb:Password>lkj</kb:Password>
<kb:Name>Raj</kb:Name>
<kb:Email>[email protected]</kb:Email>
</rdf:Description>
<rdf:Description rdf:about="http://protege.stanford.edu/kb#Anvika">
<rdf:type rdf:resource="http://protege.stanford.edu/kb#USER"/>
<kb:Email>[email protected]</kb:Email>
<kb:Fname>Anvika</kb:Fname>
<kb:Interest>C Language</kb:Interest>
<kb:Occupation>Student</kb:Occupation>
<kb:Password>anu</kb:Password>
<kb:Uname>anu</kb:Uname>
<rdfs:label>Anvika</rdfs:label>
<rdf:type rdf:resource="http://protege.stanford.edu/kb#LikeC++"/>
</rdf:Description>
</rdf:RDF>
Now due to the line having
<rdf:type rdf:resource="http://protege.stanford.edu/kb#LikeC++"/>
inference gives the user Anvika recommendation of LikeC++. But the same is missing for the user Raj who also is interested in C Language. I understood that the line
has caused only user Anvika to be infered. But this line was automatically added through Protege. My programme does not do that. So how do I add that line through my application. If this is not possible please tell me how do I change the rules to infer the correct results.
Please help me. I am struck at this for a long time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用比 RDF/XML 更简洁的表示法,答案就会变得更加清晰。复制
user.rdf
文件后,我执行了以下操作:(假设您已正确设置 CLASSPATH 环境变量)。生成的输出为:
从中您可以看到 kb:Raj 没有 rdf:type kb:USER,因此规则的第一个子句失败。
The answer becomes clearer if you use a less verbose notation than RDF/XML. After copying your
user.rdf
file, I did the following:(this assumes you have your CLASSPATH environment variable set up correctly). The output this produces is:
from which you can see that kb:Raj does not have
rdf:type kb:USER
, hence failing the first clause of your rule.