View Javadoc
1   /*
2    * Copyright 2013-2023 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package cz.zcu.mre.sparkle.tools.sparqlParser;
21  
22  import org.antlr.v4.runtime.tree.ParseTree;
23  import java.util.LinkedList;
24  import java.util.Queue;
25  
26  /**
27   * Wrapping class saves information about visiting of node children (it's used
28   * during query loading from text file).
29   *
30   * @author Josef Kazak
31   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
32   */
33  public class SparqlParserNode {
34  
35      private final ParseTree data;
36      private final Queue<ParseTree> childQueue;
37      private boolean visited = false;
38  
39      public SparqlParserNode(ParseTree data) {
40          this.data = data;
41          this.childQueue = new LinkedList<>();
42          for (int i = 0; i < data.getChildCount(); i++) {
43              childQueue.add(data.getChild(i));
44          }
45      }
46  
47      /**
48       * Returns unvisited child of current node.
49       *
50       * @return ParseTree node
51       */
52      public ParseTree getUnvisitedNode() {
53          this.visited = true;
54          if (this.childQueue.isEmpty()) {
55              return null;
56          } else {
57              return this.childQueue.remove();
58          }
59      }
60  
61      /**
62       * Returns "true" if all node children were visited (during walking through
63       * SparqlParserContextHelp tree).
64       *
65       * @return state of visited children
66       */
67      public boolean notExistsUnvisitedNode() {
68          return this.childQueue.isEmpty();
69      }
70  
71      /**
72       * Returns ParseTree data of node.
73       *
74       * @return ParseTree node
75       */
76      public ParseTree getNodeData() {
77          return this.data;
78      }
79  
80      /**
81       * Node is visited when it was getting its child.
82       *
83       * @return state of node (node was visited or not)
84       */
85      public boolean getVisitedNodeState() {
86          return this.visited;
87      }
88  }