Search in sources :

Example 1 with HOGMRandomVariableDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration in project aic-praise by aic-sri-international.

the class HOGModel method extractRandom.

private static List<HOGMRandomVariableDeclaration> extractRandom(Expression modelTupleExpr) {
    List<HOGMRandomVariableDeclaration> result = new ArrayList<>();
    if (isLegalModelTuple(modelTupleExpr)) {
        Expression randomsTuple = modelTupleExpr.get(2);
        randomsTuple.getArguments().forEach(randomExpr -> result.add(HOGMRandomVariableDeclaration.makeRandomVariableDeclaration(randomExpr)));
    }
    return result;
}
Also used : HOGMRandomVariableDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

Example 2 with HOGMRandomVariableDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration in project aic-praise by aic-sri-international.

the class HOGMModelValidator method determineSortType.

HOGMSortDeclaration determineSortType(Expression expr, Map<Expression, HOGMConstantDeclaration> scopedConstants) {
    HOGMSortDeclaration result = null;
    if (IfThenElse.isIfThenElse(expr)) {
        HOGMSortDeclaration condition = determineSortType(IfThenElse.condition(expr), scopedConstants);
        HOGMSortDeclaration thenBranch = determineSortType(IfThenElse.thenBranch(expr), scopedConstants);
        HOGMSortDeclaration elseBranch = determineSortType(IfThenElse.elseBranch(expr), scopedConstants);
        // Ensure legal
        if (condition == HOGMSortDeclaration.IN_BUILT_BOOLEAN) {
            if (thenBranch == elseBranch) {
                // same as elseBranch
                result = thenBranch;
            } else if ((thenBranch == HOGMSortDeclaration.IN_BUILT_INTEGER || thenBranch == HOGMSortDeclaration.IN_BUILT_REAL) && (elseBranch == HOGMSortDeclaration.IN_BUILT_INTEGER || elseBranch == HOGMSortDeclaration.IN_BUILT_REAL)) {
                // subsumes Integer branch
                result = HOGMSortDeclaration.IN_BUILT_REAL;
            } else {
                // Don't know
                result = null;
            }
        } else {
            // Don't know
            result = null;
        }
    } else {
        Expression functor = expr.getFunctor();
        if (functor == null) {
            if (Expressions.FALSE.equals(expr) || Expressions.TRUE.equals(expr)) {
                result = HOGMSortDeclaration.IN_BUILT_BOOLEAN;
            } else if (ForAll.isForAll(expr) || ThereExists.isThereExists(expr)) {
                // NOTE: quantifiers are not functions
                result = HOGMSortDeclaration.IN_BUILT_BOOLEAN;
            } else if (CountingFormulaEquivalentExpressions.isCountingFormulaEquivalentExpression(expr)) {
                // NOTE: counting formulas are not functions which is a subset of counting formula equivalent expressions
                result = HOGMSortDeclaration.IN_BUILT_INTEGER;
            } else if (Sets.isIntensionalMultiSet(expr)) {
                Map<Expression, HOGMConstantDeclaration> intensionalMultiSetScope = new LinkedHashMap<>(scopedConstants);
                intensionalMultiSetScope.putAll(getQuantifiedExpressionScope(expr));
                result = determineSortType(((IntensionalSet) expr).getHead(), intensionalMultiSetScope);
            } else if (Expressions.isNumber(expr)) {
                if (expr.rationalValue().isInteger()) {
                    result = HOGMSortDeclaration.IN_BUILT_INTEGER;
                } else {
                    result = HOGMSortDeclaration.IN_BUILT_REAL;
                }
            } else if (Expressions.isStringLiteral(expr)) {
                result = HOGMSortDeclaration.IN_BUILT_STRING;
            } else {
                if (isDeclaredConstantFunctor(expr.getFunctorOrSymbol(), scopedConstants)) {
                    HOGMConstantDeclaration constantDeclaration = scopedConstants.get(expr.getFunctorOrSymbol());
                    result = getSort(constantDeclaration.getRangeSort());
                } else if (isDeclaredRandomFunctor(expr.getFunctorOrSymbol())) {
                    HOGMRandomVariableDeclaration rvDeclaration = randoms.get(expr.getFunctorOrSymbol());
                    result = getSort(rvDeclaration.getRangeSort());
                } else if ((result = getSort(expr)) != null) {
                // has been assigned.
                } else if (sortConstants.contains(expr)) {
                    for (HOGMSortDeclaration sort : sorts.values()) {
                        // Have mapped the unique constant sort.
                        if (ExtensionalSets.getElements(sort.getConstants()).contains(expr)) {
                            result = sort;
                            break;
                        }
                    }
                } else {
                // Don't know
                }
            }
        } else {
            String functorName = functorName(functor);
            if (booleanTypeFunctors.contains(functorName)) {
                result = HOGMSortDeclaration.IN_BUILT_BOOLEAN;
            } else if (numericTypeFunctors.contains(functorName)) {
                result = HOGMSortDeclaration.IN_BUILT_INTEGER;
                if (!FunctorConstants.CARDINALITY.equals(functorName)) {
                    if (FunctorConstants.PRODUCT.equals(functorName)) {
                        result = determineSortType(expr.get(0), scopedConstants);
                    } else {
                        for (Expression arg : expr.getArguments()) {
                            if (Expressions.isNumber(arg)) {
                                if (!arg.rationalValue().isInteger()) {
                                    result = HOGMSortDeclaration.IN_BUILT_REAL;
                                    break;
                                }
                            } else {
                                result = determineSortType(arg, scopedConstants);
                                if (result == HOGMSortDeclaration.IN_BUILT_REAL) {
                                    break;
                                } else if (result != HOGMSortDeclaration.IN_BUILT_INTEGER) {
                                    // Something wrong as the argument sort is not numeric
                                    result = null;
                                    break;
                                }
                            }
                        }
                    }
                }
            } else if (isDeclaredConstantFunctor(functor, scopedConstants)) {
                HOGMConstantDeclaration constantDeclaration = scopedConstants.get(functor);
                result = getSort(constantDeclaration.getRangeSort());
            } else if (isDeclaredRandomFunctor(functor)) {
                HOGMRandomVariableDeclaration rvDeclaration = randoms.get(functor);
                result = getSort(rvDeclaration.getRangeSort());
            } else {
                // Don't know
                result = null;
            }
        }
    }
    return result;
}
Also used : HOGMRandomVariableDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration) HOGMSortDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with HOGMRandomVariableDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration 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);
}
Also used : HOGMRandomVariableDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration) Pair(com.sri.ai.util.base.Pair)

Example 4 with HOGMRandomVariableDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration in project aic-praise by aic-sri-international.

the class HOGMModelValidator method isValidDeclaredRandomFunctorArity.

boolean isValidDeclaredRandomFunctorArity(Expression expr) {
    boolean result = true;
    HOGMRandomVariableDeclaration rvDeclaration = randoms.get(expr.getFunctorOrSymbol());
    if (rvDeclaration.getArity().intValue() != expr.numberOfArguments()) {
        result = false;
    }
    return result;
}
Also used : HOGMRandomVariableDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration)

Aggregations

HOGMRandomVariableDeclaration (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMRandomVariableDeclaration)4 Expression (com.sri.ai.expresso.api.Expression)3 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)2 HOGMConstantDeclaration (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration)2 HOGMSortDeclaration (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration)1 Pair (com.sri.ai.util.base.Pair)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1