Search in sources :

Example 1 with HOGMConstantDeclaration

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

the class HOGMModelValidator method isValidDeclaredConstantFunctorArity.

boolean isValidDeclaredConstantFunctorArity(Expression expr, Map<Expression, HOGMConstantDeclaration> scopedConstants) {
    boolean result = true;
    HOGMConstantDeclaration constantDeclaration = scopedConstants.get(expr.getFunctorOrSymbol());
    if (constantDeclaration.getArity().intValue() != expr.numberOfArguments()) {
        result = false;
    }
    return result;
}
Also used : HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration)

Example 2 with HOGMConstantDeclaration

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

the class HOGModel method extractConstants.

private static List<HOGMConstantDeclaration> extractConstants(Expression modelTupleExpr) {
    List<HOGMConstantDeclaration> result = new ArrayList<>();
    if (isLegalModelTuple(modelTupleExpr)) {
        Expression constantsTuple = modelTupleExpr.get(1);
        constantsTuple.getArguments().forEach(constantExpr -> result.add(HOGMConstantDeclaration.makeConstantDeclaration(constantExpr)));
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) HOGMConstantDeclaration(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMConstantDeclaration)

Example 3 with HOGMConstantDeclaration

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

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

the class HOGMModelValidator method checkNonConstantAndRandomFunctionWithScope.

private void checkNonConstantAndRandomFunctionWithScope(Pair<Expression, Map<Expression, HOGMConstantDeclaration>> nonConstantAndRandomFunctionWithScope, StatementInfo termStatement) {
    Expression nonConstantAndRandomFunction = nonConstantAndRandomFunctionWithScope.first;
    Map<Expression, HOGMConstantDeclaration> scopedConstants = nonConstantAndRandomFunctionWithScope.second;
    if (isKnownFunctor(nonConstantAndRandomFunction.getFunctor())) {
        checkDeclaredNonConstantAndRandomFunction(termStatement, nonConstantAndRandomFunction, scopedConstants);
    } else {
        newError(Type.TERM_TYPE_OF_FUNCTOR_NOT_DECLARED, nonConstantAndRandomFunction.getFunctor(), termStatement);
    }
}
Also used : 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)

Example 5 with HOGMConstantDeclaration

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

Aggregations

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