Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ok, first refactoring, reorganizing packages

  • Loading branch information...
commit e595d12750e7549db08bc43d9413330cd86afdca 1 parent 3000714
@douglasrodrigo douglasrodrigo authored
View
21 spec/gen_spec.rb
@@ -1,21 +0,0 @@
-require 'spec_helper'
-
-describe Gen do
- describe ".of" do
-
- it "should return the pattern for literal" do
- Gen.of("x{3}").should match("x{3}")
- Gen.of("a{3,8}").should match("a{3,8}")
- end
-
- it "should return the pattern for number quantifier" do
- Gen.of("\\w{6}").should match("\\w{6}")
- Gen.of("\\d{3,8}").should match("\\d{3,8}")
- end
-
- it "should return value for possessive quantifier" do
- Gen.of("\\w+").should match("\\w+")
- Gen.of("\\d+").should match("\\d+")
- end
- end
-end
View
30 spec/regex_gen_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe RegexGen do
+ describe ".of" do
+
+ it "should return the pattern for literal" do
+ RegexGen.of("x{3}").should match("x{3}")
+ RegexGen.of("a{3,8}").should match("a{3,8}")
+ end
+
+ it "should return the pattern for number quantifier" do
+ RegexGen.of("\\w{6}").should match("\\w{6}")
+ RegexGen.of("\\d{3,8}").should match("\\d{3,8}")
+ end
+
+ it "should return value for possessive quantifier" do
+ RegexGen.of("\\w+").should match("\\w+")
+ RegexGen.of("\\d+").should match("\\d+")
+ end
+
+ it "should return union of literal values" do
+ RegexGen.of("abc").should match("abc")
+ end
+
+ it "should return intersection of values" do
+ RegexGen.of("(\\w{5,7})|(\\d+)").should match("(\\w{5,7})|(\\d+)")
+ end
+
+ end
+end
View
6 spec/spec_helper.rb
@@ -1,8 +1,8 @@
require 'java'
-import 'br.com.bfgex.Parser'
-import 'br.com.bfgex.Sexp'
-import 'br.com.bfgex.Gen'
+import 'br.com.bfgex.interpreter.Parser'
+import 'br.com.bfgex.interpreter.Sexp'
+import 'br.com.bfgex.RegexGen'
require 'rubygems'
require 'spec'
View
5 src/main/java/br/com/bfgex/QuantifierGenerator.java
@@ -1,5 +0,0 @@
-package br.com.bfgex;
-
-public interface QuantifierGenerator {
- public String generate(Integer length);
-}
View
8 src/main/java/br/com/bfgex/Randgen.java → src/main/java/br/com/bfgex/RandomGen.java
@@ -9,8 +9,10 @@
import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;
+import br.com.bfgex.resource.Dictionary;
-public class Randgen {
+
+public class RandomGen {
private static NumberRange WORDS_PER_SENTENCE = new NumberRange(3, 20);
private static NumberRange SENTENCES_PER_PARAGRAPH = new NumberRange(3, 8);
@@ -203,8 +205,4 @@ private static Integer getValidLength(Integer length, NumberRange numberRange) {
}
return length;
}
-
- public static void main(String[] args) {
- System.out.println(lastName(10));
- }
}
View
4 src/main/java/br/com/bfgex/Gen.java → src/main/java/br/com/bfgex/RegexGen.java
@@ -1,6 +1,8 @@
package br.com.bfgex;
-public class Gen {
+import br.com.bfgex.interpreter.Parser;
+
+public class RegexGen {
public static String of(String pattern) {
return Parser.parse(pattern).reduce();
View
2  src/main/java/br/com/bfgex/Exp.java → ...in/java/br/com/bfgex/interpreter/Exp.java
@@ -1,4 +1,4 @@
-package br.com.bfgex;
+package br.com.bfgex.interpreter;
public enum Exp {
LITERAL,
View
12 src/main/java/br/com/bfgex/Parser.java → ...java/br/com/bfgex/interpreter/Parser.java
@@ -1,4 +1,4 @@
-package br.com.bfgex;
+package br.com.bfgex.interpreter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -7,11 +7,11 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberRange;
-import static br.com.bfgex.Exp.INTERSECTION;
-import static br.com.bfgex.Exp.LITERAL;
-import static br.com.bfgex.Exp.UNION;
-import static br.com.bfgex.Exp.QUANTIFY;
-import static br.com.bfgex.Exp.RANDOM;
+import static br.com.bfgex.interpreter.Exp.INTERSECTION;
+import static br.com.bfgex.interpreter.Exp.UNION;
+import static br.com.bfgex.interpreter.Exp.LITERAL;
+import static br.com.bfgex.interpreter.Exp.RANDOM;
+import static br.com.bfgex.interpreter.Exp.QUANTIFY;
public class Parser {
View
17 src/main/java/br/com/bfgex/Quantifier.java → .../br/com/bfgex/interpreter/Quantifier.java
@@ -1,11 +1,14 @@
-package br.com.bfgex;
+package br.com.bfgex.interpreter;
import static org.apache.commons.lang.ObjectUtils.defaultIfNull;
import java.util.regex.Pattern;
+import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberRange;
+import br.com.bfgex.RandomGen;
+
public class Quantifier {
private static Pattern QUANTIFIERS = Pattern.compile("[+?*]");
@@ -37,7 +40,7 @@ public String genValue(Object value) {
private String verifyReluctant(Object value) {
- return quantity != null && quantity.equals("?") ? Randgen.pickOne("", pickValue(value, 1)) : null;
+ return quantity != null && quantity.equals("?") ? RandomGen.pickOne("", pickValue(value, 1)) : null;
}
private String verifyGreedy(Object value) {
@@ -47,7 +50,7 @@ private String verifyGreedy(Object value) {
private String verifyNumberRange(Object value) {
Integer numberValue = null;
if (numberRange != null ) {
- numberValue = Randgen.pickRange(numberRange).intValue();
+ numberValue = RandomGen.pickRange(numberRange).intValue();
} else if (number != null) {
numberValue = number;
}
@@ -58,14 +61,14 @@ private String pickValue(Object value, Integer length) {
String result = null;
if (value instanceof String) {
- result = (String) value;
+ result = (String) StringUtils.repeat((String) value, length);
} else {
switch ((RandomLiteral) value) {
- case DIGIT: result = Randgen.pickDigits(length);
+ case DIGIT: result = RandomGen.pickDigits(length);
break;
- case SPACE: result = Randgen.pickWhiteSpaces(length);
+ case SPACE: result = RandomGen.pickWhiteSpaces(length);
break;
- case WORD: result = Randgen.word(length);
+ case WORD: result = RandomGen.word(length);
break;
}
}
View
2  ...main/java/br/com/bfgex/RandomLiteral.java → .../com/bfgex/interpreter/RandomLiteral.java
@@ -1,4 +1,4 @@
-package br.com.bfgex;
+package br.com.bfgex.interpreter;
import java.util.Map;
import java.util.TreeMap;
View
8 src/main/java/br/com/bfgex/Sexp.java → ...n/java/br/com/bfgex/interpreter/Sexp.java
@@ -1,10 +1,12 @@
-package br.com.bfgex;
+package br.com.bfgex.interpreter;
import java.util.Arrays;
import java.util.LinkedList;
import org.apache.commons.lang.StringUtils;
+import br.com.bfgex.RandomGen;
+
public class Sexp {
private LinkedList<Object> expressions = new LinkedList<Object>();
@@ -35,7 +37,6 @@ public Object removeLast() {
}
public String reduce() {
- System.out.println(this);
return (String) reduce(this.getValues());
}
@@ -63,7 +64,7 @@ private Object reduceExp(Exp exp, LinkedList<Object> expressions, Quantifier qua
result = genValue(StringUtils.join(mapReduce(expressions), ""), quantity);
break;
case INTERSECTION:
- result = genValue(Randgen.pickCollection(mapReduce(expressions)), quantity);
+ result = genValue(reduce(((Sexp) RandomGen.pickCollection(expressions)).getValues()), quantity);
break;
case RANDOM:
case LITERAL: result = genValue(expressions.get(0), quantity);
@@ -93,4 +94,5 @@ private String genValue(Object reducedValue, Quantifier quantity) {
public String toString() {
return "(" + StringUtils.join(expressions, ",") + ")";
}
+
}
View
10 src/main/java/br/com/bfgex/Dictionary.java → ...ava/br/com/bfgex/resource/Dictionary.java
@@ -1,4 +1,4 @@
-package br.com.bfgex;
+package br.com.bfgex.resource;
import java.util.Map;
import java.util.Set;
@@ -15,28 +15,28 @@
public static Set<String> getWordsByLength(Integer length) {
if (WORDS_BY_LENGTH == null) {
- WORDS_BY_LENGTH = Resource.of("words");
+ WORDS_BY_LENGTH = ResourceLoader.of("words");
}
return WORDS_BY_LENGTH.get(length);
}
public static Set<String> getMaleNameByLength(Integer length) {
if (MALE_NAMES_BY_LENGTH == null) {
- MALE_NAMES_BY_LENGTH = Resource.of("male_names");
+ MALE_NAMES_BY_LENGTH = ResourceLoader.of("male_names");
}
return MALE_NAMES_BY_LENGTH.get(length);
}
public static Set<String> getFemaleNameByLength(Integer length) {
if (FEMALE_NAMES_BY_LENGTH == null) {
- FEMALE_NAMES_BY_LENGTH = Resource.of("female_names");
+ FEMALE_NAMES_BY_LENGTH = ResourceLoader.of("female_names");
}
return FEMALE_NAMES_BY_LENGTH.get(length);
}
public static Set<String> getLastNameByLength(Integer length) {
if (LAST_NAMES_BY_LENGTH == null) {
- LAST_NAMES_BY_LENGTH = Resource.of("surnames");
+ LAST_NAMES_BY_LENGTH = ResourceLoader.of("surnames");
}
return LAST_NAMES_BY_LENGTH.get(length);
}
View
4 src/main/java/br/com/bfgex/Resource.java → ...br/com/bfgex/resource/ResourceLoader.java
@@ -1,4 +1,4 @@
-package br.com.bfgex;
+package br.com.bfgex.resource;
import java.io.File;
import java.io.FileNotFoundException;
@@ -10,7 +10,7 @@
import java.util.Scanner;
import java.util.Set;
-public class Resource {
+public class ResourceLoader {
public static Map<Integer, Set<String>> of(String resource) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Please sign in to comment.
Something went wrong with that request. Please try again.