Search in sources :

Example 1 with ClassFieldAccessorStore

use of org.drools.mvel.accessors.ClassFieldAccessorStore in project drools by kiegroup.

the class BetaNodeBuilder method build.

public BetaNode build() {
    NodeFactory nFactory = CoreComponentFactory.get().getNodeFactoryService();
    EntryPointNode epn = buildContext.getRuleBase().getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = nFactory.buildObjectTypeNode(buildContext.getNextNodeId(), epn, new ClassObjectType(leftType), buildContext);
    LeftInputAdapterNode leftInput = nFactory.buildLeftInputAdapterNode(buildContext.getNextNodeId(), otn, buildContext, false);
    ObjectSource rightInput = nFactory.buildObjectTypeNode(buildContext.getNextNodeId(), epn, new ClassObjectType(rightType), buildContext);
    ReteTesterHelper reteTesterHelper = new ReteTesterHelper();
    Pattern pattern = new Pattern(0, new ClassObjectType(leftType));
    // BetaNodeFieldConstraint betaConstraint = null;
    BetaConstraints betaConstraints = null;
    if (constraintFieldName != null) {
        ClassFieldAccessorStore store = (ClassFieldAccessorStore) reteTesterHelper.getStore();
        InternalReadAccessor extractor = store.getReader(leftType, leftFieldName);
        Declaration declr = new Declaration(leftVariableName, extractor, pattern);
        betaConstraints = new SingleBetaConstraints(reteTesterHelper.getBoundVariableConstraint(rightType, constraintFieldName, declr, constraintOperator), buildContext.getRuleBase().getConfiguration());
    } else {
        betaConstraints = new EmptyBetaConstraints();
    }
    switch(nodeType) {
        case NodeTypeEnums.JoinNode:
            return new JoinNode(0, leftInput, rightInput, betaConstraints, buildContext);
        case NodeTypeEnums.NotNode:
            return new NotNode(0, leftInput, rightInput, betaConstraints, buildContext);
        case NodeTypeEnums.ExistsNode:
            return new ExistsNode(0, leftInput, rightInput, betaConstraints, buildContext);
    }
    throw new IllegalStateException("Unable to build Node");
}
Also used : Pattern(org.drools.core.rule.Pattern) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) NotNode(org.drools.core.reteoo.NotNode) BetaConstraints(org.drools.core.common.BetaConstraints) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ClassObjectType(org.drools.core.base.ClassObjectType) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) ExistsNode(org.drools.core.reteoo.ExistsNode) EntryPointNode(org.drools.core.reteoo.EntryPointNode) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectSource(org.drools.core.reteoo.ObjectSource) Declaration(org.drools.core.rule.Declaration) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 2 with ClassFieldAccessorStore

use of org.drools.mvel.accessors.ClassFieldAccessorStore in project drools by kiegroup.

the class AbstractTraitFactory method buildClassDefinition.

