Skip to content

Commit

Permalink
Added dsn.s_expr.concoct_history for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanschelven committed Oct 2, 2017
1 parent 1a49718 commit 0cdfc73
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doctests/concoct.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
>>> from hashstore import NoutHashStore
>>> from memoization import Stores, Memoization
>>> from dsn.historiography.legato import HistoriographyNoteNoutHash, HistoriographyNoteNout, HistoriographyNoteCapo
>>> from dsn.s_expr.legato import NoteNout, NoteCapo, NoteNoutHash

>>> from dsn.s_expr.from_python import s_expr_from_python
>>> from dsn.s_expr.construct_x import construct_x
>>> from dsn.s_expr.concoct import concoct_history

>>> p = NoutHashStore(NoteNoutHash, NoteNout, NoteCapo)
>>> historiography_note_nout_store = NoutHashStore(HistoriographyNoteNoutHash, HistoriographyNoteNout, HistoriographyNoteCapo)
>>> stores = Stores(p, historiography_note_nout_store)
>>> m = Memoization()

>>> s_expr = s_expr_from_python(("foo", ("bar",)))
>>> result = concoct_history(m, stores, s_expr)
>>> construct_x(m, stores, result[-1].nout_hash)
(foo (bar))
27 changes: 27 additions & 0 deletions dsn/s_expr/concoct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from dsn.s_expr.clef import BecomeNode, TextBecome, Insert
from dsn.s_expr.structure import TreeNode, TreeText
from dsn.s_expr.utils import stored_nouts_for_notes_da_capo


def concoct_history(m_, stores, s_expr):
"""Given an s_expression, concoct a history that produces the s_expression.
The method of concocting is The Simplest Thing That Could Possibly Work, i.e. in the concocted history things are
created in the order of (spatial) appearance; each child is added only once using a single Insert (which contains
the child's entire history).
The history is returned as a list of NoutAndHash (which have been added to the store, a.k.a. "seen")
"""

if isinstance(s_expr, TreeNode):
notes = [BecomeNode()]

for i, child in enumerate(s_expr.children):
child_history = concoct_history(m_, stores, child)
notes.append(Insert(i, child_history[-1].nout_hash))

if isinstance(s_expr, TreeText):
notes = [TextBecome(s_expr.unicode_)]

return list(stored_nouts_for_notes_da_capo(stores.note_nout, notes)) # wrapped in list to ensure it happens
6 changes: 6 additions & 0 deletions dsn/s_expr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def calc_actuality(nout_hash):
# TODO In the below (nouts_for_notes_*, some_cut_paste*), we're not consistent in whether we're returning Possibilities,
# Nouts, NoutAndHash tuples etc. I'm confident that consistency will emerge once we know what good defaults are.

def stored_nouts_for_notes_da_capo(hashstore, notes):
for nh in nouts_for_notes_da_capo(notes):
hashstore.add(nh.nout)
yield nh


def nouts_for_notes_da_capo(notes):
"""Given notes without prior history, connect them as Nouts"""
possibility, previous_hash = calc_possibility(NoteCapo())
Expand Down
1 change: 1 addition & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocFileSuite("doctests/unused_definitions.txt"))
tests.addTests(doctest.DocFileSuite("doctests/name_dependencies.txt"))
tests.addTests(doctest.DocFileSuite("doctests/lexical_addressing_x.txt"))
tests.addTests(doctest.DocFileSuite("doctests/concoct.txt"))

return tests

Expand Down

0 comments on commit 0cdfc73

Please sign in to comment.