summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/compound_expression.cpp
blob: bcde09e1b679c1bb034e75588c33f57fc1427bc1 (plain)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include <mbgl/style/expression/compound_expression.hpp>
#include <mbgl/style/expression/check_subtype.hpp>
#include <mbgl/style/expression/util.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/util/i18n.hpp>
#include <mbgl/util/ignore.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/platform.hpp>
#include <cmath>

namespace mbgl {
namespace style {
namespace expression {

namespace detail {

/*
    The Signature<Fn> structs are wrappers around an "evaluate()" function whose
    purpose is to extract the necessary Type data from the evaluate function's
    type.  There are three key (partial) specializations:
    
    Signature<R (Params...)>:
    Wraps a simple evaluate function (const T0&, const T1&, ...) -> Result<U>
 
    Signature<R (const Varargs<T>&)>:
    Wraps an evaluate function that takes an arbitrary number of arguments (via
    a Varargs<T>, which is just an alias for std::vector).
 
    Signature<R (const EvaluationContext&, Params...)>:
    Wraps an evaluate function that needs to access the expression evaluation
    parameters in addition to its subexpressions, i.e.,
    (const EvaluationParams& const T0&, const T1&, ...) -> Result<U>.  Needed
    for expressions like ["zoom"], ["get", key], etc.
    
    In each of the above evaluate signatures, T0, T1, etc. are the types of
    the successfully evaluated subexpressions.
*/
template <class, class Enable = void>
struct Signature;

// Simple evaluate function (const T0&, const T1&, ...) -> Result<U>
template <class R, class... Params>
struct Signature<R (Params...)> : SignatureBase {
    using Args = std::array<std::unique_ptr<Expression>, sizeof...(Params)>;
    
    Signature(R (*evaluate_)(Params...), std::string name_) :
        SignatureBase(
            valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
            std::vector<type::Type> {valueTypeToExpressionType<std::decay_t<Params>>()...},
            std::move(name_)
        ),
        evaluate(evaluate_)    {}
    
    EvaluationResult apply(const EvaluationContext& evaluationParameters, const Args& args) const {
        return applyImpl(evaluationParameters, args, std::index_sequence_for<Params...>{});
    }
    
    std::unique_ptr<Expression> makeExpression(std::vector<std::unique_ptr<Expression>> args) const override {
        typename Signature::Args argsArray;
        std::copy_n(std::make_move_iterator(args.begin()), sizeof...(Params), argsArray.begin());
        return std::make_unique<CompoundExpression<Signature>>(name, *this, std::move(argsArray));
    }

    R (*evaluate)(Params...);
private:
    template <std::size_t ...I>
    EvaluationResult applyImpl(const EvaluationContext& evaluationParameters, const Args& args, std::index_sequence<I...>) const {
        const std::array<EvaluationResult, sizeof...(I)> evaluated = {{std::get<I>(args)->evaluate(evaluationParameters)...}};
        for (const auto& arg : evaluated) {
            if(!arg) return arg.error();
        }
        const R value = evaluate(*fromExpressionValue<std::decay_t<Params>>(*(evaluated[I]))...);
        if (!value) return value.error();
        return *value;
    }
};

// Varargs evaluate function (const Varargs<T>&) -> Result<U>
template <class R, typename T>
struct Signature<R (const Varargs<T>&)> : SignatureBase {
    using Args = std::vector<std::unique_ptr<Expression>>;
    
    Signature(R (*evaluate_)(const Varargs<T>&), std::string name_) :
        SignatureBase(
            valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
            VarargsType { valueTypeToExpressionType<T>() },
            std::move(name_)
        ),
        evaluate(evaluate_)
    {}
    
    std::unique_ptr<Expression> makeExpression(std::vector<std::unique_ptr<Expression>> args) const override  {
        return std::make_unique<CompoundExpression<Signature>>(name, *this, std::move(args));
    };
    
