Search in sources :

Example 1 with TraitFactoryImpl

use of org.drools.traits.core.factmodel.TraitFactoryImpl 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 TraitFactoryImpl

use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.

the class TraitHelperImpl method applyTrait.

protected <T, K> T applyTrait(Activation activation, K core, Class<T> trait, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
    TraitFactoryImpl builder = TraitFactoryImpl.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
    TraitableBean inner = makeTraitable(core, builder, logical, activation);
    boolean needsProxy = trait.isAssignableFrom(inner.getClass());
    boolean hasTrait = inner.hasTrait(trait.getName());
    boolean needsUpdate = needsProxy || core != inner;
    checkStaticTypeCode(inner);
    Collection<Thing> mostSpecificTraits = getTraitBoundary(inner, needsProxy, hasTrait, trait);
    T thing = asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
    configureTrait(thing, value);
    thing = doInsertTrait(activation, thing, core, logical, modes);
    refresh(thing, core, inner, trait, mostSpecificTraits, logical, activation);
    if (trait != Thing.class && inner._getFieldTMS() != null) {
        inner._getFieldTMS().resetModificationMask();
    }
    return thing;
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing)

Example 3 with TraitFactoryImpl

use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.

the class TraitHelperImpl method applyManyTraits.

protected <T, K> T applyManyTraits(Activation activation, K core, Collection<Class<? extends Thing>> traits, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
    // Precondition : traits is not empty, checked by don
    TraitFactoryImpl builder = TraitFactoryImpl.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
    TraitableBean inner = makeTraitable(core, builder, logical, activation);
    Collection<Thing> mostSpecificTraits = inner.getMostSpecificTraits();
    boolean newTraitsAdded = false;
    T firstThing = null;
    List<Thing> things = new ArrayList<Thing>(traits.size());
    checkStaticTypeCode(inner);
    for (Class<?> trait : traits) {
        boolean needsProxy = trait.isAssignableFrom(inner.getClass());
        boolean hasTrait = inner.hasTrait(trait.getName());
        boolean needsUpdate = needsProxy || core != inner;
        if (!hasTrait) {
            T thing = (T) asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
            configureTrait(thing, value);
            things.add((Thing) thing);
            if (!newTraitsAdded && trait != Thing.class) {
                firstThing = thing;
                newTraitsAdded = true;
            }
        }
    }
    for (Thing t : things) {
        doInsertTrait(activation, t, core, logical, modes);
    }
    if (newTraitsAdded) {
        if (mostSpecificTraits != null) {
            updateCore(inner, core, null, logical, activation);
            if (!mostSpecificTraits.isEmpty()) {
                updateManyTraits(inner, onlyTraitBitSetMask(), things, core.getClass(), mostSpecificTraits, activation);
            }
        }
    }
    return firstThing;
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing)

Example 4 with TraitFactoryImpl

use of org.drools.traits.core.factmodel.TraitFactoryImpl 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 TraitFactoryImpl

use of org.drools.traits.core.factmodel.TraitFactoryImpl 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

TraitableBean (org.drools.core.factmodel.traits.TraitableBean)11 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)11 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)9 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)9 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)9 TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)9 Test (org.junit.Test)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 HashMap (java.util.HashMap)3 Thing (org.drools.core.factmodel.traits.Thing)3 Entity (org.drools.traits.core.factmodel.Entity)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 ArrayList (java.util.ArrayList)1