use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class AbstractFunctionBasedGroup method makeProblemExpression.
public Expression makeProblemExpression(List<Expression> indices, List<Expression> indicesTypes, Expression constraint, Expression body) {
List<Expression> indexExpressions = makeIndexExpressions(indices, indicesTypes);
IndexExpressionsSet indexExpressionsSet = new ExtensionalIndexExpressionsSet(indexExpressions);
DefaultIntensionalMultiSet set = new DefaultIntensionalMultiSet(indexExpressionsSet, body, constraint);
Expression problem = apply(getFunctionString(), set);
return problem;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class AbstractFunctionBasedGroup method getExpressionAndIndexExpressionsFromProblemExpression.
@Override
public Pair<Expression, IndexExpressionsSet> getExpressionAndIndexExpressionsFromProblemExpression(Expression expression, Context context) {
String functorString = getFunctionString();
Util.myAssert(() -> expression.hasFunctor(functorString), () -> "Expression expected to be application of " + functorString + " but is " + expression);
IntensionalSet set = (IntensionalSet) expression.get(0);
Expression body = IfThenElse.make(set.getCondition(), set.getHead(), additiveIdentityElement());
Pair<Expression, IndexExpressionsSet> result = Pair.make(body, set.getIndexExpressions());
return result;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class SampleCommonInterpreterTest method run.
private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
SamplingCommonInterpreter interpreter = new SamplingCommonInterpreter(sampleSizeN, alwaysSample, random);
Expression expression = parse(expressionString);
if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
if (indices.size() == 1) {
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(indexExpressions);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
SingleVariableConstraint singleVariableConstraint = null;
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
}
if (singleVariableConstraint != null) {
singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
expression = expression.set(0, intensionalSet);
}
}
}
Expression result = interpreter.apply(expression, context);
System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
return result;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class Expressions method makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees.
private static Expression makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
Expression indexExpressionsKleeneList = subTreeExpressions.get(0);
IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(ensureListFromKleeneList(indexExpressionsKleeneList));
Expression body = subTreeExpressions.get(1);
Expression result = new DefaultUniversallyQuantifiedFormula(indexExpressions, body);
return result;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class ExtensionalIndexExpressionsSet method replaceSymbol.
@Override
public IndexExpressionsSet replaceSymbol(Expression symbol, Expression newSymbol, Registry registry) {
Function<Expression, Expression> renameSymbol = e -> IndexExpressions.renameSymbol(e, symbol, newSymbol, registry);
List<Expression> newList = replaceElementsNonDestructively(getList(), renameSymbol);
IndexExpressionsSet newIndexExpressions;
if (newList != getList()) {
newIndexExpressions = new ExtensionalIndexExpressionsSet(newList);
} else {
newIndexExpressions = this;
}
return newIndexExpressions;
}
Aggregations