Search code examples
jenainferencejena-rules

How to change the rules for inference


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.


Solution

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