Search Mailing List Archives


Limit search to: Subject & Body Subject Author
Sort by: Reverse Sort
Limit to: All This Week Last Week This Month Last Month
Select Date Range     through    

[protege-owl] problem with SWRLRuleEngineBridge

Hrishikesh Sharma hrishikesh.sharma at gmail.com
Wed Aug 8 08:17:32 PDT 2007


There seems to be a serious bug?? with with getAssertedProperties(). I
was suggested(see the mail chain below) to extend SWRLJessBridge class
to do what I required.
I am using the attached ontology and sample instances to run some rules.
When I use the Protege editor(front-end) and use JessTab and execute
JESS rule engine, I can see all the asserted properties, right ones,
in the appropriate pane as follows.

(assert (needsBatAlarm Lenovo "true"))
(assert (needsCharging Lenovo "true"))
(assert (possCharging Lenovo "true"))

While when I use SWRLJessBridge and enumerate all the asserted
properties, I get garbled output.

Lenovo
true
Lenovo
true
Didier_Bourse
true
Lenovo
true

The subclass that I implemented on your suggestion is also attached. I
am almost sure it is a bug with the bridge code. I desperately need to
have/demonstrate a standalone application in Java which uses
SWRLJessBridge. Please help.

Regards
         Hrishikesh.

