Search in sources :

Example 1 with MVELCompileable

use of org.drools.core.base.mvel.MVELCompileable in project drools by kiegroup.

the class TypeDeclarationConfigurator method getFieldExtractor.

private static InternalReadAccessor getFieldExtractor(TypeDeclaration type, String timestampField, InternalKnowledgePackage pkg, MVELAnalysisResult results) {
    InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader(ClassUtils.getPackage(type.getTypeClass()), type.getTypeClass().getName(), timestampField, type.isTypesafe(), results.getReturnType());
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("mvel");
    data.addCompileable((MVELCompileable) reader);
    ((MVELCompileable) reader).compile(data);
    return reader;
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) MVELCompileable(org.drools.core.base.mvel.MVELCompileable) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor)

Example 2 with MVELCompileable

use of org.drools.core.base.mvel.MVELCompileable in project drools by kiegroup.

the class MVELDialectRuntimeData method onBeforeExecute.

public void onBeforeExecute() {
    for (Wireable target : wireList) {
        for (MVELCompileable compileable : invokerLookups.get(target)) {
            compileable.compile(this);
            // now wire up the target
            target.wire(compileable);
        }
    }
    wireList.clear();
    for (MVELCompileable compileable : mvelReaders) {
        compileable.compile(this);
    }
    if (dirty) {
        rewireImportedMethods();
        dirty = false;
    }
}
Also used : MVELCompileable(org.drools.core.base.mvel.MVELCompileable) Wireable(org.drools.core.spi.Wireable)

Example 3 with MVELCompileable

use of org.drools.core.base.mvel.MVELCompileable in project drools by kiegroup.

the class MVELAccumulateBuilder method build.

@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    boolean typesafe = context.isTypesafe();
    try {
        final AccumulateDescr accumDescr = (AccumulateDescr) descr;
        if (!accumDescr.hasValidInput()) {
            return null;
        }
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
        // create source CE
        final RuleConditionElement source = builder.build(context, accumDescr.getInput());
        if (source == null) {
            return null;
        }
        MVELDialect dialect = (MVELDialect) context.getDialect();
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
        Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
        declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
        BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
        final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
        Accumulator[] accumulators;
        if (accumDescr.isExternalFunction()) {
            // uses accumulate functions
            accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
        } else {
            // it is a custom accumulate
            accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
        }
        List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
        for (Accumulator acc : accumulators) {
            MvelAccumulator mvelAcc = (MvelAccumulator) acc;
            Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
        }
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        Accumulate accumulate;
        if (accumDescr.isMultiFunction()) {
            accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators);
            int index = 0;
            for (Accumulator accumulator : accumulators) {
                data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
                ((MVELCompileable) accumulator).compile(data, context.getRule());
            }
        } else {
            accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
            data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
            ((MVELCompileable) accumulators[0]).compile(data, context.getRule());
        }
        return accumulate;
    } catch (Exception e) {
        DialectUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
        return null;
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : MVELAccumulator(org.drools.core.base.mvel.MVELAccumulator) Accumulator(org.drools.core.spi.Accumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) MVELCompileable(org.drools.core.base.mvel.MVELCompileable) ArrayList(java.util.ArrayList) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) Declaration(org.drools.core.rule.Declaration) MultiAccumulate(org.drools.core.rule.MultiAccumulate) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) SingleAccumulate(org.drools.core.rule.SingleAccumulate) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MvelAccumulator(org.drools.core.spi.MvelAccumulator)

Example 4 with MVELCompileable

use of org.drools.core.base.mvel.MVELCompileable in project drools by kiegroup.

the class MVELAccumulateBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(new PackageDescr("pkg1"));
    InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final AccumulateDescr accDescr = new AccumulateDescr();
    final PatternDescr inputPattern = new PatternDescr("org.drools.compiler.Cheese", "$cheese");
    accDescr.setInputPattern(inputPattern);
    accDescr.setInitCode("total = 0;");
    accDescr.setActionCode("total += $cheese.price;");
    accDescr.setReverseCode("total -= $cheese.price;");
    accDescr.setResultCode("new Integer(total)");
    final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
    final Accumulate acc = (Accumulate) builder.build(context, accDescr);
    ((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    MockLeftTupleSink sink = new MockLeftTupleSink();
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final Cheese cheddar2 = new Cheese("cheddar", 8);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    Object wmContext = acc.createWorkingMemoryContext();
    Object accContext = acc.createContext();
    acc.init(wmContext, accContext, tuple, ksession);
    acc.accumulate(wmContext, accContext, tuple, f1, ksession);
    acc.accumulate(wmContext, accContext, tuple, f2, ksession);
    assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
    acc.reverse(wmContext, accContext, tuple, f1, ksession);
    assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
Also used : MVELCompileable(org.drools.core.base.mvel.MVELCompileable) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MockLeftTupleSink(org.drools.compiler.reteoo.MockLeftTupleSink) Cheese(org.drools.compiler.Cheese) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) InitialFactImpl(org.drools.core.reteoo.InitialFactImpl) Accumulate(org.drools.core.rule.Accumulate) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

MVELCompileable (org.drools.core.base.mvel.MVELCompileable)4 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)2 Accumulate (org.drools.core.rule.Accumulate)2 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)2 ArrayList (java.util.ArrayList)1 Cheese (org.drools.compiler.Cheese)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 MockLeftTupleSink (org.drools.compiler.reteoo.MockLeftTupleSink)1 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)1 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)1 MVELAccumulator (org.drools.core.base.mvel.MVELAccumulator)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1