|
From: Gary P. <gpa...@gm...> - 2009-06-10 10:52:10
|
The need to have a decent parser for the domain strings is something that has been on the cards for a very long time. The main problem with the current implementation is that it is very non-expressive. Illegal domains such as: - R(8, 9)^-1 - R^0 - Any other strange combination were allowed in the previous parser. These patches propose a new generated parser based on a provided grammar file. JJTree and JavaCC were used to achieve the parser. Gary Pampara (3): Semi-complete parser definition. Deprecation of parser for generated parser. Prevent invalid values / options in domain strings. pom.xml | 13 + .../bioinf/sequencealignment/BinaryMSAProblem.java | 12 +- .../cilib/bioinf/sequencealignment/MSAProblem.java | 5 +- .../controlparameter/BoundedControlParameter.java | 12 +- .../functions/discrete/KnightCoverageProblem.java | 1 - .../cilib/problem/ClusteringProblem.java | 5 +- .../CooperativeOptimisationProblemAdapter.java | 21 +- .../net/sourceforge/cilib/type/DomainBuilder.java | 2 + .../net/sourceforge/cilib/type/DomainParser.java | 2 + .../net/sourceforge/cilib/type/DomainRegistry.java | 2 +- .../sourceforge/cilib/type/DomainValidator.java | 1 + .../cilib/type/StringBasedDomainRegistry.java | 44 +--- .../java/net/sourceforge/cilib/type/creator/B.java | 16 +- .../java/net/sourceforge/cilib/type/creator/R.java | 8 + .../java/net/sourceforge/cilib/type/creator/T.java | 7 + .../cilib/type/creator/TypeCreator.java | 7 + .../java/net/sourceforge/cilib/type/creator/Z.java | 9 + .../cilib/type/parser/DomainParserVisitorImpl.java | 272 ++++++++++++++++++++ src/main/jjtree/parser.jjt | 84 ++++++ .../cilib/measurement/generic/IterationsTest.java | 10 +- .../generic/PercentageCompleteTest.java | 10 +- .../cilib/measurement/generic/RestartsTest.java | 10 +- .../cilib/measurement/generic/TimeTest.java | 10 +- .../measurement/multiple/MultipleFitnessTest.java | 10 +- .../multiple/MultipleSolutionsTest.java | 10 +- .../single/BestParticlePositionTest.java | 14 +- .../cilib/measurement/single/DiameterTest.java | 10 +- .../measurement/single/FitnessEvaluationsTest.java | 10 +- .../cilib/measurement/single/FitnessTest.java | 10 +- .../single/FunctionOptimisationErrorTest.java | 12 +- .../measurement/single/ParticlePositionsTest.java | 10 +- .../cilib/measurement/single/SolutionTest.java | 12 +- .../sourceforge/cilib/type/DomainParserTest.java | 165 ++---------- .../cilib/type/StringBasedDomainRegistryTest.java | 20 +- .../cilib/type/parser/DomainParserTest.java | 128 +++++++++ 35 files changed, 682 insertions(+), 292 deletions(-) create mode 100644 src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java create mode 100644 src/main/jjtree/parser.jjt create mode 100644 src/test/java/net/sourceforge/cilib/type/parser/DomainParserTest.java |
|
From: Gary P. <gpa...@gm...> - 2009-06-10 10:52:17
|
---
src/main/jjtree/parser.jjt | 43 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 43 insertions(+), 0 deletions(-)
create mode 100644 src/main/jjtree/parser.jjt
diff --git a/src/main/jjtree/parser.jjt b/src/main/jjtree/parser.jjt
new file mode 100644
index 0000000..f070b64
--- /dev/null
+++ b/src/main/jjtree/parser.jjt
@@ -0,0 +1,43 @@
+options {
+ STATIC=false;
+ MULTI=true;
+ VISITOR=true;
+}
+
+PARSER_BEGIN( DomainParser )
+package net.sourceforge.cilib.parser;
+public class DomainParser {}
+
+class DomainNode extends SimpleNode {
+ String value;
+ public DomainNode() { super(0); }
+ public DomainNode(String value) { super(0); this.value = value; }
+ public String getValue() { return value; }
+ public String toString() { return value; }
+}
+PARSER_END( DomainParser )
+
+ASTrootElement rootElement() : {} { element() <EOF> { return jjtThis; }}
+void element() : {} { domainElement() (repeat())? }
+void domainElement() : {} { type() (dimension())? }
+void type() : { Token t = null; } { t=<TYPE> {jjtThis.jjtAddChild(new DomainNode(t.image), 0);}}
+void exponent() : { Token t = null; } { <EXPONENT> t=<INTEGER> {jjtThis.jjtAddChild(new DomainNode(t.image), 0);}}
+void dimension() : {} { (lowerDim() | exponent() ) }
+void lowerDim() : {} { "(" number() ( upperDim() | value() ) }
+void upperDim() : {} { "," number() ")" (exponent())? }
+void value() : {} { ")" (exponent())? }
+void repeat() : {} { "," element() }
+void number() : { Token t = null; } {
+ t=<NUMBER> { jjtThis.jjtAddChild(new DomainNode(t.image), 0);}
+| <MINUS> t=<NUMBER> {jjtThis.jjtAddChild(new DomainNode("-"+t.image), 0);}
+}
+
+SKIP : { " " | "\t" | "\n" | "\r" }
+TOKEN : {
+ < INTEGER : (<DIGIT>)+ >
+| < NUMBER : <INTEGER> ( "." <INTEGER> )? >
+| < DIGIT : ["0"-"9"] >
+| < TYPE : ( "R" | "B" | "Z" ) >
+| < MINUS : "-" >
+| < EXPONENT : "^" >
+}
\ No newline at end of file
--
1.6.2.3
|
|
From: Gary P. <gpa...@gm...> - 2009-06-10 10:52:21
|
Defining the Domain parser within a grammar file and letting the parser
be generated is infinitely more useful than the old manner in which we
defined the parser. The JavaCC grammar may be might not be the best, but
it's a step in the right direction.
Signed-off-by: Gary Pampara <gpa...@gm...>
---
pom.xml | 14 ++
.../bioinf/sequencealignment/BinaryMSAProblem.java | 12 +-
.../cilib/bioinf/sequencealignment/MSAProblem.java | 5 +-
.../controlparameter/BoundedControlParameter.java | 12 +-
.../functions/discrete/KnightCoverageProblem.java | 1 -
.../cilib/problem/ClusteringProblem.java | 5 +-
.../CooperativeOptimisationProblemAdapter.java | 21 ++-
.../net/sourceforge/cilib/type/DomainBuilder.java | 2 +
.../net/sourceforge/cilib/type/DomainParser.java | 2 +
.../net/sourceforge/cilib/type/DomainRegistry.java | 2 +-
.../sourceforge/cilib/type/DomainValidator.java | 1 +
.../cilib/type/StringBasedDomainRegistry.java | 34 ++--
.../java/net/sourceforge/cilib/type/creator/B.java | 16 +-
.../java/net/sourceforge/cilib/type/creator/R.java | 8 +
.../java/net/sourceforge/cilib/type/creator/T.java | 7 +
.../cilib/type/creator/TypeCreator.java | 7 +
.../java/net/sourceforge/cilib/type/creator/Z.java | 9 +
.../cilib/type/parser/DomainParserVisitorImpl.java | 193 ++++++++++++++++
src/main/jjtree/parser.jjt | 89 ++++++--
.../cilib/measurement/generic/IterationsTest.java | 10 +-
.../generic/PercentageCompleteTest.java | 10 +-
.../cilib/measurement/generic/RestartsTest.java | 10 +-
.../cilib/measurement/generic/TimeTest.java | 10 +-
.../measurement/multiple/MultipleFitnessTest.java | 10 +-
.../multiple/MultipleSolutionsTest.java | 10 +-
.../single/BestParticlePositionTest.java | 14 +-
.../cilib/measurement/single/DiameterTest.java | 10 +-
.../measurement/single/FitnessEvaluationsTest.java | 10 +-
.../cilib/measurement/single/FitnessTest.java | 10 +-
.../single/FunctionOptimisationErrorTest.java | 12 +-
.../measurement/single/ParticlePositionsTest.java | 10 +-
.../cilib/measurement/single/SolutionTest.java | 12 +-
.../sourceforge/cilib/type/DomainParserTest.java | 233 ++++++++++----------
.../cilib/type/StringBasedDomainRegistryTest.java | 26 +--
.../cilib/type/parser/DomainParserTest.java | 86 +++++++
35 files changed, 644 insertions(+), 279 deletions(-)
create mode 100644 src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java
create mode 100644 src/test/java/net/sourceforge/cilib/type/parser/DomainParserTest.java
diff --git a/pom.xml b/pom.xml
index ead9cc4..d94420c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -163,6 +163,20 @@
<version>2.4.2</version>
</plugin>
<plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>javacc-maven-plugin</artifactId>
+ <!--<version>2.5</version>-->
+ <version>2.4.1</version>
+ <executions>
+ <execution>
+ <id>jjtree-javacc</id>
+ <goals>
+ <goal>jjtree-javacc</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
diff --git a/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/BinaryMSAProblem.java b/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/BinaryMSAProblem.java
index 58148af..b1b1671 100644
--- a/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/BinaryMSAProblem.java
+++ b/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/BinaryMSAProblem.java
@@ -26,9 +26,10 @@ import java.util.Collection;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.problem.MaximisationFitness;
import net.sourceforge.cilib.problem.OptimisationProblemAdapter;
-import net.sourceforge.cilib.type.DomainParser;
import net.sourceforge.cilib.type.DomainRegistry;
import net.sourceforge.cilib.type.StringBasedDomainRegistry;
+import net.sourceforge.cilib.type.parser.DomainParser;
+import net.sourceforge.cilib.type.parser.ParseException;
import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.type.types.container.Vector;
@@ -163,7 +164,7 @@ public class BinaryMSAProblem extends OptimisationProblemAdapter {
*/
public DomainRegistry getDomain() { //computes the domain according to the input sequences and amount of gaps to insert
if (this.domainRegistry.getDomainString() == null) {
- DomainParser parser = new DomainParser();
+// DomainParser parser = new DomainParser();
//reads in the input data sets.
FASTADataSetBuilder stringBuilder = (FASTADataSetBuilder) this.getDataSetBuilder();
@@ -207,7 +208,12 @@ public class BinaryMSAProblem extends OptimisationProblemAdapter {
System.out.println("Domain: "+rep); //extra for debug, can be commented out
- parser.parse(rep);
+ try {
+ //extra for debug, can be commented out
+// parser.parse(rep);
+ DomainParser.parse(rep);
+ } catch (ParseException ex) {
+ }
domainRegistry.setDomainString(rep);
}
diff --git a/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/MSAProblem.java b/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/MSAProblem.java
index 1e9f690..ed2d2d2 100644
--- a/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/MSAProblem.java
+++ b/src/main/java/net/sourceforge/cilib/bioinf/sequencealignment/MSAProblem.java
@@ -26,7 +26,6 @@ import java.util.Collection;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.problem.MaximisationFitness;
import net.sourceforge.cilib.problem.OptimisationProblemAdapter;
-import net.sourceforge.cilib.type.DomainParser;
import net.sourceforge.cilib.type.DomainRegistry;
import net.sourceforge.cilib.type.StringBasedDomainRegistry;
import net.sourceforge.cilib.type.types.Type;
@@ -118,7 +117,7 @@ public class MSAProblem extends OptimisationProblemAdapter {
public DomainRegistry getDomain() { //computes the domain according to the input sequences and amount of gaps to insert
if (this.domainRegistry.getDomainString() == null) {
- DomainParser parser = new DomainParser();
+// DomainParser parser = new DomainParser();
//reads in the input data sets.
FASTADataSetBuilder stringBuilder = (FASTADataSetBuilder) this.getDataSetBuilder();
@@ -163,7 +162,7 @@ public class MSAProblem extends OptimisationProblemAdapter {
System.out.println("Domain: "+rep); //extra for debug, can be commented out
- parser.parse(rep);
+// parser.parse(rep);
domainRegistry.setDomainString(rep);
}
diff --git a/src/main/java/net/sourceforge/cilib/controlparameter/BoundedControlParameter.java b/src/main/java/net/sourceforge/cilib/controlparameter/BoundedControlParameter.java
index b10557e..f864cb3 100644
--- a/src/main/java/net/sourceforge/cilib/controlparameter/BoundedControlParameter.java
+++ b/src/main/java/net/sourceforge/cilib/controlparameter/BoundedControlParameter.java
@@ -21,9 +21,9 @@
*/
package net.sourceforge.cilib.controlparameter;
-import net.sourceforge.cilib.type.DomainParser;
+import net.sourceforge.cilib.type.parser.DomainParser;
+import net.sourceforge.cilib.type.parser.ParseException;
import net.sourceforge.cilib.type.types.Bounds;
-import net.sourceforge.cilib.type.types.BoundsFactory;
import net.sourceforge.cilib.type.types.Real;
import net.sourceforge.cilib.type.types.container.Vector;
@@ -167,11 +167,11 @@ public abstract class BoundedControlParameter implements ControlParameter {
* Set the range of the parameter.
* @param range The domain string representing the range.
*/
- public void setRange(String range) {
+ public void setRange(String range) throws ParseException {
this.range = range;
- DomainParser parser = new DomainParser();
- parser.parse(this.range);
- Vector v = (Vector) parser.getBuiltRepresentation();
+ Vector v = (Vector) DomainParser.parse(this.range);
+// parser.parse(this.range);
+// Vector v = (Vector) parser.getBuiltRepresentation();
if (v.getDimension() != 1)
throw new RuntimeException("Range incorrect in BoundedUpdateStrategy! Please correct");
diff --git a/src/main/java/net/sourceforge/cilib/functions/discrete/KnightCoverageProblem.java b/src/main/java/net/sourceforge/cilib/functions/discrete/KnightCoverageProblem.java
index d34fc3e..3aaf649 100644
--- a/src/main/java/net/sourceforge/cilib/functions/discrete/KnightCoverageProblem.java
+++ b/src/main/java/net/sourceforge/cilib/functions/discrete/KnightCoverageProblem.java
@@ -24,7 +24,6 @@ package net.sourceforge.cilib.functions.discrete;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.problem.MinimisationFitness;
import net.sourceforge.cilib.problem.OptimisationProblemAdapter;
-import net.sourceforge.cilib.type.DomainParser;
import net.sourceforge.cilib.type.DomainRegistry;
import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.type.types.container.Vector;
diff --git a/src/main/java/net/sourceforge/cilib/problem/ClusteringProblem.java b/src/main/java/net/sourceforge/cilib/problem/ClusteringProblem.java
index 75b2a11..df99a21 100644
--- a/src/main/java/net/sourceforge/cilib/problem/ClusteringProblem.java
+++ b/src/main/java/net/sourceforge/cilib/problem/ClusteringProblem.java
@@ -25,7 +25,6 @@ import net.sourceforge.cilib.problem.dataset.AssociatedPairDataSetBuilder;
import net.sourceforge.cilib.problem.dataset.ClusterableDataSet;
import net.sourceforge.cilib.problem.dataset.DataSetBuilder;
import net.sourceforge.cilib.problem.dataset.DataSetManager;
-import net.sourceforge.cilib.type.DomainParser;
import net.sourceforge.cilib.type.DomainRegistry;
import net.sourceforge.cilib.type.StringBasedDomainRegistry;
import net.sourceforge.cilib.type.types.Type;
@@ -227,8 +226,8 @@ public class ClusteringProblem extends OptimisationProblemAdapter {
* clustered
*/
public void setDomain(String representation) {
- DomainParser parser = new DomainParser();//DomainParser.getInstance();
- parser.parse(representation);
+// DomainParser parser = new DomainParser();//DomainParser.getInstance();
+// parser.parse(representation);
domainRegistry.setDomainString(representation);
regenerateDomain();
diff --git a/src/main/java/net/sourceforge/cilib/problem/CooperativeOptimisationProblemAdapter.java b/src/main/java/net/sourceforge/cilib/problem/CooperativeOptimisationProblemAdapter.java
index 8d45549..37fee26 100644
--- a/src/main/java/net/sourceforge/cilib/problem/CooperativeOptimisationProblemAdapter.java
+++ b/src/main/java/net/sourceforge/cilib/problem/CooperativeOptimisationProblemAdapter.java
@@ -21,11 +21,13 @@
*/
package net.sourceforge.cilib.problem;
-import net.sourceforge.cilib.algorithm.InitialisationException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import net.sourceforge.cilib.cooperative.CooperativeEntity;
-import net.sourceforge.cilib.type.DomainParser;
import net.sourceforge.cilib.type.DomainRegistry;
import net.sourceforge.cilib.type.StringBasedDomainRegistry;
+import net.sourceforge.cilib.type.parser.DomainParser;
+import net.sourceforge.cilib.type.parser.ParseException;
import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.type.types.TypeUtil;
import net.sourceforge.cilib.type.types.container.Vector;
@@ -65,12 +67,17 @@ public class CooperativeOptimisationProblemAdapter extends OptimisationProblemAd
if (i < offset + dimension - 1)
expandedDomain += ",";
}
- DomainParser dp = new DomainParser();
- if (dp.parse(expandedDomain)) {
- domainRegistry.setDomainString(expandedDomain);
+ try {
+// DomainParser dp = new DomainParser();
+// if (dp.parse(expandedDomain)) {
+// domainRegistry.setDomainString(expandedDomain);
+// }
+// else
+// throw new InitialisationException("The expanded domain string \"" + expandedDomain + "\" could not be parsed.");
+ DomainParser.parse(expandedDomain);
+ } catch (ParseException ex) {
+ Logger.getLogger(CooperativeOptimisationProblemAdapter.class.getName()).log(Level.SEVERE, null, ex);
}
- else
- throw new InitialisationException("The expanded domain string \"" + expandedDomain + "\" could not be parsed.");
}
public CooperativeOptimisationProblemAdapter(CooperativeOptimisationProblemAdapter copy) {
diff --git a/src/main/java/net/sourceforge/cilib/type/DomainBuilder.java b/src/main/java/net/sourceforge/cilib/type/DomainBuilder.java
index f6a4637..4c44d67 100644
--- a/src/main/java/net/sourceforge/cilib/type/DomainBuilder.java
+++ b/src/main/java/net/sourceforge/cilib/type/DomainBuilder.java
@@ -52,7 +52,9 @@ import net.sourceforge.cilib.type.types.container.Vector;
* </ul>
*
* @author Gary Pampara
+ * @deprecated in favor of the JavaCC generated parser.
*/
+@Deprecated
public class DomainBuilder {
private TypeList representation;
diff --git a/src/main/java/net/sourceforge/cilib/type/DomainParser.java b/src/main/java/net/sourceforge/cilib/type/DomainParser.java
index b3c918e..abfb06a 100644
--- a/src/main/java/net/sourceforge/cilib/type/DomainParser.java
+++ b/src/main/java/net/sourceforge/cilib/type/DomainParser.java
@@ -29,7 +29,9 @@ import net.sourceforge.cilib.type.types.container.StructuredType;
* represenation.
*
* @author Gary Pampara
+ * @deprecated in favor of the JavaCC generated parser.
*/
+@Deprecated
public final class DomainParser {
private DomainBuilder builder;
diff --git a/src/main/java/net/sourceforge/cilib/type/DomainRegistry.java b/src/main/java/net/sourceforge/cilib/type/DomainRegistry.java
index 6f512ff..d5f765b 100644
--- a/src/main/java/net/sourceforge/cilib/type/DomainRegistry.java
+++ b/src/main/java/net/sourceforge/cilib/type/DomainRegistry.java
@@ -56,7 +56,7 @@ public interface DomainRegistry extends Cloneable, Serializable {
* a dimensional string with a descriptve component for each dimension.
* @return Returns the expandedRepresentation.
*/
- public String getExpandedRepresentation();
+// public String getExpandedRepresentation();
/**
* Get the instance of the built representation for this domain string.
diff --git a/src/main/java/net/sourceforge/cilib/type/DomainValidator.java b/src/main/java/net/sourceforge/cilib/type/DomainValidator.java
index 4cc662f..bfed51d 100644
--- a/src/main/java/net/sourceforge/cilib/type/DomainValidator.java
+++ b/src/main/java/net/sourceforge/cilib/type/DomainValidator.java
@@ -52,6 +52,7 @@ import java.util.ArrayList;
* where <tt>[]</tt> represents an empty string.
*
* @author Gary Pampara
+ * @deprecated in favor of the JavaCC generated parser.
*/
public class DomainValidator {
diff --git a/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java b/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java
index a19605c..4309009 100644
--- a/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java
+++ b/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java
@@ -21,6 +21,9 @@
*/
package net.sourceforge.cilib.type;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import net.sourceforge.cilib.type.parser.ParseException;
import net.sourceforge.cilib.type.types.container.StructuredType;
import net.sourceforge.cilib.type.types.container.Vector;
@@ -38,7 +41,7 @@ public class StringBasedDomainRegistry implements DomainRegistry {
*/
private static final long serialVersionUID = 3821361290684036030L;
private String domainString;
- private String expandedRepresentation;
+// private String expandedRepresentation;
private StructuredType builtRepresenation;
@@ -55,7 +58,7 @@ public class StringBasedDomainRegistry implements DomainRegistry {
*/
public StringBasedDomainRegistry(StringBasedDomainRegistry copy) {
this.domainString = copy.domainString;
- this.expandedRepresentation = copy.expandedRepresentation;
+// this.expandedRepresentation = copy.expandedRepresentation;
this.builtRepresenation = copy.builtRepresenation.getClone();
}
@@ -79,28 +82,31 @@ public class StringBasedDomainRegistry implements DomainRegistry {
*/
public void setDomainString(String domainString) {
this.domainString = domainString;
-
- DomainParser parser = new DomainParser();
- parser.parse(domainString);
-
- setExpandedRepresentation(parser.expandDomainString(domainString));
- setBuiltRepresenation(parser.getBuiltRepresentation());
+ try {
+// DomainParser parser = new DomainParser();
+// parser.parse(domainString);
+// setExpandedRepresentation(parser.expandDomainString(domainString));
+// setBuiltRepresenation(parser.getBuiltRepresentation());
+ setBuiltRepresenation(net.sourceforge.cilib.type.parser.DomainParser.parse(domainString));
+ } catch (ParseException ex) {
+ Logger.getLogger(StringBasedDomainRegistry.class.getName()).log(Level.SEVERE, null, ex);
+ }
}
/**
* {@inheritDoc}
*/
- public String getExpandedRepresentation() {
- return expandedRepresentation;
- }
+// public String getExpandedRepresentation() {
+// return expandedRepresentation;
+// }
/**
* Set the value of the expaded domain string.
* @param expandedRepresentation The expandedRepresentation to set.
*/
- public void setExpandedRepresentation(String expandedRepresentation) {
- this.expandedRepresentation = expandedRepresentation;
- }
+// public void setExpandedRepresentation(String expandedRepresentation) {
+// this.expandedRepresentation = expandedRepresentation;
+// }
/**
* {@inheritDoc}
diff --git a/src/main/java/net/sourceforge/cilib/type/creator/B.java b/src/main/java/net/sourceforge/cilib/type/creator/B.java
index 525ebb7..b4a0f39 100644
--- a/src/main/java/net/sourceforge/cilib/type/creator/B.java
+++ b/src/main/java/net/sourceforge/cilib/type/creator/B.java
@@ -30,15 +30,12 @@ import net.sourceforge.cilib.type.types.Type;
*
*/
public final class B implements TypeCreator {
-
- /**
- *
- */
private static final long serialVersionUID = 7124782787032789332L;
/**
* {@inheritDoc}
*/
+ @Override
public Type create() {
return new Bit();
}
@@ -46,6 +43,17 @@ public final class B implements TypeCreator {
/**
* {@inheritDoc}
*/
+ @Override
+ public Type create(double value) {
+ Bit b = new Bit();
+ b.setReal(value);
+ return b;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public Type create(final double lower, final double upper) {
throw new UnsupportedOperationException("Bit types cannot be constructed with bounds");
}
diff --git a/src/main/java/net/sourceforge/cilib/type/creator/R.java b/src/main/java/net/sourceforge/cilib/type/creator/R.java
index 68c7e38..01d8ac8 100644
--- a/src/main/java/net/sourceforge/cilib/type/creator/R.java
+++ b/src/main/java/net/sourceforge/cilib/type/creator/R.java
@@ -41,6 +41,14 @@ public final class R implements TypeCreator {
/**
* {@inheritDoc}
*/
+ @Override
+ public Type create(double value) {
+ return new Real(value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public Type create(final double lower, final double upper) {
return new Real(lower, upper);
}
diff --git a/src/main/java/net/sourceforge/cilib/type/creator/T.java b/src/main/java/net/sourceforge/cilib/type/creator/T.java
index 65b71d6..d12e7a1 100644
--- a/src/main/java/net/sourceforge/cilib/type/creator/T.java
+++ b/src/main/java/net/sourceforge/cilib/type/creator/T.java
@@ -46,6 +46,13 @@ public final class T implements TypeCreator {
/**
* {@inheritDoc}
*/
+ public Type create(double value) {
+ throw new UnsupportedOperationException("StringTypes with single values do not exist");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public Type create(final double lower, final double upper) {
throw new UnsupportedOperationException("StringTypes with bounds do not exist");
}
diff --git a/src/main/java/net/sourceforge/cilib/type/creator/TypeCreator.java b/src/main/java/net/sourceforge/cilib/type/creator/TypeCreator.java
index 9816e75..2096c3b 100644
--- a/src/main/java/net/sourceforge/cilib/type/creator/TypeCreator.java
+++ b/src/main/java/net/sourceforge/cilib/type/creator/TypeCreator.java
@@ -42,6 +42,13 @@ public interface TypeCreator {
*/
public Type create();
+ /**
+ * Create the type with the specified value.
+ * @param value The value for the {@code Type}.
+ * @return The created {@code Type} with the provided value.
+ */
+ public Type create(double value);
+
/**
* Create the type using the bounds <tt>lower</tt> and <tt>upper</tt>.
diff --git a/src/main/java/net/sourceforge/cilib/type/creator/Z.java b/src/main/java/net/sourceforge/cilib/type/creator/Z.java
index 8fa4feb..c8d6609 100644
--- a/src/main/java/net/sourceforge/cilib/type/creator/Z.java
+++ b/src/main/java/net/sourceforge/cilib/type/creator/Z.java
@@ -46,6 +46,15 @@ public final class Z implements TypeCreator {
/**
* {@inheritDoc}
*/
+ public Type create(double value) {
+ Int i = new Int();
+ i.setReal(value);
+ return i;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public Type create(final double lower, final double upper) {
return new Int(Double.valueOf(lower).intValue(), Double.valueOf(upper).intValue());
}
diff --git a/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java b/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java
new file mode 100644
index 0000000..66de851
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java
@@ -0,0 +1,193 @@
+/**
+ * Copyright (C) 2003 - 2008
+ * Computational Intelligence Research Group (CIRG@UP)
+ * Department of Computer Science
+ * University of Pretoria
+ * South Africa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+package net.sourceforge.cilib.type.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import net.sourceforge.cilib.type.creator.TypeCreator;
+import net.sourceforge.cilib.type.parser.DomainParser.DomainNode;
+import net.sourceforge.cilib.type.types.Type;
+import net.sourceforge.cilib.type.types.container.TypeList;
+
+/**
+ *
+ * @author gpampara
+ */
+public class DomainParserVisitorImpl implements DomainParserVisitor {
+
+ @Override
+ public Object visit(SimpleNode node, Object data) {
+ return null;
+ }
+
+ @Override
+ public Object visit(ASTrootElement node, Object data) {
+ ASTelement element = (ASTelement) node.jjtGetChild(0);
+ return element.jjtAccept(this, data);
+ }
+
+ @Override
+ public Object visit(ASTelement node, Object data) {
+ ASTdomainElement domainElement = (ASTdomainElement) node.jjtGetChild(0);
+ domainElement.jjtAccept(this, data);
+
+ if (node.jjtGetNumChildren() > 1) {
+ ASTrepeat repeat = (ASTrepeat) node.jjtGetChild(1);
+ repeat.jjtAccept(this, data);
+ }
+
+ return null;
+ }
+
+ @Override
+ public Object visit(ASTdomainElement node, Object data) {
+ int children = node.jjtGetNumChildren();
+ TypeCreator creator = null;
+ List<Double> bounds = null;
+ int exponent = 1;
+
+ // type()
+ ASTtype type = (ASTtype) node.jjtGetChild(0);
+ creator = (TypeCreator) type.jjtAccept(this, data);
+
+ if (children > 1) {
+ SimpleNode dimensionOrExponent = (SimpleNode) node.jjtGetChild(1);
+
+ if (dimensionOrExponent instanceof ASTdimension) {
+ ASTdimension dimension = (ASTdimension) dimensionOrExponent;
+ bounds = (List<Double>) dimension.jjtAccept(this, data);
+
+ if (children > 2) { // We have an exponent as well
+ exponent = getExponent(node, data, 2);
+ }
+ }
+
+ if (dimensionOrExponent instanceof ASTexponent) {
+ exponent = getExponent(node, data, 1);
+ }
+
+ }
+
+ expandDomain(creator, bounds, exponent, data);
+
+ return null;
+ }
+
+ @Override
+ public Object visit(ASTtype node, Object data) {
+ DomainNode domainNode = (DomainNode) node.jjtGetChild(0);
+ TypeCreator instance = null;
+
+ try {
+ // create an instance of the TypeCreator
+ Class<?> creatorClass = Class.forName("net.sourceforge.cilib.type.creator." + domainNode.getValue());
+ instance = (TypeCreator) creatorClass.newInstance();
+ } catch (ClassNotFoundException ex) {
+ } catch (InstantiationException ex) {
+ } catch (IllegalAccessException ex) {
+ }
+
+ return instance;
+ }
+
+ @Override
+ public Object visit(ASTexponent node, Object data) {
+ DomainNode domainNode = (DomainNode) node.jjtGetChild(0);
+ return domainNode.getValue();
+ }
+
+ @Override
+ public Object visit(ASTdimension node, Object data) {
+ ASTlowerDim lowerDim = (ASTlowerDim) node.jjtGetChild(0);
+ return lowerDim.jjtAccept(this, data);
+ }
+
+ @Override
+ public Object visit(ASTlowerDim node, Object data) {
+ List<Double> bounds = new ArrayList<Double>();
+
+ ASTnumber number1 = (ASTnumber) node.jjtGetChild(0);
+
+ Double value1 = Double.valueOf((String) number1.jjtAccept(this, data));
+ bounds.add(value1);
+
+ SimpleNode remainder = (SimpleNode) node.jjtGetChild(1);
+ if (remainder instanceof ASTupperDim) {
+ ASTupperDim upper = (ASTupperDim) remainder;
+ Double value2 = Double.valueOf((String) upper.jjtAccept(this, data));
+ bounds.add(value2);
+ }
+
+ if (remainder instanceof ASTvalue) {
+ ASTvalue value = (ASTvalue) remainder;
+ value.jjtAccept(this, data);
+ }
+
+ return bounds;
+ }
+
+ @Override
+ public Object visit(ASTupperDim node, Object data) {
+ ASTnumber number = (ASTnumber) node.jjtGetChild(0);
+ return number.jjtAccept(this, data);
+ }
+
+ @Override
+ public Object visit(ASTvalue node, Object data) {
+ // Nothing to be done as this is a terminal node that simply ends a statement.
+ return null;
+ }
+
+ @Override
+ public Object visit(ASTrepeat node, Object data) {
+ ASTelement element = (ASTelement) node.jjtGetChild(0);
+ return element.jjtAccept(this, data);
+ }
+
+ @Override
+ public Object visit(ASTnumber node, Object data) {
+ DomainNode domainNode = (DomainNode) node.jjtGetChild(0);
+ return domainNode.getValue();
+ }
+
+ private int getExponent(ASTdomainElement node, Object data, int index) throws NumberFormatException {
+ ASTexponent astexponent = (ASTexponent) node.jjtGetChild(index);
+ return Integer.valueOf((String) astexponent.jjtAccept(this, data)).intValue();
+ }
+
+ private void expandDomain(TypeCreator creator, List<Double> bounds, int exponent, Object data) {
+ TypeList list = (TypeList) data;
+
+ for (int i = 0; i < exponent; i++) {
+ Type type = null;
+ if (bounds == null || bounds.size() == 0)
+ type = creator.create();
+ else if (bounds.size() == 1)
+ type = creator.create(bounds.get(0));
+ else // 2 bound values provided
+ type = creator.create(bounds.get(0), bounds.get(1));
+
+ list.add(type);
+ }
+ }
+}
diff --git a/src/main/jjtree/parser.jjt b/src/main/jjtree/parser.jjt
index f070b64..aeb3423 100644
--- a/src/main/jjtree/parser.jjt
+++ b/src/main/jjtree/parser.jjt
@@ -5,31 +5,72 @@ options {
}
PARSER_BEGIN( DomainParser )
-package net.sourceforge.cilib.parser;
-public class DomainParser {}
-
-class DomainNode extends SimpleNode {
- String value;
- public DomainNode() { super(0); }
- public DomainNode(String value) { super(0); this.value = value; }
- public String getValue() { return value; }
- public String toString() { return value; }
+package net.sourceforge.cilib.type.parser;
+
+import java.io.StringReader;
+import net.sourceforge.cilib.type.types.Numeric;
+import net.sourceforge.cilib.type.types.Type;
+import net.sourceforge.cilib.type.types.container.AbstractList;
+import net.sourceforge.cilib.type.types.container.StructuredType;
+import net.sourceforge.cilib.type.types.container.TypeList;
+import net.sourceforge.cilib.type.types.container.Vector;
+
+public class Domain...
[truncated message content] |
|
From: Gary P. <gpa...@gm...> - 2009-06-10 10:52:22
|
Sometimes the domain string may be specified with an invalid value. Some
examples of this are:
- R(8.0, 9.0)^0 (Zero exponent)
- R(9.0, 9.0)^-1 (Negative exponent)
- R(9.0, 8.0) (Lower bound is greater than upper bound)
To correct this, the parser grammar was corrected. Tests have been added
to verify the behaviour of the parser.
Streamlined the domain registry by removing a member that is no longer
used.
Signed-off-by: Gary Pampara <gpa...@gm...>
---
pom.xml | 3 +-
.../cilib/type/StringBasedDomainRegistry.java | 36 +----
.../cilib/type/parser/DomainParserVisitorImpl.java | 85 ++++++++++-
src/main/jjtree/parser.jjt | 10 +-
.../sourceforge/cilib/type/DomainParserTest.java | 160 +++-----------------
.../cilib/type/StringBasedDomainRegistryTest.java | 32 ++--
.../cilib/type/parser/DomainParserTest.java | 46 ++++++-
7 files changed, 177 insertions(+), 195 deletions(-)
diff --git a/pom.xml b/pom.xml
index d94420c..46704b7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -165,8 +165,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javacc-maven-plugin</artifactId>
- <!--<version>2.5</version>-->
- <version>2.4.1</version>
+ <version>2.5</version>
<executions>
<execution>
<id>jjtree-javacc</id>
diff --git a/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java b/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java
index 4309009..4fc5c17 100644
--- a/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java
+++ b/src/main/java/net/sourceforge/cilib/type/StringBasedDomainRegistry.java
@@ -25,23 +25,16 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.cilib.type.parser.ParseException;
import net.sourceforge.cilib.type.types.container.StructuredType;
-import net.sourceforge.cilib.type.types.container.Vector;
/**
* Class to perform the needed mappings between a top level domain string
* and the built representation.
*
* @author Gary Pampara
- *
*/
public class StringBasedDomainRegistry implements DomainRegistry {
-
- /**
- * Generated <u>Serial Version UID</u> for the serialization.
- */
private static final long serialVersionUID = 3821361290684036030L;
private String domainString;
-// private String expandedRepresentation;
private StructuredType builtRepresenation;
@@ -58,13 +51,13 @@ public class StringBasedDomainRegistry implements DomainRegistry {
*/
public StringBasedDomainRegistry(StringBasedDomainRegistry copy) {
this.domainString = copy.domainString;
-// this.expandedRepresentation = copy.expandedRepresentation;
this.builtRepresenation = copy.builtRepresenation.getClone();
}
/**
* {@inheritDoc}
*/
+ @Override
public StringBasedDomainRegistry getClone() {
return new StringBasedDomainRegistry(this);
}
@@ -72,6 +65,7 @@ public class StringBasedDomainRegistry implements DomainRegistry {
/**
* {@inheritDoc}
*/
+ @Override
public String getDomainString() {
return domainString;
}
@@ -80,14 +74,11 @@ public class StringBasedDomainRegistry implements DomainRegistry {
* Set the value of the string representing the domain.
* @param domainString The domainString to set.
*/
+ @Override
public void setDomainString(String domainString) {
this.domainString = domainString;
try {
-// DomainParser parser = new DomainParser();
-// parser.parse(domainString);
-// setExpandedRepresentation(parser.expandDomainString(domainString));
-// setBuiltRepresenation(parser.getBuiltRepresentation());
- setBuiltRepresenation(net.sourceforge.cilib.type.parser.DomainParser.parse(domainString));
+ this.builtRepresenation = net.sourceforge.cilib.type.parser.DomainParser.parse(domainString);
} catch (ParseException ex) {
Logger.getLogger(StringBasedDomainRegistry.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -96,21 +87,7 @@ public class StringBasedDomainRegistry implements DomainRegistry {
/**
* {@inheritDoc}
*/
-// public String getExpandedRepresentation() {
-// return expandedRepresentation;
-// }
-
- /**
- * Set the value of the expaded domain string.
- * @param expandedRepresentation The expandedRepresentation to set.
- */
-// public void setExpandedRepresentation(String expandedRepresentation) {
-// this.expandedRepresentation = expandedRepresentation;
-// }
-
- /**
- * {@inheritDoc}
- */
+ @Override
public StructuredType getBuiltRepresenation() {
return this.builtRepresenation;
}
@@ -128,8 +105,9 @@ public class StringBasedDomainRegistry implements DomainRegistry {
/**
* {@inheritDoc}
*/
+ @Override
public int getDimension() {
- return ((Vector) this.builtRepresenation).getDimension();
+ return this.builtRepresenation.size();
}
}
diff --git a/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java b/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java
index 66de851..c0079ee 100644
--- a/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java
+++ b/src/main/java/net/sourceforge/cilib/type/parser/DomainParserVisitorImpl.java
@@ -35,17 +35,37 @@ import net.sourceforge.cilib.type.types.container.TypeList;
*/
public class DomainParserVisitorImpl implements DomainParserVisitor {
+ /**
+ * Top level visit operation - no actions are performed.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(SimpleNode node, Object data) {
return null;
}
+ /**
+ * The root starting point of the grammar. No actions are performed apart from
+ * deferring to other production rules.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTrootElement node, Object data) {
ASTelement element = (ASTelement) node.jjtGetChild(0);
return element.jjtAccept(this, data);
}
+ /**
+ * Obtain the results of the visitation of the {@code domainElement}
+ * and any possible {@code repeat}s.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTelement node, Object data) {
ASTdomainElement domainElement = (ASTdomainElement) node.jjtGetChild(0);
@@ -59,6 +79,13 @@ public class DomainParserVisitorImpl implements DomainParserVisitor {
return null;
}
+ /**
+ * Obtain the data from the domainElement. Actual types are constructed based
+ * on the determined type, bounds and exponent values.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTdomainElement node, Object data) {
int children = node.jjtGetNumChildren();
@@ -85,14 +112,18 @@ public class DomainParserVisitorImpl implements DomainParserVisitor {
if (dimensionOrExponent instanceof ASTexponent) {
exponent = getExponent(node, data, 1);
}
-
}
expandDomain(creator, bounds, exponent, data);
-
return null;
}
+ /**
+ * Obtain the type defined in the portion of the domain string.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTtype node, Object data) {
DomainNode domainNode = (DomainNode) node.jjtGetChild(0);
@@ -103,31 +134,51 @@ public class DomainParserVisitorImpl implements DomainParserVisitor {
Class<?> creatorClass = Class.forName("net.sourceforge.cilib.type.creator." + domainNode.getValue());
instance = (TypeCreator) creatorClass.newInstance();
} catch (ClassNotFoundException ex) {
+ ex.printStackTrace();
} catch (InstantiationException ex) {
+ ex.printStackTrace();
} catch (IllegalAccessException ex) {
+ ex.printStackTrace();
}
return instance;
}
+ /**
+ * Extract the exponent value for the portion of the domain string.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTexponent node, Object data) {
DomainNode domainNode = (DomainNode) node.jjtGetChild(0);
return domainNode.getValue();
}
+ /**
+ * Determine the dimension elements of the potion of the domain string.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTdimension node, Object data) {
ASTlowerDim lowerDim = (ASTlowerDim) node.jjtGetChild(0);
return lowerDim.jjtAccept(this, data);
}
+ /**
+ * Extract the lower bounds information from the portion of the domain string.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTlowerDim node, Object data) {
List<Double> bounds = new ArrayList<Double>();
ASTnumber number1 = (ASTnumber) node.jjtGetChild(0);
-
Double value1 = Double.valueOf((String) number1.jjtAccept(this, data));
bounds.add(value1);
@@ -136,6 +187,10 @@ public class DomainParserVisitorImpl implements DomainParserVisitor {
ASTupperDim upper = (ASTupperDim) remainder;
Double value2 = Double.valueOf((String) upper.jjtAccept(this, data));
bounds.add(value2);
+
+ if (value1.compareTo(value2) > 0)
+ throw new UnsupportedOperationException("Parsed bound values are not in order." +
+ "Upper bound is less than lower bound.");
}
if (remainder instanceof ASTvalue) {
@@ -146,24 +201,48 @@ public class DomainParserVisitorImpl implements DomainParserVisitor {
return bounds;
}
+ /**
+ * Obtain the value for the upper bound and return it.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTupperDim node, Object data) {
ASTnumber number = (ASTnumber) node.jjtGetChild(0);
return number.jjtAccept(this, data);
}
+ /**
+ * Perfrom no actions. This is a terminal node for bounds.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return {@code null} - no action is performed.
+ */
@Override
public Object visit(ASTvalue node, Object data) {
// Nothing to be done as this is a terminal node that simply ends a statement.
return null;
}
+ /**
+ * Apply the repeat action as defined in the grammar.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTrepeat node, Object data) {
ASTelement element = (ASTelement) node.jjtGetChild(0);
return element.jjtAccept(this, data);
}
+ /**
+ * Extract the number that is defined in the domain string.
+ * @param node The abstract syntax tree node to visit.
+ * @param data The passed in data object.
+ * @return The result of the visit opertion.
+ */
@Override
public Object visit(ASTnumber node, Object data) {
DomainNode domainNode = (DomainNode) node.jjtGetChild(0);
diff --git a/src/main/jjtree/parser.jjt b/src/main/jjtree/parser.jjt
index aeb3423..00da3ba 100644
--- a/src/main/jjtree/parser.jjt
+++ b/src/main/jjtree/parser.jjt
@@ -62,21 +62,21 @@ ASTrootElement rootElement() : {} { element() <EOF> { return jjtThis; }}
void element() : {} { domainElement() (repeat())? }
void domainElement() : {} { type() (dimension() (exponent())? | exponent())? }
void type() : { Token t = null; } { t=<TYPE> {jjtThis.jjtAddChild(new DomainNode(t.image), 0);}}
-void exponent() : { Token t = null; } { <EXPONENT> t=<INTEGER> {jjtThis.jjtAddChild(new DomainNode(t.image), 0);}}
+void exponent() : { Token t = null; } { <EXPONENT> t=<POSITIVE_INTEGER> {jjtThis.jjtAddChild(new DomainNode(t.image), 0);}}
void dimension() : {} { lowerDim() }
void lowerDim() : {} { "(" number() ( upperDim() | value() ) }
void upperDim() : {} { "," number() ")" }
void value() : {} { ")" }
void repeat() : {} { "," element() }
void number() : { Token t = null; } {
- (t=<NUMBER> | t=<INTEGER>) { jjtThis.jjtAddChild(new DomainNode(t.image), 0);}
-| <MINUS> (t=<NUMBER> | t=<INTEGER>) {jjtThis.jjtAddChild(new DomainNode("-"+t.image), 0);}
+ (t=<POSITIVE_INTEGER> | t=<NUMBER>) { jjtThis.jjtAddChild(new DomainNode(t.image), 0);}
+| <MINUS> (t=<POSITIVE_INTEGER> | t=<NUMBER>) {jjtThis.jjtAddChild(new DomainNode("-"+t.image), 0);}
}
SKIP : { " " | "\t" | "\n" | "\r" }
TOKEN : {
- < INTEGER : (<DIGIT>)+ >
-| < NUMBER : <INTEGER> ( "." <INTEGER> )? >
+ < POSITIVE_INTEGER : (["1"-"9"]) (<DIGIT>)* >
+| < NUMBER : (<DIGIT>)+ ( "." (<DIGIT>)+ )? >
| < DIGIT : ["0"-"9"] >
| < TYPE : ( "R" | "B" | "Z" | "T" ) >
| < MINUS : "-" >
diff --git a/src/test/java/net/sourceforge/cilib/type/DomainParserTest.java b/src/test/java/net/sourceforge/cilib/type/DomainParserTest.java
index 0e8894d..d2a1362 100644
--- a/src/test/java/net/sourceforge/cilib/type/DomainParserTest.java
+++ b/src/test/java/net/sourceforge/cilib/type/DomainParserTest.java
@@ -23,7 +23,8 @@
package net.sourceforge.cilib.type;
import net.sourceforge.cilib.type.parser.ParseException;
-import static org.junit.Assert.fail;
+import net.sourceforge.cilib.type.types.container.Vector;
+import org.junit.Assert;
import org.junit.Test;
@@ -35,162 +36,43 @@ public class DomainParserTest {
@Test
public void testParseReal() throws ParseException {
-// DomainParser parser = new DomainParser();
-// try {
-// parser.parse("R(0,INF)");
-// parser.parse("R");
-// assertEquals("R", parser.expandDomainString("R"));
-// parser.parse("R^6");
-// assertEquals("R,R,R,R,R,R", parser.expandDomainString("R^6"));
-// parser.parse("R(-9,9)");
-// assertEquals("R(-9.0,9.0)", parser.expandDomainString("R(-9,9)"));
-// parser.parse("R(-30.0,30.0)^6");
-// assertEquals("R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)", parser.expandDomainString("R(-30.0,30.0)^6"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("R(0.0,9.0)");
- net.sourceforge.cilib.type.parser.DomainParser.parse("R");
-// assertEquals("R", parser.expandDomainString("R"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("R^6");
-// assertEquals("R,R,R,R,R,R", parser.expandDomainString("R^6"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("R(-9.0,9.0)");
-// assertEquals("R(-9.0,9.0)", parser.expandDomainString("R(-9,9)"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("R(-30.0,30.0)^6");
-// assertEquals("R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)", parser.expandDomainString("R(-30.0,30.0)^6"));
-// }
-// catch (Exception e) {
-// fail("Parser cannot handle parsing of R! " + e.getMessage());
-// }
+ net.sourceforge.cilib.type.parser.DomainParser.parse("R(0.0,9.0)");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("R");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("R^6");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("R(-9.0,9.0)");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("R(-30.0,30.0)^6");
}
@Test
public void testParseBit() throws ParseException {
-// DomainParser parser = new DomainParser();
- net.sourceforge.cilib.type.parser.DomainParser.parse("B");
- net.sourceforge.cilib.type.parser.DomainParser.parse("B^6");
-
-// parser.parse("B");
-// assertEquals("B", parser.expandDomainString("B"));
-// parser.parse("B^6");
-// assertEquals("B,B,B,B,B,B", parser.expandDomainString("B^6"));
+ net.sourceforge.cilib.type.parser.DomainParser.parse("B");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("B^6");
}
@Test
public void testParseInteger() throws ParseException {
-// DomainParser parser = new DomainParser();
- net.sourceforge.cilib.type.parser.DomainParser.parse("Z");
-// parser.parse("Z");
-// assertEquals("Z", parser.expandDomainString("Z"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("Z(-1,0)");
-// assertEquals("Z(-1.0,0.0)", parser.expandDomainString("Z(-1,0)"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("Z(0,1)");
-// assertEquals("Z(0.0,1.0)", parser.expandDomainString("Z(0,1)"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("Z(-999,999)");
-// assertEquals("Z(-999.0,999.0)", parser.expandDomainString("Z(-999,999)"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("Z^8");
-// assertEquals("Z,Z,Z,Z,Z,Z,Z,Z", parser.expandDomainString("Z^8"));
- net.sourceforge.cilib.type.parser.DomainParser.parse("Z(0,1)^10");
-// assertEquals("Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0),Z(0.0,1.0)", parser.expandDomainString("Z(0,1)^10"));
+ net.sourceforge.cilib.type.parser.DomainParser.parse("Z");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("Z(-1,0)");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("Z(0,1)");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("Z(-999,999)");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("Z^8");
+ net.sourceforge.cilib.type.parser.DomainParser.parse("Z(0,1)^10");
}
@Test
- public void testParseString() {
-// DomainParser parser = new DomainParser();
- try {
- net.sourceforge.cilib.type.parser.DomainParser.parse("T^5");
-// assertEquals("T,T,T,T,T", parser.expandDomainString("T^5"));
- }
- catch (Exception e) {
- fail("Parser cannot handle parsing of T! " + e.getMessage());
- }
+ public void testParseString() throws ParseException {
+ net.sourceforge.cilib.type.parser.DomainParser.parse("T^5");
}
@Test
- public void testParseComplexDomain() {
-// DomainParser parser = new DomainParser();
- try {
- net.sourceforge.cilib.type.parser.DomainParser.parse("R(-30.0,30.0)^30,B,Z^6");
-// assertEquals("R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),B,Z,Z,Z,Z,Z,Z",
-// parser.expandDomainString("R(-30.0,30.0)^30,B,Z^6"));
- }
- catch (Exception e) {
- fail("Parser cannot handle parsing of a complex domain! " + e.getMessage());
- }
- }
-
-
- /**
- * This test determines if the parser can successfully parse and build
- * domains that can be represented as a matrix (also regarded as second order
- * domains).
- */
-// @Test
-// public void testParseMatrixDomain() {
-// DomainParser parser = new DomainParser();
-// try {
-// parser.parse("[R(-30.0,30.0)^4]^5,B,B,Z(2,5)");
-// assertEquals("[R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)],[R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)],[R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)],[R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)],[R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0),R(-30.0,30.0)],B,B,Z(2.0,5.0)",
-// parser.expandDomainString("[R(-30.0,30.0)^4]^5,B,B,Z(2,5)"));
-// }
-// catch (Exception e) {
-// fail("Parser cannot handle martix representation domains! " + e.getMessage());
-// }
-// }
+ public void testParseComplexDomain() throws ParseException {
+ Vector vector = (Vector) net.sourceforge.cilib.type.parser.DomainParser.parse("R(-30.0,30.0)^30,B,Z^6");
-
-// @Test
-// public void testBuildMatrixDomain() {
-// DomainParser parser = new DomainParser();
-// try {
-// parser.parse("[R(-30.0,30.0)^4]^5");
-//
-// TypeList matrix = (TypeList) parser.getBuiltRepresentation();
-//
-// // Test the dimensionality
-// assertEquals(5, matrix.size());
-//
-// for (int i = 0; i < matrix.size(); i++) {
-// assertEquals(4, ((TypeList)matrix.get(i)).size());
-// }
-//
-// // Test object equality
-// Vector v1 = (Vector) matrix.get(0);
-// Vector v2 = (Vector) matrix.get(1);
-// Vector v3 = (Vector) matrix.get(2);
-// Vector v4 = (Vector) matrix.get(3);
-// Vector v5 = (Vector) matrix.get(4);
-//
-// assertTrue(assertObjectNotSame(v1, v2));
-// assertTrue(assertObjectNotSame(v1, v3));
-// assertTrue(assertObjectNotSame(v1, v4));
-// assertTrue(assertObjectNotSame(v1, v5));
-// assertTrue(assertObjectNotSame(v2, v3));
-// assertTrue(assertObjectNotSame(v2, v4));
-// assertTrue(assertObjectNotSame(v2, v5));
-// assertTrue(assertObjectNotSame(v3, v4));
-// assertTrue(assertObjectNotSame(v3, v4));
-// assertTrue(assertObjectNotSame(v4, v5));
-// } catch (Exception e) {
-//
-// }
-// }
-
- /**
- *
- * @param v1
- * @param v2
- * @return
- */
-// private boolean assertObjectNotSame(Vector v1, Vector v2) {
-
-// for (int i = 0; i < v1.getDimension(); i++) {
-// if (v1.get(i).equals(v2.get(i)))
-// return false;
-// }
-//
-// return true;
-// }
+ Assert.assertEquals(37, vector.size());
+ }
}
diff --git a/src/test/java/net/sourceforge/cilib/type/StringBasedDomainRegistryTest.java b/src/test/java/net/sourceforge/cilib/type/StringBasedDomainRegistryTest.java
index 56104da..34c52e7 100644
--- a/src/test/java/net/sourceforge/cilib/type/StringBasedDomainRegistryTest.java
+++ b/src/test/java/net/sourceforge/cilib/type/StringBasedDomainRegistryTest.java
@@ -4,17 +4,17 @@
* Department of Computer Science
* University of Pretoria
* South Africa
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -22,23 +22,25 @@
package net.sourceforge.cilib.type;
-import org.junit.Ignore;
+import net.sourceforge.cilib.type.types.TypeUtil;
+import net.sourceforge.cilib.type.types.container.StructuredType;
+import org.junit.Assert;
+import org.junit.Test;
/**
*
* @author gpampara
*/
-@Ignore
public class StringBasedDomainRegistryTest {
-// @Test
-// public void matrixCreation() {
-// DomainRegistry registry = new StringBasedDomainRegistry();
-// registry.setDomainString("[R(-5.0, 5.0)^10]^10");
-//
-// TypeList matrix = (TypeList) registry.getBuiltRepresenation();
-// Assert.assertEquals(10, matrix.size());
-// Assert.assertEquals(10, ((TypeList) matrix.get(0)).size());
-// Assert.assertTrue(TypeUtil.isInsideBounds(matrix));
-// }
+ @Test
+ public void structureExists() {
+ DomainRegistry registry = new StringBasedDomainRegistry();
+ registry.setDomainString("R(-30.0, 30)^30");
+
+ StructuredType<?> structure = registry.getBuiltRepresenation();
+ Assert.assertEquals(30, structure.size());
+ Assert.assertTrue(TypeUtil.isInsideBounds(structure));
+ }
+
}
diff --git a/src/test/java/net/sourceforge/cilib/type/parser/DomainParserTest.java b/src/test/java/net/sourceforge/cilib/type/parser/DomainParserTest.java
index 3dd25e5..e71a7bf 100644
--- a/src/test/java/net/sourceforge/cilib/type/parser/DomainParserTest.java
+++ b/src/test/java/net/sourceforge/cilib/type/parser/DomainParserTest.java
@@ -27,11 +27,15 @@ import org.junit.Assert;
import org.junit.Test;
/**
- *
+ * Tests related to the parsing of domain strings.
* @author gpampara
*/
public class DomainParserTest {
+ /**
+ * Creation of {@code StringType}.
+ * @throws ParseException if an exception occurs during parsing.
+ */
@Test
public void stringType() throws ParseException {
StructuredType vector = DomainParser.parse("T");
@@ -39,8 +43,12 @@ public class DomainParserTest {
Assert.assertEquals(1, vector.size());
}
+ /**
+ * The default kind of domain string that would be quite common.
+ * @throws ParseException
+ */
@Test
- public void test() throws ParseException {
+ public void dimensionRange() throws ParseException {
Vector vector = (Vector) DomainParser.parse("R(-9.0, 9.0)^6");
Assert.assertEquals(6, vector.size());
@@ -83,4 +91,38 @@ public class DomainParserTest {
Assert.assertEquals(10, vector.size());
}
+
+ @Test(expected=TokenMgrError.class)
+ public void invalidDomain() throws ParseException {
+ DomainParser.parse("Y");
+ }
+
+ @Test(expected=ParseException.class)
+ public void parseNotValid() throws ParseException {
+ DomainParser.parse("R(-5, -4, -5)^-7");
+ }
+
+ @Test(expected=ParseException.class)
+ public void negativeExponent() throws ParseException {
+ DomainParser.parse("R^-9");
+ }
+
+ @Test(expected=ParseException.class)
+ public void zeroExponent() throws ParseException {
+ DomainParser.parse("R^0");
+ }
+
+ @Test
+ public void integerBounds() throws ParseException {
+ DomainParser.parse("R(1,3)");
+ DomainParser.parse("R(-1,3)");
+ DomainParser.parse("R(-3,-1)");
+ DomainParser.parse("R(-3,-1)^9");
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void incorrectBoundsOrder() throws ParseException {
+ DomainParser.parse("R(3, 2)"); // Lower bound > Upper bound = WRONG!
+ }
+
}
--
1.6.2.3
|
|
From: Theuns C. <the...@gm...> - 2009-06-11 06:28:18
|
Hey Gary, Would it make sense to split the parser into a separate maven module? This way the parser code only has to be generated once, and the JAR file can just be included in the classpath. Regards -- Theuns On Wed, Jun 10, 2009 at 12:51 PM, Gary Pampara<gpa...@gm...> wrote: > The need to have a decent parser for the domain strings is something > that has been on the cards for a very long time. The main problem with > the current implementation is that it is very non-expressive. > > Illegal domains such as: > - R(8, 9)^-1 > - R^0 > - Any other strange combination > > were allowed in the previous parser. > > These patches propose a new generated parser based on a provided grammar > file. > > JJTree and JavaCC were used to achieve the parser. > > Gary Pampara (3): > Semi-complete parser definition. > Deprecation of parser for generated parser. > Prevent invalid values / options in domain strings. |
|
From: Gary P. <gpa...@gm...> - 2009-06-11 06:33:48
|
For now, I don't think so. The parser is quite an important internal component. The generation side is only required for development. Once the classes are packaged it will remain to seem like "any other POJO" in the project. If the parser gets more complex and starts doing oither things, then we could consider it. As it's just focused on the domain strings, it's actually rather small. On Thursday 11 June 2009 08:28:16 Theuns Cloete wrote: > Hey Gary, > > Would it make sense to split the parser into a separate maven module? > This way the parser code only has to be generated once, and the JAR > file can just be included in the classpath. > > Regards > -- > Theuns > > On Wed, Jun 10, 2009 at 12:51 PM, Gary Pampara<gpa...@gm...> wrote: > > The need to have a decent parser for the domain strings is something > > that has been on the cards for a very long time. The main problem with > > the current implementation is that it is very non-expressive. > > > > Illegal domains such as: > > - R(8, 9)^-1 > > - R^0 > > - Any other strange combination > > > > were allowed in the previous parser. > > > > These patches propose a new generated parser based on a provided grammar > > file. > > > > JJTree and JavaCC were used to achieve the parser. > > > > Gary Pampara (3): > > Semi-complete parser definition. > > Deprecation of parser for generated parser. > > Prevent invalid values / options in domain strings. > > --------------------------------------------------------------------------- >--- Crystal Reports - New Free Runtime and 30 Day Trial > Check out the new simplified licensing option that enables unlimited > royalty-free distribution of the report engine for externally facing > server and web deployment. > http://p.sf.net/sfu/businessobjects > _______________________________________________ > cilib-devel mailing list > cil...@li... > https://lists.sourceforge.net/lists/listinfo/cilib-devel |