use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testWrapperSize.
@Test
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());
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);
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();
TraitProxyImpl proxy2 = (TraitProxyImpl) 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();
TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2);
proxy99.getFields().put("surname", "xxx");
proxy99.getFields().put("name", "xyz");
proxy99.getFields().put("school", "skol");
assertEquals(3, proxy99.getFields().size());
TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait);
assertEquals(4, proxy100.getFields().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitHelperImpl method deleteWMAssertedTraitProxies.
public void deleteWMAssertedTraitProxies(InternalFactHandle handle, RuleImpl rule, TerminalNode terminalNode) {
TraitableBean traitableBean = (TraitableBean) handle.getObject();
if (traitableBean.hasTraits()) {
PriorityQueue<TraitProxyImpl> removedTypes = new PriorityQueue<TraitProxyImpl>(traitableBean._getTraitMap().values().size());
removedTypes.addAll(traitableBean._getTraitMap().values());
while (!removedTypes.isEmpty()) {
TraitProxyImpl 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.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitHelperImpl method reassignNodes.
private <K, X extends TraitableBean> void reassignNodes(TraitableBean<K, X> core, Collection<Thing<K>> removedTraits) {
if (!core.hasTraits()) {
return;
}
Collection<Thing<K>> mst = ((TraitTypeMapImpl) core._getTraitMap()).getMostSpecificTraits();
for (Thing<K> shedded : removedTraits) {
for (BitSet bs : ((TraitProxyImpl) shedded).listAssignedOtnTypeCodes()) {
boolean found = false;
for (Thing<K> tp : mst) {
TraitProxyImpl candidate = (TraitProxyImpl) tp;
if (supersetOrEqualset(candidate._getTypeCode(), bs)) {
candidate.assignOtn(bs);
found = true;
break;
}
}
if (found) {
continue;
}
}
}
}
use of org.drools.traits.core.factmodel.TraitProxyImpl in project drools by kiegroup.
the class TraitTest method testWrapperEmpty.
@Test
public void testWrapperEmpty() {
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 studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = studentClass.getFactClass();
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "name", "john");
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "name", null);
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "age", 32);
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "age", null);
assertFalse(wrapper.isEmpty());
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
TraitableBean ind = new Entity();
FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = RoleClass.getFactClass();
TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait2);
Map<String, Object> wrapper2 = proxy2.getFields();
assertTrue(wrapper2.isEmpty());
proxy2.getFields().put("name", "john");
assertFalse(wrapper2.isEmpty());
proxy2.getFields().put("name", null);
assertFalse(wrapper2.isEmpty());
} 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 testHasTypes.
@Test
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());
TraitFactoryImpl traitBuilder = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
TraitFactoryImpl.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);
TraitProxyImpl proxy = (TraitProxyImpl) traitBuilder.getProxy(imp, trait);
Thing thing = traitBuilder.getProxy(imp, Thing.class);
TraitableBean core = (TraitableBean) proxy.getObject();
TraitProxyImpl proxy2 = (TraitProxyImpl) 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());
}
}
Aggregations