use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.
the class TraitTest method testWrapperSize.
@Test
public void testWrapperSize() {
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());
TraitFactoryImpl.setMode(mode, kb);
TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
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();
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
impClass.set(imp, "name", "john");
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
proxy.getFields().put("school", "skol");
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
proxy.getFields().put("surname", "xxx");
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
// TraitableBean ind = (TraitableBean) indClass.newInstance();
TraitableBean ind = new Entity();
TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait);
Map virtualFields2 = ind._getDynamicProperties();
Map wrapper2 = proxy2.getFields();
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
traitClass.set(proxy2, "name", "john");
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
proxy2.getFields().put("school", "skol");
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
proxy2.getFields().put("surname", "xxx");
assertEquals(4, wrapper2.size());
assertEquals(4, virtualFields2.size());
FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = traitClass2.getFactClass();
// TraitableBean ind2 = (TraitableBean) indClass.newInstance();
TraitableBean ind2 = new Entity();
TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2);
proxy99.getFields().put("surname", "xxx");
proxy99.getFields().put("name", "xyz");
proxy99.getFields().put("school", "skol");
assertEquals(3, proxy99.getFields().size());
TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait);
assertEquals(4, proxy100.getFields().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.
the class TraitHelperImpl method applyTrait.
protected <T, K> T applyTrait(Activation activation, K core, Class<T> trait, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
TraitFactoryImpl builder = TraitFactoryImpl.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
TraitableBean inner = makeTraitable(core, builder, logical, activation);
boolean needsProxy = trait.isAssignableFrom(inner.getClass());
boolean hasTrait = inner.hasTrait(trait.getName());
boolean needsUpdate = needsProxy || core != inner;
checkStaticTypeCode(inner);
Collection<Thing> mostSpecificTraits = getTraitBoundary(inner, needsProxy, hasTrait, trait);
T thing = asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
configureTrait(thing, value);
thing = doInsertTrait(activation, thing, core, logical, modes);
refresh(thing, core, inner, trait, mostSpecificTraits, logical, activation);
if (trait != Thing.class && inner._getFieldTMS() != null) {
inner._getFieldTMS().resetModificationMask();
}
return thing;
}
use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.
the class TraitHelperImpl method applyManyTraits.
protected <T, K> T applyManyTraits(Activation activation, K core, Collection<Class<? extends Thing>> traits, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
// Precondition : traits is not empty, checked by don
TraitFactoryImpl builder = TraitFactoryImpl.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
TraitableBean inner = makeTraitable(core, builder, logical, activation);
Collection<Thing> mostSpecificTraits = inner.getMostSpecificTraits();
boolean newTraitsAdded = false;
T firstThing = null;
List<Thing> things = new ArrayList<Thing>(traits.size());
checkStaticTypeCode(inner);
for (Class<?> trait : traits) {
boolean needsProxy = trait.isAssignableFrom(inner.getClass());
boolean hasTrait = inner.hasTrait(trait.getName());
boolean needsUpdate = needsProxy || core != inner;
if (!hasTrait) {
T thing = (T) asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
configureTrait(thing, value);
things.add((Thing) thing);
if (!newTraitsAdded && trait != Thing.class) {
firstThing = thing;
newTraitsAdded = true;
}
}
}
for (Thing t : things) {
doInsertTrait(activation, t, core, logical, modes);
}
if (newTraitsAdded) {
if (mostSpecificTraits != null) {
updateCore(inner, core, null, logical, activation);
if (!mostSpecificTraits.isEmpty()) {
updateManyTraits(inner, onlyTraitBitSetMask(), things, core.getClass(), mostSpecificTraits, activation);
}
}
}
return firstThing;
}
use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.
the class TraitTest method testWrapperEmpty.
@Test
public void testWrapperEmpty() {
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());
TraitFactoryImpl.setMode(mode, kb);
TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = studentClass.getFactClass();
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "name", "john");
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "name", null);
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "age", 32);
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "age", null);
assertFalse(wrapper.isEmpty());
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
TraitableBean ind = new Entity();
FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = RoleClass.getFactClass();
TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait2);
Map<String, Object> wrapper2 = proxy2.getFields();
assertTrue(wrapper2.isEmpty());
proxy2.getFields().put("name", "john");
assertFalse(wrapper2.isEmpty());
proxy2.getFields().put("name", null);
assertFalse(wrapper2.isEmpty());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.traits.core.factmodel.TraitFactoryImpl in project drools by kiegroup.
the class TraitTest method testHasTypes.
@Test
public void testHasTypes() {
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());
TraitFactoryImpl traitBuilder = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
TraitFactoryImpl.setMode(mode, kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
impClass.set(imp, "name", "aaabcd");
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
assertNotNull(trait);
TraitProxyImpl proxy = (TraitProxyImpl) traitBuilder.getProxy(imp, trait);
Thing thing = traitBuilder.getProxy(imp, Thing.class);
TraitableBean core = (TraitableBean) proxy.getObject();
TraitProxyImpl proxy2 = (TraitProxyImpl) traitBuilder.getProxy(imp, trait);
Thing thing2 = traitBuilder.getProxy(imp, Thing.class);
assertSame(proxy, proxy2);
assertSame(thing, thing2);
assertEquals(2, core.getTraits().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations