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