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;
}
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;
}
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;
}
Aggregations