/src/lib/backtracking/backtracking.e

http://github.com/tybor/Liberty · Specman e · 123 lines · 59 code · 10 blank · 54 comment · 0 complexity · 09f5c509cf560fee8c38957d23844101 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class BACKTRACKING
  5. --
  6. -- This class is intended to explore structures that matches the and/or
  7. -- graph of BACKTRACKING_NODE. The alternatives have a context that
  8. -- gotten and restored using the features 'get_context' and 'restore_context'.
  9. --
  10. -- See tutorial/backtracking for examples.
  11. --
  12. -- The instances of the BACKTRACKING children are typically
  13. -- used through lines like the following ones that enumerate the
  14. -- solutions:
  15. --
  16. -- from
  17. -- set_current_node(root)
  18. -- search_first
  19. -- until
  20. -- is_off
  21. -- loop
  22. -- ... -- do something
  23. -- search_next
  24. -- end
  25. --
  26. -- These features are declared to be bound to ANY but don't hesitate to
  27. -- change the type of the context to what your context is.
  28. --
  29. inherit
  30. ABSTRACT_BACKTRACKING
  31. insert
  32. BACKTRACKING_GLOBALS
  33. feature {ANY}
  34. set_current_node (node: like current_node)
  35. -- Sets the next node of the BACKTRACKING_NODE graph to evaluate.
  36. do
  37. current_node := node
  38. ensure
  39. definition: current_node = node
  40. end
  41. push_and (node: BACKTRACKING_NODE)
  42. -- Pushes `node' in front of the continuation path.
  43. require
  44. node_not_void: node /= Void
  45. local
  46. sequence: BACKTRACKING_SEQUENCE
  47. do
  48. sequence := pool_of_sequence.get_instance
  49. sequence.set_next(node)
  50. push_sequence(sequence)
  51. end
  52. push_and_list (list: BACKTRACKING_NODE_AND_LIST)
  53. -- Pushes `list' in front of the continuation path.
  54. require
  55. list_not_void: list /= Void
  56. local
  57. sequence: BACKTRACKING_SEQUENCE_LIST
  58. do
  59. sequence := pool_of_sequence_list.get_instance
  60. sequence.set_list(list)
  61. push_sequence(sequence)
  62. end
  63. push_or (node: BACKTRACKING_NODE)
  64. -- Pushes `node' in front of the possible alternatives.
  65. require
  66. node_not_void: node /= Void
  67. local
  68. alternative: BACKTRACKING_ALTERNATIVE
  69. do
  70. alternative := pool_of_alternative.get_instance
  71. alternative.set_next(node)
  72. push_alternative(alternative)
  73. end
  74. push_or_list (list: BACKTRACKING_NODE_OR_LIST)
  75. -- Pushes `list' in front of the possible alternatives.
  76. require
  77. list_not_void: list /= Void
  78. local
  79. alternative: BACKTRACKING_ALTERNATIVE_LIST
  80. do
  81. alternative := pool_of_alternative_list.get_instance
  82. alternative.set_list(list)
  83. push_alternative(alternative)
  84. end
  85. feature {} -- internal
  86. current_node: BACKTRACKING_NODE
  87. -- Current node of the BACKTRACKING_NODE graph to be evaluated.
  88. evaluate_current_state
  89. -- That feature is called to evaluate the current state
  90. do
  91. current_node.explore(Current)
  92. end
  93. end -- class BACKTRACKING
  94. --
  95. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  96. --
  97. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  98. -- of this software and associated documentation files (the "Software"), to deal
  99. -- in the Software without restriction, including without limitation the rights
  100. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  101. -- copies of the Software, and to permit persons to whom the Software is
  102. -- furnished to do so, subject to the following conditions:
  103. --
  104. -- The above copyright notice and this permission notice shall be included in
  105. -- all copies or substantial portions of the Software.
  106. --
  107. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  108. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  109. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  110. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  111. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  112. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  113. -- THE SOFTWARE.