-5

I need compare two objects of the same class in one rule drools. But, how I can know that one atribute belongs to object created in main class? I need help!

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;
    }
}

I want compare the atribute nameConceptDeontic instanced in objects deoCon1 and deoCon2 in one rule. How I can do this?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Jean Zahn
  • 1
  • 1
  • where is that attribute? is it a member of the class ConceptDeontic? what type is it? maybe show use the code of ConceptDeontic. – Philipp Sander Sep 12 '13 at 21:24
  • You can do that with .... equals? deoCon1.getNameConceptDeontic().equals(deoCon2.getNameConceptDeontic()); I'm assuming the attributes are String. – n3k0 Sep 12 '13 at 22:11
  • Look, i'm using the Jboss Drools. I instance two objects (deoCon1 and deoCon2). In my rule (Rules.drl), I need Compare the attributes of my instances objects. The attributes (nameDeonticConcepts) belongs to my class (DeonticConcept). I create the getts and setts in my class. But, I try use my instance in my rules and a message error is presents. **BuildError: Unable to resolve ObjectType deoCon1.getNameConceptDeontic'** My Rule have a condiction: when deoCon1.getNameConceptDeontic() == deoCon2.getNameConceptDeontic() – Jean Zahn Sep 12 '13 at 22:26

2 Answers2

0

I think you're asking, "How can I determine instanceFoo's class?" but the grammar is hard to decipher.

If so, use the instanceof operator. In your case, instanceFoo will be the attribute's parent object:

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

Or you can try the getClass() method, which is explained in detail at http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

JaneGoodall
  • 1,478
  • 2
  • 15
  • 22
0

Something like this?

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

Note that the above will activate twice. For 1&2 and for 2&1. If you need to prevent that, you may wish to block a second activation. One way to do this would be to insert a MatchedConcepts fact and add an additional constraint that such a fact does not exist.

Steve
  • 9,270
  • 5
  • 47
  • 61