use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testShedVacancy.
@Test
public void testShedVacancy() {
String drl = "package org.drools.test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive end " + "declare trait B @propertyReactive end " + "declare trait C extends A,B @propertyReactive end " + "declare trait D extends B @propertyReactive end " + "rule Trait when " + "then " + " Entity e = new Entity( 'x1' ); " + " don( e, C.class ); " + " don( e, D.class ); " + "end " + "rule Mood when " + " $x : B() " + "then " + " System.out.println( 'Found B' ); " + "end " + "rule Shed when " + " $s : String() " + " $x : Entity() " + "then " + " delete( $s ); " + " shed( $x, A.class ); " + "end " + "";
KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
HierarchyEncoder<String> hier = ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().getTraitRegistry().getHierarchy();
BitSet a = (BitSet) hier.getCode("org.drools.test.A").clone();
BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone();
BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone();
BitSet d = (BitSet) hier.getCode("org.drools.test.D").clone();
int n = ksession.fireAllRules();
assertEquals(2, n);
System.err.print("---------------------------------------------------------------\n\n\n ");
int counter = 0;
for (Object o : ksession.getObjects()) {
if (o instanceof TraitProxy) {
TraitProxy tp = (TraitProxy) o;
if (tp._getTypeCode().equals(c)) {
assertEquals(1, tp.listAssignedOtnTypeCodes().size());
assertTrue(tp.listAssignedOtnTypeCodes().contains(b));
counter++;
} else if (tp._getTypeCode().equals(d)) {
assertTrue(tp.listAssignedOtnTypeCodes().isEmpty());
counter++;
}
} else if (o instanceof TraitableBean) {
TraitableBean tb = (TraitableBean) o;
System.out.println(tb.getCurrentTypeCode());
counter++;
}
}
assertEquals(3, counter);
ksession.insert("go");
ksession.fireAllRules();
System.err.print("---------------------------------------------------------------\n\n\n ");
int counter2 = 0;
for (Object o : ksession.getObjects()) {
if (o instanceof TraitProxy) {
TraitProxy tp = (TraitProxy) o;
assertEquals(d, tp._getTypeCode());
assertEquals(1, tp.listAssignedOtnTypeCodes().size());
assertTrue(tp.listAssignedOtnTypeCodes().contains(b));
counter2++;
} else if (o instanceof TraitableBean) {
TraitableBean tb = (TraitableBean) o;
assertEquals(d, tb.getCurrentTypeCode());
counter2++;
}
}
assertEquals(2, counter2);
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testTraitActualTypeCodeWithEntities.
void testTraitActualTypeCodeWithEntities(String trig, VirtualPropertyMode mode) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("org/drools/compiler/factmodel/traits/testComplexDonShed.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
TraitFactory.setMode(mode, kbase);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
ksession.insert(trig);
ksession.fireAllRules();
TraitableBean ent = (TraitableBean) ksession.getGlobal("core");
assertEquals(CodedHierarchyImpl.stringToBitSet("1"), ent.getCurrentTypeCode());
ksession.insert("b");
ksession.fireAllRules();
assertEquals(CodedHierarchyImpl.stringToBitSet("11"), ent.getCurrentTypeCode());
ksession.insert("c");
ksession.fireAllRules();
assertEquals(CodedHierarchyImpl.stringToBitSet("1011"), ent.getCurrentTypeCode());
ksession.insert("e");
ksession.fireAllRules();
assertEquals(CodedHierarchyImpl.stringToBitSet("11011"), ent.getCurrentTypeCode());
ksession.insert("-c");
ksession.fireAllRules();
assertEquals(CodedHierarchyImpl.stringToBitSet("11"), ent.getCurrentTypeCode());
ksession.insert("dg");
ksession.fireAllRules();
assertEquals(CodedHierarchyImpl.stringToBitSet("111111"), ent.getCurrentTypeCode());
ksession.insert("-f");
ksession.fireAllRules();
assertEquals(CodedHierarchyImpl.stringToBitSet("111"), ent.getCurrentTypeCode());
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testWrapperKeySetAndValues.
@Test(timeout = 10000)
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());
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();
FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxy proxy = (TraitProxy) 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());
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testWrapperSize.
@Test(timeout = 10000)
public void testWrapperSize() {
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();
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();
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
impClass.set(imp, "name", "john");
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
proxy.getFields().put("school", "skol");
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
proxy.getFields().put("surname", "xxx");
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
// TraitableBean ind = (TraitableBean) indClass.newInstance();
TraitableBean ind = new Entity();
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
Map virtualFields2 = ind._getDynamicProperties();
Map wrapper2 = proxy2.getFields();
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
traitClass.set(proxy2, "name", "john");
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
proxy2.getFields().put("school", "skol");
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
proxy2.getFields().put("surname", "xxx");
assertEquals(4, wrapper2.size());
assertEquals(4, virtualFields2.size());
FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = traitClass2.getFactClass();
// TraitableBean ind2 = (TraitableBean) indClass.newInstance();
TraitableBean ind2 = new Entity();
TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
proxy99.getFields().put("surname", "xxx");
proxy99.getFields().put("name", "xyz");
proxy99.getFields().put("school", "skol");
assertEquals(3, proxy99.getFields().size());
TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind2, trait);
assertEquals(4, proxy100.getFields().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 testNodePartitioningByProxies.
@Test
public void testNodePartitioningByProxies() {
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 @PropertyReactive end " + "declare trait B extends A @PropertyReactive end " + "declare trait C extends B @PropertyReactive end " + "declare trait D extends A @PropertyReactive end " + "declare trait E extends C, D @PropertyReactive end " + "declare trait F extends E @PropertyReactive end " + "declare trait G extends A @PropertyReactive 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 ); " + "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() B() C() D() E() F() 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', 'B', 'C', 'D', 'E', 'F', '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(7, otns.size());
}
}
Aggregations