Search in sources :

Example 11 with TypeDeclarationDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.TypeDeclarationDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 12 with TypeDeclarationDescr

use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.

the class ModelBuilderImpl method registerTypeDeclarations.

private void registerTypeDeclarations(Collection<CompositePackageDescr> packages) {
    for (CompositePackageDescr packageDescr : packages) {
        InternalKnowledgePackage pkg = getOrCreatePackageRegistry(packageDescr).getPackage();
        for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
            normalizeAnnotations(typeDescr, pkg.getTypeResolver(), false);
            TypeDeclaration type = new TypeDeclaration(typeDescr.getTypeName());
            type.setResource(typeDescr.getResource());
            TypeDeclarationFactory.processAnnotations(typeDescr, type);
            pkg.addTypeDeclaration(type);
        }
    }
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 13 with TypeDeclarationDescr

use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools-wb by kiegroup.

the class FactModelPersistence method toModel.

private static List<FactMetaModel> toModel(String drl) throws DroolsParserException {
    Preconditions.checkNotNull(drl, "The string representing DRL can't be null!");
    if (drl.startsWith("#advanced") || drl.startsWith("//advanced")) {
        throw new DroolsParserException("Using advanced editor");
    }
    final DrlParser parser = new DrlParser();
    final StringReader reader = new StringReader(drl);
    final PackageDescr pkg = parser.parse(reader);
    if (parser.hasErrors()) {
        throw new DroolsParserException("The model drl " + drl + " is not valid");
    }
    if (pkg == null) {
        return emptyList();
    }
    final List<TypeDeclarationDescr> types = pkg.getTypeDeclarations();
    final List<FactMetaModel> list = new ArrayList<FactMetaModel>(types.size());
    for (final TypeDeclarationDescr td : types) {
        final FactMetaModel mm = new FactMetaModel();
        mm.setName(td.getTypeName());
        mm.setSuperType(td.getSuperTypeName());
        final Map<String, TypeFieldDescr> fields = td.getFields();
        for (Map.Entry<String, TypeFieldDescr> en : fields.entrySet()) {
            final String fieldName = en.getKey();
            final TypeFieldDescr descr = en.getValue();
            final FieldMetaModel fm = new FieldMetaModel(fieldName, descr.getPattern().getObjectType());
            mm.getFields().add(fm);
        }
        for (final AnnotationDescr descr : td.getAnnotations()) {
            final String annotationName = descr.getName();
            final Map<String, String> values = extractStringValues(descr);
            final AnnotationMetaModel am = new AnnotationMetaModel(annotationName, values);
            mm.getAnnotations().add(am);
        }
        list.add(mm);
    }
    return list;
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) FactMetaModel(org.drools.workbench.screens.factmodel.model.FactMetaModel) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with TypeDeclarationDescr

use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.

the class ClassHierarchyManager method inheritFields.

public void inheritFields(PackageRegistry pkgRegistry, AbstractClassTypeDeclarationDescr typeDescr, Collection<AbstractClassTypeDeclarationDescr> sortedTypeDescriptors, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs) {
    TypeDeclarationDescr tDescr = (TypeDeclarationDescr) typeDescr;
    boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, pkgRegistry);
    boolean inferFields = !isNovel && typeDescr.getFields().isEmpty();
    for (QualifiedName qname : tDescr.getSuperTypes()) {
        // descriptor needs fields inherited from superclass
        mergeInheritedFields(tDescr, unresolvedTypes, unprocessableDescrs, pkgRegistry.getTypeResolver());
    }
    if (inferFields) {
        // not novel, but only an empty declaration was provided.
        // after inheriting the fields from supertypes, now we fill in the locally declared fields
        Class existingClass = TypeDeclarationUtils.getExistingDeclarationClass(typeDescr, pkgRegistry);
        buildDescrsFromFields(existingClass, tDescr, pkgRegistry, tDescr.getFields());
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName)

Example 15 with TypeDeclarationDescr

use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.

the class TypeDeclarationBuilder method mergeTypeDescriptors.

private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
    boolean isDef1 = isDefinition(prev);
    boolean isDef2 = isDefinition(descr);
    if (isDef1 && isDef2) {
        return false;
    }
    if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
        return false;
    }
    if (prev.getSuperTypes().isEmpty()) {
        for (QualifiedName qn : descr.getSuperTypes()) {
            ((TypeDeclarationDescr) prev).addSuperType(qn);
        }
    }
    if (prev.getFields().isEmpty()) {
        for (String fieldName : descr.getFields().keySet()) {
            prev.addField(descr.getFields().get(fieldName));
        }
    }
    for (AnnotationDescr ad : descr.getAnnotations()) {
        prev.addQualifiedAnnotation(ad);
    }
    for (AnnotationDescr ad : prev.getAnnotations()) {
        if (!descr.getAnnotations().contains(ad)) {
            descr.addQualifiedAnnotation(ad);
        }
    }
    return true;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Aggregations

TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)20 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)10 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)9 Test (org.junit.Test)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)5 TypeDeclaration (org.drools.core.rule.TypeDeclaration)5 ArrayList (java.util.ArrayList)4 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)4 EnumDeclarationDescr (org.drools.compiler.lang.descr.EnumDeclarationDescr)4 HashMap (java.util.HashMap)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 QualifiedName (org.drools.compiler.lang.descr.QualifiedName)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)3 Map (java.util.Map)2 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)2 ImportDescr (org.drools.compiler.lang.descr.ImportDescr)2 Traitable (org.drools.core.factmodel.traits.Traitable)2