Guest User

lsystem

a guest
May 21st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace ConsoleLSystem {
  9. public class LSystemNodeLiteral {
  10. public string name { get; }
  11. public int values_number { get; }
  12.  
  13. public float[] values { get; set; }
  14.  
  15. public LSystemNodeLiteral(string name, int values_number) {
  16. this.name = name;
  17. this.values_number = values_number;
  18. //in case it would be 0
  19. values = new float[values_number];
  20. }
  21. public LSystemNodeLiteral(LSystemNodeLiteral other) {
  22. this.name = other.name;
  23. this.values_number = other.values_number;
  24. //in case it would be 0
  25. values = new float[this.values_number];
  26. other.values.CopyTo(values, 0);
  27. }
  28.  
  29. public static bool operator ==(LSystemNodeLiteral thisLiteral, LSystemNodeLiteral other) {
  30. return thisLiteral.name == other.name && thisLiteral.values_number == other.values_number;
  31. }
  32. public static bool operator !=(LSystemNodeLiteral thisLiteral, LSystemNodeLiteral other) {
  33. return !(thisLiteral.name == other.name && thisLiteral.values_number == other.values_number);
  34. }
  35.  
  36. public override string ToString() {
  37. if (values_number == 0) {
  38. return name;
  39. }
  40. else {
  41. StringBuilder sb = new StringBuilder(name, name.Length + values_number * 7 + 4);
  42. sb.Append("(");
  43. for (int i = 0; i < values_number; i++) {
  44. var v = values[i];
  45. sb.AppendFormat("{0:0.##}", v);
  46. if (i < values_number - 1) {
  47. sb.Append(",");
  48. }
  49. }
  50. sb.Append(")");
  51. return sb.ToString();
  52. }
  53. }
  54. }
  55.  
  56. public class LSystemNode {
  57. public LSystemNodeLiteral literal { get; set; }
  58. public List<LSystemNode> children { get; set; }
  59. public LSystemNode mainChild { get; set; }
  60.  
  61. private LSystemNode _parent;
  62. public LSystemNode parent {
  63. get { if (_parent is null) { return new LSystemNode(new LSystemNodeLiteral(" _ ", 0)); }
  64. else { return _parent; } }
  65. set { _parent = value; } }
  66.  
  67. public LSystemNode(LSystemNodeLiteral nodeLiteral) {
  68. literal = nodeLiteral;
  69. children = new List<LSystemNode>();
  70. mainChild = null;
  71. }
  72.  
  73. public LSystemNode(LSystemNodeLiteral nodeLiteral, List<LSystemNode> children, LSystemNode mainChild) {
  74. literal = nodeLiteral;
  75. this.children = children;
  76. this.mainChild = mainChild;
  77. }
  78. public LSystemNode(LSystemNodeLiteral nodeLiteral, List<LSystemNode> children) {
  79. literal = nodeLiteral;
  80. this.children = children;
  81. this.mainChild = null;
  82. }
  83. public List<LSystemNode> getAllChildren() {
  84. var result = new List<LSystemNode>(children);
  85. if (mainChild != null){
  86. result.Add(mainChild);
  87. }
  88. return result;
  89. }
  90.  
  91. public LSystemNode deep_copy() {
  92. if (children.Count() == 0 && mainChild == null) {
  93. return new LSystemNode(new LSystemNodeLiteral(literal));
  94. }
  95. else if (mainChild != null) {
  96. var result = new LSystemNode(new LSystemNodeLiteral(literal));
  97. result.mainChild = mainChild.deep_copy();
  98. return result;
  99. }
  100. else {
  101. var new_children = new List<LSystemNode>();
  102. foreach (var x in children) {
  103. new_children.Add(x.deep_copy());
  104. }
  105. return new LSystemNode(literal, new_children, this.mainChild.deep_copy());
  106. }
  107. }
  108.  
  109. private static LSystemNode deepestNode(LSystemNode node) {
  110. while (node.mainChild != null) {
  111. node = node.mainChild;
  112. }
  113. return node;
  114. }
  115.  
  116. public LSystemNode newParent() {
  117. return deepestNode(this);
  118. }
  119.  
  120. public override string ToString() {
  121. StringBuilder sb = new StringBuilder(literal.ToString());
  122. var node = this;
  123. while (true) {
  124. foreach (var child in node.children) {
  125. sb.Append("[");
  126. sb.Append(child.ToString());
  127. sb.Append("]");
  128. }
  129. if (node.mainChild != null) {
  130. sb.Append(node.mainChild.literal.ToString());
  131. node = node.mainChild;
  132. }
  133. else {
  134. return sb.ToString();
  135. }
  136. }
  137. }
  138. }
  139.  
  140.  
  141. public class LSystemEvaluator {
  142. public LSystemNode lSystemString { get; set; }
  143. public List<LSystemRule> lSystemRules { get; set; }
  144. public String[] ignored { get; set; }
  145.  
  146. public LSystemEvaluator(LSystemNode startingString, List<LSystemRule> rules) {
  147. lSystemString = startingString;
  148. lSystemRules = rules;
  149. ignored = new String[0];
  150. }
  151. public LSystemEvaluator(LSystemNode startingString, List<LSystemRule> rules, String[] ignored) {
  152. lSystemString = startingString;
  153. lSystemRules = rules;
  154. this.ignored = ignored;
  155. }
  156. private LSystemNode _rewrite(LSystemNode node) {
  157. foreach (var rule in lSystemRules) {
  158. if (rule.is_aplicable(node, ignored)) {
  159. return rule.rewrite(node, ignored);
  160. }
  161. }
  162. return new LSystemNode(node.literal);
  163. }
  164. //private void _rewrite_recursive(LSystemNode node, LSystemNode parent) {
  165. // var new_node = _rewrite(node);
  166. // parent.children.Add(new_node);
  167. // new_node.parent = parent;
  168. // var new_parent = new_node.newParent();
  169.  
  170. // foreach (var child in node.children) {
  171. // _rewrite_recursive(child, new_parent);
  172. // }
  173. //}
  174.  
  175.  
  176. private void _rewrite_recursive(LSystemNode node, LSystemNode parent) {
  177. var new_node = _rewrite(node);
  178. parent.children.Add(new_node);
  179. new_node.parent = parent;
  180. var new_parent = new_node.newParent();
  181.  
  182. foreach (var child in node.children) {
  183. _rewrite_recursive(child, new_parent);
  184. }
  185. while (node.mainChild != null) {
  186. new_node = _rewrite(node.mainChild);
  187. new_node.parent = new_parent;
  188.  
  189. new_parent.mainChild = new_node;
  190. new_parent = new_node.newParent();
  191. foreach (var child in node.mainChild.children) {
  192. _rewrite_recursive(child, new_parent);
  193. }
  194. node = node.mainChild;
  195. }
  196. }
  197. public void rewrite() {
  198. var new_root = _rewrite(lSystemString);
  199. var new_parent = new_root.newParent();
  200. foreach (var child in lSystemString.children) {
  201. _rewrite_recursive(child, new_parent);
  202. }
  203. var node = lSystemString;
  204.  
  205. while (node.mainChild != null) {
  206. var new_node = _rewrite(node.mainChild);
  207. new_node.parent = new_parent;
  208.  
  209. new_parent.mainChild = new_node;
  210. new_parent = new_node.newParent();
  211. foreach (var child in node.mainChild.children) {
  212. _rewrite_recursive(child, new_parent);
  213. }
  214. node = node.mainChild;
  215. }
  216. lSystemString = new_root;
  217. }
  218. //private void rewrite(ref LSystemNode currentNode)
  219. }
  220. //abstract class LSystemWordParser { }
  221. //abstract class LSystemRulesParser { }
  222.  
  223. class A {
  224. public int B { get; set; }
  225. public A[] AA { get; set; }
  226. }
  227. class Program {
  228. static void runParametric() {
  229.  
  230. Dictionary<string, int> variableIndex = new Dictionary<string, int> {
  231. {"a", 0},
  232. {"b", 1},
  233.  
  234. };
  235. var predecesor1 = LSystemFileParser.parseContextLiteral("B(a,b)");
  236. var predecesor2 = LSystemFileParser.parseContextLiteral("A(a)");
  237. var condition1 = MathExpressionComparasionParser.parse(variableIndex, "a>b");
  238. var condition2 = MathExpressionComparasionParser.parse(variableIndex, "a>5");
  239.  
  240. var builder = new LSystemWordGeneratorBuilder(variableIndex);
  241.  
  242. var consequent1 = (LSystemNodeGenerator)LSystemFileParser.parseWord("B(0,b)[A(a/2)]A(a/2)", builder);
  243. var consequent2 = (LSystemNodeGenerator)LSystemFileParser.parseWord("B(a+3.5,b+1)", builder);
  244. var consequent3 = (LSystemNodeGenerator)LSystemFileParser.parseWord("B(0,a)", builder);
  245. var consequent4 = (LSystemNodeGenerator)LSystemFileParser.parseWord("A(a+1)", builder);
  246.  
  247. var rule1 = new LSystemRuleParametric(predecesor1, new MathExpressionComparison[] { condition1 }, consequent1 );
  248. var rule2 = new LSystemRuleParametric(predecesor1, new MathExpressionComparison[] { }, consequent2 );
  249. var rule3 = new LSystemRuleParametric(predecesor2, new MathExpressionComparison[] { condition2 }, consequent3 );
  250. var rule4 = new LSystemRuleParametric(predecesor2, new MathExpressionComparison[] { }, consequent4);
  251.  
  252. var axiom = LSystemFileParser.parseWord("A(6)", new LSystemWordBuilder());
  253.  
  254. var rules = new List<LSystemRule> { rule1, rule2, rule3, rule4 };
  255. var evaluator = new LSystemEvaluator(axiom, rules);
  256. Console.WriteLine(evaluator.lSystemString.ToString());
  257. for (int i = 0; i < 20; i++) {
  258. evaluator.rewrite();
  259. Console.WriteLine(evaluator.lSystemString.ToString());
  260. }
  261. }
  262. static void runContext() {
  263. var axiom = LSystemFileParser.parseWord("A(1)A(1)A(1)A(1)A(1)A(1)B(2)", new LSystemWordBuilder());
  264. var predecesor = LSystemFileParser.parseContextLiteral("A(a)");
  265. var predecesor1 = LSystemFileParser.parseContextLiteral("B(b)");
  266.  
  267. var contex = new LSystemContext();
  268. contex.setOnlySucceeding(new LSystemNodeLiteral[] { new LSystemNodeLiteral("B", 1) });
  269.  
  270. var contex1 = new LSystemContext();
  271. contex1.setOnlyPreceding(new LSystemNodeLiteral("A", 1));
  272.  
  273.  
  274. Dictionary<string, int> variableIndex = new Dictionary<string, int> {
  275. { "a", 0 },
  276. { "b", 1 },
  277. };
  278. var builder = new LSystemWordGeneratorBuilder(variableIndex);
  279. var consequent = (LSystemNodeGenerator)LSystemFileParser.parseWord("B(a)", builder);
  280. var consequent1 = (LSystemNodeGenerator)LSystemFileParser.parseWord("A(1)", builder);
  281. var consequent2 = (LSystemNodeGenerator)LSystemFileParser.parseWord("A(a+1)", builder);
  282.  
  283. MathExpressionComparison comparison = MathExpressionComparasionParser.parse(variableIndex, "a>b");
  284. MathExpressionComparison comparison1 = MathExpressionComparasionParser.parse(variableIndex, "a<=b");
  285.  
  286. var rule = new LSystemRuleParametricStochasticContext(predecesor, new MathExpressionComparison[] { comparison }, consequent, contex);
  287. var rule1 = new LSystemRuleParametricStochasticContext(predecesor, new MathExpressionComparison[] { }, consequent2, contex);
  288. var rule2 = new LSystemRuleParametricStochasticContext(predecesor1, new MathExpressionComparison[] { comparison }, consequent1, contex1);
  289.  
  290.  
  291. var rules = new List<LSystemRule> { rule, rule1, rule2 };
  292. var evaluator = new LSystemEvaluator(axiom, rules);
  293. Console.WriteLine(evaluator.lSystemString.ToString());
  294. for (int i = 0; i < 20; i++) {
  295. evaluator.rewrite();
  296. Console.WriteLine(evaluator.lSystemString.ToString());
  297. }
  298.  
  299. }
  300. static void runFile() {
  301. Console.WriteLine("Enter filepath:");
  302. string filepath = Console.ReadLine();
  303. var evaluator = LSystemFileParser.parseLSystem(new StreamReader(filepath));
  304. Console.WriteLine(evaluator.lSystemString.ToString());
  305. while (true) {
  306. Console.ReadLine();
  307. evaluator.rewrite();
  308. Console.WriteLine(evaluator.lSystemString.ToString());
  309. }
  310.  
  311. }
  312. static void Main(string[] args) {
  313. runFile();
  314. //runContext();
  315. //var Al = new LSystemNodeLiteral("Al", 0);
  316. //var Ar = new LSystemNodeLiteral("Ar", 0);
  317. //var Bl = new LSystemNodeLiteral("Bl", 0);
  318. //var Br = new LSystemNodeLiteral("Br", 0);
  319.  
  320. //var root = new LSystemNode(Ar);
  321. //var rule_list = new List<LSystemRule>();
  322.  
  323. //var result = new LSystemNode(Al);
  324. //result.children.Add(new LSystemNode(Br));
  325.  
  326. //rule_list.Add(new LSystemRuleBasic(Ar, result));
  327.  
  328. //result = new LSystemNode(Bl);
  329. //result.children.Add(new LSystemNode(Ar));
  330. //rule_list.Add(new LSystemRuleBasic(Al, result));
  331.  
  332. //result = new LSystemNode(Ar);
  333. //rule_list.Add(new LSystemRuleBasic(Br, result));
  334.  
  335. //result = new LSystemNode(Al);
  336. //rule_list.Add(new LSystemRuleBasic(Bl, result));
  337.  
  338.  
  339. //var evaluator = new LSystemEvaluator(root, rule_list);
  340. //for (int i=0; i < 5; i++) {
  341. // Console.WriteLine(evaluator.lSystemString.ToString());
  342. // evaluator.rewrite();
  343. //}
  344. //Console.WriteLine(evaluator.lSystemString.ToString());
  345.  
  346.  
  347.  
  348.  
  349. //var node = LSystemFileParser.parseWord("A[AA][A]AB(1,2)[A(1)]C", new LSystemWordBuilder());
  350. //Console.WriteLine(node.ToString());
  351. }
  352. }
  353. }
  354.  
Advertisement
Add Comment
Please, Sign In to add comment