Search in sources :

Example 1 with Import

use of org.drools.verifier.components.Import in project drools by kiegroup.

the class PackageDescrVisitor method visitImports.

private void visitImports(List<ImportDescr> importDescrs) throws IOException, ClassNotFoundException {
    HashSet<String> imports = new HashSet<String>();
    for (ImportDescr i : importDescrs) {
        String fullName = i.getTarget();
        String name = fullName.substring(fullName.lastIndexOf(".") + 1);
        imports.add(fullName);
        Import objectImport = new Import(i, rulePackage);
        objectImport.setName(fullName);
        objectImport.setShortName(name);
        data.add(objectImport);
        ObjectType objectType = this.data.getObjectTypeByFullName(fullName);
        if (objectType == null) {
            objectType = new ObjectType(i);
        }
        objectType.setName(name);
        objectType.setFullName(fullName);
        data.add(objectType);
    }
    PackageHeaderLoader packageHeaderLoader = new PackageHeaderLoader(imports, jars);
    for (String factTypeName : packageHeaderLoader.getClassNames()) {
        String name = factTypeName.substring(factTypeName.lastIndexOf(".") + 1);
        Collection<String> fieldNames = packageHeaderLoader.getFieldNames(factTypeName);
        for (String fieldName : fieldNames) {
            ObjectType objectType = this.data.getObjectTypeByObjectTypeNameAndPackageName(name, rulePackage.getName());
            Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
            if (field == null) {
                field = ObjectTypeFactory.createField(objectType.getDescr(), fieldName, objectType);
                field.setFieldType(packageHeaderLoader.getFieldType(objectType.getName(), fieldName));
                data.add(field);
            }
        }
    }
}
Also used : PackageHeaderLoader(org.drools.verifier.jarloader.PackageHeaderLoader) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) HashSet(java.util.HashSet)

Example 2 with Import

use of org.drools.verifier.components.Import in project drools by kiegroup.

the class PatternDescrVisitor method visitPatternDescr.

private Pattern visitPatternDescr(PatternDescr descr, VerifierComponent parent, int orderNumber) throws UnknownDescriptionException {
    objectType = data.getObjectTypeByFullName(descr.getObjectType());
    if (objectType == null) {
        Import objectImport = data.getImportByName(descr.getObjectType());
        if (objectImport != null) {
            objectType = ObjectTypeFactory.createObjectType(descr, objectImport);
        } else {
            objectType = ObjectTypeFactory.createObjectType(descr, descr.getObjectType());
        }
        data.add(objectType);
    }
    pattern = new Pattern(descr, rule);
    if (parent != null) {
        pattern.setParentPath(parent.getPath());
        pattern.setParentType(parent.getVerifierComponentType());
    }
    pattern.setObjectTypePath(objectType.getPath());
    pattern.setName(objectType.getName());
    pattern.setPatternNot(solvers.getRuleSolver().isChildNot());
    pattern.setPatternExists(solvers.getRuleSolver().isExists());
    pattern.setPatternForall(solvers.getRuleSolver().isForall());
    pattern.setOrderNumber(orderNumber);
    if (descr.getIdentifier() != null) {
        PatternVariable patternVariable = new PatternVariable(rule);
        patternVariable.setName(descr.getIdentifier());
        patternVariable.setParentPath(pattern.getPath());
        patternVariable.setParentType(pattern.getVerifierComponentType());
        data.add(patternVariable);
    }
    // visit source.
    if (descr.getSource() != null) {
        visit(descr.getSource());
    } else {
        if (workingMemory == null) {
            workingMemory = new WorkingMemory();
            data.add(workingMemory);
        }
        pattern.setSourcePath(workingMemory.getPath());
        pattern.setSourceType(workingMemory.getVerifierComponentType());
    }
    solvers.startPatternSolver(pattern);
    visit(descr.getConstraint());
    solvers.endPatternSolver();
    data.add(pattern);
    return pattern;
}
Also used : Pattern(org.drools.verifier.components.Pattern) PatternVariable(org.drools.verifier.components.PatternVariable) Import(org.drools.verifier.components.Import) WorkingMemory(org.drools.verifier.components.WorkingMemory)

Example 3 with Import

