Fixed rewritter
This commit is contained in:
parent
83a606f2ec
commit
74f98c2692
@ -2,6 +2,7 @@ package lsystem.engine;
|
|||||||
|
|
||||||
import lsystem.utils.Pair;
|
import lsystem.utils.Pair;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Rewrite {
|
public class Rewrite {
|
||||||
@ -16,13 +17,29 @@ public class Rewrite {
|
|||||||
this.recurrences = recurrences;
|
this.recurrences = recurrences;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String replaceRulesByID(final String rewritted) {
|
||||||
|
String toRewrite = rewritted;
|
||||||
|
for(int j = 0; j < rules.size(); ++j){
|
||||||
|
Pair<String, String> pair = rules.get(j);
|
||||||
|
toRewrite = toRewrite.replace(pair.getLeft(), "$" + j);
|
||||||
|
}
|
||||||
|
return toRewrite;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replaceIDByRuleApplication(final String toRewrite) {
|
||||||
|
String rewritted = toRewrite;
|
||||||
|
for(int j = 0; j < rules.size(); ++j){
|
||||||
|
rewritted = rewritted.replace("$" + j, rules.get(j).getRight());
|
||||||
|
}
|
||||||
|
return rewritted;
|
||||||
|
}
|
||||||
|
|
||||||
public String rewrite() {
|
public String rewrite() {
|
||||||
String rewritted = axiom;
|
String rewritted = axiom;
|
||||||
for(int i = 0; i < recurrences; ++i) {
|
for(int i = 0; i < recurrences; ++i) {
|
||||||
for(int j = 0; j < rules.size(); ++j){
|
String toRewrite = replaceRulesByID(rewritted);
|
||||||
Pair<String, String> pair = rules.get(j);
|
rewritted = replaceIDByRuleApplication(toRewrite);
|
||||||
rewritted = rewritted.replace(pair.getLeft(), pair.getRight());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return rewritted;
|
return rewritted;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user