    EvaluationResult apply(const EvaluationContext& evaluationParameters, const Args& args) const {
        Varargs<T> evaluated;
        evaluated.reserve(args.size());
        for (const auto& arg : args) {
            const EvaluationResult evaluatedArg = arg->evaluate(evaluationParameters);
            if(!evaluatedArg) return evaluatedArg.error();
            evaluated.push_back(*fromExpressionValue<std::decay_t<T>>(*evaluatedArg));
        }
        const R value = evaluate(evaluated);
        if (!value) return value.error();
        return *value;
    }
    
    R (*evaluate)(const Varargs<T>&);
};

// Evaluate function needing parameter access,
// (const EvaluationParams&, const T0&, const T1&, ...) -> Result<U>
template <class R, class... Params>
struct Signature<R (const EvaluationContext&, Params...)> : SignatureBase {
    using Args = std::array<std::unique_ptr<Expression>, sizeof...(Params)>;
    
    Signature(R (*evaluate_)(const EvaluationContext&, Params...), std::string name_) :
        SignatureBase(
            valueTypeToExpressionType<std::decay_t<typename R::Value>>(),
            std::vector<type::Type> {valueTypeToExpressionType<std::decay_t<Params>>()...},
            std::move(name_)
        ),
        evaluate(evaluate_)
    {}
    
    std::unique_ptr<Expression> makeExpression(std::vector<std::unique_ptr<Expression>> args) const override {
        typename Signature::Args argsArray;
        std::copy_n(std::make_move_iterator(args.begin()), sizeof...(Params), argsArray.begin());
        return std::make_unique<CompoundExpression<Signature>>(name, *this, std::move(argsArray));
    }
    
    EvaluationResult apply(const EvaluationContext& evaluationParameters, const Args& args) const {
        return applyImpl(evaluationParameters, args, std::index_sequence_for<Params...>{});
    }
    
private:
    template <std::size_t ...I>
    EvaluationResult applyImpl(const EvaluationContext& evaluationParameters, const Args& args, std::index_sequence<I...>) const {
        const std::array<EvaluationResult, sizeof...(I)> evaluated = {{std::get<I>(args)->evaluate(evaluationParameters)...}};
        for (const auto& arg : evaluated) {
            if(!arg) return arg.error();
        }
        // TODO: assert correct runtime type of each arg value
        const R value = evaluate(evaluationParameters, *fromExpressionValue<std::decay_t<Params>>(*(evaluated[I]))...);
        if (!value) return value.error();
        return *value;
    }
    
