1

我有一个班级作业是使用 Drools 作为推理机来创建一个机器人。但是,我的大多数规则都表现得很奇怪,因为它们不是为类而触发,而是为它的超类而触发。像这样的东西:

我的规则:

import the.manifested.Robotonikku;
import the.manifested.Strategy;
import the.manifested.Action;

import robocode.TeamRobot;

rule "One"
    when
        Robotonikku();
    then
        System.out.println("roboto is present");
end

rule "Two"
    when
        not Robotonikku();
    then
        System.out.println("roboto is not present");
end

rule "Three"
    when
        TeamRobot();
    then
        System.out.println("robot is present");
end

rule "Four"
    when
        not TeamRobot();
    then
        System.out.println("robot is not present");
end

正如预期的那样

public class Robotonikku extends TeamRobot

在 Robocode 的模拟器调用的 Robotonikku 的 run() 方法中,我将实例作为事实插入:

ksession.insert(this)

我希望规则一和三应该触发,但规则二和三被触发。为什么它将实例识别为 TeamRobot 而不是 Robotonikku?

提前致谢。

加载代码:

    String ficheroReglas = System.getProperty("robot.reglas", RobotDrools.FICHERO_REGLAS);

    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder.add(ResourceFactory.newClassPathResource(ficheroReglas, RobotDrools.class), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors().toString());
    }

    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    ksession = kbase.newStatefulKnowledgeSession();
4

1 回答 1

1

Robocode 引擎将机器人加载到安全的类加载器中。加载到机器人 classLoader 中的类对 robocode 进程中的其他类加载器不可见。我猜你必须将drools加载到与机器人相同的classLoader中(最简单的方法是将机器人上的类合并到classPath并添加drools .class文件或合并jar)。我不确定 drools 是否仍能在 robocode 的安全限制下工作,因此您可能需要关闭 robocode 安全性。

于 2011-07-14T13:43:42.847 回答