Here is my main file:

import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
 
import java.util.List;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.vocabulary.*;
import com.hp.hpl.jena.datatypes.xsd.*;
import com.hp.hpl.jena.rdf.listeners.ObjectListener;
import com.hp.hpl.jena.rdf.listeners.ChangedListener;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.shared.DoesNotExistException;
import com.hp.hpl.jena.graph.query.Query;
import com.hp.hpl.jena.reasoner.rulesys.BuiltinRegistry;
 
 
public class TestFamilyOWL {
  
   // Base URI and
namespace declarations
 
   static final String
baseURI = "http://family.org/family";;
   static final
String      ns =
"http://family.org/family#";;
 
   // Ontology model
declaration ... 
 
   private OntModel
model;
   private
Ontology  onto;
   private OntClass
person;
   private
ObjectProperty hasFather, hasSon;
   private
ModelChangedListener L;
   private Individual
mark, chris;
 
  
   //
============================================================
   // Constructor
method to create an empty model and ontology ...
   //
============================================================
 
   public
TestFamilyOWL() {
 
      // Create
ontology model ....
 
      model =
ModelFactory.createOntologyModel();
      onto  =
model.createOntology( baseURI );
                  L = new MyModelChangedListener();
      model.register(
L );
                 
   }
 
   //
============================================================
   // Manually
assemble the people ontology ..
   //
============================================================
 
   public void
buildOntology() {
 
      // Define
classes ...
 
      person =
model.createClass( ns + "Person");
       
      // Create object
properties for the class Person ...
                  
                  hasSon = model.createObjectProperty(ns +
"hasSon");
                  hasSon.setDomain(person);
                  hasSon.setRange(person);
 
   }
   //
============================================================
   // Create
individuals ...
   //
============================================================
 
   public void
addIndividuals() {
 
      // Add mark,
chris and nina to the model ...
 
      mark   = person.createIndividual(ns +
"Mark");
      chris  = person.createIndividual(ns +
"Christopher");
      
                  
                  // Create statements "Mark has son
Christopher" 
      Statement
markSon = model.createStatement( mark, hasSon, chris );
      model.add (  markSon );
                  
   }
 
   
  //
============================================================
  // Now let's
transform the model with input rules ....
  // ============================================================
 
  public void
addRules() {
     String inputRules
= "src/rules/family.rules";
     File f = new File
( inputRules );
 
     if (f.exists() ==
true ) {
        
System.out.printf("Reading file: %s\n", inputRules );
        
List<Rule> rules = Rule.rulesFromURL("file:" +
inputRules );
        
GenericRuleReasoner r = new GenericRuleReasoner(rules);
        
r.setOWLTranslation(true);           
         r.setTransitiveClosureCaching(true);
 
         InfModel
infmodel = ModelFactory.createInfModel(r, model);
        
model.add(infmodel.getDeductionsModel());
        
System.out.println("Model size = " + model.size());
     } else
        
System.out.println("ERROR: That rules file does not exist.");
  }
  public void
printStatements( String sTitle ) {
      Selector s = new
SimpleSelector ( (Resource) null, (Property) null, (RDFNode) null );
      printStatements(
sTitle, s );
   }
 
