Skip to content

Commit

Permalink
Merge pull request #11 from codelion/feature-bfs
Browse files Browse the repository at this point in the history
Feature bfs
  • Loading branch information
codelion authored Mar 8, 2017
2 parents 56b6707 + 4a298cf commit 1089cfe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sourceclear</groupId>
<artifactId>gramtest</artifactId>
<version>0.2.2-SNAPSHOT</version>
<version>0.2.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand All @@ -26,7 +26,7 @@
<groupId>junit</groupId>
<version>4.12</version>
</dependency>
<!-- The following two dependencies are added just overide the transitives -->
<!-- The following two dependencies are added just override the transitives -->
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-bean</artifactId>
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/sourceclear/gramtest/GeneratorVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package com.sourceclear.gramtest;

import org.antlr.v4.runtime.RuleContext;

import java.util.*;

/**
Expand Down Expand Up @@ -99,7 +101,19 @@ else if(ctx.text() != null) {
else {
String lhs = ctx.id().getText();
prodHist.push(lhs);
return visitRhs(productionsMap.get(lhs));
bnfParser.RhsContext rhs = productionsMap.get(lhs);
if(rhs.alternatives() == null ||
rhs.alternatives().alternative().stream().allMatch(ac -> isTerminalContext(ac.element())) ||
prodHist.size() < productionsMap.size()) {
return visitRhs(productionsMap.get(lhs));
}
else {
int choices = rhs.alternatives().alternative().size();
int rand = new Random().nextInt(choices);
result = visitAlternative(rhs.alternatives().alternative(rand));
if(!prodHist.empty()) prodHist.pop();
return result;
}
}
return result;
}
Expand All @@ -121,7 +135,9 @@ public List<String> visitAlternative(bnfParser.AlternativeContext ctx) {
@Override
public List<String> visitAlternatives(bnfParser.AlternativesContext ctx) {
List<String> altStrs = new LinkedList<>();
for(bnfParser.AlternativeContext ac : ctx.alternative()) {
List<bnfParser.AlternativeContext> acList = ctx.alternative();
Collections.shuffle(acList);
for(bnfParser.AlternativeContext ac : acList) {
altStrs.addAll(visitAlternative(ac));
}
return altStrs;
Expand Down Expand Up @@ -211,6 +227,12 @@ private List<String> combineTwoLists(List<String> preList, List<String> postList
return combList;
}

private Boolean isTerminalContext(List<bnfParser.ElementContext> eclist) {
for(bnfParser.ElementContext ec : eclist) {
if (ec.text() == null && ec.captext() == null) return false;
}
return true;
}
//---------------------------- Property Methods -----------------------------

public List<String> getTests() {
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/com/sourceclear/gramtest/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,27 @@ public void testJSONGram() throws IOException {
CommonTokenStream tokens = new CommonTokenStream(lexer);
bnfParser grammarparser = new bnfParser(tokens);
ParserRuleContext tree = grammarparser.rulelist();
GeneratorVisitor extractor = new GeneratorVisitor();
GeneratorVisitor extractor = new GeneratorVisitor(100,2,10,100,true);
extractor.visit(tree);
List<String> generatedTests = extractor.getTests();
Assert.assertEquals(94, generatedTests.size());
Assert.assertEquals(100, generatedTests.size());
}

/**
* Test with Datalog grammar
* @throws java.io.IOException
*/
@Test
@Ignore("Ignoring long running test")
public void testDatalogGram() throws IOException {
Lexer lexer = new bnfLexer(new ANTLRInputStream(getClass().getResourceAsStream("/datalog.bnf")));
CommonTokenStream tokens = new CommonTokenStream(lexer);
bnfParser grammarparser = new bnfParser(tokens);
ParserRuleContext tree = grammarparser.rulelist();
GeneratorVisitor extractor = new GeneratorVisitor();
GeneratorVisitor extractor = new GeneratorVisitor(100,2,8,16,true);
extractor.visit(tree);
List<String> generatedTests = extractor.getTests();
Assert.assertEquals(15, generatedTests.size());
Assert.assertEquals(100, generatedTests.size());
}

/**
Expand Down

0 comments on commit 1089cfe

Please sign in to comment.