Search in sources :

Example 1 with TraitRegistry

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

the class TraitHelper method checkStaticTypeCode.

private void checkStaticTypeCode(TraitableBean inner) {
    if (!inner.hasTraits()) {
        TraitTypeMap ttm = (TraitTypeMap) inner._getTraitMap();
        if (ttm != null && ttm.getStaticTypeCode() == null) {
            TraitRegistry registry = this.workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getTraitRegistry();
            // 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 : TraitRegistry(org.drools.core.factmodel.traits.TraitRegistry) BitSet(java.util.BitSet) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap)

Example 2 with TraitRegistry

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

the class TraitTest method testTraitEncoding.

@Test(timeout = 10000)
public void testTraitEncoding() {
    String s1 = "package org.drools.core.factmodel.traits;\n" + "declare trait A end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "declare trait F extends C end\n" + "declare trait G extends D end\n" + "declare trait H extends D end\n" + "declare trait I extends E end\n" + "declare trait J extends F end\n" + "declare trait K extends G, H end\n" + "declare trait L extends G, H end\n" + "declare trait M extends I, J end\n" + "declare trait N extends K, L end\n" + "" + "rule \"donOneThing\"\n" + "when\n" + "    $x : Entity()\n" + "then\n" + "    don( $x, A.class );\n" + "end\n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    String( this == \"y\" ) \n" + "    $x : Entity()\n" + "then\n" + "    don( $x, B.class );\n" + "    don( $x, D.class );\n" + "    don( $x, F.class );\n" + "    don( $x, E.class );\n" + "    don( $x, I.class );\n" + "    don( $x, K.class );\n" + "    don( $x, J.class );\n" + "    don( $x, C.class );\n" + "    don( $x, H.class );\n" + "    don( $x, G.class );\n" + "    don( $x, L.class );\n" + "    don( $x, M.class );\n" + "    don( $x, N.class );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    TraitRegistry tr = ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().getTraitRegistry();
    System.out.println(tr.getHierarchy());
    Entity ent = new Entity("x");
    KieSession ksession = kbase.newKieSession();
    ksession.insert(ent);
    ksession.fireAllRules();
    assertEquals(1, ent.getMostSpecificTraits().size());
    ksession.insert("y");
    ksession.fireAllRules();
    System.out.println(ent.getMostSpecificTraits());
    assertEquals(2, ent.getMostSpecificTraits().size());
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) TraitRegistry(org.drools.core.factmodel.traits.TraitRegistry) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

TraitRegistry (org.drools.core.factmodel.traits.TraitRegistry)2 BitSet (java.util.BitSet)1 Entity (org.drools.core.factmodel.traits.Entity)1 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1