Search in sources :

Example 66 with Expression

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);
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 67 with Expression

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 68 with Expression

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;
}
Also used : IN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.IN) Type(com.sri.ai.expresso.api.Type) Triple(com.sri.ai.util.base.Triple) Collection(java.util.Collection) Expressions(com.sri.ai.expresso.helper.Expressions) Set(java.util.Set) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) Triple.triple(com.sri.ai.util.base.Triple.triple) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Predicate(com.google.common.base.Predicate) Map(java.util.Map) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Registry(com.sri.ai.grinder.api.Registry) Util(com.sri.ai.util.Util) IndexExpressions.getIndices(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions.getIndices) Util.thereExists(com.sri.ai.util.Util.thereExists) Symbol(com.sri.ai.expresso.api.Symbol) ArrayList(java.util.ArrayList) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 69 with Expression

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionStepSolverToLiteralSplitterStepSolverAdapter(com.sri.ai.grinder.sgdpllt.core.solver.ExpressionStepSolverToLiteralSplitterStepSolverAdapter)

Example 70 with Expression

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1392 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)196 Context (com.sri.ai.grinder.api.Context)187 Type (com.sri.ai.expresso.api.Type)124 TrueContext (com.sri.ai.grinder.core.TrueContext)113 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)100 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)91 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)90 Context (com.sri.ai.grinder.sgdpllt.api.Context)87 Theory (com.sri.ai.grinder.api.Theory)78 Map (java.util.Map)78 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)71 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)68 List (java.util.List)68 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)63 CommonTheory (com.sri.ai.grinder.application.CommonTheory)55 LinkedHashMap (java.util.LinkedHashMap)55 LinkedHashSet (java.util.LinkedHashSet)54 Pair (com.sri.ai.util.base.Pair)52