-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSystem.py
71 lines (59 loc) · 2.22 KB
/
LSystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
""" -*- iso-8859-1 -*-
LSystem.py
Módulo para LSystems """
import re
import random
class LSystem:
__axiom = __productions = ''
""" @func init: Construtor da classe LSystem
@param axiom: Palavra inicial do L-System
@param productions: Regras de produção """
def __init__(self, axiom, productions):
self.__axiom = axiom
self.__productions = self.__validateProductions(productions)
def getAxiom(self):
return self.__axiom
def getProductions(self):
return self.__productions
""" @func __produce: Realiza uma etapa de derivação
@param w: axioma """
def __produce(self, w):
output = ""
for i in w:
output += self.__productions.get(i, i)
return output
""" @func iterate: Realiza sucessivas etapas derivações e retorna a string resultado
@param iteractions: Numero de etapas
@param axiom: axioma """
def iterate(self, iteractions, axiom = ''):
if not axiom:
axiom = self.__axiom
if iteractions > 0:
axiom = self.__produce(axiom)
return self.iterate(iteractions - 1, axiom)
return axiom
@staticmethod
def __validateProductions(productions):
LETTER = '[^\d→:,()<>]'
productions = re.sub('->', '→', productions)
productions = re.sub('\s*', '', productions)
PRODUCTION_RULE = '^' + LETTER + '→' + LETTER + '+' + '(,'+ LETTER + '→' + LETTER + '+' +')*' + '$'
if (re.compile(PRODUCTION_RULE).search(productions)):
dic = {}
look = ''
for i in range(len(productions)):
if (re.compile(LETTER).search(productions[i])):
look += productions[i]
elif (productions[i] == '→'):
state = look
if (not dic.get(state)): dic[state] = ''
look = ''
elif (productions[i] == ','):
dic[state] = look
look = ''
dic[state] = look
return dic
else:
print('Erro: Definição de produções inválida!')
exit()
# Fim da classe LSystems