public ClassDefinition buildClassDefinition(Class<?> klazz, Class<?> wrapperClass) throws IOException {
    ClassFieldInspectorImpl inspector = new ClassFieldInspectorImpl(klazz);
    ClassFieldAccessorStore store = getClassFieldAccessorStore();
    ClassDefinition def;
    if (!klazz.isInterface()) {
        String className = wrapperClass.getName();
        String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
        String[] interfaces = new String[klazz.getInterfaces().length + 1];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        interfaces[interfaces.length - 1] = CoreWrapper.class.getName();
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(wrapperClass);
        Traitable tbl = wrapperClass.getAnnotation(Traitable.class);
        def.setTraitable(true, tbl != null && tbl.logical());
        Map<String, Field> fields = inspector.getFieldTypesField();
        for (Field f : fields.values()) {
            if (f != null) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(f.getName());
                fld.setTypeName(f.getType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                if (inspector.getGetterMethods().containsKey(f.getName())) {
                    fld.setGetterName(inspector.getGetterMethods().get(f.getName()).getName());
                }
                if (inspector.getSetterMethods().containsKey(f.getName())) {
                    fld.setSetterName(inspector.getSetterMethods().get(f.getName()).getName());
                }
                def.addField(fld);
            }
        }
    } else {
        String className = klazz.getName();
        String superClass = Object.class.getName();
        String[] interfaces = new String[klazz.getInterfaces().length];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(klazz);
        Map<String, Method> properties = inspector.getGetterMethods();
        for (Map.Entry<String, Method> propEntry : properties.entrySet()) {
            Method m = propEntry.getValue();
            if (m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class && inspector.getSetterMethods().containsKey(propEntry.getKey())) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(getterToFieldName(m.getName()));
                fld.setTypeName(m.getReturnType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                fld.setGetterName(m.getName());
                fld.setSetterName(inspector.getSetterMethods().get(propEntry.getKey()).getName());
                def.addField(fld);
            }
        }
    }
    return def;
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) TraitType(org.drools.core.factmodel.traits.TraitType) ClassFieldInspectorImpl(org.drools.mvel.asm.ClassFieldInspectorImpl) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) Method(java.lang.reflect.Method) ClassDefinition(org.drools.core.factmodel.ClassDefinition) Field(java.lang.reflect.Field) ClassFieldAccessor(org.drools.mvel.accessors.ClassFieldAccessor) Traitable(org.drools.core.factmodel.traits.Traitable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ClassFieldAccessorStore

use of org.drools.mvel.accessors.ClassFieldAccessorStore in project drools by kiegroup.

the class ClassBuilderTest method build.

private Class build(ClassBuilder builder, ClassDefinition classDef) throws Exception {
    classLoader = new PackageClassLoader(data.getStore(), ProjectClassLoader.createProjectClassLoader());
    byte[] d = builder.buildClass(classDef, classLoader);
    data.write(convertClassToResourcePath(classDef.getClassName()), d);
    this.store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(classLoader));
    store.setEagerWire(true);
    Class clazz = classLoader.loadClass(classDef.getClassName());
    classDef.setDefinedClass(clazz);
    return clazz;
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) PackageClassLoader(org.drools.wiring.dynamic.PackageClassLoader)

Example 4 with ClassFieldAccessorStore

use of org.drools.mvel.accessors.ClassFieldAccessorStore in project drools by kiegroup.

the class InstancesHashcodedTest method build.

/*
        <dimension name="bck_cycle_dm"  item-expression="cycleDimension"
                   mode="ignore" cache="10" export-to="cycle_id">
            <id    name="objid"            type="long"             generator-sql="select bck_dimensions_uid.nextval from dual"/>
            <field name="cut_date"         type="java.util.Date"   value="cutDate"     key="true"/>
            <field name="due_date"         type="java.util.Date"   value="dueDate"     key="true"/>
            <field name="cycle_code"       type="java.lang.String" value="cycleCode"/>
            <field name="issue_date"       type="java.util.Date"   value="issueDate"/>
        </dimension>
 */
private Class build(ClassBuilder builder, ClassDefinition classDef) throws Exception {
    ProjectClassLoader prjClassLoader = ProjectClassLoader.createProjectClassLoader();
    byte[] d = builder.buildClass(classDef, prjClassLoader);
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.write(convertClassToResourcePath(classDef.getClassName()), d);
    ClassLoader classLoader = new PackageClassLoader(data.getStore(), prjClassLoader);
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(classLoader));
    store.setEagerWire(true);
    Class clazz = classLoader.loadClass(classDef.getClassName());
    classDef.setDefinedClass(clazz);
    return clazz;
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) PackageClassLoader(org.drools.wiring.dynamic.PackageClassLoader) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) PackageClassLoader(org.drools.wiring.dynamic.PackageClassLoader)

Example 5 with ClassFieldAccessorStore

use of org.drools.mvel.accessors.ClassFieldAccessorStore in project drools by kiegroup.

the class CharClassFieldExtractorTest method setUp.

@Before
public void setUp() throws Exception {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    this.reader = store.getReader(TestBean.class, "charAttr");
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) TestBean(org.drools.core.base.TestBean) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) Before(org.junit.Before)

Aggregations

ClassFieldAccessorStore (org.drools.mvel.accessors.ClassFieldAccessorStore)18 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)16 Before (org.junit.Before)11 TestBean (org.drools.core.base.TestBean)9 ClassObjectType (org.drools.core.base.ClassObjectType)5 Pattern (org.drools.core.rule.Pattern)5 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)4 Declaration (org.drools.core.rule.Declaration)3 IOException (java.io.IOException)2 ObjectInput (java.io.ObjectInput)2 ObjectOutput (java.io.ObjectOutput)2 BetaConstraints (org.drools.core.common.BetaConstraints)2 ReteEvaluator (org.drools.core.common.ReteEvaluator)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)2 Consequence (org.drools.core.spi.Consequence)2 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)2 PackageClassLoader (org.drools.wiring.dynamic.PackageClassLoader)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1