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