Search in sources :

Example 1 with TraitRegistryImpl

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

the class TraitTest method testTraitEncoding.

@Test
public void testTraitEncoding() {
    String s1 = "package org.drools.core.factmodel.traits;\n" + "import " + Entity.class.getCanonicalName() + ";\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();
    TraitFactoryImpl.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    TraitRegistryImpl tr = (TraitRegistryImpl) ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(kbase);
    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.traits.core.factmodel.Entity) TraitRegistryImpl(org.drools.traits.core.factmodel.TraitRegistryImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 2 with TraitRegistryImpl

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

Aggregations

TraitRegistryImpl (org.drools.traits.core.factmodel.TraitRegistryImpl)2 BitSet (java.util.BitSet)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)1 Entity (org.drools.traits.core.factmodel.Entity)1 TraitTypeMapImpl (org.drools.traits.core.factmodel.TraitTypeMapImpl)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