Search in sources :

Example 1 with ConditionException

use of org.matheclipse.core.eval.exception.ConditionException in project symja_android_library by axkr.

the class PatternMatcher method evalAST.

/**
	 * 
	 * @param lhsPatternAST
	 * @param lhsEvalAST
	 * @param rhsExpr
	 * @param stackMatcher
	 * @return <code>F.NIL</code> if no match was found.
	 */
protected IExpr evalAST(final IAST lhsPatternAST, final IAST lhsEvalAST, final IExpr rhsExpr) {
    if (lhsPatternAST.size() < lhsEvalAST.size()) {
        if (lhsPatternAST.isOrderlessAST() && lhsPatternAST.isFlatAST()) {
            if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head(), null)) {
                return F.NIL;
            }
            final OrderlessMatcher foMatcher = new OrderlessMatcher(lhsPatternAST, lhsEvalAST);
            boolean matched = foMatcher.matchOrderlessAST(1, null);
            if (matched) {
                IAST lhsResultAST = (lhsEvalAST).clone();
                foMatcher.filterResult(lhsResultAST);
                IExpr result = fPatternMap.substituteSymbols(rhsExpr);
                try {
                    result = F.eval(result);
                    lhsResultAST.append(result);
                    return lhsResultAST;
                } catch (final ConditionException e) {
                    logConditionFalse(lhsEvalAST, lhsPatternAST, rhsExpr);
                // fall through
                } catch (final ReturnException e) {
                    lhsResultAST.append(e.getValue());
                    return lhsResultAST;
                }
            }
            return F.NIL;
        }
        if (lhsPatternAST.isFlatAST()) {
            if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head(), null)) {
                return F.NIL;
            }
            int len = lhsEvalAST.size() - lhsPatternAST.size();
            for (int i = 0; i < len; i++) {
                if (matchASTSequence(lhsPatternAST, lhsEvalAST, i, null)) {
                    IAST lhsResultAST = (lhsEvalAST).clone();
                    for (int j = 1; j < lhsPatternAST.size(); j++) {
                        lhsResultAST.remove(i + 1);
                    }
                    try {
                        IExpr result = fPatternMap.substituteSymbols(rhsExpr);
                        result = F.eval(result);
                        lhsResultAST.append(i + 1, result);
                        return lhsResultAST;
                    } catch (final ConditionException e) {
                        logConditionFalse(lhsEvalAST, lhsPatternAST, rhsExpr);
                    } catch (final ReturnException e) {
                        lhsResultAST.append(i + 1, e.getValue());
                        return lhsResultAST;
                    }
                    return F.NIL;
                }
            }
        }
    }
    return F.NIL;
}
Also used : ConditionException(org.matheclipse.core.eval.exception.ConditionException) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Example 2 with ConditionException

use of org.matheclipse.core.eval.exception.ConditionException in project symja_android_library by axkr.

the class PatternMatcher method matchFlatSequenceFromIndex.

/**
 * Match two {@link ISymbol#FLAT} <code>ASTs</code> where the <code>lhsEvalFlatAST</code> sequence
 * length can be greater equal than the <code>lhsPatternFlatAST</code> sequence length.
 *
 * <p>
 * Example:
 *
 * <pre>
 * >> SetAttributes(fl, Flat)
 *
 * >> fl(fl(a, b), c)", //
 * fl(a,b,c)
 *
 * >> fl(x_, x_) := fl(x)
 *
 * >> fl(b, b, b, c, c)
 * fl(b,c)
 *
 * >> fl(a, a, a, b, b, b, c, c)
 * fl(a,b,c)
 * </pre>
 *
 * @param lhsPatternFlatAST
 * @param lhsEvalFlatAST
 * @param rhsExpr
 * @param engine
 * @return
 */
