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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use std::collections::{HashMap, HashSet};

use mercator_db::space;
use mercator_db::Core;
use mercator_db::CoreQueryParameters;
use mercator_db::Properties;

use super::expressions::*;
use super::symbols::*;

impl From<&LiteralPosition> for space::Position {
    fn from(literal: &LiteralPosition) -> Self {
        let v: Vec<f64> = literal.into();
        v.into()
    }
}

impl From<&LiteralNumber> for space::Coordinate {
    fn from(literal: &LiteralNumber) -> Self {
        match literal {
            LiteralNumber::Float(f) => (*f).into(),
            LiteralNumber::Int(i) => (*i as u64).into(),
        }
    }
}

fn complement_helper<'c>(
    core: &'c Core,
    parameters: &CoreQueryParameters<'c>,
    space_id: &str,
    inside: Vec<(&'c String, Vec<(space::Position, &'c Properties)>)>,
) -> mercator_db::ResultSet<'c> {
    let (low, high) = parameters.db.space(space_id)?.bounding_box();
    match core.get_by_shape(parameters, &space::Shape::BoundingBox(low, high), space_id) {
        e @ Err(_) => e,
        Ok(points) => {
            let hashmap = inside.into_iter().collect::<HashMap<_, _>>();

            Ok(points
                .into_iter()
                .filter_map(|(space, v)| match hashmap.get(space) {
                    None => None,
                    Some(list) => {
                        Some((space, v.into_iter().filter(|t| !list.contains(t)).collect()))
                    }
                })
                .collect::<Vec<_>>())
        }
    }
}

fn view_port<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    bag: &Bag,
) -> mercator_db::ResultSet<'c> {
    if let Some((low, high)) = parameters.view_port {
        let vp = Bag::Inside(Shape::HyperRectangle(
            bag.space().clone(),
            vec![low.into(), high.into()],
        ));
        intersection(core_id, parameters, &vp, bag)
    } else {
        bag.execute(core_id, parameters)
    }
}

fn distinct<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    bag: &Bag,
) -> mercator_db::ResultSet<'c> {
    match bag.execute(core_id, parameters) {
        e @ Err(_) => e,
        Ok(mut v) => {
            let set: HashSet<_> = v.drain(..).collect(); // dedup
            v.extend(set.into_iter());

            Ok(v)
        }
    }
}
fn filter_helper<'c>(
    predicate: &Predicate,
    bag: &Bag,
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
) -> mercator_db::ResultSet<'c> {
    match bag.execute(core_id, parameters) {
        e @ Err(_) => e,
        Ok(results) => Ok(results
            .into_iter()
            .filter_map(|(space, positions)| {
                let filtered = positions
                    .into_iter()
                    .filter(|(position, properties)| predicate.eval((space, position, properties)))
                    .collect::<Vec<_>>();
                if filtered.is_empty() {
                    None
                } else {
                    Some((space, filtered))
                }
            })
            .collect::<Vec<_>>()),
    }
}

fn filter<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    predicate: &Option<Predicate>,
    bag: &Option<Box<Bag>>,
) -> mercator_db::ResultSet<'c> {
    match predicate {
        None => {
            if let Some(bag) = bag {
                bag.execute(core_id, parameters)
            } else {
                Err("Filter without predicate nor data set.".to_string())
            }
        }
        Some(predicate) => match bag {
            None => {
                let (low, high) = space::Space::universe().bounding_box();
                let low: Vec<_> = low.into();
                let high: Vec<_> = high.into();
                let shape = Shape::HyperRectangle(
                    space::Space::universe().name().clone(),
                    vec![
                        LiteralPosition(
                            low.into_iter()
                                .map(LiteralNumber::Float)
                                .collect::<Vec<_>>(),
                        ),
                        LiteralPosition(
                            high.into_iter()
                                .map(LiteralNumber::Float)
                                .collect::<Vec<_>>(),
                        ),
                    ],
                );
                filter_helper(predicate, &Bag::Inside(shape), core_id, parameters)
            }
            Some(bag) => filter_helper(predicate, bag.as_ref(), core_id, parameters),
        },
    }
}