On 7/25/07, Martin O'Connor <martin.oconnor at stanford.edu> wrote:
>
>
> The bridge is documented extensively here:
> http://protege.cim3.net/cgi-bin/wiki.pl?SWRLRuleEngineBridgeFAQ
>
> You can access protected methods using the standard Java subclassing
> technique, viz:
>
>
>
> public class MyBridge extends JessRuleEngineBridge
>
> {
>
>    .
>
>    public Set<RuleInfo> myGetImportedSWRLRules() { return
> getImportedSWRLRules(); }
>
>
>
> etc.
>
>
>
> Martin
>
>
>
>   _____
>
> From: protege-owl-bounces at lists.stanford.edu
> [mailto:protege-owl-bounces at lists.stanford.edu] On Behalf Of Hrishikesh
> Sharma
> Sent: Wednesday, July 25, 2007 7:21 AM
> To: protege-owl at lists.stanford.edu
> Subject: Re: [protege-owl] problem with SWRLRuleEngineBridge
>
>
> Essentially my problem is (at least) twofold:
> 1. The javadocs for SWRLRuleEngineBridge and SWRLJessBridge give too less
> information. Various methods I try to invoke based on some approximate
> understanding misfire!
> 2. How to get hold of the delta between new overall knowledge after a Jess
> run, and the old knowledge in Java code. In the front end, I am able to do
> same by examining the "Asserted Properties/Individuals" sub-tab within Jess
> Tab.
>
>
> Sincerely,
>
>         Hrishikesh.
>
>
> On 7/24/07, Martin O'Connor <martin.oconnor at stanford.edu> wrote:
>
>
> You are trying to call a protected method in this code fragment.
> Invoking through the SWRLJessBridge does not make it unprotected - only
> code inside the SWRLJessBridge can access it.
>
> Martin
>
> Hrishikesh Sharma wrote:
>
> > Hi,
> >     I am having problem with getAssertedProperties() method of
> > SWRLRuleEngineBridge Class. From the javadoc, I gather it is a
> > protected method. However, in the simple java code file attached,
> > where I invoke this method from a subclass called SWRLJessBridge, I
> > get a compilation error
> >
> > RunSwrlOnOnt.java:30: getAssertedProperties() has protected access in
> > edu.stanford.smi.protegex.owl.swrl.bridge.SWRLRuleEngineBridge
> >          Collection ass_p = bridge.getAssertedProperties();
> >                                   ^
> > It is a bit surprising, for SWRLJessBridge is supposed to be subclass
> > and hence have access to protected methods of the superclass from
> > within objects of that type. Am I missing something trivial here? Or
> > is it a bug(missing 'extends' definition for SWRLJessBridge)? Or is
> > the method getAssertedProperties() an abstract method?
> >
> > Sincerely,
> >        Hrishikesh
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ont+rule.owl
Type: text/xml
Size: 40289 bytes
Desc: not available
URL: <http://mailman.stanford.edu/pipermail/protege-owl/attachments/20070808/c618c92e/attachment.owl>
-------------- next part --------------

import edu.stanford.smi.protegex.owl.model.*;
import edu.stanford.smi.protegex.owl.swrl.bridge.jess.*;
import edu.stanford.smi.protegex.owl.swrl.bridge.exceptions.*;

// Archives needed for JESS, the rule reasoner
import jess.*;
import java.util.*;

public class MyBridge extends SWRLJessBridge
{
    public MyBridge( OWLModel model, Rete reteObj ) throws SWRLRuleEngineBridgeException
    {
      super( model, reteObj );
    }

    public Collection getProperties( )
    {
      return getAssertedProperties( );
    }
}
-------------- next part --------------
import edu.stanford.smi.protegex.owl.model.*;
import edu.stanford.smi.protegex.owl.model.impl.*;
import edu.stanford.smi.protegex.owl.ProtegeOWL;

// Archives needed for SWRL-JESS bridge
import edu.stanford.smi.protegex.owl.swrl.bridge.exceptions.*;
import edu.stanford.smi.protegex.owl.swrl.bridge.jess.*;
import edu.stanford.smi.protegex.owl.swrl.bridge.*;
// Archives needed for JESS, the rule reasoner
import jess.*;
import java.util.*;

public class RunSwrlOnOnt
{
    public RunSwrlOnOnt( Ont ont )
    {
      OWLModel owlModel = ont.getOnt( );
      // Populate the current time
      updateOnt( owlModel );

      Rete rete = new Rete();  // Exception handling: TBD
      try
      {
        bridge = new MyBridge( owlModel, rete );
      }
      catch( SWRLRuleEngineBridgeException e )
      {
        e.printStackTrace();
        System.out.println( e.getMessage() );
        System.out.println( e.toString() );
      }
    }

    public void fire( )
    {
      try
      {
        // Now work with the bridge
        bridge.importSWRLRulesAndOWLKnowledge();
        bridge.exportSWRLRulesAndOWLKnowledge();
        bridge.runRuleEngine();
      }
      catch( SWRLRuleEngineBridgeException e )
      {
        e.printStackTrace();
        System.out.println( e.getMessage() );
        System.out.println( e.toString() );
      }

    }

    public boolean takeAction( int batLevel )
    {
      boolean result = false;

      try
      {
        Collection newProps = bridge.getProperties( );
        Iterator i = newProps.iterator();
 
        // Now iterate and print
        do
        {
          PropertyInfo prop = (PropertyInfo) i.next();
          IndividualInfo sub = (IndividualInfo)prop.getSubject( );
          LiteralInfo pred = (LiteralInfo)prop.getPredicate( );
          // System.out.println( sub.getIndividualName( ) );
          // System.out.println( pred.getString( ) );
          if( sub.getIndividualName( ).equals( "possCharging" ) )
          {
            if( pred.isBoolean( ) && pred.getBoolean( ) )
            {
              result = true;
              break;
            }
          }
        }
        while( i.hasNext() );
      }
      catch( DatatypeConversionException e )
      {
        e.printStackTrace();
        System.out.println( e.getMessage() );
        System.out.println( e.toString() );
      }
      if( batLevel <= 20 )
        result = true;

      return result;
    }

    private void updateOnt( OWLModel model )
    {
      OWLDatatypeProperty hasTimeValueProperty =
                    model.getOWLDatatypeProperty("hasTimeValue");
      OWLIndividual currTime = model.getOWLIndividual( "currTime" );

      RDFSDatatype xsdDate = model.getRDFSDatatypeByName( "xsd:date" );
      RDFSLiteral dateLiteral = model.createRDFSLiteral
                                       ( Timing.getTime( ), xsdDate );
      currTime.setPropertyValue( hasTimeValueProperty, dateLiteral );

      return;
    }

    private MyBridge bridge;
}
-------------- next part --------------
/* This class encapsulates all things to do with an ontology */

/* References
TBD
*/

// Import Protege class for OWL
import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;

// For OWL model manipulation
import edu.stanford.smi.protegex.owl.jena.*;

// For URI handling
import java.net.*;

// For utilities such as collection
import java.util.*;

// For file i/o
import java.io.*;

// For file utils
import com.hp.hpl.jena.util.*;

public class Ont
{
    public Ont( )
    {
      model = null;
    }

    public Ont( String ontPath )
    {
      load( ontPath );
    }

    public boolean load( String ontPath )
    {
      try
      {
        String ontURI = getURI( ontPath );
        model = ProtegeOWL.createJenaOWLModelFromURI( ontURI );
      }
      catch(Exception except)
      {
        except.printStackTrace( );
        return false;
      }

      return true;
    }

    private String getURI( String absPath )
    {
      String uri = null;

      try
      {
        URL url = new URL( "file", null, absPath );
        uri = url.toURI().toString();
      }
      catch( MalformedURLException except )
      {
        except.printStackTrace( );
      }
      catch( URISyntaxException except )
      {
        except.printStackTrace( );
      }

      return uri;
    }

    public OWLModel getOnt( )
    {
      return model;
    }

    public boolean CreateDataTypeProp( )
    {
      OWLNamedClass personClass = model.createOWLNamedClass("Person");
      String fileName = "/root/ddrive/office/e2r/m2.3-docs/travel-saved.owl";
      Collection errors = new ArrayList();
      // OWLModel is just an interface; JenaOWLModel is a class
      // implementing it.
      ((JenaOWLModel)model).save(new File(fileName).toURI(), FileUtils.langXMLAbbrev, errors);
      System.out.println("File saved with " + errors.size() + " errors.");

      return true;
    }

    private OWLModel model;
}


More information about the protege-owl mailing list