Repast Java:创建自定义边缘代理以安排特定操作

时间:2019-08-22 02:53:09

标签: repast-simphony

我有一个模型,在不同种类的其他代理(对象)之间有很多边(链接)。我想将这些边缘建模为代理,以便在其中添加属性和安排操作。看看如何完成这项工作的简单示例会很有帮助?

更新: 我按照您的指示进行操作,并在运行模型时出现错误:

FATAL [Thread-2] 12:45:02,901 repast.simphony.ui.GUIScheduleRunner - RunTimeException when running the schedule
Current tick (1.0)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at repast.simphony.engine.schedule.DynamicTargetAction.execute(DynamicTargetAction.java:72)
    at repast.simphony.engine.controller.ScheduledMethodControllerAction$ScheduleMethodAllAction.execute(ScheduledMethodControllerAction.java:333)
    at repast.simphony.engine.schedule.DefaultAction.execute(DefaultAction.java:38)
    at repast.simphony.engine.schedule.ScheduleGroup.executeList(ScheduleGroup.java:205)
    at repast.simphony.engine.schedule.ScheduleGroup.execute(ScheduleGroup.java:231)
    at repast.simphony.engine.schedule.Schedule.execute(Schedule.java:352)
    at repast.simphony.ui.GUIScheduleRunner$ScheduleLoopRunnable.run(GUIScheduleRunner.java:52)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
    at jzombies.Zombie$$FastClassByCGLIB$$6141f31.invoke(<generated>)
    at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53)
    at repast.simphony.engine.schedule.DynamicTargetAction.execute(DynamicTargetAction.java:69)
    ... 7 more
Caused by: java.lang.NullPointerException
    at repast.simphony.query.PropertyGreaterThan.createPredicate(PropertyGreaterThan.java:72)
    at repast.simphony.query.AbstractPropertyQuery.query(AbstractPropertyQuery.java:83)
    at jzombies.Zombie.query_energy(Zombie.java:141)
    at jzombies.Zombie.step(Zombie.java:67)
    ... 10 more

我认为它受到Zombie中此方法的影响:(但我不知道哪里出了问题,因为错误消息未提供具体说明)

    public void query_energy() {
//      Zombie this_zombie = new Zombie (space, grid, 9999);
        Context<Object> context = ContextUtils.getContext(this);
        Query<Object> query = new PropertyGreaterThan<Object>(context, "id", 2);
        for (Object o : query.query()) {
            Zombie h = (Zombie)o;
            System.out.println("zombie id: " + h.getID());
        }

    }

1 个答案:

答案 0 :(得分:3)

这是一个基于JZombies_Demo模型的示例。

首先创建一个CustomEdge类。

const Parent = () => {
  const [isLoading, setIsLoading] = useState(false);
  const [setDataToDownload, dataToDownload] = useState();
  return (
    <>
      {isLoading && <Spinner />}
      <Dropdown>
        {({ close }) =>
          options.map(op => (
            <ChildOption
              isLoading={isLoading}
              setDataToDownload={setDataToDownload}
              dataToDownload={dataToDownload}
              setIsLoading={setIsLoading}
              close={close}
              op={op}
            />
          ))
        }
      </Dropdown>
    </>
  );
};

const ChildOption = ({ close, isLoading, setIsLoading }) => {
  return (
    <div
      onClick={async () => {
        close();
        setIsLoading(true);
        const data = await fetchSomeStuff();
        setDataToDownload(data);
        setIsLoading(false);
      }}
    >
      {op.name}
    </div>
  );
};

然后创建CustomEdgeCreator类:

package jzombies;

import repast.simphony.space.graph.RepastEdge;

public class CustomEdge<T> extends RepastEdge<T> {

    private double customProperty;

    public double getCustomProperty() {
        return customProperty;
    }

    public void setCustomProperty(double customProperty) {
        this.customProperty = customProperty;
    }

    public CustomEdge(T source, T target, boolean directed, double weight) {
        super(source, target, directed, weight);
    }

    public CustomEdge(T source, T target, boolean directed) {
        super(source, target, directed);
    }

    public void customAction() {
        // define custom action here
    }
}

然后,当您在JZombiesBuilder类中定义NetworkBuilder时,将提供一个CustomEdgeCreator实例:

package jzombies;

import repast.simphony.space.graph.EdgeCreator;

public class CustomEdgeCreator<T> implements EdgeCreator<CustomEdge<T>, T> {

    public Class<CustomEdge> getEdgeType() {
        return CustomEdge.class;
    }

    public CustomEdge<T> createEdge(T source, T target, boolean isDirected, double weight) {
        return new CustomEdge<T>(source, target, isDirected, weight);
    }

}

这时,每当您向网络添加边缘时,您就可以访问边缘实例并安排您定义的任何自定义操作,例如Zombie类中的此类操作:

NetworkBuilder<Object> netBuilder = new NetworkBuilder<Object>(
                "infection network", context, true).setEdgeCreator(new CustomEdgeCreator());
        netBuilder.buildNetwork();

我希望这会有所帮助。