Search in sources :

Example 1 with PatternDescr

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

the class ClassDefinitionFactory method populateDefinitionFromClass.

public static void populateDefinitionFromClass(ClassDefinition def, Resource resource, Class<?> concrete, boolean asTrait) {
    try {
        def.setClassName(concrete.getName());
        if (concrete.getSuperclass() != null) {
            def.setSuperClass(concrete.getSuperclass().getName());
        }
        ClassFieldInspector inspector = new ClassFieldInspector(concrete);
        Map<String, Method> methods = inspector.getGetterMethods();
        Map<String, Method> setters = inspector.getSetterMethods();
        int j = 0;
        Map<String, TypeFieldDescr> fields = new HashMap<String, TypeFieldDescr>();
        for (String fieldName : methods.keySet()) {
            if (asTrait && ("core".equals(fieldName) || "fields".equals(fieldName))) {
                continue;
            }
            if (!inspector.isNonGetter(fieldName) && setters.keySet().contains(fieldName)) {
                Position position = null;
                if (!concrete.isInterface()) {
                    try {
                        Field fld = concrete.getDeclaredField(fieldName);
                        position = fld.getAnnotation(Position.class);
                    } catch (NoSuchFieldException nsfe) {
                    // @Position can only annotate fields. This x means that a getter/setter pair was found with no field
                    }
                }
                Class ret = methods.get(fieldName).getReturnType();
                TypeFieldDescr field = new TypeFieldDescr();
                field.setResource(resource);
                field.setFieldName(fieldName);
                field.setPattern(new PatternDescr(ret.getName()));
                field.setIndex(position != null ? position.value() : -1);
                fields.put(fieldName, field);
            }
        }
        if (!fields.isEmpty()) {
            List<FieldDefinition> fieldDefs = sortFields(fields, null, null);
            int i = 0;
            for (FieldDefinition fieldDef : fieldDefs) {
                fieldDef.setIndex(i++);
                def.addField(fieldDef);
            }
        }
        Set<String> interfaces = new HashSet<String>();
        Collections.addAll(interfaces, def.getInterfaces());
        for (Class iKlass : ClassUtils.getAllImplementedInterfaceNames(concrete)) {
            interfaces.add(iKlass.getName());
        }
        def.setInterfaces(interfaces.toArray(new String[interfaces.size()]));
        def.setDefinedClass(concrete);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) HashMap(java.util.HashMap) Position(org.kie.api.definition.type.Position) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Method(java.lang.reflect.Method) IOException(java.io.IOException) Field(java.lang.reflect.Field) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector) HashSet(java.util.HashSet)

Example 2 with PatternDescr

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

the class ClassHierarchyManager method buildDescrsFromFields.

private static void buildDescrsFromFields(Class klass, TypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, Map<String, TypeFieldDescr> fieldMap) {
    ClassFieldInspector inspector = null;
    try {
        inspector = new ClassFieldInspector(klass);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    for (String name : inspector.getGetterMethods().keySet()) {
        // classFieldAccessor requires both getter and setter
        if (inspector.getSetterMethods().containsKey(name)) {
            if (!inspector.isNonGetter(name) && !"class".equals(name)) {
                Resource resource = typeDescr.getResource();
                PatternDescr patternDescr = new PatternDescr(inspector.getFieldType(name).getName());
                patternDescr.setResource(resource);
                TypeFieldDescr inheritedFlDescr = new TypeFieldDescr(name, patternDescr);
                inheritedFlDescr.setResource(resource);
                inheritedFlDescr.setInherited(!Modifier.isAbstract(inspector.getGetterMethods().get(name).getModifiers()));
                if (!fieldMap.containsKey(inheritedFlDescr.getFieldName()))
                    fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
            }
        }
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Resource(org.kie.api.io.Resource) IOException(java.io.IOException) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 3 with PatternDescr

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

the class ClassHierarchyManager method buildInheritedFieldDescrFromDefinition.

protected TypeFieldDescr buildInheritedFieldDescrFromDefinition(org.kie.api.definition.type.FactField fld, TypeDeclarationDescr typeDescr) {
    TypeFieldDescr inheritedFldDescr = new TypeFieldDescr();
    inheritedFldDescr.setFieldName(fld.getName());
    inheritedFldDescr.setResource(typeDescr.getResource());
    PatternDescr fldType = new PatternDescr();
    fldType.setObjectType(((FieldDefinition) fld).getTypeName());
    // also sets resource for PatternDescr fldType
    inheritedFldDescr.setPattern(fldType);
    if (fld.isKey()) {
        AnnotationDescr keyAnnotation = new AnnotationDescr(Key.class.getCanonicalName());
        keyAnnotation.setFullyQualifiedName(Key.class.getCanonicalName());
        keyAnnotation.setResource(typeDescr.getResource());
        inheritedFldDescr.addAnnotation(keyAnnotation);
    }
    inheritedFldDescr.setIndex(((FieldDefinition) fld).getDeclIndex());
    inheritedFldDescr.setInherited(true);
    String initExprOverride = ((FieldDefinition) fld).getInitExpr();
    int overrideCount = 0;
    // only @aliasing local fields may override defaults.
    for (TypeFieldDescr localField : typeDescr.getFields().values()) {
        Alias alias = localField.getTypedAnnotation(Alias.class);
        if (alias != null && fld.getName().equals(alias.value().replaceAll("\"", "")) && localField.getInitExpr() != null) {
            overrideCount++;
            initExprOverride = localField.getInitExpr();
        }
    }
    if (overrideCount > 1) {
        // however, only one is allowed
        initExprOverride = null;
    }
    inheritedFldDescr.setInitExpr(initExprOverride);
    return inheritedFldDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Alias(org.drools.core.factmodel.traits.Alias) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Key(org.kie.api.definition.type.Key)

Example 4 with PatternDescr

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

the class PatternHandler method start.

public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final String objectType = attrs.getValue("object-type");
    if (objectType == null || objectType.trim().equals("")) {
        throw new SAXParseException("<pattern> requires an 'object-type' attribute", parser.getLocator());
    }
    PatternDescr patternDescr = null;
    final String identifier = attrs.getValue("identifier");
    if (identifier == null || identifier.trim().equals("")) {
        patternDescr = new PatternDescr(objectType);
    } else {
        patternDescr = new PatternDescr(objectType, identifier);
    }
    return patternDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) SAXParseException(org.xml.sax.SAXParseException)

Example 5 with PatternDescr

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

the class PatternHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final PatternDescr patternDescr = (PatternDescr) parser.getCurrent();
    final Object parent = parser.getParent();
    if (parent instanceof PatternDestinationDescr) {
        final PatternDestinationDescr parentDescr = (PatternDestinationDescr) parent;
        parentDescr.setInputPattern(patternDescr);
    } else {
        final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
        parentDescr.addDescr(patternDescr);
    }
    return patternDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) PatternDestinationDescr(org.drools.compiler.lang.descr.PatternDestinationDescr) Element(org.w3c.dom.Element) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Aggregations

PatternDescr (org.drools.compiler.lang.descr.PatternDescr)150 Test (org.junit.Test)122 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)84 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)56 AndDescr (org.drools.compiler.lang.descr.AndDescr)52 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)19 Cheese (org.drools.compiler.Cheese)14 FromDescr (org.drools.compiler.lang.descr.FromDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 NotDescr (org.drools.compiler.lang.descr.NotDescr)11 OrDescr (org.drools.compiler.lang.descr.OrDescr)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 Element (org.w3c.dom.Element)9 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 InputStreamReader (java.io.InputStreamReader)7 List (java.util.List)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7