如何更改推理规则

发布于 2024-10-21 05:53:41 字数 5041 浏览 3 评论 0原文

我正在使用 Eclipse 和 Jena 框架来开发 Web 应用程序。我的应用程序执行以下操作:

  1. 使用姓名、电子邮件、兴趣(C 语言、Java 等)、职业、用户名和密码信息注册新用户。

  2. 此信息存储在名为 user.rdf 的 rdf 文件中。

  3. 使用所需的新用户名和密码,将创建一个新用户帐户。新用户登录后,将根据用户的兴趣打开数据库中的所有相关书籍。

  4. 现在我需要向新用户推荐以下内容:

如果他/她对 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:

  1. Register the new users with the information of name, email, interest(C language, Java, etc),Occupation, Username and password.

  2. This information is stored in the rdf file named user.rdf.

  3. 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.

  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放血 2024-10-28 05:53:41

如果您使用比 RDF/XML 更简洁的表示法,答案就会变得更加清晰。复制 user.rdf 文件后,我执行了以下操作:(

java jena.rdfcat -out Turtle user.rdf

假设您已正确设置 CLASSPATH 环境变量)。生成的输出为:

@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix kb:      <http://protege.stanford.edu/kb#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

kb:Suresh
  a       kb:USER ;
  rdfs:label "Suresh" ;
  kb:Email "[email protected]" ;
  kb:Fname "Suresh" ;
  kb:Interest "Java" ;
  kb:Occupation "Agent" ;
  kb:Password "suresh" ;
  kb:Uname "suresh" .

kb:Raj
  kb:Email "[email protected]" ;
  kb:Interest "C Language" ;
  kb:Name "Raj" ;
  kb:Occupation "Student" ;
  kb:Password "lkj" ;
  kb:Uname "raj" .

kb:Anvika
  a       kb:USER ;
  rdfs:label "Anvika" ;
  kb:Email "[email protected]" ;
  kb:Fname "Anvika" ;
  kb:Interest "C Language" ;
  kb:Occupation "Student" ;
  kb:Password "anu" ;
  kb:Uname "anu" .

从中您可以看到 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:

java jena.rdfcat -out Turtle user.rdf

(this assumes you have your CLASSPATH environment variable set up correctly). The output this produces is:

@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix kb:      <http://protege.stanford.edu/kb#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

kb:Suresh
  a       kb:USER ;
  rdfs:label "Suresh" ;
  kb:Email "[email protected]" ;
  kb:Fname "Suresh" ;
  kb:Interest "Java" ;
  kb:Occupation "Agent" ;
  kb:Password "suresh" ;
  kb:Uname "suresh" .

kb:Raj
  kb:Email "[email protected]" ;
  kb:Interest "C Language" ;
  kb:Name "Raj" ;
  kb:Occupation "Student" ;
  kb:Password "lkj" ;
  kb:Uname "raj" .

kb:Anvika
  a       kb:USER ;
  rdfs:label "Anvika" ;
  kb:Email "[email protected]" ;
  kb:Fname "Anvika" ;
  kb:Interest "C Language" ;
  kb:Occupation "Student" ;
  kb:Password "anu" ;
  kb:Uname "anu" .

from which you can see that kb:Raj does not have rdf:type kb:USER, hence failing the first clause of your rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文