Search in sources :

Example 11 with TraitFactoryImpl

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

the class TraitTest method testWrapperKeySetAndValues.

@Test
public void testWrapperKeySetAndValues() {
    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);
        impClass.set(imp, "name", "john");
        proxy.getFields().put("surname", "xxx");
        proxy.getFields().put("name2", "john");
        proxy.getFields().put("nfield", null);
        Set set = new HashSet();
        set.add("name");
        set.add("surname");
        set.add("age");
        set.add("school");
        set.add("name2");
        set.add("nfield");
        assertEquals(6, proxy.getFields().keySet().size());
        assertEquals(set, proxy.getFields().keySet());
        Collection col1 = proxy.getFields().values();
        Collection col2 = Arrays.asList("john", null, 0, "xxx", "john", null);
        Comparator comp = new Comparator() {

            public int compare(Object o1, Object o2) {
                if (o1 == null && o2 != null) {
                    return 1;
                }
                if (o1 != null && o2 == null) {
                    return -1;
                }
                if (o1 == null && o2 == null) {
                    return 0;
                }
                return o1.toString().compareTo(o2.toString());
            }
        };
        Collections.sort((List) col1, comp);
        Collections.sort((List) col2, comp);
        assertEquals(col1, col2);
        assertTrue(proxy.getFields().containsValue(null));
        assertTrue(proxy.getFields().containsValue("john"));
        assertTrue(proxy.getFields().containsValue(0));
        assertTrue(proxy.getFields().containsValue("xxx"));
        assertFalse(proxy.getFields().containsValue("randomString"));
        assertFalse(proxy.getFields().containsValue(-96));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) Comparator(java.util.Comparator) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Collection(java.util.Collection) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) HashSet(java.util.HashSet) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

TraitableBean (org.drools.core.factmodel.traits.TraitableBean)11 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)11 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)9 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)9 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)9 TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)9 Test (org.junit.Test)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 HashMap (java.util.HashMap)3 Thing (org.drools.core.factmodel.traits.Thing)3 Entity (org.drools.traits.core.factmodel.Entity)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 ArrayList (java.util.ArrayList)1