use of org.drools.core.factmodel.traits.TraitProxy in project drools by kiegroup.
the class NamedEntryPoint method delete.
public PropagationContext delete(InternalFactHandle handle, Object object, ObjectTypeConf typeConf, RuleImpl rule, Activation activation, TerminalNode terminalNode) {
final PropagationContext propagationContext = pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.DELETION, rule, terminalNode, handle, this.entryPoint);
this.entryPointNode.retractObject(handle, propagationContext, typeConf, this.wm);
if (handle.isTraiting() && handle.getObject() instanceof TraitProxy) {
(((TraitProxy) handle.getObject()).getObject()).removeTrait(((TraitProxy) handle.getObject())._getTypeCode());
} else if (handle.isTraitable()) {
traitHelper.deleteWMAssertedTraitProxies(handle, rule, terminalNode);
}
this.objectStore.removeHandle(handle);
this.wm.getRuleRuntimeEventSupport().fireObjectRetracted(propagationContext, handle, object, this.wm);
return propagationContext;
}
use of org.drools.core.factmodel.traits.TraitProxy 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.TraitProxy 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.TraitProxy 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.TraitProxy in project drools by kiegroup.
the class TraitTest method checkOTNPartitioning.
protected Set<BitSet> checkOTNPartitioning(TraitableBean core, KieSession wm) {
Set<BitSet> otns = new HashSet<BitSet>();
for (Object o : core._getTraitMap().values()) {
TraitProxy tp = (TraitProxy) o;
Set<BitSet> localNodes = tp.listAssignedOtnTypeCodes();
for (BitSet code : localNodes) {
assertFalse(otns.contains(code));
otns.add(code);
}
}
return otns;
}
Aggregations