• 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 #include "utils/grammar/parsing/derivation.h"
18 
19 #include <algorithm>
20 #include <vector>
21 
22 namespace libtextclassifier3::grammar {
23 
IsValid() const24 bool Derivation::IsValid() const {
25   bool result = true;
26   Traverse(parse_tree, [&result](const ParseTree* node) {
27     if (node->type != ParseTree::Type::kAssertion) {
28       // Only validation if all checks so far passed.
29       return result;
30     }
31     // Positive assertions are by definition fulfilled,
32     // fail if the assertion is negative.
33     if (static_cast<const AssertionNode*>(node)->negative) {
34       result = false;
35     }
36     return result;
37   });
38   return result;
39 }
40 
ValidDeduplicatedDerivations(const std::vector<Derivation> & derivations)41 std::vector<Derivation> ValidDeduplicatedDerivations(
42     const std::vector<Derivation>& derivations) {
43   std::vector<Derivation> result;
44   for (const Derivation& derivation :
45        DeduplicateDerivations<Derivation>(derivations)) {
46     // Check that asserts are fulfilled.
47     if (derivation.IsValid()) {
48       result.push_back(derivation);
49     }
50   }
51   return result;
52 }
53 
54 }  // namespace libtextclassifier3::grammar
55