Search in sources :

Example 11 with TraitFactory

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

the class TraitHelper method applyTrait.

protected <T, K> T applyTrait(Activation activation, K core, Class<T> trait, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
    TraitFactory builder = TraitFactory.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 : TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing) TraitFactory(org.drools.core.factmodel.traits.TraitFactory)

Example 12 with TraitFactory

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

the class TraitHelper 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
    TraitFactory builder = TraitFactory.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 : ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing) TraitFactory(org.drools.core.factmodel.traits.TraitFactory)

Aggregations

TraitFactory (org.drools.core.factmodel.traits.TraitFactory)12 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)11 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)9 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)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 Entity (org.drools.core.factmodel.traits.Entity)3 Thing (org.drools.core.factmodel.traits.Thing)3 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)3 ArrayList (java.util.ArrayList)2 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2