Search in sources :

Example 1 with TripleBasedBean

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

the class TraitTest method testInternalComponents1.

@Test(timeout = 10000)
public void testInternalComponents1() {
    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();
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Object proxyFields = proxy.getFields();
        Object coreTraits = imp._getTraitMap();
        Object coreProperties = imp._getDynamicProperties();
        assertTrue(proxy.getObject() instanceof TraitableBean);
        assertNotNull(proxyFields);
        assertNotNull(coreTraits);
        assertNotNull(coreProperties);
        if (mode == VirtualPropertyMode.MAP) {
            assertTrue(proxyFields instanceof MapWrapper);
            assertTrue(coreTraits instanceof TraitTypeMap);
            assertTrue(coreProperties instanceof HashMap);
        } else {
            assertEquals("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName());
            assertTrue(proxyFields instanceof TripleBasedStruct);
            assertTrue(coreTraits instanceof TraitTypeMap);
            assertTrue(coreProperties instanceof TripleBasedBean);
        }
        StudentProxy2 sp2 = new StudentProxy2(new Imp2(), null);
        System.out.println(sp2.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) HashMap(java.util.HashMap) 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) MapWrapper(org.drools.core.factmodel.traits.MapWrapper) TripleBasedBean(org.drools.core.factmodel.traits.TripleBasedBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) TripleBasedStruct(org.drools.core.factmodel.traits.TripleBasedStruct) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)1 MapWrapper (org.drools.core.factmodel.traits.MapWrapper)1 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)1 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)1 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)1 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)1 TripleBasedBean (org.drools.core.factmodel.traits.TripleBasedBean)1 TripleBasedStruct (org.drools.core.factmodel.traits.TripleBasedStruct)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 Test (org.junit.Test)1 FactType (org.kie.api.definition.type.FactType)1 Resource (org.kie.api.io.Resource)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1