Commit 60b46125 authored by Adam P. Goucher's avatar Adam P. Goucher

Equality comparison between ruletrees

parent 644323fa
Pipeline #49829372 passed with stages
in 8 minutes and 23 seconds
......@@ -62,11 +62,20 @@ def ParseRuleTree(list_of_lines):
return n_states, nhood, list_of_nodes
def compress_tree(list_of_nodes, n_states, numInputs):
def compress_tree(list_of_nodes, n_states, numInputs, seqonly=True):
rt = RuleTree(n_states, numInputs, initialise=False)
rt.seq = list(map(tuple, list_of_nodes))
rt._shrink()
return list(map(list, rt.seq))
if seqonly:
return list(map(list, rt.seq))
else:
return rt
def make_tree(n_states, nhood, list_of_nodes):
numInputs = GetNumberOfInputs(nhood)
return compress_tree(list_of_nodes, n_states, numInputs, seqonly=False)
def optimise_tree(n_states, nhood, list_of_nodes):
......
......@@ -127,6 +127,42 @@ class RuleTree:
list_of_lines.append(' '.join(map(str,rule))+'\n')
return list_of_lines
def _treecompare(self, other, selfnode, othernode, memdict=set([])):
if (selfnode, othernode) in memdict:
return True
sn = self.seq[selfnode]
on = other.seq[othernode]
lev = sn[0]
if lev == 1:
return (sn == on)
for (sni, oni) in zip(sn[1:], on[1:]):
if not self._treecompare(other, sni, oni, memdict):
return False
memdict.add((selfnode, othernode))
return True
def __eq__(self, other):
'''
Extensional comparison for equality; assumes trees are optimised.
'''
if self.numInputs != other.numInputs:
return False
if self.numStates != other.numStates:
return False
return self._treecompare(other, self.curndd, other.curndd)
def __ne__(self, other):
return not self.__eq__(other)
def TransitionsToTree(n_states, nhood, transitions):
'''Convert a set of transitions directly to a rule tree.'''
......
......@@ -23,5 +23,10 @@ class TestTreeduce(unittest.TestCase):
self.assertEqual(ruletree2[1], [(-1, -1), (1, 1), (0, -1), (-1, 0), (1, 0), (0, 1), (0, 0), (0, 0)])
self.assertEqual(len(ruletree2[2]), 27)
rt = parsetree.make_tree(*ruletree)
rt2 = parsetree.make_tree(*ruletree)
self.assertEqual(rt, rt2)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment