1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/grammar/semantics/evaluators/merge-values-eval.h"
18
19 namespace libtextclassifier3::grammar {
20
Apply(const EvalContext & context,const SemanticExpression * expression,UnsafeArena * arena) const21 StatusOr<const SemanticValue*> MergeValuesEvaluator::Apply(
22 const EvalContext& context, const SemanticExpression* expression,
23 UnsafeArena* arena) const {
24 const MergeValueExpression* merge_value_expression =
25 expression->expression_as_MergeValueExpression();
26 std::unique_ptr<MutableFlatbuffer> result =
27 semantic_value_builder_.NewTable(merge_value_expression->type());
28
29 if (result == nullptr) {
30 return Status(StatusCode::INVALID_ARGUMENT, "Invalid result type.");
31 }
32
33 for (const SemanticExpression* semantic_expression :
34 *merge_value_expression->values()) {
35 TC3_ASSIGN_OR_RETURN(const SemanticValue* value,
36 composer_->Apply(context, semantic_expression, arena));
37 if (value == nullptr) {
38 continue;
39 }
40 if ((value->type() != result->type()) ||
41 !result->MergeFrom(value->Table())) {
42 return Status(StatusCode::INVALID_ARGUMENT,
43 "Could not merge the results.");
44 }
45 }
46 return SemanticValue::Create<const MutableFlatbuffer*>(result.get(), arena);
47 }
48
49 } // namespace libtextclassifier3::grammar
50