Search in sources :

Example 11 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactVerifierTest method testVerifyFactExplanation.

@Test
public void testVerifyFactExplanation() throws Exception {
    Cheese f1 = new Cheese();
    f1.setType(null);
    TypeResolver typeResolver = mock(TypeResolver.class);
    HashMap<String, Object> populatedData = new HashMap<String, Object>();
    populatedData.put("f1", f1);
    // configure the mock to return the value
    Set o = Collections.singleton((Object) f1);
    when(ksession.getObjects()).thenReturn(o);
    FactVerifier factVerifier = new FactVerifier(populatedData, typeResolver, ksession, new HashMap<String, Object>());
    VerifyFact vf = new VerifyFact();
    vf.setName("f1");
    vf.getFieldValues().add(new VerifyField("type", "boo", "!="));
    factVerifier.verify(vf);
    VerifyField vfl = vf.getFieldValues().get(0);
    assertEquals("[f1] field [type] was not [boo].", vfl.getExplanation());
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Cheese(org.drools.workbench.models.testscenarios.backend.Cheese) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

Example 12 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class ClassHierarchyManager method mergeFields.

protected void mergeFields(String simpleSuperTypeName, String superTypePackageName, String fullSuper, TypeDeclarationDescr typeDescr, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver resolver) {
    Map<String, TypeFieldDescr> fieldMap = new LinkedHashMap<String, TypeFieldDescr>();
    boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, kbuilder.getPackageRegistry(typeDescr.getNamespace()));
    PackageRegistry registry = kbuilder.getPackageRegistry(superTypePackageName);
    InternalKnowledgePackage pack = null;
    if (registry != null) {
        pack = registry.getPackage();
    }
    if (unprocessableDescrs.containsKey(fullSuper)) {
        unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
        return;
    }
    // if a class is declared in DRL, its package can't be null? The default package is replaced by "defaultpkg"
    boolean isSuperClassTagged = false;
    // in the same package, or in a previous one
    boolean isSuperClassDeclared = true;
    if (pack != null) {
        // look for the supertype declaration in available packages
        TypeDeclaration superTypeDeclaration = pack.getTypeDeclaration(simpleSuperTypeName);
        if (superTypeDeclaration != null && superTypeDeclaration.getTypeClassDef() != null) {
            ClassDefinition classDef = superTypeDeclaration.getTypeClassDef();
            // inherit fields
            for (org.kie.api.definition.type.FactField fld : classDef.getFields()) {
                TypeFieldDescr inheritedFlDescr = buildInheritedFieldDescrFromDefinition(fld, typeDescr);
                fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
            }
            // new classes are already distinguished from tagged external classes
            isSuperClassTagged = !superTypeDeclaration.isNovel();
        }
    } else {
        isSuperClassDeclared = false;
    }
    // look for the class externally
    if (!isSuperClassDeclared || isSuperClassTagged) {
        try {
            Class superKlass;
            if (registry != null) {
                superKlass = registry.getTypeResolver().resolveType(fullSuper);
            } else {
                // if the supertype has not been declared, and we have got so far, it means that this class is not novel
                superKlass = resolver.resolveType(fullSuper);
            }
            buildDescrsFromFields(superKlass, typeDescr, registry, fieldMap);
        } catch (ClassNotFoundException cnfe) {
            unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
            return;
        }
    }
    // notice that it is not possible to override a field changing its type
    for (String fieldName : typeDescr.getFields().keySet()) {
        if (fieldMap.containsKey(fieldName)) {
            String type1 = fieldMap.get(fieldName).getPattern().getObjectType();
            String type2 = typeDescr.getFields().get(fieldName).getPattern().getObjectType();
            if (type2.lastIndexOf(".") < 0) {
                try {
                    TypeResolver typeResolver = kbuilder.getPackageRegistry(typeDescr.getNamespace()).getTypeResolver();
                    type1 = typeResolver.resolveType(type1).getName();
                    type2 = typeResolver.resolveType(type2).getName();
                    // now that we are at it... this will be needed later anyway
                    fieldMap.get(fieldName).getPattern().setObjectType(type1);
                    typeDescr.getFields().get(fieldName).getPattern().setObjectType(type2);
                } catch (ClassNotFoundException cnfe) {
                // will fail later
                }
            }
            boolean clash = !type1.equals(type2);
            TypeFieldDescr overriding = null;
            if (clash) {
                // this may still be an override using a subclass of the original type
                try {
                    Class<?> sup = resolver.resolveType(type1);
                    Class<?> loc = resolver.resolveType(type2);
                    if (sup.isAssignableFrom(loc)) {
                        clash = false;
                        // mark as non inherited so that a new field is actually built
                        overriding = fieldMap.get(fieldName);
                    }
                } catch (ClassNotFoundException cnfe) {
                // not much to do
                }
            }
            if (clash) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot redeclare field '" + fieldName + " from " + type1 + " to " + type2));
                typeDescr.setType(null, null);
                return;
            } else {
                String initVal = fieldMap.get(fieldName).getInitExpr();
                TypeFieldDescr fd = typeDescr.getFields().get(fieldName);
                if (fd.getInitExpr() == null) {
                    fd.setInitExpr(initVal);
                }
                fd.setInherited(fieldMap.get(fieldName).isInherited());
                fd.setOverriding(overriding);
                for (String key : fieldMap.get(fieldName).getAnnotationNames()) {
                    if (fd.getAnnotation(key) == null) {
                        fd.addAnnotation(fieldMap.get(fieldName).getAnnotation(key));
                    }
                }
                if (fd.getIndex() < 0) {
                    fd.setIndex(fieldMap.get(fieldName).getIndex());
                }
            }
        }
        fieldMap.put(fieldName, typeDescr.getFields().get(fieldName));
    }
    typeDescr.setFields(fieldMap);
}
Also used : TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassDefinition(org.drools.core.factmodel.ClassDefinition) LinkedHashMap(java.util.LinkedHashMap) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 13 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactAssignmentFieldPopulatorTest method testMatryoshkaDollSituation.