   public void
printStatements( String sTitle, Selector s ) {
      int statementNo
= 1;
 
     
System.out.printf("Statements: %s\n", sTitle );
     
System.out.printf("=============================================================\n");
     
      StmtIterator
iter = model.listStatements(s);
 
      while
(iter.hasNext()) {
         Statement
stmt      = iter.next();         // get next statement
         Resource  subject  
= stmt.getSubject();   // get the
subject
         Property  predicate = stmt.getPredicate(); // get the
predicate
         RDFNode   object   
= stmt.getObject();    // get the
object
 
         // Objects
can be either Resources or Literals ....
 
        
System.out.printf("Statement[ %3d]\n", statementNo );
        
System.out.printf(" 
Subject  : %s \n",
subject.toString());
        
System.out.printf(" 
Predicate: %s \n", predicate.toString());
 
         if (object
instanceof Resource) {
           
System.out.printf(" 
Object   : %s \n", object.toString());
         }
 
         if (object
instanceof Literal) {
           
System.out.printf(" 
Object   : \"%s\"
\n", object.toString());
         }
 
         statementNo =
statementNo + 1; 
      }
 
     
System.out.println("=============================================================");
     
System.out.println("");
  }
  
  //
============================================================
  // Retrieve and
print asserted/inferred RDF types ....
  //
============================================================
 
   public void
printResources( String sTitle, String sProperty, Property p ) {
      int statementNo
= 1;
 
     
System.out.printf("Resources: %s\n", sTitle );
     
System.out.printf("=============================================================\n");
 
      // Select all
the resources with specified property
 
      ResIterator iter
= model.listSubjectsWithProperty( p );
     
System.out.printf("The database contains %s for: \n",
sProperty );
      if
(iter.hasNext()) {
          while
(iter.hasNext()) {
             Resource
r = iter.nextResource();
            
System.out.printf( "Individual: %s has %s %s\n", r.toString(),
sProperty,
                           
     r.getProperty( p
).getString() );
          }
      } else {
         
System.out.printf("No %s were found in the database ...\n",
sProperty );
      }
 
     
System.out.println("=============================================================");
      System.out.println("");
  }

  public static void
main(String args[]) {
 
    TestFamilyOWL
family = new TestFamilyOWL();
   
family.buildOntology();
   
family.addIndividuals();
    family.addRules();
   
family.model.write(System.out, "RDF/XML" );
   Selector s= new SimpleSelector ((
Resource)family.chris,(Property) null,(RDFNode)null);
Family.printStatements(“Chris …”,s);
  }
}

This is family.rules:
@prefix af:  <http://family.org/family#>.
@prefix rdf:
<http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
 
// Rule 01:
Father-Son Relationship
 
[ hasSon:    (?f rdf:type af:Person) (?f af:hasSon
?s)-> (?s af:hasFather ?f)]



if you look in the results, chris hasFather mark (thus rule is checked). 
However,i do not see the print statement as a result of this change. 
MyModelChangedListerner.jave is an implementation of ModelChangedListener.java 
that has a print statement for the function addedStatement(Statement s).
 
 Thanks for your help,
-P.



On Thursday, November 7, 2013 12:42 PM, Chris_Dollin <[email protected]> 
wrote:
 
On Thursday, November 07, 2013 09:03:56 AM Parastoo Delgoshaei wrote:
> This listener does not work if the result of change to the model is due to
> rule checking. i.e. assume I have this rule:
> @ prefix af: <http://personont.org/person#>.
> ....
> [GetAge  (?a rdf:type af:Person) (?a af:hasBirthDate ?b) getAge(?b,?d)->(?a
> af:hasAge ?d)]
> 
> getAge() is a built-in that is attached to the model. When I query the
> individuals they have "hasAge" property with the correct value associated
> with their birthdates. However, the listener is not triggered when this
> change happens at the rule level. 

You still haven't show your code. By "code" I mean a minimal complete example.
I suspect you're listening to the wrong model, but since I can't see how
you make the models and attach the listeners and run the inference I
can't see where any problem might be.

If you do rule-based inference on a model M, then no changes are
made /to M/, so if that's what you're listening to you'll hear nothing.

Give us a minimal, complete example and we can run it, tinker with it,
and use it as a test case if there's actually a problem to fix.


> On Wednesday, November 6, 2013 6:55 AM, Chris Dollin
> <[email protected]> wrote:
> On Wednesday, November 06, 2013 05:21:23 PM Dibyanshu Jaiswal wrote:
> > I feel if you register the model with a change listener as disscussed
> > earlier, like: model.register(changeListener); which has proper
> > implementation of the abstrat methods defined in ModelChangedListener
> > (atleast for Statementadded()), you will be able to detect every changes
> > made to your model.
> 
> "rule checking" suggests inference, which suggests a base model and
> a deductions model, which suggests there are two models, only one
> of which is updated -- if that's the listened-to model, everything will
> be quiet. Too quiet.
> 
> Hence my request to show code ...
> 
> Chris
> 
> > On Tue, Nov 5, 2013 at 8:28 PM, Chris Dollin
> > 
> > <[email protected]>wrote:
> > > On Tuesday, November 05, 2013 06:53:17 AM Parastoo Delgoshaei wrote:
> > > > Claude,
> > > > Thanks for your response. This solution works only if you have hard
> > > 
> > > coded add/removal statements (i.e. model.add( S, P, O2 ); or
> > > model.remove(s);) However, I do not see any add and removal
> > > notifications
> > > as a result of rule checking. The model certainly has changed I see it
> > > in
> > > the print out after the rule checking. However, the listener does not
> > > recognize this add/remove.
> > > 
> > > > Your help is appreciated,
> > > 
> > > Show code.
> > > 
> > > (Are you listening to the model that's being changed?)
> > > 
> > > Chris
> > > 
> > > --
> > > "The wizard seemed quite willing when I talked to him."  /Howl's Moving
> > > Castle/
> > > 
> > > Epimorphics Ltd, http://www.epimorphics.com
> > > Registered address: Court Lodge, 105 High Street, Portishead, Bristol
> > > BS20
> > > 6PT
> > > Epimorphics Ltd. is a limited company registered in England (number
> > > 7016688)

Reply via email to