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?
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:Or you can try the
getClass()
method, which is explained in detail at http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html