use of org.drools.core.factmodel.traits.TraitProxy in project drools by kiegroup.
the class TraitTest method testTraitWrapGetAndSet.
@Test(timeout = 10000)
public void testTraitWrapGetAndSet() {
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();
TraitFactory.setMode(mode, kb);
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
wrapper.put("name", "john");
wrapper.put("virtualField", "xyz");
wrapper.entrySet();
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
assertEquals("john", wrapper.get("name"));
assertEquals("xyz", wrapper.get("virtualField"));
assertEquals("john", impClass.get(imp, "name"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.TraitProxy in project drools by kiegroup.
the class TraitHelper method extractTrait.
public static <K> K extractTrait(InternalFactHandle handle, Class<K> klass) {
TraitableBean tb;
if (handle.isTraitable()) {
tb = (TraitableBean) handle.getObject();
} else if (handle.isTraiting()) {
tb = ((TraitProxy) handle.getObject()).getObject();
} else {
return null;
}
K k = (K) tb.getTrait(klass.getCanonicalName());
if (k != null) {
return k;
}
for (Object t : tb.getMostSpecificTraits()) {
if (klass.isAssignableFrom(t.getClass())) {
return (K) t;
}
}
return null;
}
use of org.drools.core.factmodel.traits.TraitProxy 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;
}
}
}
}
use of org.drools.core.factmodel.traits.TraitProxy in project drools by kiegroup.
the class TraitHelper method getTraitBoundary.
protected <K> Collection<Thing> getTraitBoundary(TraitableBean<K, ?> inner, boolean needsProxy, boolean hasTrait, Class trait) {
boolean refresh = !needsProxy && !hasTrait && Thing.class != trait;
if (!refresh) {
return null;
}
if (inner._getTraitMap() == null || inner instanceof Thing)
return Collections.EMPTY_LIST;
if (inner._getTraitMap().isEmpty())
return null;
Collection<Thing> ts = new ArrayList<Thing>();
for (Thing t : inner._getTraitMap().values()) {
if (t instanceof TraitProxy) {
if (((TraitProxy) t).hasOtns()) {
ts.add(t);
}
}
}
return ts;
}
Aggregations