// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "documentcalculator.h" #include #include #include #include #include #include namespace document { DocumentCalculator::DocumentCalculator( const DocumentTypeRepo& repo, const vespalib::string & expression) { BucketIdFactory factory; select::Parser parser(repo, factory); _selectionNode = parser.parse(expression + " == 0"); } DocumentCalculator::~DocumentCalculator() { } double DocumentCalculator::evaluate(const Document& doc, std::unique_ptr variables) { select::Compare& compare(static_cast(*_selectionNode)); const select::ValueNode& left = compare.getLeft(); select::Context context(doc); context.setVariableMap(std::move(variables)); std::unique_ptr value = left.getValue(context); select::NumberValue* num = dynamic_cast(value.get()); if (!num) { throw vespalib::IllegalArgumentException("Expression could not be evaluated - some components of the expression may be missing"); } return num->getCommonValue(); } }