fn complement<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    core: &'c Core,
    bag: &Bag,
) -> mercator_db::ResultSet<'c> {
    match bag.execute(core_id, parameters) {
        // FIXME: The complement of a set is computed within its definition space.
        e @ Err(_) => e,
        Ok(inside) => complement_helper(
            core,
            parameters,
            mercator_db::space::Space::universe().name(),
            inside,
        ),
    }
}

fn intersection<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    rh: &Bag,
    lh: &Bag,
) -> mercator_db::ResultSet<'c> {
    let l = lh.execute(core_id, parameters);
    if let Ok(l) = l {
        let r = rh.execute(core_id, parameters);
        if let Ok(r) = r {
            let mut v = vec![];

            if rh.predict(parameters.db) < lh.predict(parameters.db) {
                for o in r {
                    if l.contains(&o) {
                        v.push(o);
                    }
                }
            } else {
                for o in l {
                    if r.contains(&o) {
                        v.push(o);
                    }
                }
            }
            Ok(v)
        } else {
            r
        }
    } else {
        l
    }
}

fn union<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    rh: &Bag,
    lh: &Bag,
) -> mercator_db::ResultSet<'c> {
    let l = lh.execute(core_id, parameters);
    if let Ok(mut l) = l {
        let r = rh.execute(core_id, parameters);
        if let Ok(mut r) = r {
            if rh.predict(parameters.db) < lh.predict(parameters.db) {
                l.append(&mut r);
                Ok(l)
            } else {
                r.append(&mut l);
                Ok(r)
            }
        } else {
            r
        }
    } else {
        l
    }
}

fn bag<'c>(
    core_id: &str,
    parameters: &CoreQueryParameters<'c>,
    bags: &[Bag],
) -> mercator_db::ResultSet<'c> {
    let mut v = vec![];
    for bag in bags {
        let b = bag.execute(core_id, parameters);
        match b {
            e @ Err(_) => {
                return e;
            }
            Ok(mut b) => {
                v.append(&mut b);
            }
        }
    }

    Ok(v)
}

fn inside<'c>(
    parameters: &CoreQueryParameters<'c>,
    core: &'c Core,
    shape: &Shape,
) -> mercator_db::ResultSet<'c> {
    let db = parameters.db;
    let param = match shape {
        Shape::Point(space_id, position) => {
            let space = db.space(space_id)?;
            let position: Vec<f64> = position.into();
            let position = space.encode(&position)?;
            Ok((space_id, space::Shape::Point(position)))
        }
        Shape::HyperRectangle(space_id, bounding_box) => {
            if bounding_box.len() != 2 {
                Err("The number of position is different from 2, which is unsupported.".to_string())
            } else {
                let space = db.space(space_id)?;
                let low: Vec<f64> = (&bounding_box[0]).into();
                let high: Vec<f64> = (&bounding_box[1]).into();
                let low = space.encode(&low)?;
                let high = space.encode(&high)?;

                Ok((space_id, space::Shape::BoundingBox(low, high)))
            }
        }
        Shape::HyperSphere(space_id, position, radius) => {
            let space = db.space(space_id)?;
            let position: Vec<f64> = position.into();
            let position = space.encode(&position)?;
            let mut r = vec![];
            for _ in 0..position.dimensions() {
                r.push(radius.into());
            }
            let radius = space.encode(&r)?[0];

            //FIXME: RADIUS IS A LENGTH, HOW TO ENCODE IT INTO THE SPACE?
            Ok((space_id, space::Shape::HyperSphere(position, radius)))
        }
        Shape::Label(_, id) => {
            // Not a real shape, so short circuit and return.
            return core.get_by_label(parameters, id);
        }
        Shape::Nifti(_space_id) => Err("Inside-Nifti: not yet implemented".to_string()),
    };

    match param {
        Ok((space_id, shape)) => core.get_by_shape(parameters, &shape, space_id),
        Err(e) => Err(e),
    }
}