@Test
public void testMatryoshkaDollSituation() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    TypeResolver typeResolver = new ClassTypeResolver(new HashSet<String>(), classLoader);
    typeResolver.addImport("org.drools.workbench.models.testscenarios.backend.MatryoshkaDoll");
    MatryoshkaDoll matryoshkaDoll = new MatryoshkaDoll();
    FactAssignmentField factAssignmentField = createFactAssignmentField();
    addFactAssignmentFieldIntoFactAssignmentField(factAssignmentField, 5);
    FactAssignmentFieldPopulator factAssignmentFieldPopulator = new FactAssignmentFieldPopulator(matryoshkaDoll, factAssignmentField, typeResolver);
    factAssignmentFieldPopulator.populate(new HashMap<String, Object>());
    assertNotNull(matryoshkaDoll.getMatryoshkaDoll());
    assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll());
    assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
    assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
    assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
    assertNotNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
    assertNull(matryoshkaDoll.getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll().getMatryoshkaDoll());
}
Also used : ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) MatryoshkaDoll(org.drools.workbench.models.testscenarios.backend.MatryoshkaDoll) FactAssignmentField(org.drools.workbench.models.testscenarios.shared.FactAssignmentField) Test(org.junit.Test)

Example 14 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactPopulatorTest method testPopulateNested.

@Test
public void testPopulateNested() throws Exception {
    TypeResolver typeResolver = getTypeResolver();
    FactData cheeseFactData = new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, cheeseFactData));
    FactData outerFactData = new FactData("OuterFact", "p1", Arrays.<Field>asList(new FieldData("name", "mic"), new FieldData("innerFact", "=c1")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, outerFactData));
    factPopulator.populate();
    assertTrue(populatedData.containsKey("c1"));
    assertTrue(populatedData.containsKey("p1"));
    OuterFact o = (OuterFact) populatedData.get("p1");
    assertEquals(populatedData.get("c1"), o.getInnerFact());
}
Also used : CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) FactData(org.drools.workbench.models.testscenarios.shared.FactData) OuterFact(org.drools.workbench.models.testscenarios.backend.OuterFact) Test(org.junit.Test)

Example 15 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactPopulatorTest method testCollectionSums.

@Test
public void testCollectionSums() throws Exception {
    TypeResolver typeResolver = getTypeResolver();
    List<Field> fieldData = new ArrayList<Field>();
    CollectionFieldData collectionFieldData = new CollectionFieldData();
    collectionFieldData.setName("list");
    fieldData.add(collectionFieldData);
    collectionFieldData.getCollectionFieldList().add(new FieldData("list", "=1+3"));
    FactData wrapperFactData = new FactData("MyCollectionWrapper", "wrapper", fieldData, false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, wrapperFactData));
    factPopulator.populate();
    assertTrue(populatedData.containsKey("wrapper"));
    MyCollectionWrapper wrapper = (MyCollectionWrapper) populatedData.get("wrapper");
    assertNotNull(wrapper);
    assertEquals(1, wrapper.getList().size());
    assertNotNull(wrapper.getList().get(0));
    assertEquals(4, wrapper.getList().get(0));
}
Also used : CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Field(org.drools.workbench.models.testscenarios.shared.Field) MyCollectionWrapper(org.drools.workbench.models.testscenarios.backend.MyCollectionWrapper) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ArrayList(java.util.ArrayList) CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) Test(org.junit.Test)

Aggregations

TypeResolver (org.kie.soup.project.datamodel.commons.types.TypeResolver)24 Test (org.junit.Test)16 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)14 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)8 Cheese (org.drools.workbench.models.testscenarios.backend.Cheese)7 FactData (org.drools.workbench.models.testscenarios.shared.FactData)7 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)7 HashMap (java.util.HashMap)6 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)6 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 CollectionFieldData (org.drools.workbench.models.testscenarios.shared.CollectionFieldData)5 Field (org.drools.workbench.models.testscenarios.shared.Field)4 HashSet (java.util.HashSet)3 FactAssignmentField (org.drools.workbench.models.testscenarios.shared.FactAssignmentField)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 Cheesery (org.drools.workbench.models.testscenarios.backend.Cheesery)2 Mouse (org.drools.workbench.models.testscenarios.backend.Mouse)2 OuterFact (org.drools.workbench.models.testscenarios.backend.OuterFact)2 KieSession (org.kie.api.runtime.KieSession)2