Search in sources :

Example 1 with LogicalTypeInconsistencyException

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());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with LogicalTypeInconsistencyException

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());
    }
}
Also used : LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) StandaloneTraitFactory(org.drools.core.util.StandaloneTraitFactory) Test(org.junit.Test)

Aggregations

LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)2 Test (org.junit.Test)2 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)1 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)1 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 ClassPathResource (org.drools.core.io.impl.ClassPathResource)1 StandaloneTraitFactory (org.drools.core.util.StandaloneTraitFactory)1 FactType (org.kie.api.definition.type.FactType)1 Resource (org.kie.api.io.Resource)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1