use of at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testNegatedExternalLiteral.
@Test
public void testNegatedExternalLiteral() throws Exception {
String asp = "claimedTruth(bla). truth(X) :- claimedTruth(X), &sayTrue[X]. lie(X) :- claimedTruth(X), not &sayTrue[X].";
Map<String, PredicateInterpretation> externals = new HashMap<>();
externals.put("sayTrue", Externals.processPredicateMethod(this.getClass().getMethod("sayTrue", Object.class)));
ProgramParser parserWithExternals = new ProgramParserImpl();
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalizer.apply(parserWithExternals.parse(asp, externals)));
CompiledProgram evaluated = new StratifiedEvaluation().apply(analyzed);
Set<AnswerSet> answerSets = solveCompiledProg.apply(evaluated);
TestUtils.assertAnswerSetsEqual("claimedTruth(bla), truth(bla)", answerSets);
}
use of at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation in project Alpha by alpha-asp.
the class ParseTreeVisitor method visitExternal_atom.
@Override
public ExternalAtom visitExternal_atom(ASPCore2Parser.External_atomContext ctx) {
if (ctx.MINUS() != null) {
throw notSupported(ctx);
}
final String predicateName = ctx.ID().getText();
final PredicateInterpretation interpretation = externals.get(predicateName);
if (interpretation == null) {
throw new IllegalArgumentException("Unknown interpretation name encountered: " + predicateName);
}
List<Term> outputTerms = visitTerms(ctx.output);
return Atoms.newExternalAtom(Predicates.getPredicate(predicateName, outputTerms.size()), interpretation, visitTerms(ctx.input), outputTerms);
}
use of at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation in project Alpha by alpha-asp.
the class ProgramParserImpl method parse.
public ASPCore2Program parse(CharStream stream, Map<String, PredicateInterpretation> externals) {
// @formatter:off
/*
* // In order to require less memory: use unbuffered streams and avoid constructing a full parse tree.
* ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is));
* lexer.setTokenFactory(new CommonTokenFactory(true));
* final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer));
* parser.setBuildParseTree(false);
*/
// @formatter:on
CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(stream));
final ASPCore2Parser parser = new ASPCore2Parser(tokens);
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
final CustomErrorListener errorListener = new CustomErrorListener(stream.getSourceName());
ASPCore2Parser.ProgramContext programContext;
try {
// Parse program
programContext = parser.program();
} catch (ParseCancellationException e) {
// retry with LL parser and DefaultErrorStrategy printing errors to console.
if (e.getCause() instanceof RecognitionException) {
tokens.seek(0);
parser.addErrorListener(errorListener);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
// Re-run parse.
programContext = parser.program();
} else {
throw e;
}
}
// is attempted) and the user will only see the first error encountered.
if (errorListener.getRecognitionException() != null) {
throw errorListener.getRecognitionException();
}
// Abort parsing if there were some (recoverable) syntax errors.
if (parser.getNumberOfSyntaxErrors() != 0) {
throw new ParseCancellationException();
}
// The union of this parser's preloaded externals and the (program-specific) externals passed to the parse method
Map<String, PredicateInterpretation> knownExternals;
if (externals != null && !externals.isEmpty()) {
knownExternals = new HashMap<>(preloadedExternals);
knownExternals.putAll(externals);
} else {
knownExternals = preloadedExternals;
}
// Construct internal program representation.
ParseTreeVisitor visitor = new ParseTreeVisitor(knownExternals);
return visitor.translate(programContext);
}
use of at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation in project Alpha by alpha-asp.
the class Externals method scanMethods.
private static Map<String, PredicateInterpretation> scanMethods(Iterable<Method> methods) {
Map<String, PredicateInterpretation> retVal = new HashMap<>();
String name;
for (Method method : methods) {
name = method.getAnnotation(Predicate.class).name();
if (name.isEmpty()) {
name = method.getName();
}
retVal.put(name, Externals.processPredicateMethod(method));
}
return retVal;
}
Aggregations