use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testTraitdeleteOrder.
@Test
public void testTraitdeleteOrder() {
String drl = "" + "package org.drools.core.factmodel.traits.test;\n" + "\n" + "import org.drools.core.factmodel.traits.*;\n" + "import org.drools.traits.core.factmodel.*;\n" + "import java.util.*;\n" + "\n" + "declare trait A end \n" + "declare trait B extends A end \n" + "declare trait C end \n" + "\n" + "rule \"don\"\n" + "when \n" + " $e : Entity() \n" + "then\n" + " don( $e, A.class ); \n" + " don( $e, C.class ); \n" + " don( $e, B.class ); \n" + "end\n" + "";
KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession();
TraitFactoryImpl.setMode(mode, ksession.getKieBase());
FactHandle handle = ksession.insert(new Entity());
ksession.fireAllRules();
final ArrayList list = new ArrayList();
ksession.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent) {
}
public void objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent objectUpdatedEvent) {
}
public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectRetractedEvent) {
Object o = objectRetractedEvent.getOldObject();
if (o instanceof TraitProxyImpl) {
String traitName = ((TraitProxyImpl) o)._getTraitName();
list.add(traitName.substring(traitName.lastIndexOf(".") + 1));
}
}
});
ksession.delete(handle);
ksession.fireAllRules();
System.out.println(list);
assertEquals(Arrays.asList("B", "C", "A"), list);
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testTraitProxy.
@Test
public void testTraitProxy() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactoryImpl.setMode(mode, kb);
TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
impClass.set(imp, "name", "aaa");
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
assertNotNull(trait);
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
proxy.getFields().put("field", "xyz");
// proxy.getFields().put("name", "aaa");
assertNotNull(proxy);
TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait);
assertSame(proxy2, proxy);
TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, trait2);
assertNotNull(proxy3);
assertEquals("xyz", proxy3.getFields().get("field"));
assertEquals("aaa", proxy3.getFields().get("name"));
TraitableBean imp2 = (TraitableBean) impClass.newInstance();
impClass.set(imp2, "name", "aaa");
TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, trait);
// proxy4.getFields().put("name", "aaa");
proxy4.getFields().put("field", "xyz");
assertEquals(proxy2, proxy4);
} catch (InstantiationException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (LogicalTypeInconsistencyException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testWrapperKeySetAndValues.
@Test
public void testWrapperKeySetAndValues() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactoryImpl.setMode(mode, kb);
TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
impClass.set(imp, "name", "john");
proxy.getFields().put("surname", "xxx");
proxy.getFields().put("name2", "john");
proxy.getFields().put("nfield", null);
Set set = new HashSet();
set.add("name");
set.add("surname");
set.add("age");
set.add("school");
set.add("name2");
set.add("nfield");
assertEquals(6, proxy.getFields().keySet().size());
assertEquals(set, proxy.getFields().keySet());
Collection col1 = proxy.getFields().values();
Collection col2 = Arrays.asList("john", null, 0, "xxx", "john", null);
Comparator comp = new Comparator() {
public int compare(Object o1, Object o2) {
if (o1 == null && o2 != null) {
return 1;
}
if (o1 != null && o2 == null) {
return -1;
}
if (o1 == null && o2 == null) {
return 0;
}
return o1.toString().compareTo(o2.toString());
}
};
Collections.sort((List) col1, comp);
Collections.sort((List) col2, comp);
assertEquals(col1, col2);
assertTrue(proxy.getFields().containsValue(null));
assertTrue(proxy.getFields().containsValue("john"));
assertTrue(proxy.getFields().containsValue(0));
assertTrue(proxy.getFields().containsValue("xxx"));
assertFalse(proxy.getFields().containsValue("randomString"));
assertFalse(proxy.getFields().containsValue(-96));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations