Skip to content

Commit

Permalink
Merge pull request #248 from alpha-asp/program_part_parser
Browse files Browse the repository at this point in the history
Introduce ProgramPartParser to simplify construction of parts of programs
  • Loading branch information
AntoniusW authored Jul 10, 2020
2 parents b254067 + a9efedf commit a2dd412
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 103 deletions.
30 changes: 6 additions & 24 deletions src/main/java/at/ac/tuwien/kr/alpha/grounder/Substitution.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2016-2019, the Alpha Team.
/*
* Copyright (c) 2016-2020, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
Expand Down Expand Up @@ -27,29 +27,23 @@
*/
package at.ac.tuwien.kr.alpha.grounder;

import at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer;
import at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser;
import at.ac.tuwien.kr.alpha.common.atoms.Atom;
import at.ac.tuwien.kr.alpha.common.atoms.Literal;
import at.ac.tuwien.kr.alpha.common.terms.ConstantTerm;
import at.ac.tuwien.kr.alpha.common.terms.FunctionTerm;
import at.ac.tuwien.kr.alpha.common.terms.Term;
import at.ac.tuwien.kr.alpha.common.terms.VariableTerm;
import at.ac.tuwien.kr.alpha.grounder.parser.ParseTreeVisitor;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import at.ac.tuwien.kr.alpha.grounder.parser.ProgramPartParser;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import static at.ac.tuwien.kr.alpha.Util.oops;

public class Substitution {
private static final ParseTreeVisitor VISITOR = new ParseTreeVisitor(Collections.emptyMap(), false);

private static final ProgramPartParser PROGRAM_PART_PARSER = new ProgramPartParser();

protected TreeMap<VariableTerm, Term> substitution;

Expand Down Expand Up @@ -198,24 +192,12 @@ public static Substitution fromString(String substitution) {
for (String assignment : assignments) {
String keyVal[] = assignment.split("->");
VariableTerm variable = VariableTerm.getInstance(keyVal[0]);
Term assignedTerm = parseTerm(keyVal[1]);
Term assignedTerm = PROGRAM_PART_PARSER.parseTerm(keyVal[1]);
ret.put(variable, assignedTerm);
}
return ret;
}

private static Term parseTerm(String s) {
try {
final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(CharStreams.fromString(s))));
return (Term)VISITOR.visit(parser.term());
} catch (RecognitionException | ParseCancellationException e) {
// If there were issues parsing the given string, we
// throw something that suggests that the input string
// is malformed.
throw new IllegalArgumentException("Could not parse term.", e);
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2018-2020, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package at.ac.tuwien.kr.alpha.grounder.parser;

import at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer;
import at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser;
import at.ac.tuwien.kr.alpha.common.atoms.BasicAtom;
import at.ac.tuwien.kr.alpha.common.atoms.Literal;
import at.ac.tuwien.kr.alpha.common.terms.Term;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.misc.ParseCancellationException;

import java.util.Collections;

/**
* A parser that, in contrast to {@link ProgramParser}, does not parse full programs but only program parts like
* atoms, terms and such.
*/
public class ProgramPartParser {
private final ParseTreeVisitor visitor = new ParseTreeVisitor(Collections.emptyMap(), true);

public Term parseTerm(String s) {
final ASPCore2Parser parser = getASPCore2Parser(s);
return (Term)parse(parser.term());
}

public BasicAtom parseBasicAtom(String s) {
final ASPCore2Parser parser = getASPCore2Parser(s);
return (BasicAtom)parse(parser.classical_literal());
}

public Literal parseLiteral(String s) {
final ASPCore2Parser parser = getASPCore2Parser(s);
return (Literal)parse(parser.naf_literal());
}

private ASPCore2Parser getASPCore2Parser(String s) {
return new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(CharStreams.fromString(s))));
}

private Object parse(ParserRuleContext context) {
try {
return visitor.visit(context);
} catch (RecognitionException | ParseCancellationException e) {
// If there were issues parsing the given string, we
// throw something that suggests that the input string
// is malformed.
throw new IllegalArgumentException("Could not parse term.", e);
}
}
}
13 changes: 2 additions & 11 deletions src/test/java/at/ac/tuwien/kr/alpha/TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019 Siemens AG
/*
* Copyright (c) 2019-2020 Siemens AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -30,7 +30,6 @@
import at.ac.tuwien.kr.alpha.common.Predicate;
import at.ac.tuwien.kr.alpha.common.atoms.Atom;
import at.ac.tuwien.kr.alpha.common.atoms.BasicAtom;
import at.ac.tuwien.kr.alpha.common.atoms.Literal;
import at.ac.tuwien.kr.alpha.common.terms.ConstantTerm;
import at.ac.tuwien.kr.alpha.common.terms.Term;
import at.ac.tuwien.kr.alpha.common.terms.VariableTerm;
Expand Down Expand Up @@ -65,14 +64,6 @@ public static Atom atom(String predicateName, int... termInts) {
}
return new BasicAtom(Predicate.getInstance(predicateName, terms.length), terms);
}

public static Literal literal(String predicateName, String... termStrings) {
return atom(predicateName, termStrings).toLiteral();
}

public static Literal literal(String predicateName, int... termInts) {
return atom(predicateName, termInts).toLiteral();
}

public static void printNoGoods(AtomStore atomStore, Collection<NoGood> noGoods) {
System.out.println(noGoods.stream().map(atomStore::noGoodToString).collect(Collectors.toSet()));
Expand Down
Loading

0 comments on commit a2dd412

Please sign in to comment.