-5

我需要在一个规则中比较同一类的两个对象流口水。但是,我怎么知道一个属性属于在主类中创建的对象呢?我需要帮助!

public class CheckerMain {

    public static void main(String[] args) {
        try {
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "ConflictChecker");

            DeonticConcept deoCon1 = new DeonticConcept("forbidden");        
            DeonticConcept deoCon2 = new DeonticConcept("permission");

            ksession.insert(deoCon1);
            ksession.insert(deoCon2);

            ksession.fireAllRules();
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("Rules.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }
}

我想在一个规则中比较对象 deoCon1 和 deoCon2 中实例化的属性 nameConceptDeontic。我怎么能做到这一点?

4

2 回答 2

0

我想你在问,“我怎样才能确定 instanceFoo 的类?” 但是语法很难破译。

如果是这样,请使用instanceof运算符。在您的情况下, instanceFoo 将是属性的父对象:

if (instanceFoo instanceof classBar){
    //do stuff
}

或者你可以试试这个getClass()方法,详细解释在http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

于 2013-09-13T00:01:45.430 回答
0

像这样的东西?

rule "..."
when
    $dc1: DeonticConcept()
    $dc2: DeonticConcept(this != $dc1, nameConceptDeontic == $dc1.nameConceptDeontic)
then
    ...
end

请注意,以上将激活两次。对于 1&2 和 2&1。如果您需要阻止这种情况,您可能希望阻止第二次激活。做到这一点的一种方法是插入一个 MatchedConcepts 事实并添加一个额外的约束,该事实不存在。

于 2013-10-04T14:05:45.100 回答