Search in sources :

Example 6 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class TypeDeclarationDescrVisitor method visit.

public void visit(List<TypeDeclarationDescr> typeDeclarationDescrs) {
    for (TypeDeclarationDescr typeDeclaration : typeDeclarationDescrs) {
        Import objectImport = data.getImportByName(typeDeclaration.getTypeName());
        String objectTypeName;
        if (objectImport == null) {
            objectTypeName = typeDeclaration.getTypeName();
        } else {
            objectTypeName = objectImport.getName();
        }
        ObjectType objectType = this.data.getObjectTypeByFullName(objectTypeName);
        if (objectType == null) {
            objectType = new ObjectType(typeDeclaration);
            objectType.setName(typeDeclaration.getTypeName());
            objectType.setFullName(typeDeclaration.getTypeName());
            data.add(objectType);
        }
        for (String fieldName : typeDeclaration.getFields().keySet()) {
            Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
            if (field == null) {
                field = ObjectTypeFactory.createField(typeDeclaration.getFields().get(fieldName), fieldName, objectType);
                field.setFieldType(typeDeclaration.getFields().get(fieldName).getPattern().getObjectType());
                data.add(field);
            }
        }
        for (AnnotationDescr annDescr : typeDeclaration.getAnnotations()) {
            Map<String, Object> values = typeDeclaration.getAnnotation(annDescr.getName()).getValueMap();
            for (String value : values.keySet()) {
                objectType.getMetadata().put(annDescr.getName(), value);
            }
        }
    }
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 7 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class TypeDeclarationNameResolver method qualifyNames.

private void qualifyNames(List<TypeDefinition> unresolvedTypes, PackageDescr packageDescr, TypeResolver typeResolver) {
    for (TypeDeclarationDescr declarationDescr : packageDescr.getTypeDeclarations()) {
        qualifyNames(declarationDescr, packageDescr, unresolvedTypes, typeResolver);
        discoverHierarchyForRedeclarations(declarationDescr, packageDescr, typeResolver);
    }
    for (EnumDeclarationDescr enumDeclarationDescr : packageDescr.getEnumDeclarations()) {
        qualifyNames(enumDeclarationDescr, packageDescr, unresolvedTypes, typeResolver);
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr)

Example 8 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class ChangeSetBuilder method build.

public static KieJarChangeSet build(InternalKieModule original, InternalKieModule currentJar) {
    KieJarChangeSet result = new KieJarChangeSet();
    Collection<String> originalFiles = original.getFileNames();
    Collection<String> currentFiles = currentJar.getFileNames();
    ArrayList<String> removedFiles = new ArrayList<>(originalFiles);
    removedFiles.removeAll(currentFiles);
    if (!removedFiles.isEmpty()) {
        for (String file : removedFiles) {
            // there should be a way to get the JAR name/url to produce a proper URL for the file in it
            result.removeFile(file);
        }
    }
    List<TypeDeclarationDescr> typeDeclarations = new ArrayList<>();
    Map<String, String> changedClasses = new HashMap<>();
    for (String file : currentFiles) {
        if (originalFiles.contains(file)) {
            // check for modification
            byte[] ob = original.getBytes(file);
            byte[] cb = currentJar.getBytes(file);
            if (!Arrays.equals(ob, cb)) {
                if (file.endsWith(".class")) {
                    changedClasses.put(convertResourceToClassName(file), file);
                } else if (!ResourceType.DRL.matchesExtension(file) || !StringUtils.codeAwareEqualsIgnoreSpaces(new String(ob), new String(cb))) {
                    // check that: (NOT drl file) OR (NOT equalsIgnoringSpaces)
                    // parse the file to figure out the difference
                    result.registerChanges(file, diffResource(file, ob, cb, typeDeclarations));
                }
            }
        } else {
            // file was added
            result.addFile(file);
        }
    }
    for (TypeDeclarationDescr typeDeclaration : typeDeclarations) {
        String fqn = typeDeclaration.getFullTypeName();
        if (changedClasses.containsKey(fqn)) {
            continue;
        }
        InternalKnowledgePackage pkg = original.getPackage(typeDeclaration.getNamespace());
        if (pkg == null) {
            continue;
        }
        TypeResolver resolver = pkg.getTypeResolver();
        for (TypeFieldDescr field : typeDeclaration.getFields().values()) {
            String fieldType;
            try {
                fieldType = resolver.resolveType(field.getPattern().getObjectType()).getCanonicalName();
            } catch (ClassNotFoundException e) {
                continue;
            }
            if (changedClasses.containsKey(fieldType)) {
                changedClasses.put(fqn, convertClassToResourcePath(fqn));
                break;
            }
        }
    }
    for (String changedClass : changedClasses.values()) {
        result.registerChanges(changedClass, new ResourceChangeSet(changedClass, ChangeType.UPDATED));
    }
    if (original.getKieModuleModel() != null) {
        for (String kieBaseName : original.getKieModuleModel().getKieBaseModels().keySet()) {
            KnowledgeBuilder originalKbuilder = original.getKnowledgeBuilderForKieBase(kieBaseName);
            if (originalKbuilder != null && currentJar.getKnowledgeBuilderForKieBase(kieBaseName) == null) {
                currentJar.cacheKnowledgeBuilderForKieBase(kieBaseName, originalKbuilder);
            }
        }
    }
    return result;
}
Also used : TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) HashMap(java.util.HashMap) TypeResolver(org.drools.core.addon.TypeResolver) ArrayList(java.util.ArrayList) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 9 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class ClassDefinitionFactory method createClassDefinition.

protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    // extracts type, supertype and interfaces
    String fullName = typeDescr.getType().getFullName();
    if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
        TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
        if (tdescr.getSuperTypes().size() > 1) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + "  - has more than one supertype;"));
            return null;
        } else if (tdescr.getSuperTypes().isEmpty()) {
            tdescr.addSuperType("java.lang.Object");
        }
    }
    Traitable traitableAnn = getTypedAnnotation(typeDescr, Traitable.class);
    boolean traitable = traitableAnn != null;
    String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
    int j = 0;
    for (QualifiedName qname : typeDescr.getSuperTypes()) {
        fullSuperTypes[j++] = qname.getFullName();
    }
    fullSuperTypes[j] = Thing.class.getName();
    List<String> interfaceList = new ArrayList<>();
    interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
    if (traitable) {
        interfaceList.add(TraitableBean.class.getName());
    }
    String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
    // prepares a class definition
    ClassDefinition def;
    switch(type.getKind()) {
        case TRAIT:
            def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
            break;
        case ENUM:
            def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
            break;
        case CLASS:
        default:
            def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
            def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
    }
    return def;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) QualifiedName(org.drools.drl.ast.descr.QualifiedName) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) Traitable(org.drools.core.factmodel.traits.Traitable) Thing(org.drools.core.factmodel.traits.Thing)

Example 10 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class DeclaredClassBuilder method ensureJavaTypeConsistency.

private boolean ensureJavaTypeConsistency(AbstractClassTypeDeclarationDescr typeDescr, ClassDefinition def, TypeResolver typeResolver) {
    try {
        if (typeDescr instanceof TypeDeclarationDescr && !((TypeDeclarationDescr) typeDescr).isTrait() && typeResolver.resolveType(def.getSuperClass()).isInterface()) {
            def.addInterface(def.getSuperClass());
            def.setSuperClass(null);
        }
        for (String sup : def.getInterfaces()) {
            if (!typeResolver.resolveType(sup).isInterface()) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Non-interface type used as super interface : " + sup));
                return false;
            }
        }
    } catch (ClassNotFoundException cnfe) {
        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to resolve parent type :" + cnfe.getMessage()));
        return false;
    }
    return true;
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)

Aggregations

TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)22 PackageDescr (org.drools.drl.ast.descr.PackageDescr)10 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)9 Test (org.junit.Test)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)6 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)5 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)5 ArrayList (java.util.ArrayList)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 HashSet (java.util.HashSet)3 TypeResolver (org.drools.core.addon.TypeResolver)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)3 RuleDescr (org.drools.drl.ast.descr.RuleDescr)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2