use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitHelper method deleteWMAssertedTraitProxies.
public void deleteWMAssertedTraitProxies(InternalFactHandle handle, RuleImpl rule, TerminalNode terminalNode) {
TraitableBean traitableBean = (TraitableBean) handle.getObject();
if (traitableBean.hasTraits()) {
PriorityQueue<TraitProxy> removedTypes = new PriorityQueue<TraitProxy>(traitableBean._getTraitMap().values().size());
removedTypes.addAll(traitableBean._getTraitMap().values());
while (!removedTypes.isEmpty()) {
TraitProxy proxy = removedTypes.poll();
if (!proxy._isVirtual()) {
InternalFactHandle proxyHandle = (InternalFactHandle) getFactHandle(proxy);
if (proxyHandle.getEqualityKey() == null || proxyHandle.getEqualityKey().getLogicalFactHandle() != proxyHandle) {
entryPoint.delete(proxyHandle, rule, terminalNode);
}
}
}
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testNodePartitioningByProxiesAfterShed.
@Test
public void testNodePartitioningByProxiesAfterShed() {
String source = "package t.x " + "import java.util.*; " + "import org.drools.core.factmodel.traits.Thing " + "import org.drools.core.factmodel.traits.Traitable " + " " + "global java.util.List list; " + " " + "" + "declare trait A end " + "declare trait B extends A end " + "declare trait C extends B end " + "declare trait D extends A end " + "declare trait E extends C, D end " + "declare trait F extends E end " + "declare trait G extends A end " + "" + "declare Kore " + " @Traitable " + "end " + "" + "rule Init when " + "then " + " Kore k = new Kore(); " + " don( k, C.class ); " + " don( k, D.class ); " + " don( k, B.class ); " + " don( k, A.class ); " + " don( k, F.class ); " + " don( k, E.class ); " + " don( k, G.class ); " + " shed( k, B.class ); " + "end ";
for (char c = 'A'; c <= 'G'; c++) {
String C = "" + c;
source += "rule Rule" + C + " when " + C + "() then list.add( '" + C + "' ); end ";
}
source += "rule RuleAll " + "when " + " A() D() G() " + "then " + " list.add( 'Z' ); " + "end " + "";
KieSession ks = getSessionFromString(source);
TraitFactory.setMode(mode, ks.getKieBase());
List list = new ArrayList();
ks.setGlobal("list", list);
ks.fireAllRules();
System.out.println(list);
assertEquals(Arrays.asList('A', 'D', 'G', 'Z'), list);
for (Object o : ks.getObjects(new ObjectFilter() {
@Override
public boolean accept(Object object) {
return object instanceof TraitableBean;
}
})) {
Set<BitSet> otns = checkOTNPartitioning((TraitableBean) o, ks);
assertEquals(3, otns.size());
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testTraitProxy.
@Test(timeout = 10000)
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());
TraitFactory.setMode(mode, kb);
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
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);
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
proxy.getFields().put("field", "xyz");
// proxy.getFields().put("name", "aaa");
assertNotNull(proxy);
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(imp, trait);
assertSame(proxy2, proxy);
TraitProxy proxy3 = (TraitProxy) 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");
TraitProxy proxy4 = (TraitProxy) 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.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testHasTypes.
@Test(timeout = 10000)
public void testHasTypes() {
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());
TraitFactory traitBuilder = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
TraitFactory.setMode(mode, kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
impClass.set(imp, "name", "aaabcd");
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
assertNotNull(trait);
TraitProxy proxy = (TraitProxy) traitBuilder.getProxy(imp, trait);
Thing thing = traitBuilder.getProxy(imp, Thing.class);
TraitableBean core = (TraitableBean) proxy.getObject();
TraitProxy proxy2 = (TraitProxy) traitBuilder.getProxy(imp, trait);
Thing thing2 = traitBuilder.getProxy(imp, Thing.class);
assertSame(proxy, proxy2);
assertSame(thing, thing2);
assertEquals(2, core.getTraits().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testWrapperContainsKey.
@Test(timeout = 10000)
public void testWrapperContainsKey() {
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());
TraitFactory.setMode(mode, kb);
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
impClass.set(imp, "name", "john");
FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertTrue(wrapper.containsKey("name"));
assertTrue(wrapper.containsKey("school"));
assertTrue(wrapper.containsKey("age"));
assertFalse(wrapper.containsKey("surname"));
proxy.getFields().put("school", "skol");
proxy.getFields().put("surname", "xxx");
assertTrue(wrapper.containsKey("surname"));
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
TraitableBean ind = new Entity();
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
Map virtualFields2 = ind._getDynamicProperties();
Map wrapper2 = proxy2.getFields();
assertTrue(wrapper2.containsKey("name"));
assertTrue(wrapper2.containsKey("school"));
assertTrue(wrapper2.containsKey("age"));
assertFalse(wrapper2.containsKey("surname"));
traitClass.set(proxy2, "name", "john");
proxy2.getFields().put("school", "skol");
proxy2.getFields().put("surname", "xxx");
assertTrue(wrapper2.containsKey("surname"));
FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = traitClass2.getFactClass();
TraitableBean ind2 = new Entity();
TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
Map<String, Object> wrapper99 = proxy99.getFields();
assertFalse(wrapper99.containsKey("name"));
assertFalse(wrapper99.containsKey("school"));
assertFalse(wrapper99.containsKey("age"));
assertFalse(wrapper99.containsKey("surname"));
proxy99.getFields().put("surname", "xxx");
proxy99.getFields().put("name", "xyz");
proxy99.getFields().put("school", "skol");
assertTrue(wrapper99.containsKey("name"));
assertTrue(wrapper99.containsKey("school"));
assertFalse(wrapper99.containsKey("age"));
assertTrue(wrapper99.containsKey("surname"));
assertEquals(3, proxy99.getFields().size());
TraitableBean ind0 = new Entity();
TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind0, trait2);
Map<String, Object> wrapper100 = proxy100.getFields();
assertFalse(wrapper100.containsKey("name"));
assertFalse(wrapper100.containsKey("school"));
assertFalse(wrapper100.containsKey("age"));
assertFalse(wrapper100.containsKey("surname"));
TraitProxy proxy101 = (TraitProxy) tFactory.getProxy(ind0, trait);
// object gains properties by virtue of another trait
// so new props are accessible even using the old proxy
assertTrue(wrapper100.containsKey("name"));
assertTrue(wrapper100.containsKey("school"));
assertTrue(wrapper100.containsKey("age"));
assertFalse(wrapper100.containsKey("surname"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations