Search in sources :

Example 46 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class CommutativeAssociativeWithOperationOnJavaConstantsOnly method operationOnOperableArguments.

@Override
public Expression operationOnOperableArguments(LinkedList<Expression> operableArguments) {
    List<Object> operableArgumentValues = Util.mapIntoList(operableArguments.iterator(), GetValue.INSTANCE);
    Object resultOnOperableArguments = operationOnOperableValues(operableArgumentValues);
    Expression result = Expressions.makeSymbol(resultOnOperableArguments);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 47 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class Max method add.

@Override
public Expression add(Expression value1, Expression value2, Context context) {
    Expression result;
    if (value1.getValue() instanceof Number && value2.getValue() instanceof Number) {
        Rational rationalValue1 = value1.rationalValue();
        Rational rationalValue2 = value2.rationalValue();
        if (rationalValue1.compareTo(rationalValue2) > 0) {
            result = value1;
        } else {
            result = value2;
        }
    } else if (value1.equals(INFINITY) || value2.equals(INFINITY)) {
        result = INFINITY;
    } else if (value1.equals(MINUS_INFINITY)) {
        result = value2;
    } else if (value2.equals(MINUS_INFINITY)) {
        result = value1;
    } else {
        result = Expressions.apply(MAX, value1, value2);
    }
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 48 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class Product method add.

@Override
public Expression add(Expression value1, Expression value2, Context context) {
    Expression result;
    if (value1.getValue() instanceof Number && value2.getValue() instanceof Number) {
        // not necessary, as else clause is generic enough to deal with this case as well, but hopefully this saves time.
        result = Expressions.makeSymbol(value1.rationalValue().multiply(value2.rationalValue()));
    } else {
        Expression multiplication = Times.make(arrayList(value1, value2));
        result = numericSimplifier.apply(multiplication, context);
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 49 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.

the class AbstractSGVET method product.

private Expression product(AssociativeCommutativeSemiRing semiRing, Collection<Expression> factors, Context context) {
    Expression multiplication = apply(semiRing.multiplicativeFunctor(), factors);
    Expression result = semiRing.multiply(multiplication, context);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 50 with Expression

use of com.sri.ai.expresso.api.Expression in project aic-praise by aic-sri-international.

the class HOGModelGrounding method contextSensitiveGroundingFrom.

private static void contextSensitiveGroundingFrom(int variableIndex, Expression expression, ArrayList<Expression> variables, BinaryFunction<Integer, Integer, Expression> fromVariableIndexAndValueIndexToValue, Function<Integer, Integer> fromVariableIndexToDomainSize, Theory theory, boolean firstIterationForVariable, boolean lastIterationForVariable, TernaryProcedure<Boolean, Boolean, Expression> recordValue, Context context) {
    Expression variable = variables.get(variableIndex);
    boolean isLastVariable = variableIndex == variables.size() - 1;
    int numberOfVariableValues = fromVariableIndexToDomainSize.apply(variableIndex);
    for (int variableValueIndex = 0; variableValueIndex != numberOfVariableValues; variableValueIndex++) {
        boolean thisVariableIsAtItsFirstValue = variableValueIndex == 0;
        boolean thisVariableIsAtItsLastValue = variableValueIndex == numberOfVariableValues - 1;
        Expression value = fromVariableIndexAndValueIndexToValue.apply(variableIndex, variableValueIndex);
        Expression expressionWithReplacedValue = expression.replaceAllOccurrences(variable, value, context);
        Expression simplifiedExpression = theory.simplify(expressionWithReplacedValue, context);
        boolean expressionIsSimplifiedToConstant = isLastVariable || simplifiedExpression.getSyntacticFormType().equals(Symbol.SYNTACTIC_FORM_TYPE);
        if (expressionIsSimplifiedToConstant) {
            myAssert(() -> isNumber(simplifiedExpression), () -> "Expression being grounded has been simplified to a symbol that is not a numerical constant: " + simplifiedExpression);
            int numberOfTimesThisValueMustBeWritten = numberOfAssignmentsForVariablesStartingAt(variableIndex + 1, variables.size(), fromVariableIndexToDomainSize);
            for (int i = 0; i != numberOfTimesThisValueMustBeWritten; i++) {
                boolean isFirstOverallValue = firstIterationForVariable && thisVariableIsAtItsFirstValue && i == 0;
                boolean isLastOverallValue = lastIterationForVariable && thisVariableIsAtItsLastValue && i == numberOfTimesThisValueMustBeWritten - 1;
                recordValue.apply(isFirstOverallValue, isLastOverallValue, simplifiedExpression);
            }
        } else {
            boolean firstIterationForNextVariable = firstIterationForVariable && thisVariableIsAtItsFirstValue;
            boolean lastIterationForNextVariable = lastIterationForVariable && thisVariableIsAtItsLastValue;
            contextSensitiveGroundingFrom(variableIndex + 1, simplifiedExpression, variables, fromVariableIndexAndValueIndexToValue, fromVariableIndexToDomainSize, theory, firstIterationForNextVariable, lastIterationForNextVariable, recordValue, context);
        }
    }
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

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