第一次执行 execLinkedOp 时 CArtAgO (JaCaMo) 中的 java.lang.IllegalMonitorStateException

标签 java artificial-intelligence agent multi-agent

我遇到 java.lang.IllegalMonitorStateException,它只发生一次,在第一个 execLinkedOp 调用中。我想我缺少一些应该很好地同步 cartago 功能的 Jason 程序。可能 execLinkedOp 正在其末尾重新生成一些上下文,为下一次调用修复此条件。

下面的论坛说这种问题可能发生在缺少 o synchronized (foo) 调用的情况下。 http://www.jguru.com/faq/view.jsp?EID=128732

[counter] setValue invoked sucessfully! old: count(-1), received: -1 opid:opId(0,setValue,counter,robot)
[robot] New is (-1) old number is: count(-1)
[robot] Linking router and counter...
TRACE: Camel Artifact 'listenCamelRoutes' is true
DEBUG: Listening by reading the incoming queue...
DEBUG: InOpRequest received! Artifact: router, inc2
DEBUG: Adding in the inQueue: router: inc2
DEBUG: Message added in the incoming queue!
DEBUG: A message was founded in the incoming queue! Artifact:router, op:inc2, body []
DEBUG: Getting artifact id of router
DEBUG: Executing inc2 without parameters.
DEBUG: InOpRequest received! Artifact: counter, inc3
DEBUG: Adding in the inQueue: counter: inc3
DEBUG: Body received: [string...test...counter, 34]
DEBUG: Parameters details: [string...test...counter, 34]
DEBUG: Message added in the incoming queue!
DEBUG: A message was founded in the incoming queue! Artifact:counter, op:inc3, body [string...test...counter, 34]
DEBUG: Getting artifact id of counter
DEBUG: artifact name/id/type: counter/7/artifacts.Counter
DEBUG: Forwarding inc3 with following parameters: [string...test...counter, 34]
[counter] Counter:inc3 called! A tick signal is going to be send. Parameters: string...test...counter, 34
ERROR: Error on execLinkedOp with parameters!
cartago.OperationException: execLinkedOp failed java.lang.IllegalMonitorStateException
    at cartago.Artifact.execLinkedOp(Artifact.java:965)
    at camelartifact.CamelArtifact.receiveMsg(CamelArtifact.java:128)
    at camelartifact.CamelArtifact$ReadCmd.exec(CamelArtifact.java:203)
    at cartago.Artifact.await(Artifact.java:832)
    at camelartifact.CamelArtifact.setListenCamelRoute(CamelArtifact.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at cartago.ArtifactOpMethod.exec(ArtifactOpMethod.java:39)
    at cartago.Artifact.doOperation(Artifact.java:407)
    at cartago.Artifact.access$200(Artifact.java:32)
    at cartago.Artifact$ArtifactAdapter.doOperation(Artifact.java:1235)
    at cartago.WorkspaceKernel.serveOperation(WorkspaceKernel.java:1045)
    at cartago.WorkspaceKernel.access$000(WorkspaceKernel.java:49)
    at cartago.WorkspaceKernel$EnvironmentController.run(WorkspaceKernel.java:1359)
DEBUG: Forwarding with parameters done!
DEBUG: InOpRequest received! Artifact: router, inc2
DEBUG: Adding in the inQueue: router: inc2
DEBUG: Message added in the incoming queue!
DEBUG: A message was founded in the incoming queue! Artifact:router, op:inc2, body []

最佳答案

正如 Alessadro Ricci 所提议的,您获得的每个 java.lang.IllegalMonitorStateException 都应该取决于您尝试从不属于 CArtAgO 的线程调用 CArtAgO/工件代码这一事实。

假设您有自己的线程 T(CArtAgO 外部)想要访问工件 A 并调用一些公共(public)方法(例如,其目的是更新工件的 obs 状态..),然后,从你刚刚调用的线程代码:

art.beginExternalSession();  /* new primitive */
art.yourArtifactPublichMethod();
...
art.yourArtifactPublichMethod();
art.endExternalSession(true); /* new primitive. true = every thing was OK, false in the case of failures */

其中 art 是对工件 Java 对象的直接引用。

package c4jtest;

import cartago.*;

class ExtThread extends Thread {

    private ArtifactWithExtUse art;

    public ExtThread(ArtifactWithExtUse art){
        this.art = art;
    }

    public void run(){
        while (true){
            try {
                Thread.sleep(1000);
                art.beginExternalSession();
                art.externalInc();
                art.endExternalSession(true);
            } catch (Exception ex){
                art.endExternalSession(false);
            }
        }
    }

}

public class ArtifactWithExtUse extends Artifact {

    void init(){
        defineObsProperty("a",0);
        new ExtThread(this).start();
    }

    /* ext API */

    public void externalInc(){
        ObsProperty prop = this.getObsProperty("a");
        prop.updateValue(prop.intValue()+1);
    }

    @OPERATION void reset(){
        ObsProperty prop = this.getObsProperty("a");
        prop.updateValue(0);
    }

}

关于第一次执行 execLinkedOp 时 CArtAgO (JaCaMo) 中的 java.lang.IllegalMonitorStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045526/

相关文章:

java - 如何捕获类似于 alt 加打印屏幕的图像?

artificial-intelligence - Spotify web api 播放播放列表

python - 如何在 GPU 支持下运行 python 代码

artificial-intelligence - 人工智能博弈论中的功利主义与平等主义分配

java - Junit5测试同一接口(interface)的多个实现

java - 使用 GSON 将 JSON 附加到日志文件

java - DataInputStream 和 DataOutputStream,编译良好,但未按我想要的方式运行

python - 如何在不同的 (SNMPv3) 上下文中注册 MIB 模块。在SNMP代理端

java - JADE代理数据通信

Scrapy: USER_AGENT 和 ROBOTSTXT_OBEY 设置正确,但我仍然得到错误 403