diff --git a/src/lsystem/engine/Rewrite.java b/src/lsystem/engine/Rewrite.java new file mode 100644 index 0000000..bb2842b --- /dev/null +++ b/src/lsystem/engine/Rewrite.java @@ -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> rules; + private final int recurrences; + + public Rewrite(String axiom, List> 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 pair = rules.get(j); + rewritted = rewritted.replace(pair.getLeft(), pair.getRight()); + } + } + return rewritted; + } + +} diff --git a/src/lsystem/utils/Pair.java b/src/lsystem/utils/Pair.java new file mode 100644 index 0000000..d2cfb18 --- /dev/null +++ b/src/lsystem/utils/Pair.java @@ -0,0 +1,40 @@ +package lsystem.utils; + +public class Pair { + + 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(); + } +}