Last active
December 14, 2015 11:38
-
-
Save calroc/5080067 to your computer and use it in GitHub Desktop.
I'm just noodling around with the Python-Omega parser (https://gitorious.org/python-omega), and I used it to implement a simple version of the Anabaena catenula "simulator" from the book "The Algorithmic Beauty of Plants" http://algorithmicbotany.org/papers/#abop One interesting thing is that the DOL-system is meant to evaluate its terms in para…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
A simple demonstration of a deterministic and context-free "DOL-system" | |
"...used to simulate the development of a fragment of a multicellular | |
filament such as that found in the blue-green bacteria Anabaena catenula..." | |
- "The Algorithmic Beauty of Plants" http://algorithmicbotany.org/papers/#abop | |
Implemented by simple parsers in the Omega parser language (a variant of | |
Warth's OMeta language.) https://gitorious.org/python-omega | |
''' | |
from itertools import chain | |
from omega import BaseParser | |
class AnabaenaCatenulaStringParser(BaseParser): | |
__grammar = ''' | |
Ar = "Ar" -> ('AlBr') ; | |
Al = "Al" -> ('BlAr') ; | |
Br = "Br" -> ('Ar') ; | |
Bl = "Bl" -> ('Al') ; | |
strand = ( Ar | Al | Br | Bl )+:cells -> (''.join(cells)) ; | |
''' | |
class AnabaenaCatenulaSequenceParser(BaseParser): | |
__grammar = ''' | |
Ar = 'Ar' -> (('Al', 'Br')) ; | |
Al = 'Al' -> (('Bl', 'Ar')) ; | |
Br = 'Br' -> (('Ar',)) ; | |
Bl = 'Bl' -> (('Al',)) ; | |
strand = ( Ar | Al | Br | Bl )+:cells -> (tuple(chain(*cells))) ; | |
''' | |
def demo(strand, parser, iterations=5): | |
for _ in range(iterations): | |
print strand | |
strand = parser.match(strand) | |
print strand | |
if __name__ == '__main__': | |
print __doc__ | |
demo('Ar', AnabaenaCatenulaStringParser) | |
demo(('Ar',), AnabaenaCatenulaSequenceParser) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, I confused the two kinds of possible parallelism in the description above. There are two: the cells are normally growing and reproducing each independently, and they know what they are turning into next so they don't have to perform the "strand" rule. In contrast, the parser/simulator has to walk the strand of cell/terms in order by design, and it tests the options in the "strand" rule (on each term) in order as well. The model and the living system jibe.