• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LIBTEXTCLASSIFIER_UTILS_GRAMMAR_SEMANTICS_EVALUATORS_CONST_EVAL_H_
18 #define LIBTEXTCLASSIFIER_UTILS_GRAMMAR_SEMANTICS_EVALUATORS_CONST_EVAL_H_
19 
20 #include "utils/base/arena.h"
21 #include "utils/grammar/semantics/eval-context.h"
22 #include "utils/grammar/semantics/evaluator.h"
23 #include "utils/grammar/semantics/expression_generated.h"
24 #include "utils/grammar/semantics/value.h"
25 
26 namespace libtextclassifier3::grammar {
27 
28 // Returns a constant value of a given type.
29 class ConstEvaluator : public SemanticExpressionEvaluator {
30  public:
ConstEvaluator(const reflection::Schema * semantic_values_schema)31   explicit ConstEvaluator(const reflection::Schema* semantic_values_schema)
32       : semantic_values_schema_(semantic_values_schema) {}
33 
Apply(const EvalContext &,const SemanticExpression * expression,UnsafeArena * arena)34   StatusOr<const SemanticValue*> Apply(const EvalContext&,
35                                        const SemanticExpression* expression,
36                                        UnsafeArena* arena) const override {
37     TC3_DCHECK_EQ(expression->expression_type(),
38                   SemanticExpression_::Expression_ConstValueExpression);
39     const ConstValueExpression* const_value_expression =
40         expression->expression_as_ConstValueExpression();
41     const reflection::BaseType base_type =
42         static_cast<reflection::BaseType>(const_value_expression->base_type());
43     const StringPiece data = StringPiece(
44         reinterpret_cast<const char*>(const_value_expression->value()->data()),
45         const_value_expression->value()->size());
46 
47     if (base_type == reflection::BaseType::Obj) {
48       // Resolve the object type.
49       const int type_id = const_value_expression->type();
50       if (type_id < 0 ||
51           type_id >= semantic_values_schema_->objects()->size()) {
52         return Status(StatusCode::INVALID_ARGUMENT, "Invalid type.");
53       }
54       return SemanticValue::Create(semantic_values_schema_->objects()->Get(
55                                        const_value_expression->type()),
56                                    data, arena);
57     } else {
58       return SemanticValue::Create(base_type, data, arena);
59     }
60   }
61 
62  private:
63   const reflection::Schema* semantic_values_schema_;
64 };
65 
66 }  // namespace libtextclassifier3::grammar
67 
68 #endif  // LIBTEXTCLASSIFIER_UTILS_GRAMMAR_SEMANTICS_EVALUATORS_CONST_EVAL_H_
69