Search in sources :

Example 1 with TraitTypeMapImpl

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

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

the class TraitHelperImpl method checkStaticTypeCode.

private void checkStaticTypeCode(TraitableBean inner) {
    if (!inner.hasTraits()) {
        TraitTypeMapImpl ttm = (TraitTypeMapImpl) inner._getTraitMap();
        if (ttm != null && ttm.getStaticTypeCode() == null) {
            TraitRegistryImpl registry = (TraitRegistryImpl) ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(this.workingMemory.getKnowledgeBase());
            // code that summarizes ALL the static types
            BitSet staticCode = registry.getStaticTypeCode(inner.getClass().getName());
            ttm.setStaticTypeCode(staticCode);
            if (staticCode != null) {
                for (String staticTrait : registry.getStaticTypes(inner.getClass().getName())) {
                    ttm.addStaticTrait(staticTrait, registry.getHierarchy().getCode(staticTrait));
                }
            }
        }
    }
}
Also used : TraitRegistryImpl(org.drools.traits.core.factmodel.TraitRegistryImpl) TraitTypeMapImpl(org.drools.traits.core.factmodel.TraitTypeMapImpl) BitSet(java.util.BitSet)

Example 3 with TraitTypeMapImpl

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

the class TraitTest method testInternalComponents1.

@Test
public void testInternalComponents1() {
    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);
        Object proxyFields = proxy.getFields();
        Object coreTraits = imp._getTraitMap();
        Object coreProperties = imp._getDynamicProperties();
        assertTrue(proxy.getObject() instanceof TraitableBean);
        assertNotNull(proxyFields);
        assertNotNull(coreTraits);
        assertNotNull(coreProperties);
        if (mode == VirtualPropertyMode.MAP) {
            assertTrue(proxyFields instanceof MapWrapper);
            assertTrue(coreTraits instanceof TraitTypeMapImpl);
            assertTrue(coreProperties instanceof HashMap);
        } else {
            assertEquals("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName());
            assertTrue(proxyFields instanceof TripleBasedStruct);
            assertTrue(coreTraits instanceof TraitTypeMapImpl);
            assertTrue(coreProperties instanceof TripleBasedBean);
        }
        StudentProxyImpl2 sp2 = new StudentProxyImpl2(new Imp2(), null);
        System.out.println(sp2.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) HashMap(java.util.HashMap) TraitTypeMapImpl(org.drools.traits.core.factmodel.TraitTypeMapImpl) 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) MapWrapper(org.drools.traits.core.factmodel.MapWrapper) TripleBasedBean(org.drools.traits.core.factmodel.TripleBasedBean) 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) TripleBasedStruct(org.drools.traits.core.factmodel.TripleBasedStruct) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 4 with TraitTypeMapImpl

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

the class TraitObjectTypeNode method sameAndNotCoveredByDescendants.

/**
 *  Edge case: due to the way traits are encoded, consider this hierarchy:
 *  A    B
 *    C
 *    D
 *  On don/insertion of C, C may be vetoed by its parents, but might have been
 *  already covered by one of its descendants (D)
 */
private boolean sameAndNotCoveredByDescendants(TraitProxyImpl proxy, BitSet typeMask) {
    boolean isSameType = typeMask.equals(proxy._getTypeCode());
    if (isSameType) {
        TraitTypeMapImpl<String, Thing<?>, ?> ttm = (TraitTypeMapImpl<String, Thing<?>, ?>) proxy.getObject()._getTraitMap();
        Collection<Thing<?>> descs = ttm.lowerDescendants(typeMask);
        // we have to exclude the "mock" bottom proxy
        if (descs == null || descs.isEmpty()) {
            return true;
        } else {
            for (Thing sub : descs) {
                TraitType tt = (TraitType) sub;
                if (tt != proxy && tt._hasTypeCode(typeMask)) {
                    return false;
                }
            }
            return true;
        }
    } else {
        return false;
    }
}
Also used : TraitType(org.drools.core.factmodel.traits.TraitType) TraitTypeMapImpl(org.drools.traits.core.factmodel.TraitTypeMapImpl) Thing(org.drools.core.factmodel.traits.Thing)

Aggregations

TraitTypeMapImpl (org.drools.traits.core.factmodel.TraitTypeMapImpl)4 BitSet (java.util.BitSet)2 Thing (org.drools.core.factmodel.traits.Thing)2 TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)2 HashMap (java.util.HashMap)1 TraitType (org.drools.core.factmodel.traits.TraitType)1 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 ClassPathResource (org.drools.core.io.impl.ClassPathResource)1 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)1 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)1 MapWrapper (org.drools.traits.core.factmodel.MapWrapper)1 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)1 TraitRegistryImpl (org.drools.traits.core.factmodel.TraitRegistryImpl)1 TripleBasedBean (org.drools.traits.core.factmodel.TripleBasedBean)1 TripleBasedStruct (org.drools.traits.core.factmodel.TripleBasedStruct)1 Test (org.junit.Test)1 FactType (org.kie.api.definition.type.FactType)1 Resource (org.kie.api.io.Resource)1