Search in sources :

Example 1 with PredicateInterpretation

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);
}
Also used : AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) HashMap(java.util.HashMap) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 2 with PredicateInterpretation

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);
}
Also used : PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 3 with PredicateInterpretation

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);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 4 with PredicateInterpretation

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;
}
Also used : HashMap(java.util.HashMap) MethodPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation) IntPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) BinaryPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.BinaryPredicateInterpretation) BindingMethodPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.BindingMethodPredicateInterpretation) LongPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.LongPredicateInterpretation) UnaryPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.UnaryPredicateInterpretation) SuppliedPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.SuppliedPredicateInterpretation) Method(java.lang.reflect.Method)

Aggregations

PredicateInterpretation (at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation)4 HashMap (java.util.HashMap)2 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)1 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)1 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)1 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)1 ASPCore2Lexer (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer)1 ASPCore2Parser (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)1 BinaryPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.BinaryPredicateInterpretation)1 BindingMethodPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.BindingMethodPredicateInterpretation)1 IntPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation)1 LongPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.LongPredicateInterpretation)1 MethodPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation)1 SuppliedPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.SuppliedPredicateInterpretation)1 UnaryPredicateInterpretation (at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.UnaryPredicateInterpretation)1 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)1 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)1