/tutorial/backtracking/logigram/constraint_group.e

http://github.com/tybor/Liberty · Specman e · 142 lines · 92 code · 12 blank · 38 comment · 7 complexity · dc3e20ef1c07ad53ce251022665f52a0 MD5 · raw file

  1. -- See the Copyright notice at the end of this file.
  2. --
  3. class CONSTRAINT_GROUP
  4. --
  5. -- manages a group of constraints
  6. -- group of constraints shares the same variables
  7. --
  8. creation {ANY}
  9. make
  10. feature {ANY}
  11. var_set: HASHED_SET[ITEM_VAR]
  12. -- the variables of the group
  13. logic: LINKED_LIST[CONSTRAINT_LOGICAL]
  14. -- the logic constraints (equal, not equal, greater, lesser)
  15. couple: LINKED_LIST[CONSTRAINT_COUPLE]
  16. -- the couple constraints (yes, no)
  17. make is
  18. -- creation
  19. do
  20. create var_set.make
  21. create logic.make
  22. create couple.make
  23. end
  24. add (constraint: CONSTRAINT; set: HASHED_SET[ITEM_VAR]) is
  25. -- record the constraint and the set of variables
  26. local
  27. clog: CONSTRAINT_LOGICAL; cpl: CONSTRAINT_COUPLE
  28. do
  29. var_set.union(set)
  30. clog ?= constraint
  31. if clog /= Void then
  32. logic.add_last(clog)
  33. else
  34. cpl ?= constraint
  35. check
  36. cpl /= Void
  37. end
  38. couple.add_last(cpl)
  39. end
  40. end
  41. union (other: like Current) is
  42. -- add the data of other to current
  43. do
  44. var_set.union(other.var_set)
  45. logic.append_collection(other.logic)
  46. couple.append_collection(other.couple)
  47. end
  48. build_nodes (builder: MASK_BUILDER): BACKTRACKING_NODE is
  49. -- deduce backtracking graph from the current constraints
  50. do
  51. Result := get_node(builder, var_set.lower)
  52. end
  53. feature {}
  54. get_node (builder: MASK_BUILDER; ivar: INTEGER): BACKTRACKING_NODE is
  55. -- deduce backtracking graph from the current constraints
  56. -- ivar is the number of the var that will be evaluated
  57. do
  58. if ivar > var_set.upper then
  59. -- not a variable then check if configuration is allowed
  60. Result := get_node_if_true(builder)
  61. else
  62. -- loop on variable value
  63. Result := get_node_of_var(builder, ivar)
  64. end
  65. end
  66. get_node_of_var (builder: MASK_BUILDER; ivar: INTEGER): BACKTRACKING_NODE_OR_LIST is
  67. -- generate the alternative values of the variable ivar
  68. local
  69. v: ITEM_VAR; n: BACKTRACKING_NODE
  70. do
  71. v := var_set.item(ivar)
  72. from
  73. v.start
  74. until
  75. v.is_off
  76. loop
  77. n := get_node(builder, ivar + 1)
  78. if n /= Void then
  79. Result := create {BACKTRACKING_NODE_OR_LIST}.make(n, Result)
  80. end
  81. v.next
  82. end
  83. end
  84. get_node_if_true (builder: MASK_BUILDER): BACKTRACKING_NODE is
  85. local
  86. i: INTEGER
  87. do
  88. -- check if logic constraints are ok
  89. from
  90. i := logic.lower
  91. until
  92. i > logic.upper or else not logic.item(i).to_boolean
  93. loop
  94. i := i + 1
  95. end
  96. if i > logic.upper then
  97. -- logics constraints are ok
  98. -- then build the mask corresponding to the couple constraints
  99. builder.clear
  100. from
  101. i := couple.lower
  102. until
  103. i > couple.upper or else not builder.is_ok
  104. loop
  105. couple.item(i).build_masks(builder)
  106. i := i + 1
  107. end
  108. if builder.is_ok then
  109. -- possible solution
  110. Result := builder.get_node
  111. end
  112. end
  113. end
  114. end -- class CONSTRAINT_GROUP
  115. --
  116. -- ------------------------------------------------------------------------------------------------------------------------------
  117. -- Copyright notice below. Please read.
  118. --
  119. -- This file is free software, which comes along with SmartEiffel. This software is distributed in the hope that it will be
  120. -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  121. -- You can modify it as you want, provided this footer is kept unaltered, and a notification of the changes is added.
  122. -- You are allowed to redistribute it and sell it, alone or as a part of another product.
  123. --
  124. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  125. -- Copyright(C) 2003-2005: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  126. --
  127. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  128. --
  129. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  130. -- ------------------------------------------------------------------------------------------------------------------------------