use of com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError 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;
}
use of com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError 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;
}
use of com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError 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;
}
Aggregations