Search in sources :

Example 1 with HOGModelException

use of com.sri.ai.praise.model.v1.HOGModelException in project aic-praise by aic-sri-international.

the class HOGMQueryRunner method query.

public List<HOGMQueryResult> query() {
    List<HOGMQueryResult> result = new ArrayList<>();
    Expression queryExpr = null;
    //
    ParsedHOGModel parsedModel = null;
    for (String query : queries) {
        long startQuery = System.currentTimeMillis();
        List<HOGMQueryError> errors = new ArrayList<>();
        try {
            if (model == null || model.trim().equals("")) {
                errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, "Model not specified", 0, 0, 0));
            }
            if (query == null || query.trim().equals("")) {
                errors.add(new HOGMQueryError(HOGMQueryError.Context.QUERY, "Query not specified", 0, 0, 0));
            }
            if (errors.size() == 0) {
                HOGMParserWrapper parser = new HOGMParserWrapper();
                if (parsedModel == null) {
                    parsedModel = parser.parseModel(model, new QueryErrorListener(HOGMQueryError.Context.MODEL, errors));
                }
                queryExpr = parser.parseTerm(query, new QueryErrorListener(HOGMQueryError.Context.QUERY, errors));
                if (errors.size() == 0) {
                    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
                    if (!canceled) {
                        inferencer = new InferenceForFactorGraphAndEvidence(factorsAndTypes, false, null, true, getOptionalTheory());
                        startQuery = System.currentTimeMillis();
                        Expression marginal = inferencer.solve(queryExpr);
                        result.add(new HOGMQueryResult(query, queryExpr, parsedModel, marginal, System.currentTimeMillis() - startQuery));
                    }
                }
            }
        } catch (RecognitionException re) {
            errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, re.getMessage(), re.getOffendingToken().getLine(), re.getOffendingToken().getStartIndex(), re.getOffendingToken().getStopIndex()));
        } catch (UnableToParseAllTheInputError utpai) {
            errors.add(new HOGMQueryError(utpai));
        } catch (HOGModelException me) {
            me.getErrors().forEach(modelError -> {
                String inStatement = modelError.getInStatementInfo().statement.toString();
                String inSource = modelError.getInStatementInfo().sourceText;
                String inSubStatement = modelError.getMessage();
                String inInfo = "";
                if (inSubStatement.equals("") || inSubStatement.equals(inSource)) {
                    inInfo = " in '" + inStatement + "'";
                } else {
                    inInfo = " ('" + inSubStatement + "') in '" + inStatement + "'";
                }
                if (!inSource.replaceAll(" ", "").replaceAll(";", "").equals(inStatement.replaceAll(" ", ""))) {
                    inInfo = inInfo + " derived from '" + inSource + "'";
                }
                errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, modelError.getErrorType().formattedMessage() + inInfo, modelError.getInStatementInfo().line, modelError.getInStatementInfo().startIndex, modelError.getInStatementInfo().endIndex));
            });
        } catch (Throwable t) {
            // Unexpected
            errors.add(new HOGMQueryError(t));
        }
        if (errors.size() > 0) {
            result.add(new HOGMQueryResult(query, queryExpr, parsedModel, errors, System.currentTimeMillis() - startQuery));
        }
    }
    return result;
}
Also used : HOGModelException(com.sri.ai.praise.model.v1.HOGModelException) Expressions(com.sri.ai.expresso.helper.Expressions) Parser(com.sri.ai.expresso.api.Parser) ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) Expression(com.sri.ai.expresso.api.Expression) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) Context(com.sri.ai.grinder.sgdpllt.api.Context) HOGModelException(com.sri.ai.praise.model.v1.HOGModelException) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) List(java.util.List) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) RecognitionException(org.antlr.v4.runtime.RecognitionException) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) UnableToParseAllTheInputError(com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError) HOGMSortDeclaration(com.sri.ai.praise.model.v1.HOGMSortDeclaration) Collections(java.util.Collections) ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) Expression(com.sri.ai.expresso.api.Expression) UnableToParseAllTheInputError(com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with HOGModelException

use of com.sri.ai.praise.model.v1.HOGModelException in project aic-praise by aic-sri-international.

the class HOGMParserWrapper method parse.

//
// PRIVATE
//
private Expression parse(String string, Parser.ErrorListener errorListener, ParseTreeRetriever parseTreeRetriever) throws RecognitionException, UnableToParseAllTheInputError, HOGModelException {
    Expression result = null;
    AntlrErrorListener antlrErrorListener = new AntlrErrorListener(errorListener);
    ANTLRInputStream input = new ANTLRInputStream(string);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HOGMParser parser = new HOGMParser(tokens);
    lexer.removeErrorListeners();
    parser.removeErrorListeners();
    lexer.addErrorListener(antlrErrorListener);
    parser.addErrorListener(antlrErrorListener);
    ParseTree tree = parseTreeRetriever.retrieve(parser);
    boolean eofReached = parser.getInputStream().LA(1) == Recognizer.EOF;
    if (!antlrErrorListener.errorsDetected) {
        if (!eofReached) {
            throw new UnableToParseAllTheInputError();
        } else {
            lexer.removeErrorListeners();
            parser.removeErrorListeners();
            HOGModelVisitor hogmModelVisitor = new HOGModelVisitor();
            result = hogmModelVisitor.visit(tree);
        }
    }
    return result;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) HOGMParser(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)2 Beta (com.google.common.annotations.Beta)1 Parser (com.sri.ai.expresso.api.Parser)1 Expressions (com.sri.ai.expresso.helper.Expressions)1 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)1 Context (com.sri.ai.grinder.sgdpllt.api.Context)1 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)1 HOGMSortDeclaration (com.sri.ai.praise.model.v1.HOGMSortDeclaration)1 HOGModelException (com.sri.ai.praise.model.v1.HOGModelException)1 HOGMLexer (com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer)1 HOGMParser (com.sri.ai.praise.model.v1.hogm.antlr.HOGMParser)1 HOGMParserWrapper (com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper)1 ParsedHOGModel (com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel)1 UnableToParseAllTheInputError (com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError)1 ExpressionFactorsAndTypes (com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes)1 FactorsAndTypes (com.sri.ai.praise.sgsolver.solver.FactorsAndTypes)1 InferenceForFactorGraphAndEvidence (com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1