use of org.drools.core.factmodel.traits.TraitTypeMap 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));
}
}
}
}
}
use of org.drools.core.factmodel.traits.TraitTypeMap 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(TraitProxy proxy, BitSet typeMask) {
boolean isSameType = typeMask.equals(proxy._getTypeCode());
if (isSameType) {
TraitTypeMap<String, Thing<?>, ?> ttm = (TraitTypeMap<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;
}
}
use of org.drools.core.factmodel.traits.TraitTypeMap in project drools by kiegroup.
the class TraitTest method testInternalComponents1.
@Test(timeout = 10000)
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());
TraitFactory.setMode(mode, kb);
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
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();
TraitProxy proxy = (TraitProxy) 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 TraitTypeMap);
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 TraitTypeMap);
assertTrue(coreProperties instanceof TripleBasedBean);
}
StudentProxy2 sp2 = new StudentProxy2(new Imp2(), null);
System.out.println(sp2.toString());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.TraitTypeMap in project drools by kiegroup.
the class TraitHelper 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 = ((TraitTypeMap) core._getTraitMap()).getMostSpecificTraits();
for (Thing<K> shedded : removedTraits) {
for (BitSet bs : ((TraitProxy) shedded).listAssignedOtnTypeCodes()) {
boolean found = false;
for (Thing<K> tp : mst) {
TraitProxy candidate = (TraitProxy) tp;
if (HierarchyEncoderImpl.supersetOrEqualset(candidate._getTypeCode(), bs)) {
candidate.assignOtn(bs);
found = true;
break;
}
}
if (found) {
continue;
}
}
}
}
Aggregations