| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : graph.tol |
|---|
| 4 | // PURPOSE: Defines NameBlock GraphTools |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 8 | NameBlock GraphTools = |
|---|
| 9 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 10 | [[ |
|---|
| 11 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 12 | Text _.autodoc.member.Append.Indexed = "Adds and index a new element to a " |
|---|
| 13 | "set and returns 0 if itŽs all right. If an element with given name exists " |
|---|
| 14 | "already then returns its index, else returns 0"; |
|---|
| 15 | Real Append.Indexed(Set set, Anything newElement, Text name) |
|---|
| 16 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 17 | { |
|---|
| 18 | Real idx = FindIndexByName(set, name ); |
|---|
| 19 | If(idx != 0, |
|---|
| 20 | { |
|---|
| 21 | WriteLn("[Graph::Append.Indexed] Cannot add an existant element " |
|---|
| 22 | "["<<idx+"]"+name,"E"); |
|---|
| 23 | 0 |
|---|
| 24 | }, |
|---|
| 25 | { |
|---|
| 26 | Set aux = [[ Copy(newElement) ]]; |
|---|
| 27 | Anything PutName(name, aux[1]); |
|---|
| 28 | Set Append(set, aux, True); |
|---|
| 29 | idx |
|---|
| 30 | }) |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | Class @Feature.Abstract |
|---|
| 35 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | { |
|---|
| 37 | Static Text _.autodoc.description = "Class for handling with abstract " |
|---|
| 38 | "features of arbitrary entities, numerical or not, that will be stored in " |
|---|
| 39 | "an external matrix with a row for each entity."; |
|---|
| 40 | //The name of the feature |
|---|
| 41 | Text _.name; |
|---|
| 42 | //The number of the feature in the full set of features |
|---|
| 43 | Real _.position; |
|---|
| 44 | |
|---|
| 45 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 46 | Static Text _.autodoc.member.get = "Returns the stored value corresponding " |
|---|
| 47 | "to this feature related to an individual entity."; |
|---|
| 48 | Anything get(Matrix store, Real entity); |
|---|
| 49 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 53 | Class @Feature.Numeric : @Feature.Abstract |
|---|
| 54 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 55 | { |
|---|
| 56 | Static Text _.autodoc.description = "Class for handling with numerical " |
|---|
| 57 | "numerical features of arbitrary entities that will be stored in an " |
|---|
| 58 | "external matrix with a row for each entity."; |
|---|
| 59 | |
|---|
| 60 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 61 | Static Text _.autodoc.member.New = "Creates a new instance of " |
|---|
| 62 | "Class @Feature.Numeric"; |
|---|
| 63 | Static @Feature.Numeric New(Text name, Real position) |
|---|
| 64 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 65 | { |
|---|
| 66 | @Feature.Numeric aux |
|---|
| 67 | [[ |
|---|
| 68 | Text _.name = name; |
|---|
| 69 | Real _.position = position |
|---|
| 70 | ]]; |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 74 | Anything get(Matrix value, Real entity) |
|---|
| 75 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 76 | { |
|---|
| 77 | MatDat(value, entity, _.position) |
|---|
| 78 | } |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 82 | Class @Feature.NonNumeric : @Feature.Abstract |
|---|
| 83 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 84 | { |
|---|
| 85 | Static Text _.autodoc.description = "Class for handling with numerical " |
|---|
| 86 | "numerical features of arbitrary entities that will be stored in an " |
|---|
| 87 | "external matrix with a row for each entity. Here the stored value is the " |
|---|
| 88 | "index of the feature stored in an arbitrary domain."; |
|---|
| 89 | |
|---|
| 90 | //The set of valid values for this feature indexed by name |
|---|
| 91 | Set _.domain; |
|---|
| 92 | |
|---|
| 93 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 94 | Static Text _.autodoc.member.New = "Creates a new instance of " |
|---|
| 95 | "Class @Feature.NonNumeric"; |
|---|
| 96 | Static @Feature.NonNumeric New(Text name, Real position, Set domain) |
|---|
| 97 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 98 | { |
|---|
| 99 | @Feature.NonNumeric aux = |
|---|
| 100 | [[ |
|---|
| 101 | Text _.name = name; |
|---|
| 102 | Real _.position = position; |
|---|
| 103 | Set _.domain = domain |
|---|
| 104 | ]]; |
|---|
| 105 | Real SetIndexByName(aux::_.domain); |
|---|
| 106 | aux |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 110 | Anything get(Matrix value, Real entity) |
|---|
| 111 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 112 | { |
|---|
| 113 | Real idx = MatDat(value, entity, _.position); |
|---|
| 114 | _.domain[idx] |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 120 | Class @FeatureSet |
|---|
| 121 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 122 | { |
|---|
| 123 | //Stores and index by name a set of instances od @Feature.Abstract |
|---|
| 124 | Set _.feature = Copy(Empty); |
|---|
| 125 | //In each row of this matrix are stored the values or indexes of features |
|---|
| 126 | //related to an individual entity |
|---|
| 127 | Matrix _.value = Rand(0,0,0,0); |
|---|
| 128 | |
|---|
| 129 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 130 | Static Text _.autodoc.member.New = "Creates a new instance of " |
|---|
| 131 | "Class @FeatureSet"; |
|---|
| 132 | Static @FeatureSet New(Real void) |
|---|
| 133 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 134 | { |
|---|
| 135 | @FeatureSet aux |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 139 | Static Text _.autodoc.member.add.feature = "Adds a new numerical feature"; |
|---|
| 140 | Real add.feature.numeric(Text name) |
|---|
| 141 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 142 | { |
|---|
| 143 | GraphTools::Append.Indexed( |
|---|
| 144 | _.feature, |
|---|
| 145 | @Feature.Numeric::New(name, Card(_.feature)), |
|---|
| 146 | name) |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 150 | Static Text _.autodoc.member.add.feature="Adds a new non numerical feature"; |
|---|
| 151 | Real add.feature.nonnumeric(Text name, Set domain) |
|---|
| 152 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 153 | { |
|---|
| 154 | GraphTools::Append.Indexed( |
|---|
| 155 | _.feature, |
|---|
| 156 | @Feature.NonNumeric::New(name, Card(_.feature), domain), |
|---|
| 157 | name) |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 161 | Text _.autodoc.member.feature.index = "Returns the index of a feature or 0 " |
|---|
| 162 | "if it not exists"; |
|---|
| 163 | Real feature.index(Text name) |
|---|
| 164 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 165 | { |
|---|
| 166 | FindIndexByName(_.feature, name ) |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 171 | Text _.autodoc.member.get = "Returns the value of a feature for an entity"; |
|---|
| 172 | Anything get(Real entity, |
|---|
| 173 | Anything feature) //Real position or Text name |
|---|
| 174 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 175 | { |
|---|
| 176 | (_.feature[feature])::get(_.value, entity) |
|---|
| 177 | }; |
|---|
| 178 | |
|---|
| 179 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 180 | Text _.autodoc.member.get.all = "Returns a set with the values of all " |
|---|
| 181 | "features for an entity"; |
|---|
| 182 | Set get.all(Real entity) |
|---|
| 183 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 184 | { |
|---|
| 185 | SetRow(EvalSet(_.feature, Anything(Real f) { get(entity, f) })) |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 192 | Class @Graph |
|---|
| 193 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 194 | { |
|---|
| 195 | Text _.autodoc.description = "Class for handling with generic indexed by " |
|---|
| 196 | "name graphs in TOL. In abstract, a graph is a pair of sets \n" |
|---|
| 197 | " V : a finite set of vertices \n" |
|---|
| 198 | " E : a finite set of edges relating an input vertex with an output \n"; |
|---|
| 199 | "Each vertex has a set of features related with the system\n" |
|---|
| 200 | "Each edge also has its own set of features.\n" |
|---|
| 201 | |
|---|
| 202 | //The identifier of the graph |
|---|
| 203 | Text _.name; |
|---|
| 204 | //Set of real numbers that will be indexed by name |
|---|
| 205 | Set _.vertex; |
|---|
| 206 | //Number of vertices |
|---|
| 207 | Real _.vertex.num = 0; |
|---|
| 208 | //Features of vertices |
|---|
| 209 | @FeatureSet _.vertex.feature = 0; |
|---|
| 210 | //Number of edges |
|---|
| 211 | Real _.edge.num = 0; |
|---|
| 212 | //Features of edges. First features must be input and output vertex |
|---|
| 213 | @FeatureSet _.edge.feature = 0; |
|---|
| 214 | //Sparse matrix storing index of edge for each cell (out,in) |
|---|
| 215 | VMatrix _.edge.map = Rand(0,0,0,0); |
|---|
| 216 | |
|---|
| 217 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 218 | Text _.autodoc.member.vertex.index = "Returns the index of a vertex or 0 " |
|---|
| 219 | "if it not exists"; |
|---|
| 220 | Real vertex.index(Text name) |
|---|
| 221 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 222 | { |
|---|
| 223 | FindIndexByName(_.vertex, name ) |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 227 | Text _.autodoc.member.vertex.add = "Adds and index a new vertex and " |
|---|
| 228 | "returns 0 if itŽs all right. If a vertex with given name exists already " |
|---|
| 229 | "then returns its index, else returns 0"; |
|---|
| 230 | Real vertex.add(Text name, Matrix feature.values) |
|---|
| 231 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 232 | { |
|---|
| 233 | Real n = Card(_.vertex); |
|---|
| 234 | Real ok = Graph::Append.Indexed(_.vertex,n,name); |
|---|
| 235 | Matrix _.vertex.feature := _.vertex.feature << feature.values |
|---|
| 236 | }; |
|---|
| 237 | |
|---|
| 238 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 239 | Text _.autodoc.member.edge.build = ""; |
|---|
| 240 | Real edge.add(Matrix feature.data) |
|---|
| 241 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 242 | { |
|---|
| 243 | Real c = Columns(data) |
|---|
| 244 | If(c<2, False, |
|---|
| 245 | { |
|---|
| 246 | _.edge.feature := SubCol(data,Range(3,c,1))); |
|---|
| 247 | True |
|---|
| 248 | }) |
|---|
| 249 | }; |
|---|
| 250 | |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | ]]; |
|---|