fn outside<'c>(
    parameters: &CoreQueryParameters<'c>,
    core: &'c Core,
    shape: &Shape,
) -> mercator_db::ResultSet<'c> {
    match shape {
        Shape::Point(space_id, position) => {
            let position: Vec<f64> = position.into();
            match core.get_by_positions(parameters, &[position.into()], space_id) {
                e @ Err(_) => e,
                Ok(inside) => complement_helper(core, parameters, space_id, inside),
            }
        }
        Shape::HyperRectangle(space_id, bounding_box) => {
            // We need to adapt the bounding_box to ensure the
            // surface will not hit as part of the inside set, so we
            // compute the biggest bounding box contained within the
            // given box.

            // Smallest increment possible
            let mut increment = Vec::with_capacity(bounding_box[0].dimensions());
            for _ in 0..bounding_box[0].dimensions() {
                increment.push(std::f64::EPSILON);
            }

            // Add it to the lower bound
            let mut low: space::Position = (&bounding_box[0]).into();
            low += increment.clone().into();

            // Substract it from the upper bound
            let mut high: space::Position = (&bounding_box[1]).into();
            high -= increment.into();

            match core.get_by_shape(parameters, &space::Shape::BoundingBox(low, high), space_id) {
                e @ Err(_) => e,
                Ok(inside) => complement_helper(core, parameters, space_id, inside),
            }
        }
        Shape::HyperSphere(space_id, center, radius) => {
            // Smallest decrement possible, to exclude the surface
            let mut radius: f64 = radius.into();
            radius -= std::f64::EPSILON;
            let center: space::Position = center.into();

            match core.get_by_shape(
                parameters,
                &space::Shape::HyperSphere(center, radius.into()),
                space_id,
            ) {
                e @ Err(_) => e,
                Ok(inside) => complement_helper(core, parameters, space_id, inside),
            }
        }
        Shape::Label(_, _) => Err("Label: not yet implemented".to_string()),
        Shape::Nifti(_space_id) => Err("Outside-nifti: not yet implemented".to_string()),
    }
}

impl<'e> Executor<'e> for Projection {
    type ResultSet = mercator_db::ResultSet<'e>;

    fn execute<'f: 'e>(
        &self,
        core_id: &str,
        parameters: &CoreQueryParameters<'f>,
    ) -> Self::ResultSet {
        match self {
            Projection::Nifti(_, _, _bag) => Err("Proj-Nifti: not yet implemented".to_string()),
            Projection::JSON(_, _format, bag) => {
                bag.execute(core_id, parameters)
                // FIXME: Add projections here
            }
        }
    }
}

impl<'e> Executor<'e> for Bag {
    type ResultSet = mercator_db::ResultSet<'e>;

    fn execute<'f: 'e>(
        &self,
        core_id: &str,
        parameters: &CoreQueryParameters<'f>,
    ) -> Self::ResultSet {
        let core = parameters.db.core(core_id)?;

        match self {
            Bag::ViewPort(bag) => view_port(core_id, parameters, bag),
            Bag::Distinct(bag) => distinct(core_id, parameters, bag),
            Bag::Filter(predicate, bag) => filter(core_id, parameters, predicate, bag),
            Bag::Complement(bag) => complement(core_id, parameters, core, bag),
            Bag::Intersection(lh, rh) => intersection(core_id, parameters, rh, lh),
            Bag::Union(lh, rh) => union(core_id, parameters, rh, lh),
            Bag::Bag(list) => bag(core_id, parameters, list),
            Bag::Inside(shape) => inside(parameters, core, shape),
            Bag::Outside(shape) => {
                //FIXME: This is currently computed as the complement of the values within the shape, except its surface.
                //       Should this be instead a list of positions within the shape?
                //FIXME: Should we use the Shape's Space to get the maximum bounds or the output Space requested?
                outside(parameters, core, shape)
            }
        }
    }
}