Skip to content

Instantly share code, notes, and snippets.

@calroc
Last active December 14, 2015 11:38
Show Gist options
  • Save calroc/5080067 to your computer and use it in GitHub Desktop.
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…
#!/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__
print
demo('Ar', AnabaenaCatenulaStringParser)
print
demo(('Ar',), AnabaenaCatenulaSequenceParser)
print
@calroc
Copy link
Author

calroc commented Mar 4, 2013

Example output:

Ar
AlBr
BlArAr
AlAlBrAlBr
BlArBlArArBlArAr
AlAlBrAlAlBrAlBrAlAlBrAlBr
('Ar',)
('Al', 'Br')
('Bl', 'Ar', 'Ar')
('Al', 'Al', 'Br', 'Al', 'Br')
('Bl', 'Ar', 'Bl', 'Ar', 'Ar', 'Bl', 'Ar', 'Ar')
('Al', 'Al', 'Br', 'Al', 'Al', 'Br', 'Al', 'Br', 'Al', 'Al', 'Br', 'Al', 'Br')

@calroc
Copy link
Author

calroc commented Mar 4, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment