Search in sources :

Example 1 with TraitProxyImpl

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());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) Entity(org.drools.traits.core.factmodel.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Map(java.util.Map) HashMap(java.util.HashMap) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 2 with TraitProxyImpl

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

Example 3 with TraitProxyImpl

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

Example 4 with TraitProxyImpl

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());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) Entity(org.drools.traits.core.factmodel.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 5 with TraitProxyImpl

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());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) 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) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Thing(org.drools.core.factmodel.traits.Thing) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)18 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)13 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)12 Test (org.junit.Test)12 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)9 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 BitSet (java.util.BitSet)7 Entity (org.drools.traits.core.factmodel.Entity)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 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3