Jess绑定了这个java对象的实例

时间:2015-01-15 10:51:07

标签: jess

我想在Jess做类似的事情:

(bind ?instance (this))

我使用它的唯一方法是使用" new Object"而不是"这"。

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:1)

实际上,有三种方法可以从Jess环境访问Pojo,第四种方法可以访问单个。

public class Main {
    public static Main theInstance;
    public static Main getInstance(){ return theInstance; }
    private String user = "Joe";
    public String getUser(){ return user; }

    public static void main( String[] args ) throws Exception {
        Main main = new Main();
        Main.theInstance = main;
        Rete rete = new Rete();
        // as a global
        Defglobal glob = new Defglobal( "*main*", new Value( main ) );
        glob.reset( rete );
        // as a store member
        rete.store( "main", main );
        // as a fact
        rete.add( main );
        // execute demo clp
        rete.batch( "two.clp" );
    }
}

.clp是:

(printout t "?*main* = " ?*main* crlf)
(printout t "?*main*.user = " (?*main* getUser) crlf)
(printout t "main = " (fetch main)  crlf)
(printout t "main.user = " ((fetch main) getUser) crlf)
(defrule get-main-user
    (Main (user ?user))
=>
    (printout t "a Main(slot user) = " ?user crlf)
)
(run)
(printout t "Main.theInstance = " ((call Main getInstance) getUser) crlf)

输出:

?*main* = <Java-Object:Main>
?*main*.user = Joe
main = <Java-Object:Main>
main.user = Joe
a Main(slot user) = Joe
Main.theInstance = Joe

答案 1 :(得分:0)

据推测,您正在将一些Jess代码传递给Rete.exec()方法,并希望引用正在进行调用的对象。你只需要自己注入Jess。使用Rete.store()将对象存储在Jess中,然后使用(fetch)从Jess代码中的存储中检索它,或者使用全局上下文对象的方法 - 请参阅Rete.getGlobalContext() - 来设置一个引用Java对象的变量,然后在Jess代码中使用该变量。