validation.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from parser import Parser
  2. from possibilities import apply_suggestion
  3. from strategy import find_possibilities
  4. from tests.parser import ParserWrapper
  5. def validate(exp, result):
  6. """
  7. Validate that exp =>* result.
  8. """
  9. parser = ParserWrapper(Parser)
  10. exp = parser.run([exp])
  11. result = parser.run([result])
  12. # Compare the simplified expressions first, in order to avoid the
  13. # computational intensive traversal of the possibilities tree.
  14. parser.set_root_node(exp)
  15. a = parser.rewrite_all()
  16. parser.set_root_node(result)
  17. b = parser.rewrite_all()
  18. if not a or not a.equals(b):
  19. return False
  20. # TODO: make sure cycles are avoided / eliminated using cycle detection.
  21. def traverse_preorder(node, result):
  22. if node.equals(result):
  23. return True
  24. for p in find_possibilities(node):
  25. # Clone the root node because it will be used in multiple
  26. # substitutions
  27. child = apply_suggestion(node.clone(), p)
  28. if traverse_preorder(child, result):
  29. return True
  30. return False
  31. return traverse_preorder(exp, result)