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());
}
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));
}
}
}
}
}
Aggregations