    R (*evaluate)(const EvaluationContext&, Params...);
};

// Machinery to pull out function types from class methods, lambdas, etc.
template <class R, class... Params>
struct Signature<R (*)(Params...)>
    : Signature<R (Params...)>
{ using Signature<R (Params...)>::Signature; };

template <class T, class R, class... Params>
struct Signature<R (T::*)(Params...) const>
    : Signature<R (Params...)>
{ using Signature<R (Params...)>::Signature;  };

template <class T, class R, class... Params>
struct Signature<R (T::*)(Params...)>
    : Signature<R (Params...)>
{ using Signature<R (Params...)>::Signature; };

template <class Lambda>
struct Signature<Lambda, std::enable_if_t<std::is_class<Lambda>::value>>
    : Signature<decltype(&Lambda::operator())>
{ using Signature<decltype(&Lambda::operator())>::Signature; };

} // namespace detail

using Definition = CompoundExpressionRegistry::Definition;

template <typename Fn>
static std::unique_ptr<detail::SignatureBase> makeSignature(Fn evaluateFunction, std::string name) {
    return std::make_unique<detail::Signature<Fn>>(evaluateFunction, std::move(name));
}

std::unordered_map<std::string, CompoundExpressionRegistry::Definition> initializeDefinitions() {
    std::unordered_map<std::string, CompoundExpressionRegistry::Definition> definitions;
    auto define = [&](std::string name, auto fn) {
        definitions[name].push_back(makeSignature(fn, name));
    };
    
    define("e", []() -> Result<double> { return 2.718281828459045; });
    define("pi", []() -> Result<double> { return 3.141592653589793; });
    define("ln2", []() -> Result<double> { return 0.6931471805599453; });

    define("typeof", [](const Value& v) -> Result<std::string> { return toString(typeOf(v)); });
    
    define("to-string", [](const Value& value) -> Result<std::string> {
        return value.match(
            [](const Color& c) -> Result<std::string> { return c.stringify(); }, // avoid quoting
            [](const std::string& s) -> Result<std::string> { return s; }, // avoid quoting
            [](const auto& v) -> Result<std::string> { return stringify(v); }
        );
    });
    
    define("to-boolean", [](const Value& v) -> Result<bool> {
        return v.match(
            [&] (double f) { return (bool)f; },
            [&] (const std::string& s) { return s.length() > 0; },
            [&] (bool b) { return b; },
            [&] (const NullValue&) { return false; },
            [&] (const auto&) { return true; }
        );
    });
    define("to-rgba", [](const Color& color) -> Result<std::array<double, 4>> {
        return color.toArray();
    });
    
    define("rgba", rgba);
    define("rgb", [](double r, double g, double b) { return rgba(r, g, b, 1.0f); });
    
    define("zoom", [](const EvaluationContext& params) -> Result<double> {
        if (!params.zoom) {
            return EvaluationError {
                "The 'zoom' expression is unavailable in the current evaluation context."
            };
        }
        return *(params.zoom);
    });
    
    define("heatmap-density", [](const EvaluationContext& params) -> Result<double> {
        if (!params.heatmapDensity) {
            return EvaluationError {
                "The 'heatmap-density' expression is unavailable in the current evaluation context."
            };
        }
        return *(params.heatmapDensity);
    });
    
    define("has", [](const EvaluationContext& params, const std::string& key) -> Result<bool> {
        if (!params.feature) {
            return EvaluationError {
                "Feature data is unavailable in the current evaluation context."
            };
        }
        
        return params.feature->getValue(key) ? true : false;
    });
    define("has", [](const std::string& key, const std::unordered_map<std::string, Value>& object) -> Result<bool> {
        return object.find(key) != object.end();
    });
    
    define("get", [](const EvaluationContext& params, const std::string& key) -> Result<Value> {
        if (!params.feature) {
            return EvaluationError {
                "Feature data is unavailable in the current evaluation context."
            };
        }

        auto propertyValue = params.feature->getValue(key);
        if (!propertyValue) {
            return Null;
        }
        return Value(toExpressionValue(*propertyValue));
    });
    define("get", [](const std::string& key, const std::unordered_map<std::string, Value>& object) -> Result<Value> {
        if (object.find(key) == object.end()) {
            return Null;
        }
        return object.at(key);
    });
    
    define("properties", [](const EvaluationContext& params) -> Result<std::unordered_map<std::string, Value>> {
        if (!params.feature) {
            return EvaluationError {
                "Feature data is unavailable in the current evaluation context."
            };
        }
        std::unordered_map<std::string, Value> result;
        const PropertyMap properties = params.feature->getProperties();
        for (const auto& entry : properties) {
            result[entry.first] = toExpressionValue(entry.second);
        }
        return result;
    });
    
    define("geometry-type", [](const EvaluationContext& params) -> Result<std::string> {
        if (!params.feature) {
            return EvaluationError {
                "Feature data is unavailable in the current evaluation context."
            };
        }
    
        auto type = params.feature->getType();
        if (type == FeatureType::Point) {
            return "Point";
        } else if (type == FeatureType::LineString) {
            return "LineString";
        } else if (type == FeatureType::Polygon) {
            return "Polygon";
        } else {
            return "Unknown";
        }
    });
    
    define("id", [](const EvaluationContext& params) -> Result<Value> {
        if (!params.feature) {
            return EvaluationError {
                "Feature data is unavailable in the current evaluation context."
            };
        }
    
        auto id = params.feature->getID();
        if (!id) {
            return Null;
        }
        return id->match(
            [](const auto& idValue) {
                return toExpressionValue(mbgl::Value(idValue));
            }
        );
    });
    
    define("+", [](const Varargs<double>& args) -> Result<double> {
        double sum = 0.0f;
        for (auto arg : args) {
            sum += arg;
        }
        return sum;
    });
    define("-", [](double a, double b) -> Result<double> { return a - b; });
    define("-", [](double a) -> Result<double> { return -a; });
    define("*", [](const Varargs<double>& args) -> Result<double> {
        double prod = 1.0f;
        for (auto arg : args) {
            prod *= arg;
        }
        return prod;
    });
    define("/", [](double a, double b) -> Result<double> { return a / b; });
    define("%", [](double a, double b) -> Result<double> { return fmod(a, b); });
    define("^", [](double a, double b) -> Result<double> { return pow(a, b); });
    define("sqrt", [](double x) -> Result<double> { return sqrt(x); });
    define("log10", [](double x) -> Result<double> { return log10(x); });
    define("ln", [](double x) -> Result<double> { return log(x); });
    define("log2", [](double x) -> Result<double> { return util::log2(x); });
    define("sin", [](double x) -> Result<double> { return sin(x); });
    define("cos", [](double x) -> Result<double> { return cos(x); });
    define("tan", [](double x) -> Result<double> { return tan(x); });
    define("asin", [](double x) -> Result<double> { return asin(x); });
    define("acos", [](double x) -> Result<double> { return acos(x); });
    define("atan", [](double x) -> Result<double> { return atan(x); });
    
    define("min", [](const Varargs<double>& args) -> Result<double> {
        double result = std::numeric_limits<double>::infinity();
        for (double arg : args) {
            result = fmin(arg, result);
        }
        return result;
    });
    define("max", [](const Varargs<double>& args) -> Result<double> {
        double result = -std::numeric_limits<double>::infinity();
        for (double arg : args) {
            result = fmax(arg, result);
        }
        return result;
    });
    
    define("round", [](double x) -> Result<double> { return ::round(x); });
    define("floor", [](double x) -> Result<double> { return std::floor(x); });
    define("ceil", [](double x) -> Result<double> { return std::ceil(x); });
    define("abs", [](double x) -> Result<double> { return std::abs(x); });

    define(">", [](double lhs, double rhs) -> Result<bool> { return lhs > rhs; });
    define(">", [](const std::string& lhs, const std::string& rhs) -> Result<bool> { return lhs > rhs; });
    define(">=", [](double lhs, double rhs) -> Result<bool> { return lhs >= rhs; });
    define(">=",[](const std::string& lhs, const std::string& rhs) -> Result<bool> { return lhs >= rhs; });
    define("<", [](double lhs, double rhs) -> Result<bool> { return lhs < rhs; });
    define("<", [](const std::string& lhs, const std::string& rhs) -> Result<bool> { return lhs < rhs; });
    define("<=", [](double lhs, double rhs) -> Result<bool> { return lhs <= rhs; });
    define("<=", [](const std::string& lhs, const std::string& rhs) -> Result<bool> { return lhs <= rhs; });
    
    define("!", [](bool e) -> Result<bool> { return !e; });
    
    define("is-supported-script", [](const std::string& x) -> Result<bool> {
        return util::i18n::isStringInSupportedScript(x);
    });

    define("upcase", [](const std::string& input) -> Result<std::string> {
        return platform::uppercase(input);
    });
    define("downcase", [](const std::string& input) -> Result<std::string> {
        return platform::lowercase(input);
    });
    define("concat", [](const Varargs<std::string>& args) -> Result<std::string> {
        std::string s;
        for (const std::string& arg : args) {
            s += arg;
        }
        return s;
    });
    define("error", [](const std::string& input) -> Result<type::ErrorType> {
        return EvaluationError { input };
    });
    
    return definitions;
}

std::unordered_map<std::string, Definition> CompoundExpressionRegistry::definitions = initializeDefinitions();

using namespace mbgl::style::conversion;
ParseResult parseCompoundExpression(const std::string name, const Convertible& value, ParsingContext& ctx) {
    assert(isArray(value) && arrayLength(value) > 0);

    auto it = CompoundExpressionRegistry::definitions.find(name);
    if (it == CompoundExpressionRegistry::definitions.end()) {
        ctx.error(
             R"(Unknown expression ")" + name + R"(". If you wanted a literal array, use ["literal", [...]].)",
            0
        );
        return ParseResult();
    }
    const CompoundExpressionRegistry::Definition& definition = it->second;
    
    auto length = arrayLength(value);

    // Check if we have a single signature with the correct number of
    // parameters. If so, then use that signature's parameter types for parsing
    // (and inferring the types of) the arguments.
    optional<std::size_t> singleMatchingSignature;
    for (std::size_t j = 0; j < definition.size(); j++) {
        const std::unique_ptr<detail::SignatureBase>& signature = definition[j];
        if (
            signature->params.is<VarargsType>() ||
            signature->params.get<std::vector<type::Type>>().size() == length - 1
        ) {
            if (singleMatchingSignature) {
                singleMatchingSignature = {};
            } else {
                singleMatchingSignature = j;
            }
        }
    }

    // parse subexpressions first
    std::vector<std::unique_ptr<Expression>> args;
    args.reserve(length - 1);
    for (std::size_t i = 1; i < length; i++) {
        optional<type::Type> expected;
        
        if (singleMatchingSignature) {
            expected = definition[*singleMatchingSignature]->params.match(
                [](const VarargsType& varargs) { return varargs.type; },
                [&](const std::vector<type::Type>& params_) { return params_[i - 1]; }
            );
        }
    
        auto parsed = ctx.parse(arrayMember(value, i), i, expected);
        if (!parsed) {
            return parsed;
        }
        args.push_back(std::move(*parsed));
    }
    return createCompoundExpression(definition, std::move(args), ctx);
}


ParseResult createCompoundExpression(const std::string& name,
                                     std::vector<std::unique_ptr<Expression>> args,
                                     ParsingContext& ctx)
{
    return createCompoundExpression(CompoundExpressionRegistry::definitions.at(name), std::move(args), ctx);
}


ParseResult createCompoundExpression(const Definition& definition,
                                     std::vector<std::unique_ptr<Expression>> args,
                                     ParsingContext& ctx)
{
    ParsingContext signatureContext(ctx.getKey());
    
    for (const std::unique_ptr<detail::SignatureBase>& signature : definition) {
        signatureContext.clearErrors();
        
        if (signature->params.is<std::vector<type::Type>>()) {
            const std::vector<type::Type>& params = signature->params.get<std::vector<type::Type>>();
            if (params.size() != args.size()) {
                signatureContext.error(
                    "Expected " + util::toString(params.size()) +
                    " arguments, but found " + util::toString(args.size()) + " instead."
                );
                continue;
            }

            for (std::size_t i = 0; i < args.size(); i++) {
                const std::unique_ptr<Expression>& arg = args[i];
                optional<std::string> err = type::checkSubtype(params.at(i), arg->getType());
                if (err) {
                    signatureContext.error(*err, i + 1);
                }
            }
        } else if (signature->params.is<VarargsType>()) {
            const type::Type& paramType = signature->params.get<VarargsType>().type;
            for (std::size_t i = 0; i < args.size(); i++) {
                const std::unique_ptr<Expression>& arg = args[i];
                optional<std::string> err = type::checkSubtype(paramType, arg->getType());
                if (err) {
                    signatureContext.error(*err, i + 1);
                }
            }
        }
        
        if (signatureContext.getErrors().size() == 0) {
            return ParseResult(signature->makeExpression(std::move(args)));
        }
    }
    
    if (definition.size() == 1) {
        ctx.appendErrors(std::move(signatureContext));
    } else {
        std::string signatures;
        for (const auto& signature : definition) {
            signatures += (signatures.size() > 0 ? " | " : "");
            signature->params.match(
                [&](const VarargsType& varargs) {
                    signatures += "(" + toString(varargs.type) + ")";
                },
                [&](const std::vector<type::Type>& params) {
                    signatures += "(";
                    bool first = true;
                    for (const type::Type& param : params) {
                        if (!first) signatures += ", ";
                        signatures += toString(param);
                        first = false;
                    }
                    signatures += ")";
                }
            );
            
        }
        std::string actualTypes;
        for (const auto& arg : args) {
            if (actualTypes.size() > 0) {
                actualTypes += ", ";
            }
            actualTypes += toString(arg->getType());
        }
        ctx.error("Expected arguments of type " + signatures + ", but found (" + actualTypes + ") instead.");
    }
    
    return ParseResult();
}

} // namespace expression
} // namespace style
} // namespace mbgl