From 74f98c2692204537012f552a9ebe1f23de209c46 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 9 Feb 2021 17:20:22 +0100 Subject: [PATCH] Fixed rewritter --- src/lsystem/engine/Rewrite.java | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/lsystem/engine/Rewrite.java b/src/lsystem/engine/Rewrite.java index 4a2c74f..6fc4696 100644 --- a/src/lsystem/engine/Rewrite.java +++ b/src/lsystem/engine/Rewrite.java @@ -2,6 +2,7 @@ package lsystem.engine; import lsystem.utils.Pair; +import java.util.Arrays; import java.util.List; public class Rewrite { @@ -16,13 +17,29 @@ public class Rewrite { this.recurrences = recurrences; } + + private String replaceRulesByID(final String rewritted) { + String toRewrite = rewritted; + for(int j = 0; j < rules.size(); ++j){ + Pair 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() { 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()); - } + String toRewrite = replaceRulesByID(rewritted); + rewritted = replaceIDByRuleApplication(toRewrite); } return rewritted; }