java - 如何使 NetLogo 5.x 中的扩展返回值 "nobody"?

标签 java netlogo

我创建了一些方法,允许我使用扩展将 turtle/breed 变量作为字符串并返回值(数字、 boolean 值、字符串、对象(例如列表/表)。但是,我希望它如果海龟/品种不具有变量名称,则失败安全并返回“nobody”。

我无法将 Java 的 null 转换为 NetLogo 的 nobody 但是这样在 NetLogo 环境中来自 netlogo 世界的 nobody(1) 和从扩展返回的 nobody(2) 是相同的.. 即:

没有人(1)=没有人(2)为真

部分代码:

import org.nlogo.api.*;
import org.nlogo.agent.Agent;
import org.nlogo.agent.AgentSet;
import org.nlogo.agent.Patch;
import org.nlogo.api.Nobody$;

public void load(PrimitiveManager primitiveManager) throws ExtensionException {
    primitiveManager.addPrimitive("get-variable-by-name", new GetVariableByName());
}

public static class GetVariableByName extends DefaultReporter{
    public String getAgentClassString() {
        return "OTPL";
    }

    public Syntax getSyntax() {
        return Syntax.reporterSyntax(
                new int[] { Syntax.AgentType(), Syntax.StringType() },
                Syntax.WildcardType());
    }

    public Object report(Argument[] args, Context arg1)
            throws ExtensionException, LogoException {

        Agent a = (Agent) args[0].get();
        String name = (String) args[1].get();

        Object value = null;

        for (int i = 0; i < a.getVariableCount(); i++ ){
            if (a.variableName(i).compareToIgnoreCase(name) == 0){
                value = a.getVariable(i);
                break;
            }
        }

        if (value instanceof Double){
            return (Double) value;
        }
        else if (value instanceof Boolean){
            return (Boolean) value;
        }
        else if (value instanceof String){
            return (String) value;
        }
        else if (value == null){
            return Nobody$.MODULE$;  // I've tried several things here, but none work.  This the last attempt.
        }
        else {
            return value;
        }
    }
}

最佳答案

我相信

return Nobody$.MODULE$;

实际上是正确的。

为什么一开始它对您不起作用很难猜。重建扩展后,有必要重新启动 NetLogo 或使用 __reload-extensions 命令使更改生效——也许你没有这样做?

关于java - 如何使 NetLogo 5.x 中的扩展返回值 "nobody"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837358/

相关文章:

netlogo - “Expecting a constant value” 构建NetLogo列表时出错

netlogo - NetLogo 可以处理数百万个座席吗?

netlogo - 如何选择netlogo中列表的前n项

java - 安装 JMeter 的问题

后代列表上的 Java 二进制搜索算法

java - 如何找出 JMS 主题的所有订阅者都已回复?

boolean - 在 Netlogo 中,是否可以将 boolean 变量列表从 true/false 转换为 1/0?

java - GWT AsyncCallback 不填充静态成员

java - Spring 数据 MongoDB : aggregation framework - sort with nested property throws invalid reference

Netlogo:从其他两个列表的关系中获取第三个列表