use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class VariableComponent method naiveCalcul.
public Expression naiveCalcul() {
Expression expression = this.model.naiveCalculation(this.variable);
String values = this.model.getValues(this.variable);
String string = "(" + expression + ")/sum({{ (on " + this.variable + " in " + values + " ) " + expression + " }})";
return this.model.theory.evaluate(parse(string), this.model.context);
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class Constraint method conjoinWithConjunctiveClause.
/**
* Returns the result of conjoining this constraint with all literal conjuncts of a given conjunctive clause
* (note that if <code>conjunction</code> is not an application of <code>and</code>,
* it will be considered a unit conjunction with itself the only conjunct.
* @param conjunctiveClause
* @param context
* @return the result of conjoining this constraint with all conjuncts of a given conjunction
*/
default default Constraint conjoinWithConjunctiveClause(Expression conjunctiveClause, Context context) {
Constraint result;
List<Expression> conjuncts = getConjuncts(conjunctiveClause);
if (conjuncts.size() == 1) {
// this is necessary to avoid an infinite loop
result = conjoinWithLiteral(conjuncts.get(0), context);
} else {
result = this;
for (Expression literal : conjuncts) {
result = result.conjoin(literal, context);
if (result == null) {
break;
}
}
}
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class Context method extendWith.
/**
* Extends context with index expressions, taking into account that new contextual variables may collide with existing ones.
* In this case, it renames the incoming variables to unique identifiers and replaces them in the types of remaining
* index expressions. It also renames the variables in a given expressions supposed to be in their scope (for example,
* the head and condition of an intensionally defined set).
* Returns the new context and the index expressions and expression in scope after the renaming.
* @param indexExpressions
* @param expressionInScope
* @return the new context and the index expressions and expression in scope after the renaming
*/
default default Triple<Context, ExtensionalIndexExpressionsSet, Expression> extendWith(ExtensionalIndexExpressionsSet indexExpressions, Expression expressionInScope) {
Triple<Context, ExtensionalIndexExpressionsSet, Expression> result;
if (thereExists(getIndices(indexExpressions), index -> this.containsSymbol(index))) {
// OPTIMIZATION: only kick in this entire procedure when extending with symbol in the context (previous ones could have been dealt with normally).
// the objects to be returned in the triple:
Context newContext = this;
ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
Expression newExpressionInScope = expressionInScope;
// Collects all existing symbols to be able to create unique symbols
Set<Expression> alreadyDefined = Util.set();
alreadyDefined.addAll(this.getSymbols());
alreadyDefined.addAll(Expressions.freeSymbols(new DefaultTuple(newIndexExpressionsList), this));
alreadyDefined.addAll(Expressions.freeSymbols(expressionInScope, this));
Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
for (int i = 0; i != newIndexExpressionsList.size(); i++) {
Expression indexExpression = newIndexExpressionsList.get(i);
Symbol index = (Symbol) indexExpression.get(0);
Expression type = indexExpression.get(1);
PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, newExpressionInScope);
Expression newIndex = newIndexAndNewExpressionInScope.first;
newExpressionInScope = newIndexAndNewExpressionInScope.second;
// type should not contain the index
Expression newIndexExpression = apply(IN, newIndex, type);
newIndexExpressionsList.set(i, newIndexExpression);
alreadyDefined.add(newIndex);
for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
Expression anotherIndexExpression = newIndexExpressionsList.get(j);
Expression anotherIndex = anotherIndexExpression.get(0);
Expression anotherType = anotherIndexExpression.get(1);
Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, this);
// anotherIndex is a symbols and does not contain index
Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
newIndexExpressionsList.set(j, newAnotherIndexExpression);
}
}
ExtensionalIndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(newIndexExpressionsList);
newContext = newContext.extendWith(newIndexExpressions);
result = triple(newContext, newIndexExpressions, newExpressionInScope);
} else {
// no collision; usual extension and the expressions do not change.
result = triple(extendWith(indexExpressions), indexExpressions, expressionInScope);
}
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class ExpressionStepSolver method solve.
default default Expression solve(Context context) {
ExpressionStepSolverToLiteralSplitterStepSolverAdapter adapter = new ExpressionStepSolverToLiteralSplitterStepSolverAdapter(this);
Expression result = adapter.solve(context);
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class Examples method DiamondModel.
public static VariableComponent DiamondModel() {
Expression func = DefaultSymbol.createSymbol("f");
Expression a = DefaultSymbol.createSymbol("A");
Expression b = DefaultSymbol.createSymbol("B");
Expression c = DefaultSymbol.createSymbol("C");
Expression q = DefaultSymbol.createSymbol("Q");
Expression trueValue = DefaultSymbol.createSymbol(true);
Expression f1 = apply(IF_THEN_ELSE, apply(EQUAL, a, trueValue), apply(IF_THEN_ELSE, apply(EQUAL, q, trueValue), 95, 5), apply(IF_THEN_ELSE, apply(EQUAL, q, trueValue), 5, 95));
Expression f2 = apply(IF_THEN_ELSE, apply(EQUAL, b, trueValue), apply(IF_THEN_ELSE, apply(EQUAL, q, trueValue), 5, 95), apply(IF_THEN_ELSE, apply(EQUAL, q, trueValue), 95, 5));
Expression f3 = apply(IF_THEN_ELSE, apply(EQUAL, c, trueValue), apply(IF_THEN_ELSE, apply(EQUAL, b, trueValue), 60, 40), apply(IF_THEN_ELSE, apply(EQUAL, b, trueValue), 40, 60));
Expression f4 = apply(IF_THEN_ELSE, apply(EQUAL, c, trueValue), apply(IF_THEN_ELSE, apply(EQUAL, a, trueValue), 50, 50), apply(IF_THEN_ELSE, apply(EQUAL, a, trueValue), 50, 50));
Expression f5 = apply(IF_THEN_ELSE, apply(EQUAL, a, trueValue), 1, 0);
Expression res = apply(func, q);
Set<Expression> Factor = new HashSet<Expression>();
Factor.add(f1);
Factor.add(f2);
Factor.add(f3);
Factor.add(f4);
Factor.add(f5);
Factor.add(res);
Model m = new Model(Factor);
VariableComponent ComponentResultat = new VariableComponent(q, res, m, new HashSet<Expression>());
return ComponentResultat;
}
Aggregations