use of org.drools.traits.core.factmodel.TraitProxyImpl 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);
TraitFactoryImpl.setMode(mode, kbase);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
HierarchyEncoder<String> hier = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(((InternalKnowledgeBase) kbase)).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 TraitProxyImpl) {
TraitProxyImpl tp = (TraitProxyImpl) 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 TraitProxyImpl) {
TraitProxyImpl tp = (TraitProxyImpl) 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.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitHelperImpl method extractTrait.
@Override
public <K> K extractTrait(InternalFactHandle handle, Class<K> klass) {
TraitableBean tb;
if (handle.isTraitable()) {
tb = (TraitableBean) handle.getObject();
} else if (handle.isTraiting()) {
tb = ((TraitProxyImpl) handle.getObject()).getObject();
} else {
return null;
}
K k = (K) tb.getTrait(klass.getCanonicalName());
if (k != null) {
return k;
}
for (Object t : tb.getMostSpecificTraits()) {
if (klass.isAssignableFrom(t.getClass())) {
return (K) t;
}
}
return null;
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitHelperImpl method getTraitBoundary.
protected <K> Collection<Thing> getTraitBoundary(TraitableBean<K, ?> inner, boolean needsProxy, boolean hasTrait, Class trait) {
boolean refresh = !needsProxy && !hasTrait && Thing.class != trait;
if (!refresh) {
return null;
}
if (inner._getTraitMap() == null || inner instanceof Thing)
return Collections.EMPTY_LIST;
if (inner._getTraitMap().isEmpty())
return null;
Collection<Thing> ts = new ArrayList<Thing>();
for (Thing t : inner._getTraitMap().values()) {
if (t instanceof TraitProxyImpl) {
if (((TraitProxyImpl) t).hasOtns()) {
ts.add(t);
}
}
}
return ts;
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testTraitWrapGetAndSet.
@Test
public void testTraitWrapGetAndSet() {
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();
TraitFactoryImpl.setMode(mode, kb);
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
wrapper.put("name", "john");
wrapper.put("virtualField", "xyz");
wrapper.entrySet();
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
assertEquals("john", wrapper.get("name"));
assertEquals("xyz", wrapper.get("virtualField"));
assertEquals("john", impClass.get(imp, "name"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testInternalComponents1.
@Test
public void testInternalComponents1() {
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);
Object proxyFields = proxy.getFields();
Object coreTraits = imp._getTraitMap();
Object coreProperties = imp._getDynamicProperties();
assertTrue(proxy.getObject() instanceof TraitableBean);
assertNotNull(proxyFields);
assertNotNull(coreTraits);
assertNotNull(coreProperties);
if (mode == VirtualPropertyMode.MAP) {
assertTrue(proxyFields instanceof MapWrapper);
assertTrue(coreTraits instanceof TraitTypeMapImpl);
assertTrue(coreProperties instanceof HashMap);
} else {
assertEquals("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName());
assertTrue(proxyFields instanceof TripleBasedStruct);
assertTrue(coreTraits instanceof TraitTypeMapImpl);
assertTrue(coreProperties instanceof TripleBasedBean);
}
StudentProxyImpl2 sp2 = new StudentProxyImpl2(new Imp2(), null);
System.out.println(sp2.toString());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations