use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration in project aic-praise by aic-sri-international.
the class HOGMModelValidator method validateFunctorsAndArguments.
void validateFunctorsAndArguments(StatementInfo termStatement) {
// Ensure constant functions have the correct arity and their arguments are of the correct type
List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> constantFunctionsWithScope = getConstantFunctionsWithScope(termStatement.statement);
constantFunctionsWithScope.forEach(constantFunctionAndScope -> {
Expression constantFunction = constantFunctionAndScope.first;
Map<Expression, HOGMConstantDeclaration> scopedConstants = constantFunctionAndScope.second;
if (!isValidDeclaredConstantFunctorArity(constantFunction, scopedConstants)) {
newError(Type.TERM_ARITY_OF_FUNCTOR_DOES_NOT_MATCH_DECLARATION, constantFunction, termStatement);
} else {
HOGMConstantDeclaration constantDeclaration = scopedConstants.get(constantFunction.getFunctorOrSymbol());
for (int i = 0; i < constantFunction.numberOfArguments(); i++) {
Expression arg = constantFunction.get(i);
if (isUnknownConstant(arg, scopedConstants)) {
newError(Type.TERM_CONSTANT_NOT_DEFINED, arg, termStatement);
} else {
if (getSort(constantDeclaration.getParameterSorts().get(i)) != determineSortType(arg, scopedConstants)) {
newError(Type.CONSTANT_ARGUMENT_IS_OF_THE_INCORRECT_TYPE, arg, termStatement);
}
}
}
}
});
// Ensure random functions have the correct arity and their arguments are of the correct type
List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> randomFunctionsWithScope = getRandomFunctionsWithScope(termStatement.statement);
randomFunctionsWithScope.forEach(randomFunctionAndScope -> {
Expression randomFunction = randomFunctionAndScope.first;
Map<Expression, HOGMConstantDeclaration> scopedConstants = randomFunctionAndScope.second;
if (!isValidDeclaredRandomFunctorArity(randomFunction)) {
newError(Type.TERM_ARITY_OF_FUNCTOR_DOES_NOT_MATCH_DECLARATION, randomFunction, termStatement);
} else {
HOGMRandomVariableDeclaration rvDeclaration = randoms.get(randomFunction.getFunctorOrSymbol());
for (int i = 0; i < randomFunction.numberOfArguments(); i++) {
Expression arg = randomFunction.get(i);
if (isUnknownConstant(arg, scopedConstants)) {
newError(Type.TERM_CONSTANT_NOT_DEFINED, arg, termStatement);
} else {
if (getSort(rvDeclaration.getParameterSorts().get(i)) != determineSortType(arg, scopedConstants)) {
newError(Type.RANDOM_VARIABLE_ARGUMENT_IS_OF_THE_INCORRECT_TYPE, arg, termStatement);
}
}
}
}
});
// All of these should belong to the known set of functors
List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> nonConstantAndRandomFunctionsWithScope = getNonConstantRandomFunctionsWithScope(termStatement.statement);
nonConstantAndRandomFunctionsWithScope.forEach(x -> checkNonConstantAndRandomFunctionWithScope(x, termStatement));
checkQuantifiedExpressions(termStatement);
}
use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration in project aic-praise by aic-sri-international.
the class HOGMModelValidator method updateQuantifiers.
Expression updateQuantifiers(Expression expr, Map<Expression, HOGMConstantDeclaration> currentScope) {
Expression result = expr;
if (isQuantifiedExpression(expr)) {
Expression bodyExpression = getQuantifiedExpressionBody(expr);
if (bodyExpression != null) {
Map<Expression, HOGMConstantDeclaration> quantifierScope = new LinkedHashMap<>(currentScope);
quantifierScope.putAll(getQuantifiedExpressionScope(expr));
Expression updatedBodyExpression = updateQuantifiers(bodyExpression, quantifierScope);
HOGMModelValidator.TermCategoryType updatedBodyTermCategoryType = determineTermCategoryType(updatedBodyExpression);
if (updatedBodyTermCategoryType == TermCategoryType.NUMERIC) {
Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(((QuantifiedExpression) expr).getIndexExpressions(), updatedBodyExpression, Expressions.TRUE);
result = Expressions.apply(FunctorConstants.PRODUCT, intensionalMultiSet);
} else if (bodyExpression != updatedBodyExpression) {
if (ForAll.isForAll(expr)) {
result = ForAll.make(ForAll.getIndexExpression(expr), updatedBodyExpression);
} else {
// Otherwise is existential
result = ThereExists.make(ThereExists.getIndexExpression(expr), updatedBodyExpression);
}
}
}
} else if (Expressions.isFunctionApplicationWithArguments(expr)) {
List<Expression> updatedArgs = expr.getArguments().stream().map(arg -> updateQuantifiers(arg, currentScope)).collect(Collectors.toList());
if (!sameInstancesInSameIterableOrder(expr.getArguments(), updatedArgs)) {
result = Expressions.apply(expr.getFunctor(), updatedArgs);
}
HOGMModelValidator.TermCategoryType resultTermCategoryType = determineTermCategoryType(result);
if (resultTermCategoryType == TermCategoryType.INVALID) {
if (IfThenElse.isIfThenElse(result)) {
Expression asRule = attemptMakeRule(result);
if (asRule != null) {
result = asRule;
}
}
}
}
return result;
}
use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration in project aic-praise by aic-sri-international.
the class HOGMModelValidator method getQuantifiers.
void getQuantifiers(Expression expr, List<Pair<Expression, Map<Expression, HOGMConstantDeclaration>>> quantifiersWithScope, Map<Expression, HOGMConstantDeclaration> currentScope) {
if (isQuantifiedExpression(expr)) {
Map<Expression, HOGMConstantDeclaration> quantifierScope = new LinkedHashMap<>(currentScope);
quantifierScope.putAll(getQuantifiedExpressionScope(expr));
quantifiersWithScope.add(new Pair<>(expr, quantifierScope));
Expression bodyExpression = getQuantifiedExpressionBody(expr);
if (bodyExpression != null) {
getQuantifiers(bodyExpression, quantifiersWithScope, quantifierScope);
}
}
if (Expressions.isFunctionApplicationWithArguments(expr)) {
expr.getArguments().forEach(arg -> getQuantifiers(arg, quantifiersWithScope, currentScope));
}
}
use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration in project aic-praise by aic-sri-international.
the class HOGMModelValidator method checkQuantifiedExpressionWithScope.
private void checkQuantifiedExpressionWithScope(StatementInfo termStatement, Pair<Expression, Map<Expression, HOGMConstantDeclaration>> quantifiedExpressionWithScope) {
QuantifiedExpression quantifiedExpression = (QuantifiedExpression) quantifiedExpressionWithScope.first;
Map<Expression, HOGMConstantDeclaration> scopedConstants = quantifiedExpressionWithScope.second;
checkIfIndexSortsAreDefined(termStatement, quantifiedExpression);
checkIfBodyIsBoolean(termStatement, quantifiedExpression, scopedConstants);
}
Aggregations