我使用Jason语言在两个代理之间进行通信。但是我无法使用发送操作,它会报错。
这是我的两个代理,
代理1:
// Agent Agent1 in project factorial3.mas2j/* Initial goals */!start./* Plans */+!start : true<- .print("starting.."); !query_factorial(2).+!query_factorial(X) : true <-.send(agent2,tell,giveme(X))./*+fact(X,Y) : true <-.print("factorial ", X, " is ", Y, " thank you expert").*/
代理2:
// Agent agent2 in project IdEx.mas2j/* Initial beliefs and rules *//* Initial goals */!begin./* Plans */+!begin : true <- .print("expert starting......."); !giveme(X).+!giveme(X):true <- !fact(X,Y); .print("Factorial of ", X, " is ", Y). //.send(agent1,achive,fact(X,Y)). +!fact(X,1) : X == 0.+!fact(X,Y) : X > 0<- !fact(X-1,Y1); Y = Y1 * X.
因此,当我在代理1中尝试调用发送操作时,我会得到这个错误,而代理2则会接收到错误信息。
[agent2] *** Error adding var into renamed vars. var=X, value=(_229-1).java.lang.ClassCastException: jason.asSyntax.ArithExpr cannot be cast to jason.asSyntax.VarTerm at jason.asSemantics.TransitionSystem.prepareBodyForEvent(TransitionSystem.java:877) at jason.asSemantics.TransitionSystem.applyExecInt(TransitionSystem.java:728) at jason.asSemantics.TransitionSystem.applySemanticRule(TransitionSystem.java:222) at jason.asSemantics.TransitionSystem.reasoningCycle(TransitionSystem.java:1429) at jason.infra.centralised.CentralisedAgArch.run(CentralisedAgArch.java:205) at java.lang.Thread.run(Thread.java:745)
回答:
如果你想请求一个代理执行一个计划(例如代理1中的+!query_factorial(X)...
),它应该是一个实现消息。取消注释“计划”+fact(X,Y) : true <- .print("factorial ", X, " is ", Y, " thank you expert")
,你应该在开头使用操作符!
将其变成一个计划。因此,如果我理解了你测试项目的总体思路,可以重写如下:
代理1代码:
!start.+!start : true<- .print("starting.."); !query_factorial(2).+!query_factorial(X) : true <- .send(agent2,achieve,giveme(X)).
代理2代码:
+!giveme(X):true <- !fact(X,Y); .print("Factorial of ", X, " is ", Y).+!fact(X,1) : X == 0.+!fact(X,Y) : X > 0 <- !fact(X-1,Y1); Y = Y1 * X.
你可以看到,我没有使用你原始代码中的“开始计划”,因为代理1在执行任务,使代理2在被请求时工作。