/tutorial/backtracking/logigram/constraint_set.e

http://github.com/tybor/Liberty · Specman e · 128 lines · 78 code · 8 blank · 42 comment · 6 complexity · 2bb032868761b7b15895cc7701b9c537 MD5 · raw file

  1. -- See the Copyright notice at the end of this file.
  2. --
  3. class CONSTRAINT_SET
  4. --
  5. -- the set of constraints
  6. --
  7. creation {ANY}
  8. make
  9. feature {ANY}
  10. groups: LINKED_LIST[CONSTRAINT_GROUP]
  11. -- the constraints that contains variable items
  12. -- where constraints are grouped around coupled variables
  13. unbound: CONSTRAINT_GROUP
  14. -- the constarints that are not bound to any variable
  15. make is
  16. -- creation
  17. do
  18. create groups.make
  19. create unbound.make
  20. create collector.make
  21. end
  22. add (constraint: CONSTRAINT) is
  23. -- add a constraint, taking care of where it must go:
  24. -- either in the unbound group or in one of the groups
  25. -- bound to variables
  26. local
  27. i: INTEGER; grp: CONSTRAINT_GROUP; added: BOOLEAN
  28. do
  29. -- Collects the items
  30. collector.clear
  31. constraint.get_items(collector)
  32. if collector.has_var then
  33. -- There is a variable.
  34. from
  35. i := groups.lower
  36. until
  37. i > groups.upper
  38. loop
  39. grp := groups.item(i)
  40. if not grp.var_set.is_disjoint_from(collector.var_set) then
  41. -- grp is the first group that contains one of the
  42. -- variables of the constraint.
  43. -- Add the constraint to the group
  44. grp.add(constraint, collector.var_set)
  45. added := True
  46. i := i + 1
  47. -- Checks is further groups intersect the new one.
  48. from
  49. until
  50. i > groups.upper
  51. loop
  52. if grp.var_set.is_disjoint_from(groups.item(i).var_set) then
  53. i := i + 1
  54. else
  55. -- Merge of the two intersecting groups
  56. grp.union(groups.item(i))
  57. groups.remove(i)
  58. end
  59. end
  60. check
  61. i > groups.upper -- obvious, the main loop stops
  62. end
  63. else
  64. i := i + 1
  65. end
  66. end
  67. if not added then
  68. -- When not added, the constraint begins a new group.
  69. create grp.make
  70. grp.add(constraint, collector.var_set)
  71. groups.add_last(grp)
  72. end
  73. else
  74. -- No variables, adds to the unbound group of constraints.
  75. unbound.add(constraint, collector.var_set)
  76. end
  77. end
  78. build_nodes (builder: MASK_BUILDER): BACKTRACKING_NODE_AND_LIST is
  79. -- Creates the backtracking tree corresponding to the constraints.
  80. local
  81. i: INTEGER; n: BACKTRACKING_NODE
  82. do
  83. -- The creation begins with variable groups.
  84. from
  85. i := groups.lower
  86. until
  87. i > groups.upper
  88. loop
  89. n := groups.item(i).build_nodes(builder)
  90. if n /= Void then
  91. Result := create {BACKTRACKING_NODE_AND_LIST}.make(n, Result)
  92. end
  93. i := i + 1
  94. end
  95. -- Creation of the not variable group.
  96. -- Note: made after to go before in the list.
  97. n := unbound.build_nodes(builder)
  98. if n /= Void then
  99. Result := create {BACKTRACKING_NODE_AND_LIST}.make(n, Result)
  100. end
  101. end
  102. collector: ITEM_COLLECTOR
  103. -- common collector of items
  104. end -- class CONSTRAINT_SET
  105. --
  106. -- ------------------------------------------------------------------------------------------------------------------------------
  107. -- Copyright notice below. Please read.
  108. --
  109. -- This file is free software, which comes along with SmartEiffel. This software is distributed in the hope that it will be
  110. -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  111. -- You can modify it as you want, provided this footer is kept unaltered, and a notification of the changes is added.
  112. -- You are allowed to redistribute it and sell it, alone or as a part of another product.
  113. --
  114. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  115. -- Copyright(C) 2003-2005: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  116. --
  117. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  118. --
  119. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  120. -- ------------------------------------------------------------------------------------------------------------------------------