Search in sources :

Example 1 with HOGMSortDeclaration

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

the class HOGMModelValidator method checkNonConstantAndRandomFunctionWithCorrectArity.

private void checkNonConstantAndRandomFunctionWithCorrectArity(StatementInfo termStatement, Expression nonConstantAndRandomFunction, Map<Expression, HOGMConstantDeclaration> scopedConstants, String functorName) {
    Set<HOGMSortDeclaration> argSorts = new HashSet<>();
    for (int i = 0; i < nonConstantAndRandomFunction.numberOfArguments(); i++) {
        Expression arg = nonConstantAndRandomFunction.get(i);
        checkAndCollectArgSorts(arg, i, termStatement, nonConstantAndRandomFunction, scopedConstants, functorName, argSorts);
    }
    checkThatTypesOfArgumentsAllMatch(argSorts, termStatement, nonConstantAndRandomFunction);
}
Also used : Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) HOGMSortDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with HOGMSortDeclaration

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

the class HOGMModelValidator method determineTermCategoryType.

HOGMModelValidator.TermCategoryType determineTermCategoryType(Expression expr) {
    HOGMModelValidator.TermCategoryType result = TermCategoryType.INVALID;
    HOGMSortDeclaration sortType = determineSortType(expr);
    if (sortType != null) {
        if (sortType == HOGMSortDeclaration.IN_BUILT_BOOLEAN) {
            result = TermCategoryType.BOOLEAN;
        } else if (sortType == HOGMSortDeclaration.IN_BUILT_INTEGER || sortType == HOGMSortDeclaration.IN_BUILT_REAL) {
            result = TermCategoryType.NUMERIC;
        } else if (sortType == HOGMSortDeclaration.IN_BUILT_STRING) {
            result = TermCategoryType.STRING;
        } else {
            result = TermCategoryType.OTHER;
        }
    }
    return result;
}
Also used : HOGMSortDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration)

Example 3 with HOGMSortDeclaration

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

the class HOGMModelValidator method getQuantifiedExpressionScope.

public Map<Expression, HOGMConstantDeclaration> getQuantifiedExpressionScope(Expression quantifiedExpression) {
    Map<Expression, HOGMConstantDeclaration> result = new LinkedHashMap<>();
    IndexExpressionsSet indexExpressionSet = ((QuantifiedExpression) quantifiedExpression).getIndexExpressions();
    IndexExpressions.getIndexToTypeMapWithDefaultNull(indexExpressionSet).forEach((name, type) -> {
        HOGMSortDeclaration localSort = getSort(type);
        if (localSort != null) {
            result.put(name, new HOGMConstantDeclaration(name, Expressions.ZERO, localSort.getName()));
        }
    });
    return result;
}
Also used : QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) 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) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) HOGMSortDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with HOGMSortDeclaration

use of com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration 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 5 with HOGMSortDeclaration

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

the class HOGModel method extractSorts.

private static List<HOGMSortDeclaration> extractSorts(Expression modelTupleExpr) {
    List<HOGMSortDeclaration> result = new ArrayList<>();
    if (isLegalModelTuple(modelTupleExpr)) {
        Expression sortsTuple = modelTupleExpr.get(0);
        sortsTuple.getArguments().forEach(sortExpr -> result.add(HOGMSortDeclaration.makeSortDeclaration(sortExpr)));
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) HOGMSortDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMSortDeclaration)

Aggregations

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