Below is the code showing both, the Rete.getObjects/Filter technique and the
direct access using a Map in class Door. If you do have many facts, then the
getObjects/Filter will be indeed slow because it uses a sequential search
through all facts.
-W
On Tue, Dec 2, 2008 at 12:50 PM, John Chrysakis <[EMAIL PROTECTED]>wrote:
>
> How Can I Use getObjects ??
>
> rete.getObjects(new Filter.ByClass(A))??
>
>
> > I could do that, but the example suggests that the name attribute is
> > a unique key to an object. If these objects are static once they have
> been
> > created
> > or the retract actions aren't all over the place you might consider
> keeping
> > a Map
> > as a static variable within that class A which maps the name to A
> objects.
> >
>
> How, could you be more analytic?
>
>
> > Of course, if the number of facts in your WM is small, the pass over all
> > facts won't slow you down much.
>
>
> I don't prefer this solution because I would have a lot of facts.
>
>
>
>
(clear)
(import play.Door)
(import play.DoorFilter)
(deftemplate Door (declare (from-class Door)))
(reset)
(add (new Door "front" "closed"))
(add (new Door "back" "open"))
(add (new Door "barn" "open"))
(add (new Door "heaven's" "closed"))
(add (new Door "out" "closed"))
; This uses the filter to set all doors called "heaven's" to "barred"
;
(bind ?iter ((engine) getObjects (new DoorFilter "heaven's")))
(while (?iter hasNext)
(bind ?d (?iter next))
(?d setState "barred")
((engine) updateObject ?d)
)
; This uses the map to set the door called "back" to "broken"
;
(bind ?bd (Door.getByName "back"))
(?bd setState "broken")
((engine) updateObject ?bd)
(facts)
====================================================
package play;
import java.util.HashMap;
import java.util.Map;
public class Door {
private static Map<String,Door> ourName2Door =
new HashMap<String,Door>();
public static Door getByName( String name ){
return ourName2Door.get( name );
}
private String name;
private String state;
public Door( String name, String state ){
this.name = name;
this.state = state;
ourName2Door.put( name, this );
}
public String getName(){
return name;
}
public void setName( String name ){
this.name = name;
}
public String getState(){
return state;
}
public void setState( String state ){
this.state = state;
}
}
====================================================
package play;
import jess.Filter;
public class DoorFilter extends Filter.ByClass {
private String name;
public DoorFilter( String name ){
super( Door.class );
this.name = name;
}
@Override
public boolean accept( Object o ){
return super.accept( o ) && name.equals( ((Door)o).getName() );
}
}