Search in sources :

Example 1 with LogicalTypeInconsistencyException

use of org.drools.traits.core.factmodel.LogicalTypeInconsistencyException in project drools by kiegroup.

the class LegacyTraitTest method testTraitWithNonAccessorMethodShadowing.

@Test
public void testTraitWithNonAccessorMethodShadowing() {
    StandaloneTraitFactory factory = createStandaloneTraitFactory();
    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.traits.core.factmodel.LogicalTypeInconsistencyException) TraitTestUtils.createStandaloneTraitFactory(org.drools.traits.compiler.factmodel.traits.TraitTestUtils.createStandaloneTraitFactory) StandaloneTraitFactory(org.drools.traits.core.util.StandaloneTraitFactory) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 2 with LogicalTypeInconsistencyException

use of org.drools.traits.core.factmodel.LogicalTypeInconsistencyException in project drools by kiegroup.

the class TraitTest method testTraitProxy.

@Test
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());
    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();
        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);
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        proxy.getFields().put("field", "xyz");
        // proxy.getFields().put("name", "aaa");
        assertNotNull(proxy);
        TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        assertSame(proxy2, proxy);
        TraitProxyImpl proxy3 = (TraitProxyImpl) 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");
        TraitProxyImpl proxy4 = (TraitProxyImpl) 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 : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)2 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)2 Test (org.junit.Test)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 ClassPathResource (org.drools.core.io.impl.ClassPathResource)1 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 TraitTestUtils.createStandaloneTraitFactory (org.drools.traits.compiler.factmodel.traits.TraitTestUtils.createStandaloneTraitFactory)1 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)1 TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)1 StandaloneTraitFactory (org.drools.traits.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