validation.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. from parser import Parser
  16. from possibilities import apply_suggestion
  17. from strategy import find_possibilities
  18. from tests.parser import ParserWrapper
  19. VALIDATE_FAILURE = 0
  20. VALIDATE_NOPROGRESS = 1
  21. VALIDATE_SUCCESS = 2
  22. VALIDATE_ERROR = 3
  23. def validate(a, b):
  24. """
  25. Validate that a =>* b.
  26. """
  27. parser = ParserWrapper(Parser)
  28. # Parse both expressions
  29. a = parser.run([a])
  30. b = parser.run([b])
  31. # Evaluate a and b, counting the number of steps
  32. parser.set_root_node(a)
  33. A, a_steps = parser.rewrite_and_count_all()
  34. if not a:
  35. return VALIDATE_ERROR
  36. parser.set_root_node(b)
  37. B, b_steps = parser.rewrite_and_count_all()
  38. if not B:
  39. return VALIDATE_ERROR
  40. # Evaluations must be equal
  41. if not A.equals(B):
  42. return VALIDATE_FAILURE
  43. # If evaluation of b took more staps than evaluation of a, the step from a
  44. # to b was probably useless or even bad
  45. if b_steps >= a_steps:
  46. return VALIDATE_NOPROGRESS
  47. # Evaluations match and b is evaluated quicker than a => success
  48. return VALIDATE_SUCCESS