Search in sources :

Example 1 with From

use of org.drools.core.rule.From in project drools by kiegroup.

the class FromBuilder method build.

/* (non-Javadoc)
     * @see org.kie.reteoo.builder.ReteooComponentBuilder#build(org.kie.reteoo.builder.BuildContext, org.kie.reteoo.builder.BuildUtils, org.kie.rule.RuleConditionElement)
     */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final From from = (From) rce;
    context.pushRuleComponent(from);
    @SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
    AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
    NodeFactory nodeFactory = CoreComponentFactory.get().getNodeFactoryService();
    FromNode fromNode = from.isReactive() ? nodeFactory.buildReactiveFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from) : nodeFactory.buildFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
    context.setTupleSource(utils.attachNode(context, fromNode));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    context.popRuleComponent();
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) From(org.drools.core.rule.From) FromNode(org.drools.core.reteoo.FromNode)

Example 2 with From

use of org.drools.core.rule.From in project drools by kiegroup.

the class MVELFromBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    String text = ((FromDescr) descr).getExpression();
    Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
    if (entryPointId.isPresent()) {
        return entryPointId.get();
    }
    // This builder is re-usable in other dialects, so specify by name
    MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
    boolean typeSafe = context.isTypesafe();
    if (!dialect.isStrictMode()) {
        context.setTypesafe(false);
    }
    try {
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        if (analysis == null) {
            // something bad happened
            return null;
        }
        Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
        if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
            context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
            return null;
        }
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
        int j = 0;
        for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
            declarations[j++] = decls.get(str);
        }
        Arrays.sort(declarations, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
        MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
        From from = new From(dataProvider);
        from.setResultPattern(prefixPattern);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(from, dataProvider);
        dataProvider.compile(data, context.getRule());
        return from;
    } catch (final Exception e) {
        DialectUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
        return null;
    } finally {
        context.setTypesafe(typeSafe);
    }
}
Also used : MVELDataProvider(org.drools.core.base.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) FromDescr(org.drools.compiler.lang.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Example 3 with From

use of org.drools.core.rule.From in project drools by kiegroup.

the class ReactiveFromBuilder method build.

public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final From from = (From) rce;
    context.pushRuleComponent(from);
    @SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
    AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
    ReactiveFromNode node = CoreComponentFactory.get().getNodeFactoryService().buildReactiveFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
    context.setTupleSource(utils.attachNode(context, node));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    List<XpathConstraint> xpathConstraints = context.getXpathConstraints();
    for (XpathConstraint xpathConstraint : xpathConstraints) {
        for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
            context.setAlphaConstraints(chunk.getAlphaConstraints());
            context.setBetaconstraints(chunk.getBetaConstraints());
            context.setXpathConstraints(chunk.getXpathConstraints());
            build(context, utils, chunk.asFrom());
        }
    }
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BetaConstraints(org.drools.core.common.BetaConstraints) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) From(org.drools.core.rule.From)

Example 4 with From

use of org.drools.core.rule.From in project drools by kiegroup.

the class MVELFromBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    String text = ((FromDescr) descr).getExpression();
    Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
    if (entryPointId.isPresent()) {
        return entryPointId.get();
    }
    // This builder is re-usable in other dialects, so specify by name
    MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
    boolean typeSafe = context.isTypesafe();
    if (!dialect.isStrictMode()) {
        context.setTypesafe(false);
    }
    try {
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        if (analysis == null) {
            // something bad happened
            return null;
        }
        Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
        if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
            context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
            return null;
        }
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
        int j = 0;
        for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
            declarations[j++] = decls.get(str);
        }
        Arrays.sort(declarations, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
        MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
        From from = new From(dataProvider);
        from.setResultPattern(prefixPattern);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(from, dataProvider);
        dataProvider.compile(data, context.getRule());
        return from;
    } catch (final Exception e) {
        AsmUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
        return null;
    } finally {
        context.setTypesafe(typeSafe);
    }
}
Also used : MVELDataProvider(org.drools.mvel.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) FromDescr(org.drools.drl.ast.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Aggregations

From (org.drools.core.rule.From)4 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 BetaConstraints (org.drools.core.common.BetaConstraints)2 Declaration (org.drools.core.rule.Declaration)2 EntryPointId (org.drools.core.rule.EntryPointId)2 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)2 FromDescr (org.drools.compiler.lang.descr.FromDescr)1 MVELDataProvider (org.drools.core.base.dataproviders.MVELDataProvider)1 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)1 FromNode (org.drools.core.reteoo.FromNode)1 ReactiveFromNode (org.drools.core.reteoo.ReactiveFromNode)1 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)1 XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)1 FromDescr (org.drools.drl.ast.descr.FromDescr)1 MVELDialectRuntimeData (org.drools.mvel.MVELDialectRuntimeData)1 MVELDataProvider (org.drools.mvel.dataproviders.MVELDataProvider)1 MVELCompilationUnit (org.drools.mvel.expr.MVELCompilationUnit)1