implemented rewritter

This commit is contained in:
Quentin Legot 2021-02-02 17:57:42 +01:00
parent 2216561506
commit 664d3ed06d
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package lsystem.engine;
import lsystem.utils.Pair;
import java.util.HashMap;
import java.util.List;
public class Rewrite {
private final String axiom;
private final List<Pair<String, String >> rules;
private final int recurrences;
public Rewrite(String axiom, List<Pair<String, String>> rules, int recurrences) {
this.axiom = axiom;
this.rules = rules;
this.recurrences = recurrences;
}
public String rewrite() {
String rewritted = axiom;
for(int i = 0; i < recurrences; ++i) {
for(int j = 0; j < rules.size(); ++j){
Pair<String, String> pair = rules.get(j);
rewritted = rewritted.replace(pair.getLeft(), pair.getRight());
}
}
return rewritted;
}
}

View File

@ -0,0 +1,40 @@
package lsystem.utils;
public class Pair<U, K> {
private final U left;
private final K right;
public Pair(U left, K right) {
this.left = left;
this.right = right;
}
public U getLeft() {
return left;
}
public K getRight() {
return right;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair<?, ?> other = (Pair<?, ?>) obj;
return this.left.equals(other.getLeft()) && this.left.equals(other.getRight());
}
@Override
public int hashCode() {
return 31 + left.hashCode() * right.hashCode();
}
}