Search in sources :

Example 11 with SubExpressionsDepthFirstIterator

use of com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator in project aic-expresso by aic-sri-international.

the class TupleQuantifierSimplifier method createTuplesOfVarsForTupleTypes.

private static Map<Expression, Expression> createTuplesOfVarsForTupleTypes(QuantifiedExpression quantifiedExpression, List<Map.Entry<Expression, Expression>> indexesOfTupleType) {
    Map<Expression, Expression> result = new HashMap<>();
    Set<Expression> allSubExpressions = Util.addAllToSet(new SubExpressionsDepthFirstIterator(quantifiedExpression));
    for (Map.Entry<Expression, Expression> entry : indexesOfTupleType) {
        List<Expression> tupleVars = new ArrayList<>();
        for (int i = 1; i <= entry.getValue().numberOfArguments(); i++) {
            Expression proposedVar = Expressions.makeSymbol(entry.getKey().toString() + "_" + i);
            Expression actualVar = Expressions.primedUntilUnique(proposedVar, expr -> !allSubExpressions.contains(expr));
            tupleVars.add(actualVar);
        }
        result.put(entry.getKey(), Expressions.makeTuple(tupleVars));
    }
    return result;
}
Also used : DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Example 12 with SubExpressionsDepthFirstIterator

use of com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator in project aic-expresso by aic-sri-international.

the class AbstractSGVET method width.

private int width(Expression expression, Context context) {
    Set<Expression> variables = new LinkedHashSet<Expression>();
    Iterator<Expression> iterator = new SubExpressionsDepthFirstIterator(expression);
    while (iterator.hasNext()) {
        Expression subExpression = iterator.next();
        if (isVariable(subExpression, context)) {
            variables.add(subExpression);
        }
    }
    int result = variables.size();
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Expression(com.sri.ai.expresso.api.Expression) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Constraint(com.sri.ai.grinder.api.Constraint)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)12 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)12 LinkedHashSet (java.util.LinkedHashSet)8 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)6 Type (com.sri.ai.expresso.api.Type)6 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)6 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)6 Util (com.sri.ai.util.Util)6 Map (java.util.Map)6 Set (java.util.Set)6 Beta (com.google.common.annotations.Beta)4 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)4 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Constraint (com.sri.ai.grinder.api.Constraint)3 Context (com.sri.ai.grinder.api.Context)3 And (com.sri.ai.grinder.library.boole.And)3 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)3 Sets (com.sri.ai.grinder.library.set.Sets)3 Simplifier (com.sri.ai.grinder.rewriter.api.Simplifier)3