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;
}
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;
}
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;
}
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;
}
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);
}
}
}
Aggregations