1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
// Copyright (C) 2017 Kisio Digital and/or its affiliates. // // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU Affero General Public License as published by the // Free Software Foundation, version 3. // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more // details. // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/> //! Modeling the relations between objects. //! //! This module defines types for modeling the relations between //! objects, and use them thanks to the `GetCorresponding` custom //! derive. //! //! Let's clarify that with an example. Suppose that `Bike`s have a //! `Brand`. `Bike`s also have an `Owner`, and these `Owner`s have a //! `Job`. `Bike`s also have a `Kind`. //! //! ```raw //! Brand - Bike - Owner - Job //! | //! Kind //! ``` //! //! Let's defines these relations and use them a bit: //! //! ```no_run //! # use transit_model_procmacro::*; //! # use transit_model_relations::*; //! # use typed_index_collection::Idx; //! # struct Bike; //! # struct Brand; //! # struct Owner; //! # struct Job; //! # struct Kind; //! # fn get_mbk_brand() -> Idx<Brand> { unimplemented!() } //! #[derive(Default, GetCorresponding)] //! pub struct World { //! brands_to_bikes: OneToMany<Brand, Bike>, //! owners_to_bikes: OneToMany<Owner, Bike>, //! jobs_to_owners: OneToMany<Job, Owner>, //! kinds_to_bikes: OneToMany<Kind, Bike>, //! } //! let world = World::default(); //! let mbk: Idx<Brand> = get_mbk_brand(); //! let owners_with_mbk: IdxSet<Owner> = world.get_corresponding_from_idx(mbk); //! let jobs_with_mbk: IdxSet<Job> = world.get_corresponding(&owners_with_mbk); //! println!( //! "{} owners with {} different jobs own a bike of the brand MBK.", //! owners_with_mbk.len(), //! jobs_with_mbk.len() //! ); //! ``` //! //! First, we want to model the relations between the object. One bike //! has a brand, and a brand has several bikes (hopefully). Thus, we //! use a `OneToMany<Bike, Brand>` to model this relation. //! //! We repeat this process to model every relation. We obtain without //! too much effort the `World` struct. //! //! The `GetCorresponding` derive looks at each field of the `World` //! struct, keeping the fields containing `_to_` with a type with 2 //! generics, and interpret that as a relation. For example, //! `bikes_to_brands: OneToMany<Bike, Brand>` is a relation between //! `Bike` and `Brand`. Using all the relations, it generates a graph, //! compute the shortest path between all the types, and generate an //! `impl GetCorresponding` for each feasible path. //! //! These `impl GetCorresponding` are used by //! `World::get_corresponding_from_idx` and `World::get_corresponding` //! that are helpers to explore the `World`. //! //! Thus, when we call `world.get_corresponding_from_idx(mbk)` for //! `Owner`, we will use the generated code that, basically, gets all //! the `Bike`s corresponding to the `Brand` MBK, and then gets all //! the `Owner`s corresponding to these `Bike`s. //! //! Imagine that, in our application, we use a lot the `Owner->Kind` //! and `Brand->Kind` search. To do these searches, we pass by //! `Bike`, and there is a lot of `Bike`s in our model. Thus, as an //! optimization, we want to precompute these relations. //! //! ```raw //! Brand - Bike - Owner - Job //! \ | / //! `-- Kind --' //! ``` //! //! The shortcuts `Brand - Kind` and `Kind - Owner` allow our //! optimization, but we now have a problem for the `Owner->Brand` //! search: we can do `Owner->Kind->Brand` and `Owner->Bike->Brand` //! with a cost of 2. The first solution is clearly wrong, introduced //! by our shortcuts. To fix this problem, we can put a weight of 1.9 //! on `Brand - Kind` and `Kind - Owner`. The path //! `Owner->Kind->Brand` now cost 3.8 and is discarded. //! //! Let's implement that: //! //! ``` //! # use transit_model_procmacro::*; //! # use transit_model_relations::*; //! # use typed_index_collection::Idx; //! # struct Bike; //! # struct Brand; //! # struct Owner; //! # struct Job; //! # struct Kind; //! # fn get_mbk_brand() -> Idx<Brand> { unimplemented!() } //! #[derive(GetCorresponding)] //! pub struct World { //! brands_to_bikes: OneToMany<Brand, Bike>, //! owners_to_bikes: OneToMany<Owner, Bike>, //! jobs_to_owners: OneToMany<Job, Owner>, //! kinds_to_bikes: OneToMany<Kind, Bike>, //! //! // shortcuts //! #[get_corresponding(weight = "1.9")] //! brands_to_kinds: ManyToMany<Brand, Kind>, //! #[get_corresponding(weight = "1.9")] //! kinds_to_owners: ManyToMany<Kind, Owner>, //! } //! # fn create_brands_to_bikes() -> OneToMany<Brand, Bike> { unimplemented!() } //! # fn create_owners_to_bikes() -> OneToMany<Owner, Bike> { unimplemented!() } //! # fn create_jobs_to_owners() -> OneToMany<Job, Owner> { unimplemented!() } //! # fn create_kinds_to_bikes() -> OneToMany<Kind, Bike> { unimplemented!() } //! impl World { //! fn new() -> World { //! let brands_to_bikes = create_brands_to_bikes(); //! let owners_to_bikes = create_owners_to_bikes(); //! let jobs_to_owners = create_jobs_to_owners(); //! let kinds_to_bikes = create_kinds_to_bikes(); //! World { //! brands_to_kinds: ManyToMany::from_relations_sink( //! &brands_to_bikes, //! &kinds_to_bikes, //! ), //! kinds_to_owners: ManyToMany::from_relations_sink( //! &kinds_to_bikes, //! &owners_to_bikes, //! ), //! brands_to_bikes, //! owners_to_bikes, //! jobs_to_owners, //! kinds_to_bikes, //! } //! } //! } //! ``` /// The error type used by the crate. pub type Error = failure::Error; /// The corresponding result type used by the crate. pub type Result<T> = std::result::Result<T, Error>; use typed_index_collection::{CollectionWithId, Id, Idx}; use derivative::Derivative; use failure::{format_err, ResultExt}; use std::collections::{BTreeMap, BTreeSet}; /// A set of `Idx<T>` pub type IdxSet<T> = BTreeSet<Idx<T>>; /// An object linking 2 types together. pub trait Relation { /// The type of the source object type From; /// The type of the targer object type To; /// Returns the complete set of the source objects. fn get_from(&self) -> IdxSet<Self::From>; /// Returns the complete set of the target objects. fn get_to(&self) -> IdxSet<Self::To>; /// For a given set of the source objects, returns the /// corresponding targets objects. fn get_corresponding_forward(&self, from: &IdxSet<Self::From>) -> IdxSet<Self::To>; /// For a given set of the target objects, returns the /// corresponding source objects. fn get_corresponding_backward(&self, from: &IdxSet<Self::To>) -> IdxSet<Self::From>; } /// A one to many relation, i.e. to one `T` corresponds many `U`, /// and a `U` has one corresponding `T`. #[derive(Derivative, Debug)] #[derivative(Default(bound = ""))] pub struct OneToMany<T, U> { one_to_many: BTreeMap<Idx<T>, IdxSet<U>>, many_to_one: BTreeMap<Idx<U>, Idx<T>>, } impl<T, U> OneToMany<T, U> where T: Id<T>, U: Id<U> + Id<T>, { fn new_impl(one: &CollectionWithId<T>, many: &CollectionWithId<U>) -> Result<Self> { let mut one_to_many = BTreeMap::default(); let mut many_to_one = BTreeMap::default(); for (many_idx, obj) in many { let one_id = <U as Id<T>>::id(obj); let one_idx = one .get_idx(one_id) .ok_or_else(|| format_err!("id={:?} not found", one_id))?; many_to_one.insert(many_idx, one_idx); one_to_many .entry(one_idx) .or_insert_with(IdxSet::default) .insert(many_idx); } Ok(OneToMany { one_to_many, many_to_one, }) } /// Construct the relation automatically from the 2 given /// `CollectionWithId`s. pub fn new( one: &CollectionWithId<T>, many: &CollectionWithId<U>, rel_name: &str, ) -> Result<Self> { Ok(Self::new_impl(one, many).with_context(|_| format!("Error indexing {}", rel_name))?) } } impl<T, U> Relation for OneToMany<T, U> { type From = T; type To = U; fn get_from(&self) -> IdxSet<T> { self.one_to_many.keys().cloned().collect() } fn get_to(&self) -> IdxSet<U> { self.many_to_one.keys().cloned().collect() } fn get_corresponding_forward(&self, from: &IdxSet<T>) -> IdxSet<U> { get_corresponding(&self.one_to_many, from) } fn get_corresponding_backward(&self, from: &IdxSet<U>) -> IdxSet<T> { from.iter() .filter_map(|from_idx| self.many_to_one.get(from_idx)) .cloned() .collect() } } /// A many to many relation, i.e. a `T` can have multiple `U`, and /// vice versa. #[derive(Default, Debug)] pub struct ManyToMany<T, U> { forward: BTreeMap<Idx<T>, IdxSet<U>>, backward: BTreeMap<Idx<U>, IdxSet<T>>, } impl<T, U> ManyToMany<T, U> { /// Constructor from the forward relation. pub fn from_forward(forward: BTreeMap<Idx<T>, IdxSet<U>>) -> Self { let mut backward = BTreeMap::default(); forward .iter() .flat_map(|(&from_idx, obj)| obj.iter().map(move |&to_idx| (from_idx, to_idx))) .for_each(|(from_idx, to_idx)| { backward .entry(to_idx) .or_insert_with(IdxSet::default) .insert(from_idx); }); ManyToMany { forward, backward } } /// Constructor from 2 chained relations, i.e. from the relations /// `A->B` and `B->C`, constructs the relation `A->C`. pub fn from_relations_chain<R1, R2>(r1: &R1, r2: &R2) -> Self where R1: Relation<From = T>, R2: Relation<From = R1::To, To = U>, { let forward = r1 .get_from() .into_iter() .map(|idx| { let from = Some(idx).into_iter().collect(); let tmp = r1.get_corresponding_forward(&from); (idx, r2.get_corresponding_forward(&tmp)) }) .collect(); Self::from_forward(forward) } /// Constructor from 2 relations with a common sink, i.e. from the /// relations `A->B` and `C->B`, constructs the relation `A->C`. pub fn from_relations_sink<R1, R2>(r1: &R1, r2: &R2) -> Self where R1: Relation<From = T>, R2: Relation<From = U, To = R1::To>, { let forward = r1 .get_from() .into_iter() .map(|idx| { let from = Some(idx).into_iter().collect(); let tmp = r1.get_corresponding_forward(&from); (idx, r2.get_corresponding_backward(&tmp)) }) .collect(); Self::from_forward(forward) } /// Constructor from 2 relations with a common source, i.e. from /// the relations `B->A` and `B->C`, constructs the relation /// `A->C`. pub fn from_relations_source<R1, R2>(r1: &R1, r2: &R2) -> Self where R1: Relation<To = T>, R2: Relation<From = R1::From, To = U>, { let forward = r1 .get_to() .into_iter() .map(|idx| { let from = Some(idx).into_iter().collect(); let tmp = r1.get_corresponding_backward(&from); (idx, r2.get_corresponding_forward(&tmp)) }) .collect(); Self::from_forward(forward) } } impl<T, U> Relation for ManyToMany<T, U> { type From = T; type To = U; fn get_from(&self) -> IdxSet<T> { self.forward.keys().cloned().collect() } fn get_to(&self) -> IdxSet<U> { self.backward.keys().cloned().collect() } fn get_corresponding_forward(&self, from: &IdxSet<T>) -> IdxSet<U> { get_corresponding(&self.forward, from) } fn get_corresponding_backward(&self, from: &IdxSet<U>) -> IdxSet<T> { get_corresponding(&self.backward, from) } } fn get_corresponding<T, U>(map: &BTreeMap<Idx<T>, IdxSet<U>>, from: &IdxSet<T>) -> IdxSet<U> { from.iter() .filter_map(|from_idx| map.get(from_idx)) .flat_map(|indices| indices.iter().cloned()) .collect() }