Search in sources :

Example 6 with ExpressionAndSyntacticContext

use of com.sri.ai.expresso.api.ExpressionAndSyntacticContext in project aic-expresso by aic-sri-international.

the class AbstractExpression method replace.

@Override
public Expression replace(Function<Expression, Expression> replacementFunction, ReplacementFunctionMaker makeSpecificSubExpressionAndSyntacticContextReplacementFunction, PruningPredicate prunePredicate, PruningPredicateMaker makeSpecificSubExpressionAndSyntacticContextPrunePredicate, boolean onlyTheFirstOne, boolean ignoreTopExpression, boolean replaceOnChildrenBeforeTopExpression, TernaryProcedure<Expression, Expression, Registry> listener, Registry registry) {
    if (prunePredicate != null && prunePredicate.apply(this, replacementFunction, registry)) {
        return this;
    }
    Expression result = this;
    if (!ignoreTopExpression && !replaceOnChildrenBeforeTopExpression) {
        // if replaceOnChildrenBeforeTopExpression, this is done later in the function
        result = applyReplacementFunction(replacementFunction, result, registry);
    }
    if (result == this) {
        // if no change in top expression, or need to change children first:
        Iterator<ExpressionAndSyntacticContext> subExpressionsAndContextsIterator = getImmediateSubExpressionsAndContextsIterator();
        while (subExpressionsAndContextsIterator.hasNext()) {
            ExpressionAndSyntacticContext subExpressionAndSyntacticContext = subExpressionsAndContextsIterator.next();
            Expression originalSubExpression = subExpressionAndSyntacticContext.getExpression();
            if (originalSubExpression == null) {
                // no replacement of null
                continue;
            }
            PruningPredicate prunePredicateForThisSubExpressionAndSyntacticContext = prunePredicate;
            if (makeSpecificSubExpressionAndSyntacticContextPrunePredicate != null) {
                prunePredicateForThisSubExpressionAndSyntacticContext = makeSpecificSubExpressionAndSyntacticContextPrunePredicate.apply(this, prunePredicate, subExpressionAndSyntacticContext);
                if (prunePredicateForThisSubExpressionAndSyntacticContext == Expressions.TRUE_PRUNING_PREDICATE) {
                    continue;
                }
            }
            Function<Expression, Expression> replacementFunctionForThisSubExpressionAndSyntacticContext = replacementFunction;
            if (makeSpecificSubExpressionAndSyntacticContextReplacementFunction != null) {
                replacementFunctionForThisSubExpressionAndSyntacticContext = makeSpecificSubExpressionAndSyntacticContextReplacementFunction.apply(this, replacementFunction, subExpressionAndSyntacticContext, registry);
            }
            Expression replacementSubExpression = originalSubExpression.replace(replacementFunctionForThisSubExpressionAndSyntacticContext, makeSpecificSubExpressionAndSyntacticContextReplacementFunction, prunePredicateForThisSubExpressionAndSyntacticContext, makeSpecificSubExpressionAndSyntacticContextPrunePredicate, onlyTheFirstOne, false, /* do not ignore top expression */
            replaceOnChildrenBeforeTopExpression, listener, registry);
            if (replacementSubExpression != originalSubExpression) {
                result = result.replace(subExpressionAndSyntacticContext.setExpression(replacementSubExpression));
                if (onlyTheFirstOne) {
                    break;
                }
            }
        }
        if (!ignoreTopExpression && replaceOnChildrenBeforeTopExpression) {
            result = applyReplacementFunction(replacementFunction, result, registry);
        }
    }
    if (listener != null) {
        listener.apply(this, result, registry);
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext)

Example 7 with ExpressionAndSyntacticContext

use of com.sri.ai.expresso.api.ExpressionAndSyntacticContext in project aic-expresso by aic-sri-international.

the class Expressions method replaceImmediateSubexpressions.

/**
 * Replaces immediate subexpressions by versions provided by a replacement function, conserving original instance if
 * function returns original subexpressions.
 */
public static Expression replaceImmediateSubexpressions(Expression expression, Function<Expression, Expression> replacementFunction) {
    Iterator<ExpressionAndSyntacticContext> expressionAndSyntacticContextIterator = expression.getImmediateSubExpressionsAndContextsIterator();
    while (expressionAndSyntacticContextIterator.hasNext()) {
        ExpressionAndSyntacticContext expressionAndSyntacticContext = expressionAndSyntacticContextIterator.next();
        Expression newExpression = replacementFunction.apply(expressionAndSyntacticContext.getExpression());
        Expression result = expressionAndSyntacticContext.getAddress().replace(expression, newExpression);
        expression = result;
    }
    return expression;
}
Also used : DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext)

Example 8 with ExpressionAndSyntacticContext

use of com.sri.ai.expresso.api.ExpressionAndSyntacticContext in project aic-expresso by aic-sri-international.

the class AbstractQuantifiedExpression method makeIndexExpressionSubExpressionsAndContext.

/**
 * A convenience method (for extensions' benefit) generating the {@link ExpressionAndSyntacticContext} objects with respect to the index expressions.
 * Extensions of {@link AbstractQuantifiedExpression} will still need to generate the {@link ExpressionAndSyntacticContext} objects
 * referring to other sub-expressions.
 */
protected List<ExpressionAndSyntacticContext> makeIndexExpressionSubExpressionsAndContext(List<ExpressionAndSyntacticContext> result) {
    List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) getIndexExpressions()).getList();
    for (int indexExpressionIndex = 0; indexExpressionIndex != indexExpressionsList.size(); indexExpressionIndex++) {
        Expression indexExpression = indexExpressionsList.get(indexExpressionIndex);
        Expression index = IndexExpressions.getIndex(indexExpression);
        if (index.getSyntacticFormType().equals(FunctionApplication.SYNTACTIC_FORM_TYPE)) {
            for (int argumentIndex = 0; argumentIndex != index.numberOfArguments(); argumentIndex++) {
                ExpressionAndSyntacticContext expressionAndSyntacticContext = makeAddressForIndexArgument(indexExpressionIndex, index, argumentIndex);
                result.add(expressionAndSyntacticContext);
            }
        }
        Expression type = IndexExpressions.getType(indexExpression);
        if (indexExpression.hasFunctor(FunctorConstants.IN)) {
            ExpressionAndSyntacticContext expressionAndSyntacticContext = makeAddressForIndexType(indexExpressionIndex, type);
            result.add(expressionAndSyntacticContext);
        }
    }
    return result;
}
Also used : AbstractExpression(com.sri.ai.grinder.core.AbstractExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext)

Aggregations

ExpressionAndSyntacticContext (com.sri.ai.expresso.api.ExpressionAndSyntacticContext)8 Expression (com.sri.ai.expresso.api.Expression)6 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)4 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)4 SubExpressionAddress (com.sri.ai.expresso.api.SubExpressionAddress)2 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)1 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)1 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)1 AbstractExpression (com.sri.ai.grinder.core.AbstractExpression)1