Re: JESS: Java and Jess: Hashtable etc.

2007-10-20 Thread Robert Kirby
When you add (assert) a Hashtable fact, there's only a single fact.  The Jess 
engine can act on the existence of that fact and the top-level values of slots, 
not on methods on the contents of slots, such as Hashtable::containsKey(object 
key) or Hashtable::get(object key).  You probably would be better off with each 
pair in your proposed Hashtable to be asserted as a separate fact instead.  
Since the Jess database of facts offers the functionality of Hashtables and 
more, working with Hashtables as facts seems inappropriate.

Instead of having processTable be a member variable of ImportModel, define a 
Jess template.
(deftemplate process-table
"Example process item with an ID and an attribute (more can be added)."
(declare (ordered FALSE))
(slot id (type LEXEME))
(slot attribute (type LEXEME)))
Jess rules and Java defquery related-calls can then access such a template.

Reading Jess in Action Rule-Based Systems in Java by Ernest Friedman-Hill 
should be useful (ISBN 1-930110-89-8 $49.95US).

Bob Kirby

At 06:58 AM 10/19/2007, waschtl34 wrote:

>Hello, 
>I am working on Java/Jess project for my diploma thesis. I am still not a
>Java Pro and I am completely new with Jess or any other RuleEngine/Clips
>stuff.
>Let me describe my problem for you. I have already integrated the Jess Rule
>Engine and it is running.
>
>In Java I have a Class like this:
>public class ImportModel{
>
>
>//this is a class variable:
> private Hashtable > processTable;
>
>
>
>// I fill this hashtable with some data in the "Class-Body"
>// there also exists a getter fort his hashtable.
>}
>
>I also have a Class like this:
>
>public class OwlContainer {
>
> private ImportModel repositoryModel;
> private Hashtable > repositoryProcessTable;
>//the getters also exist
>
>//Load Repository:
>File repositoryFile = new File("D:/eigeneProjekte/test/test-s-t-s.owl");
>
>if (repositoryFile != null & repositoryFile.exists()){
>
>   repositoryModel = new ImportModel(repositoryFile);} 
>
>}
>
>My Ruleengine Class looks like this:
>
>public class RuleEngine {  
>
>  private Rete engine;
>
>  private WorkingMemoryMarker marker;
>
>  private OwlContainer owlContainer; 
>
>  public RuleEngine (OwlContainer aOwlContainer) throws JessException{ 
>
>
>//Create a Jess Rule Engine
>engine = new Rete();
>engine.reset(); 
>   // Load Rules into Engine
>
>String path =   
>"D:/eigeneProjekte/eclipse/.../.../bin/org/sempet/engine/rules.clp";
>
>engine.batch(path);
>
>owlContainer = aOwlContainer;
>
>//now  the problems begin 
>
>//do I have to Add the owlContainer, the ImportModel or only the HashMap?
>
>//like this:
>
>engine.add(owlContainer);
>engine.add(owlContainer.getImportModel());   
>engine.add(owlContainer.getRepositoryProcessTable());
>
>// or direktly an ImportModel object?
>
> 
>
>Now my question(s): can you give some code example how I can write a rule
>which refers to the hashtable? Perhabs a simple Example how I can define a
>rule which just prints out the Individuals in the vector in the Value-part
>of the Hashtable? 
>
>What do I have to add to the rule engine? 
>
>And what kind of (deftemplate ImportModel  (declare(from-class
>ImportModel))) things do I have to define in the rules.clp?
>
>I hope you can help me out with my problem.
>Thanks alot
>Kind regards,
>Waschtl
>
>-- 
>View this message in context: 
>http://www.nabble.com/Java-and-Jess%3A-Hashtable-etc.-tf4653348.html#a13295080
>Sent from the Jess mailing list archive at Nabble.com.
>
>
>To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
>in the BODY of a message to [EMAIL PROTECTED], NOT to the list
>(use your own address!) List problems? Notify [EMAIL PROTECTED]
>


Re: JESS: Java and Jess: Hashtable etc.

2007-10-20 Thread Wolfgang Laun
Hello Waschtl,

Neither the code snippets nor the explanations you provided indicate
what sort of facts you want to process with your rules, so I'm
using classes of my own, which describe the facts I'm going to
use:

public class Song {
private String myTitle;
private String myAuthor;
public Song( String title, String author ){
myTitle = title;
myAuthor = author;
}
public String getTitle(){
return myTitle;
}
public void setTitle( String title ){
myTitle = title;
}
public String getAuthor(){
return myAuthor;
}
public void Author( String author ){
myTitle = author;
}
}

import java.util.List;
public class Album {
String myArtist;
private String myTitle;
List mySongs;
public Album( String artist, String title, List songs ){
myArtist = artist;
myTitle = title;
mySongs = songs;
}
public String getArtist(){
return myArtist;
}
public void setArtist( String artist ){
myArtist = artist;
}
public String getTitle(){
return myTitle;
}
public void setTitle( String title ){
myTitle = title;
}
public List getSongs(){
return mySongs;
}
public void setSongs( List songs ){
mySongs = songs;
}
}

My main does a few simple things along the lines of your main.
Notice the Rete add calls, where a Java object of one of my
fact classes is added, one at a time.

import java.util.ArrayList;
import java.util.List;
import jess.JessException;
import jess.Rete;
public class Project {
private static String DIR = ".../...";
public static void main( String[] args ) throws JessException {
System.out.println( "Start..." );
Rete engine = new Rete();
List aftermath = new ArrayList();
aftermath.add( new Song( "Mother's Little Helper", "Jagger/Richards"
) );
aftermath.add( new Song( "Out Of Time", "Jagger/Richards" ) );
aftermath.add( new Song( "Lady Jane", "Jagger/Richards" ) );
engine.add( new Album( "The Rolling Stones", "Aftermath", aftermath
) );
engine.add( new Song( "A Hard Day's Night", "Lennon/McCartney" ) );
for( Song s: aftermath ){
engine.add( s );
}

engine.batch( DIR + "shTemp.clp" );
engine.run();
System.out.println( "Stop!" );
}
}

If you want to add Java objects as facts, make sure that the classes
they stem from have getters and setters according to JavaBeans. (The notion
of adding a Hashtable isn't quite conformant with this.) See the
Jess manual's section 5.3, Shadow facts: reasoning about Java objects.

Now here are some simple rules:

(defrule SongByLennonMcCarney
?song <- (Song (author "Lennon/McCartney")( title ?title))
=>
(printout t ?title crlf)
)
(defrule AlbumWithSongByJaggerRichards
?jrsong <- (Song (author "Jagger/Richards")
 (OBJECT ?jrsongObj))
?album <- (Album (title ?title)
 (songs ?songs &: (?songs contains ?jrsongObj)))
=>
(printout t "album with a song by Jagger/Richards: " ?title crlf)
)

Notice the binding of OBJECT so that we can use the List method
"contains".


HTH
Wolfgang