-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from alpha-asp/answer_set_formatting
Command Line Interface - Make separator string for atoms within printed answer set configurable
- Loading branch information
Showing
7 changed files
with
101 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/at/ac/tuwien/kr/alpha/common/SimpleAnswerSetFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package at.ac.tuwien.kr.alpha.common; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.SortedSet; | ||
import java.util.stream.Collectors; | ||
|
||
import at.ac.tuwien.kr.alpha.common.atoms.Atom; | ||
|
||
public class SimpleAnswerSetFormatter implements AnswerSetFormatter<String> { | ||
|
||
private final String atomSeparator; | ||
|
||
public SimpleAnswerSetFormatter(String atomSeparator) { | ||
this.atomSeparator = atomSeparator; | ||
} | ||
|
||
@Override | ||
public String format(AnswerSet answerSet) { | ||
List<String> predicateInstanceStrings = new ArrayList<>(); | ||
for (Predicate p : answerSet.getPredicates()) { | ||
SortedSet<Atom> instances; | ||
if ((instances = answerSet.getPredicateInstances(p)) == null || instances.isEmpty()) { | ||
predicateInstanceStrings.add(p.getName()); | ||
} else { | ||
List<String> atomStrings = instances.stream().map((atom) -> atom.toString()).collect(Collectors.toList()); | ||
predicateInstanceStrings.add(String.join(this.atomSeparator, atomStrings)); | ||
} | ||
} | ||
return "{ " + String.join(this.atomSeparator, predicateInstanceStrings) + " }"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/test/java/at/ac/tuwien/kr/alpha/common/AnswerSetFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package at.ac.tuwien.kr.alpha.common; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class AnswerSetFormatterTest { | ||
|
||
@Test | ||
public void basicFormatterWithSeparator() { | ||
AnswerSetFormatter<String> fmt = new SimpleAnswerSetFormatter(" bla "); | ||
AnswerSet as = new AnswerSetBuilder().predicate("p").instance("a").predicate("q").instance("b").build(); | ||
String formatted = fmt.format(as); | ||
Assert.assertEquals("{ p(\"a\") bla q(\"b\") }", formatted); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters