Search in sources :

Example 1 with TraitProxy

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;
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) PropagationContext(org.drools.core.spi.PropagationContext)

Example 2 with TraitProxy

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);
                }
            }
        }
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) PriorityQueue(java.util.PriorityQueue) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 3 with TraitProxy

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());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with TraitProxy

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());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Thing(org.drools.core.factmodel.traits.Thing) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 5 with TraitProxy

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;
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) BitSet(java.util.BitSet) HashSet(java.util.HashSet)

Aggregations

TraitProxy (org.drools.core.factmodel.traits.TraitProxy)19 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)13 Test (org.junit.Test)12 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 BitSet (java.util.BitSet)7 Entity (org.drools.core.factmodel.traits.Entity)4 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Thing (org.drools.core.factmodel.traits.Thing)3 KieSession (org.kie.api.runtime.KieSession)3