use of org.drools.verifier.components.Import 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 4 with Import

use of org.drools.verifier.components.Import in project drools by kiegroup.

the class FieldConstraintDescrVisitor method visit.

/**
 * End
 *
 * @param descr
 */
private void visit(QualifiedIdentifierRestrictionDescr descr) {
    String text = descr.getText();
    String base = text.substring(0, text.indexOf("."));
    String fieldName = text.substring(text.indexOf("."));
    Variable patternVariable = data.getVariableByRuleAndVariableName(pattern.getRuleName(), base);
    if (patternVariable != null) {
        QualifiedIdentifierRestriction restriction = new QualifiedIdentifierRestriction(pattern);
        restriction.setPatternIsNot(pattern.isPatternNot());
        restriction.setFieldPath(field.getPath());
        restriction.setOperator(Operator.determineOperator(descr.getEvaluator(), descr.isNegated()));
        restriction.setVariablePath(patternVariable.getPath());
        restriction.setVariableName(base);
        restriction.setVariablePath(fieldName);
        restriction.setOrderNumber(orderNumber);
        restriction.setParentPath(pattern.getPath());
        restriction.setParentType(pattern.getVerifierComponentType());
        // Set field value, if it is not set.
        field.setFieldType(Field.VARIABLE);
        data.add(restriction);
        solvers.addPatternComponent(restriction);
    } else {
        EnumField enumField = (EnumField) data.getFieldByObjectTypeAndFieldName(base, fieldName);
        if (enumField == null) {
            ObjectType objectType = data.getObjectTypeByFullName(base);
            if (objectType == null) {
                Import objectImport = data.getImportByName(base);
                if (objectImport != null) {
                    objectType = ObjectTypeFactory.createObjectType(descr, objectImport);
                } else {
                    objectType = ObjectTypeFactory.createObjectType(descr, base);
                }
                data.add(objectType);
            }
            enumField = new EnumField(descr);
            enumField.setObjectTypePath(objectType.getPath());
            enumField.setObjectTypeName(objectType.getName());
            enumField.setName(fieldName);
            objectType.getFields().add(enumField);
            data.add(enumField);
        }
        EnumRestriction restriction = new EnumRestriction(pattern);
        restriction.setPatternIsNot(pattern.isPatternNot());
        restriction.setFieldPath(field.getPath());
        restriction.setOperator(Operator.determineOperator(descr.getEvaluator(), descr.isNegated()));
        restriction.setEnumBasePath(enumField.getPath());
        restriction.setEnumBase(base);
        restriction.setEnumName(fieldName);
        restriction.setOrderNumber(orderNumber);
        restriction.setParentPath(pattern.getPath());
        restriction.setParentType(pattern.getVerifierComponentType());
        // Set field value, if it is not set.
        field.setFieldType(Field.ENUM);
        data.add(restriction);
        solvers.addPatternComponent(restriction);
    }
}
Also used : EnumField(org.drools.verifier.components.EnumField) ObjectType(org.drools.verifier.components.ObjectType) Variable(org.drools.verifier.components.Variable) PatternVariable(org.drools.verifier.components.PatternVariable) Import(org.drools.verifier.components.Import) EnumRestriction(org.drools.verifier.components.EnumRestriction) QualifiedIdentifierRestriction(org.drools.verifier.components.QualifiedIdentifierRestriction)

Aggregations

Import (org.drools.verifier.components.Import)4 ObjectType (org.drools.verifier.components.ObjectType)3 Field (org.drools.verifier.components.Field)2 PatternVariable (org.drools.verifier.components.PatternVariable)2 HashSet (java.util.HashSet)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 ImportDescr (org.drools.compiler.lang.descr.ImportDescr)1 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)1 EnumField (org.drools.verifier.components.EnumField)1 EnumRestriction (org.drools.verifier.components.EnumRestriction)1 Pattern (org.drools.verifier.components.Pattern)1 QualifiedIdentifierRestriction (org.drools.verifier.components.QualifiedIdentifierRestriction)1 Variable (org.drools.verifier.components.Variable)1 WorkingMemory (org.drools.verifier.components.WorkingMemory)1 PackageHeaderLoader (org.drools.verifier.jarloader.PackageHeaderLoader)1