private IExpr matchFlatSequenceFromIndex(final IAST lhsPatternFlatAST, final IAST lhsEvalFlatAST, final IExpr rhsExpr, EvalEngine engine) {
    final int len = lhsEvalFlatAST.size() - lhsPatternFlatAST.size() + 1;
    for (int i = 0; i < len; i++) {
        if (matchASTSequence(lhsPatternFlatAST, lhsEvalFlatAST, i, engine, new StackMatcher(engine))) {
            IASTAppendable lhsResultAST = lhsEvalFlatAST.copyAppendable();
            for (int j = 1; j < lhsPatternFlatAST.size(); j++) {
                lhsResultAST.remove(i + 1);
            }
            try {
                IExpr result = fPatternMap.substituteSymbols(rhsExpr, F.CEmptySequence);
                result = engine.evaluate(result);
                lhsResultAST.append(i + 1, result);
                return lhsResultAST;
            } catch (final ConditionException e) {
                if (LOGGER.isDebugEnabled()) {
                    logConditionFalse(lhsEvalFlatAST, lhsPatternFlatAST, rhsExpr);
                }
            } catch (final ReturnException e) {
                lhsResultAST.append(i + 1, e.getValue());
                return lhsResultAST;
            }
            return F.NIL;
        }
    }
    return F.NIL;
}
Also used : ConditionException(org.matheclipse.core.eval.exception.ConditionException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Example 3 with ConditionException

use of org.matheclipse.core.eval.exception.ConditionException in project symja_android_library by axkr.

the class PatternMatcherAndEvaluator method checkRHSCondition.

/**
 * Check if the condition for the right-hand-sides <code>Module[], With[] or Condition[]</code>
 * expressions evaluates to <code>true</code>.
 *
 * @return <code>true</code> if the right-hand-sides condition is fulfilled or not all patterns
 *         are assigned.
 */
@Override
public boolean checkRHSCondition(EvalEngine engine) {
    IPatternMap patternMap = createPatternMap();
    if (patternMap.getRHSEvaluated()) {
        return true;
    }
    if (!(fRightHandSide.isCondition() || fRightHandSide.isModuleOrWithCondition())) {
        return true;
    } else {
        if (!patternMap.isAllPatternsAssigned()) {
            return true;
        } else {
            boolean matched = false;
            IExpr rhs = patternMap.substituteSymbols(fRightHandSide, F.CEmptySequence);
            engine.pushOptionsStack();
            IEvalStepListener stepListener = engine.getStepListener();
            try {
                engine.setOptionsPattern(fLhsPatternExpr.topHead(), patternMap);
                if (Config.TRACE_REWRITE_RULE && stepListener != null) {
                    IExpr lhs = getLHSExprToMatch();
                    if (lhs.isPresent()) {
                        stepListener.setUp(lhs, 0);
                        fReturnResult = engine.addEvaluatedTraceStep(lhs, rhs, lhs.topHead(), F.$str("RewriteRule"));
                    } else {
                        fReturnResult = engine.evaluate(rhs);
                    }
                } else {
                    fReturnResult = engine.evaluate(rhs);
                }
                matched = true;
            } catch (final ConditionException e) {
                matched = false;
            } catch (final ReturnException e) {
                fReturnResult = e.getValue();
                matched = true;
            } finally {
                engine.popOptionsStack();
            }
            if (Config.TRACE_REWRITE_RULE && stepListener != null) {
                stepListener.tearDown(0, matched);
            }
            patternMap.setRHSEvaluated(matched);
            return matched;
        }
    }
}
Also used : ConditionException(org.matheclipse.core.eval.exception.ConditionException) IEvalStepListener(org.matheclipse.core.interfaces.IEvalStepListener) IExpr(org.matheclipse.core.interfaces.IExpr) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Example 4 with ConditionException

use of org.matheclipse.core.eval.exception.ConditionException in project symja_android_library by axkr.

the class PatternMatcherAndEvaluator method replacePatternMatch.

/**
 * A match which contains a pattern was found.
 *
 * <p>
 * Assumption <code>
 * matchExpr(fLhsPatternExpr, leftHandSide, engine, new StackMatcher(engine)) == true</code>.
 *
 * @param leftHandSide
 * @param patternMap
 * @param engine
 * @param evaluate
 * @return
 */
private IExpr replacePatternMatch(final IExpr leftHandSide, IPatternMap patternMap, EvalEngine engine, boolean evaluate) {
    if (RulesData.showSteps) {
        if (fLhsPatternExpr.head().equals(S.Integrate)) {
            IExpr rhs = fRightHandSide.orElse(S.Null);
            LOGGER.info("COMPLEX: {} := {}", fLhsPatternExpr, rhs);
            LOGGER.info(">>>>> {}", this);
        }
    }
    if (fReturnResult.isPresent()) {
        return fReturnResult;
    }
    engine.pushOptionsStack();
    try {
        engine.setOptionsPattern(fLhsPatternExpr.topHead(), patternMap);
        IExpr result = patternMap.substituteSymbols(fRightHandSide, F.CEmptySequence);
        if (evaluate) {
            if (Config.TRACE_REWRITE_RULE) {
                return engine.addEvaluatedTraceStep(leftHandSide, result, leftHandSide.topHead(), F.$str("RewriteRule"));
            }
            return engine.evaluate(result);
        } else {
            return result;
        }
    } catch (final ConditionException e) {
        if (LOGGER.isDebugEnabled()) {
            logConditionFalse(leftHandSide, fLhsPatternExpr, fRightHandSide);
        }
        return F.NIL;
    } catch (final ReturnException e) {
        IExpr result = e.getValue();
        if (evaluate) {
            if (Config.TRACE_REWRITE_RULE) {
                return engine.addEvaluatedTraceStep(leftHandSide, result, leftHandSide.topHead(), F.$str("RewriteRule"));
            }
            return engine.evaluate(result);
        }
        return result;
    } finally {
        engine.popOptionsStack();
    }
}
Also used : ConditionException(org.matheclipse.core.eval.exception.ConditionException) IExpr(org.matheclipse.core.interfaces.IExpr) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Example 5 with ConditionException

use of org.matheclipse.core.eval.exception.ConditionException in project symja_android_library by axkr.

the class PatternMatcher method replaceSubExpressionOrderlessFlat.

/**
 * Replace subexpressions for <code>Rule</code> or <code>RuleDelayed</code> in Flat or Orderless
 * expressions.
 *
 * <pre>
 * >> f(a, b, c) /. f(a, b) -> d
 * f(d,c)
 * </pre>
 *
 * @param lhsPatternAST
 * @param lhsEvalAST
 * @param rhsExpr
 * @param engine
 * @return <code>F.NIL</code> if no match was found.
 */
protected IExpr replaceSubExpressionOrderlessFlat(final IAST lhsPatternAST, final IAST lhsEvalAST, final IExpr rhsExpr, EvalEngine engine) {
    if (lhsPatternAST.size() < lhsEvalAST.size()) {
        if (lhsPatternAST.isOrderlessAST() && lhsPatternAST.isFlatAST()) {
            if (!matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
                return F.NIL;
            }
            final OrderlessMatcher foMatcher = new OrderlessMatcher(lhsPatternAST, lhsEvalAST);
            boolean matched = foMatcher.matchOrderlessAST(1, new StackMatcher(engine), engine);
            if (matched) {
                IASTAppendable lhsResultAST = (lhsEvalAST).copyAppendable();
                foMatcher.filterResult(lhsResultAST);
                IExpr result = fPatternMap.substituteSymbols(rhsExpr, F.NIL);
                try {
                    result = engine.evaluate(result);
                    lhsResultAST.append(result);
                    return lhsResultAST;
                } catch (final ConditionException e) {
                    if (LOGGER.isDebugEnabled()) {
                        logConditionFalse(lhsEvalAST, lhsPatternAST, rhsExpr);
                    }
                // fall through
                } catch (final ReturnException e) {
                    lhsResultAST.append(e.getValue());
                    return lhsResultAST;
                }
            }
            return F.NIL;
        }
        if (lhsPatternAST.isFlatAST()) {
            if (!matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
                return F.NIL;
            }
            return matchFlatSequenceFromIndex(lhsPatternAST, lhsEvalAST, rhsExpr, engine);
        }
    }
    return F.NIL;
}
Also used : ConditionException(org.matheclipse.core.eval.exception.ConditionException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Aggregations

ConditionException (org.matheclipse.core.eval.exception.ConditionException)5 ReturnException (org.matheclipse.core.eval.exception.ReturnException)5 IExpr (org.matheclipse.core.interfaces.IExpr)5 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IAST (org.matheclipse.core.interfaces.IAST)1 IEvalStepListener (org.matheclipse.core.interfaces.IEvalStepListener)1