use of org.drools.core.factmodel.traits.LogicalTypeInconsistencyException in project drools by kiegroup.
the class TraitTest method testTraitProxy.
@Test(timeout = 10000)
public void testTraitProxy() {
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();
impClass.set(imp, "name", "aaa");
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
assertNotNull(trait);
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
proxy.getFields().put("field", "xyz");
// proxy.getFields().put("name", "aaa");
assertNotNull(proxy);
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(imp, trait);
assertSame(proxy2, proxy);
TraitProxy proxy3 = (TraitProxy) tFactory.getProxy(imp, trait2);
assertNotNull(proxy3);
assertEquals("xyz", proxy3.getFields().get("field"));
assertEquals("aaa", proxy3.getFields().get("name"));
TraitableBean imp2 = (TraitableBean) impClass.newInstance();
impClass.set(imp2, "name", "aaa");
TraitProxy proxy4 = (TraitProxy) tFactory.getProxy(imp2, trait);
// proxy4.getFields().put("name", "aaa");
proxy4.getFields().put("field", "xyz");
assertEquals(proxy2, proxy4);
} catch (InstantiationException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (LogicalTypeInconsistencyException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.LogicalTypeInconsistencyException in project drools by kiegroup.
the class LegacyTraitTest method testTraitWithNonAccessorMethodShadowing.
@Test
public void testTraitWithNonAccessorMethodShadowing() {
StandaloneTraitFactory factory = new StandaloneTraitFactory(ProjectClassLoader.createProjectClassLoader());
try {
SomeInterface r = (SomeInterface) factory.don(new SomeClass(), SomeInterface.class);
r.prepare();
assertEquals(42, r.getFoo());
assertEquals("I did that", r.doThis("that"));
} catch (LogicalTypeInconsistencyException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations