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<Song> mySongs;
    public Album( String artist, String title, List<Song> 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<Song> getSongs(){
        return mySongs;
    }
    public void setSongs( List<Song> 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<Song> aftermath = new ArrayList<Song>();
        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

Reply via email to