diff --git a/.classpath b/.classpath
index 1191e52..0cb6245 100644
--- a/.classpath
+++ b/.classpath
@@ -1,9 +1,8 @@
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5e56e04
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/bin
diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..d17b672
--- /dev/null
+++ b/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7
diff --git a/Grammars/mariolevel.gr b/Grammars/mariolevel.gr
new file mode 100644
index 0000000..ecd9bd4
--- /dev/null
+++ b/Grammars/mariolevel.gr
@@ -0,0 +1,27 @@
+#A# S
+#N# LEVEL CHUNKS CHUNK GAP X Y WG WBEFORE WAFTER WG PLATFORM W TUBE BOXES BOXTYPE ENEMIES ENEMY ENEMYTYPE H WC DIGIT DOSCINCO NUMBER TRESNUEVE ZEROCINCO UNOOCHO CINCONUEVE
+#T# gap platform tube hill cannon_hill tube_hill coin cannon block_coin block_powerup rock_coin rock_empty koopa goompa ( ) , 0 1 2 3 4 5 6 7 8 9 ;
+S ::= LEVEL
+LEVEL ::= CHUNKS ; ENEMIES
+CHUNKS ::= CHUNK ; CHUNK ; CHUNK ; CHUNK ; CHUNK | CHUNK ; CHUNKS
+CHUNK ::= gap ( X , Y , WG , WBEFORE , WAFTER ) | platform ( X , Y , W ) | tube ( X , Y , H , WBEFORE , WAFTER ) | hill ( X , Y , W ) | cannon_hill ( X , Y , H , WBEFORE , WAFTER ) | tube_hill ( X , Y , H , WBEFORE , WAFTER ) | coin ( X , Y , WC ) | cannon ( X , Y , H , WBEFORE , WAFTER ) | BOXES
+X ::= 1 CINCONUEVE | UNOOCHO DIGIT | 9 ZEROCINCO
+Y ::= 1 | 2 | 3
+WG ::= 2 | 3 | 4 | 5
+W ::= 0 TRESNUEVE | 1 ZEROCINCO
+WAFTER ::= DOSCINCO
+WBEFORE ::= DOSCINCO
+WC ::= DIGIT
+H ::= 2 | 3
+BOXES ::= BOXTYPE ( X , Y )
+BOXTYPE ::= block_coin | block_powerup | rock_coin | rock_empty
+ENEMIES ::= ENEMY ; ENEMY ; ENEMY ; ENEMY ; ENEMY | ENEMY ; ENEMIES
+ENEMY ::= ENEMYTYPE ( X )
+ENEMYTYPE ::= koopa | goompa
+NUMBER ::= DIGIT DIGIT
+TRESNUEVE ::= 3 | 4 | 5 | 6 | 7 | 8 | 9
+ZEROCINCO ::= 0 | 1 | 2 | 3 | 4 | 5
+UNOOCHO ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
+CINCONUEVE ::= 5 | 6 | 7 | 8 | 9
+DOSCINCO ::= 2 | 3 | 4 | 5
+DIGIT ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..71b7f55
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+
+Copyright (c) 2013 aturing
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/bin/grammar/Derivation.class b/bin/grammar/Derivation.class
deleted file mode 100644
index 6eaae76..0000000
Binary files a/bin/grammar/Derivation.class and /dev/null differ
diff --git a/bin/grammar/Element.class b/bin/grammar/Element.class
deleted file mode 100644
index a77b093..0000000
Binary files a/bin/grammar/Element.class and /dev/null differ
diff --git a/bin/grammar/Elements.class b/bin/grammar/Elements.class
deleted file mode 100644
index bb4f264..0000000
Binary files a/bin/grammar/Elements.class and /dev/null differ
diff --git a/bin/grammar/Grammar.class b/bin/grammar/Grammar.class
deleted file mode 100644
index 1b5d523..0000000
Binary files a/bin/grammar/Grammar.class and /dev/null differ
diff --git a/bin/grammar/GrammarException.class b/bin/grammar/GrammarException.class
deleted file mode 100644
index d289fd8..0000000
Binary files a/bin/grammar/GrammarException.class and /dev/null differ
diff --git a/bin/grammar/NonTerminal.class b/bin/grammar/NonTerminal.class
deleted file mode 100644
index cd83e09..0000000
Binary files a/bin/grammar/NonTerminal.class and /dev/null differ
diff --git a/bin/grammar/Production.class b/bin/grammar/Production.class
deleted file mode 100644
index f14e36c..0000000
Binary files a/bin/grammar/Production.class and /dev/null differ
diff --git a/bin/grammar/Productions.class b/bin/grammar/Productions.class
deleted file mode 100644
index 9f409ce..0000000
Binary files a/bin/grammar/Productions.class and /dev/null differ
diff --git a/bin/grammar/Terminal.class b/bin/grammar/Terminal.class
deleted file mode 100644
index 224bfa6..0000000
Binary files a/bin/grammar/Terminal.class and /dev/null differ
diff --git a/bin/grammar/util/Trees.class b/bin/grammar/util/Trees.class
deleted file mode 100644
index 98416ac..0000000
Binary files a/bin/grammar/util/Trees.class and /dev/null differ
diff --git a/bin/tests/MutationTest.class b/bin/tests/MutationTest.class
deleted file mode 100644
index ed36477..0000000
Binary files a/bin/tests/MutationTest.class and /dev/null differ
diff --git a/src/grammar/Derivation.java b/src/grammar/Derivation.java
index fcb71d3..45a7d61 100644
--- a/src/grammar/Derivation.java
+++ b/src/grammar/Derivation.java
@@ -184,7 +184,8 @@ private Tree getMaxRandomDerivation(Tree t, int currentDepth, Element knot, Posi
* root node. */
pos.set("Production", null);
/* e is the Element associated to the node (Position=node). */
- pos.set("Element", knot);
+ /* a copy of the terminal is introduced to let diferent internal values for each instance */
+ pos.set("Element", new Terminal((Terminal) knot));
}
/* Recursive step */
else{
@@ -200,7 +201,7 @@ private Tree getMaxRandomDerivation(Tree t, int currentDepth, Element knot, Posi
}
if(cP_aux.isEmpty()){
- throw new GrammarException("The NonTerminal " + knot.getSymbol() + " doesnt " + "has a Production associated so that " + "1 + Length(Production)<= " + this.grammar.getAxiom().getDepth());
+ throw new GrammarException("The NonTerminal " + knot.getSymbol() + " doesnt has a Production associated so that 1 + Length(Production)<= " + this.grammar.getAxiom().getDepth());
}
Random rand = new Random();
@@ -240,18 +241,9 @@ public Derivation mutate() throws GrammarException {
int pos = rand.nextInt(NT.size());
Position mPoint = NT.get(pos);
Element symbol = ((Element) mPoint.get("Element"));
-// System.out.print("Mutation node: "+symbol.getSymbol()+" -> ");
- Production prod = (Production) mPoint.get( "Production" );
- ArrayList E = prod.getRight();
-// for (Element e:E){
-// System.out.print(" "+e.getSymbol());
-// }
-// System.out.println();
Tree subtree = new NodeTree();
- subtree = this.getMaxRandomDerivation( subtree, dCopy.depthBackwards(mPoint)+1, symbol, null );
-
-// System.out.println("New branch: "+new Derivation(grammar, maxDepth, subtree).getWord());
+ subtree = this.getMaxRandomDerivation( subtree, dCopy.depthBackwards(mPoint), symbol, null );
copy.replaceSubtree(mPoint, subtree);
dCopy.setTree(copy);
@@ -279,7 +271,7 @@ public String getWord(){
n = (Position) poi.nextObject();
e = (Element)n.get("Element");
if (e instanceof Terminal){
- res = res.concat(((Element)n.get("Element")).getSymbol());
+ res = res.concat(((Element)n.get("Element")).getSymbol() + "");
}
}
}
@@ -748,6 +740,32 @@ public ArrayList getNonTerminalNodes() throws GrammarException {
return nodes;
}
+ /**
+ *
+ * As a Derivation is a m-Tree (a Tree where each node has several nodes), the function returns the
+ * Derivation's terminal nodes from left to right.
+ *
+ * @return
+ * A Collection with the Derivation's nodes.
+ *
+ */
+ public ArrayList getTerminalNodes() {
+ ArrayList nodes = new ArrayList();
+
+ if(this.derivation != null){
+ PreOrderIterator poi = new PreOrderIterator(this.derivation);
+ while(poi.hasNext()){
+ Position p = (Position) poi.nextObject();
+ Element e = (Element) p.get("Element");
+ if (e instanceof Terminal){
+ nodes.add((Terminal) e);
+ }
+ }
+ }
+ return nodes;
+
+ }
+
/**
*
diff --git a/src/grammar/Terminal.java b/src/grammar/Terminal.java
index 6c0b744..bcd4446 100644
--- a/src/grammar/Terminal.java
+++ b/src/grammar/Terminal.java
@@ -8,6 +8,7 @@
public class Terminal extends Element {
+ private Object value;
/**
*
@@ -23,5 +24,40 @@ public class Terminal extends Element {
public Terminal(int id, String symbol) throws GrammarException{
super(id, symbol);
this.setDepth(0);
+ this.value = null;
}
-}
+
+
+ /**
+ *
+ * Constructor that receives a terminal to get copied.
+ *
+ * @param terminal
+ * An instance of terminal.
+ *
+ **/
+ public Terminal(Terminal terminal) throws GrammarException{
+ super(terminal.getId(), terminal.getSymbol());
+ this.setDepth(0);
+ this.value = null;
+ }
+
+ /**
+ * Set value object
+ *
+ * @param object
+ * value of the termninal
+ *
+ */
+ public void value(Object value) { this.value = value; }
+
+
+ /**
+ * get value object
+ *
+ * @return object
+ * value of the terminal
+ *
+ */
+ public Object value() { return this.value; }
+}
\ No newline at end of file
diff --git a/src/tests/MutationTest.java b/src/tests/MutationTest.java
deleted file mode 100644
index c893f34..0000000
--- a/src/tests/MutationTest.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package tests;
-
-import static org.junit.Assert.*;
-import jdsl.core.api.Tree;
-import grammar.Derivation;
-import grammar.Grammar;
-import grammar.GrammarException;
-
-import org.apache.commons.lang3.StringUtils;
-import org.junit.Test;
-
-public class MutationTest {
-
- private Grammar grammar;
- int maxDepth = 100;
-
- public MutationTest() {
- try {
- grammar = new Grammar( "Grammars/g4.gr" );
- }
- catch (final GrammarException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- @Test
- public void generateANDmutate(){
- Derivation auxD = null, auxD2 = null;
-
- for (int i = 0; i < 1; i++) {
- try {
- auxD = new Derivation( grammar, maxDepth );
- }
- catch (final GrammarException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println( auxD.getWord() );
- try {
- auxD2 = auxD.mutate();
- }
- catch (GrammarException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- System.out.println( auxD2.getWord() );
- System.out.println("Mutation distance: "+StringUtils.getLevenshteinDistance( auxD.getWord(), auxD2.getWord()));
-
-
- }
-
- }
-
-}