Skip to content

Commit

Permalink
Add new checks from the most recent version of Griddle develop.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLandau committed Dec 24, 2015
1 parent 948bb56 commit 69742f9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private static AnalyzedGame analyze(List<TopLevelGdl> rules) {
private static Set<String> getNamesMatchingOrDownstreamOf(String target,
Map<String, Set<String>> ancestorGraph) {
Set<String> results = new HashSet<String>();
//Always add target, for cases like "true" or "does" that are not
//made true by sentences or rules within the GDL. (See GitHub issue #18)
results.add(target);
for (String name : ancestorGraph.keySet()) {
if (name.equalsIgnoreCase(target)) {
results.add(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.alloyggp.griddle.validator.check.DatalogKeywordsNotConstantsCheck;
import net.alloyggp.griddle.validator.check.DisjunctionNotNestedCheck;
import net.alloyggp.griddle.validator.check.EmptyBodyCheck;
import net.alloyggp.griddle.validator.check.EmptyDisjunctionCheck;
import net.alloyggp.griddle.validator.check.ErrorStringCheck;
import net.alloyggp.griddle.validator.check.FixedArityCheck;
import net.alloyggp.griddle.validator.check.InconsistentCapitalizationCheck;
Expand All @@ -29,6 +30,7 @@
import net.alloyggp.griddle.validator.check.TrueDoesAreNotStandaloneSentencesCheck;
import net.alloyggp.griddle.validator.check.UnproducedSentenceNamesCheck;
import net.alloyggp.griddle.validator.check.UnusedSentenceNamesCheck;
import net.alloyggp.griddle.validator.check.VariablesOnlyInRulesCheck;

public class ValidatorConfiguration {
private final Map<Check, Level> checks;
Expand All @@ -46,6 +48,7 @@ public static ValidatorConfiguration createStandard() {
checks.put(DisjunctionNotNestedCheck.INSTANCE, Level.WARNING);
checks.put(ErrorStringCheck.INSTANCE, Level.ERROR);
checks.put(EmptyBodyCheck.INSTANCE, Level.WARNING);
checks.put(EmptyDisjunctionCheck.INSTANCE, Level.WARNING);
checks.put(FixedArityCheck.INSTANCE, Level.WARNING);
checks.put(InconsistentCapitalizationCheck.INSTANCE, Level.ERROR);
checks.put(InitBaseInputAreConstantCheck.INSTANCE, Level.ERROR);
Expand All @@ -63,6 +66,7 @@ public static ValidatorConfiguration createStandard() {
checks.put(TrueDoesAreNotStandaloneSentencesCheck.INSTANCE, Level.ERROR);
checks.put(UnproducedSentenceNamesCheck.INSTANCE, Level.WARNING);
checks.put(UnusedSentenceNamesCheck.INSTANCE, Level.WARNING);
checks.put(VariablesOnlyInRulesCheck.INSTANCE, Level.ERROR);

return new ValidatorConfiguration(checks);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.alloyggp.griddle.validator.check;

import net.alloyggp.griddle.grammar.GdlVisitor;
import net.alloyggp.griddle.grammar.Literal;
import net.alloyggp.griddle.validator.AnalyzedGame;

public class EmptyDisjunctionCheck implements Check {
public static final EmptyDisjunctionCheck INSTANCE = new EmptyDisjunctionCheck();
private EmptyDisjunctionCheck() {
//Singleton
}

@Override
public void findProblems(AnalyzedGame game, final ProblemReporter reporter) {
game.visitAll(new GdlVisitor() {
@Override
public void visitDisjunction(Literal disjunction) {
if (disjunction.getDisjunction().isEmpty()) {
reporter.report("Disjunctions should not be empty.", disjunction.getPosition());
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.alloyggp.griddle.validator.check;

import net.alloyggp.griddle.Position;
import net.alloyggp.griddle.grammar.GdlVisitor;
import net.alloyggp.griddle.grammar.Sentence;
import net.alloyggp.griddle.grammar.TopLevelGdl;
import net.alloyggp.griddle.validator.AnalyzedGame;

public class VariablesOnlyInRulesCheck implements Check {
public static final VariablesOnlyInRulesCheck INSTANCE = new VariablesOnlyInRulesCheck();
private VariablesOnlyInRulesCheck() {
//Singleton
}

@Override
public void findProblems(AnalyzedGame game, final ProblemReporter reporter) {
for (TopLevelGdl gdl : game.getTopLevelComponents()) {
if (gdl.isSentence()) {
Sentence sentence = gdl.getSentence();
sentence.accept(new GdlVisitor() {
@Override
public void visitVariable(String variable, Position position) {
reporter.report("Variables may only appear within rules.", position);
}
});
}
}
}

}
31 changes: 31 additions & 0 deletions src/test/java/net/alloyggp/griddle/ValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import net.alloyggp.griddle.validator.AnalyzedGame;
import net.alloyggp.griddle.validator.ParenthesesValidator;
import net.alloyggp.griddle.validator.Validators;
import net.alloyggp.griddle.validator.check.Check;
import net.alloyggp.griddle.validator.check.DatalogKeywordsNotConstantsCheck;
import net.alloyggp.griddle.validator.check.ErrorStringCheck;
Expand Down Expand Up @@ -117,6 +118,36 @@ public void testMisplacedDatalogKeyword3() throws Exception {
assertEquals(9, problem.getPosition().getEnd());
}

@Test
public void testVariableInTopLevelSentence() throws Exception {
String gameString = TestGames.getGameString("varInTopLevelSentence");
Set<GdlProblem> problems = Validators.getStandardValidator().findProblems(gameString);
assertEquals(1, problems.size());
GdlProblem problem = problems.iterator().next();
assertEquals(10, problem.getPosition().getStart());
assertEquals(15, problem.getPosition().getEnd());
}

@Test
public void testEmptyDisjunction() throws Exception {
String gameString = TestGames.getGameString("emptyDisjunction");
Set<GdlProblem> problems = Validators.getStandardValidator().findProblems(gameString);
assertEquals(1, problems.size());
GdlProblem problem = problems.iterator().next();
assertEquals(31, problem.getPosition().getStart());
assertEquals(36, problem.getPosition().getEnd());
}

@Test
public void testBaseDependingOnTrue() throws Exception {
String gameString = TestGames.getGameString("baseDependsOnTrue");
Set<GdlProblem> problems = Validators.getStandardValidator().findProblems(gameString);
assertEquals(1, problems.size());
GdlProblem problem = problems.iterator().next();
assertEquals(15, problem.getPosition().getStart());
assertEquals(25, problem.getPosition().getEnd());
}

private Set<GdlProblem> findProblems(Check check, String gameString) throws Exception {
AnalyzedGame game = AnalyzedGame.parseAndAnalyze(gameString);
final Set<GdlProblem> problems = new HashSet<GdlProblem>();
Expand Down

0 comments on commit 69742f9

Please sign in to comment.