Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ProgramPartParser to simplify construction of parts of programs #248

Merged
merged 4 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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