1 package it.unical.mat.parsers.asp.dlv2;
3 import it.unical.mat.parsers.asp.ASPDataCollection;
4 import java.util.HashMap;
5 import org.antlr.v4.runtime.BailErrorStrategy;
6 import org.antlr.v4.runtime.CharStreams;
7 import org.antlr.v4.runtime.CommonTokenStream;
8 import org.antlr.v4.runtime.ConsoleErrorListener;
9 import org.antlr.v4.runtime.DefaultErrorStrategy;
10 import org.antlr.v4.runtime.RecognitionException;
11 import org.antlr.v4.runtime.atn.PredictionMode;
15 private HashMap <Integer, Integer> costs;
18 this.answerSets = answerSets;
23 answerSets.addAnswerSet();
25 if(ctx.cost() !=
null && !ctx.cost().isEmpty()) {
26 costs =
new HashMap <> ();
27 final String[] firstCost = ctx.cost().COST_LABEL().getText().split(
" ")[1].split(
"@");
29 costs.put(Integer.parseInt(firstCost[1]), Integer.parseInt(firstCost[0]));
33 costs.forEach((level, cost) -> answerSets.storeCost(level, cost));
35 return visitChildren(ctx);
40 final int level = Integer.parseInt(ctx.INTEGER(1).getText()), cost = Integer.parseInt(ctx.INTEGER(0).getText());
42 costs.put(level, cost);
43 answerSets.storeCost(level, cost);
50 answerSets.storeAtom(ctx.getText());
55 public static void parse(
final ASPDataCollection answerSets,
final String atomsList,
final boolean two_stageParsing) {
56 final CommonTokenStream tokens =
new CommonTokenStream(
new DLV2Lexer(CharStreams.fromString(atomsList)));
60 if(!two_stageParsing) {
61 visitor.visit(parser.output());
66 parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
67 parser.removeErrorListeners();
68 parser.setErrorHandler(
new BailErrorStrategy());
71 visitor.visit(parser.output());
72 }
catch (
final RuntimeException exception) {
73 if(exception.getClass() == RuntimeException.class && exception.getCause() instanceof RecognitionException) {
75 parser.addErrorListener(ConsoleErrorListener.INSTANCE);
76 parser.setErrorHandler(
new DefaultErrorStrategy());
77 parser.getInterpreter().setPredictionMode(PredictionMode.LL);
78 visitor.visit(parser.output());