Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / GremlinQueryEngine.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.serialization.engines.query;/*-
21  * ============LICENSE_START=======================================================
22  * org.onap.aai
23  * ================================================================================
24  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
25  * ================================================================================
26  * Licensed under the Apache License, Version 2.0 (the "License");
27  * you may not use this file except in compliance with the License.
28  * You may obtain a copy of the License at
29  * 
30  *      http://www.apache.org/licenses/LICENSE-2.0
31  * 
32  * Unless required by applicable law or agreed to in writing, software
33  * distributed under the License is distributed on an "AS IS" BASIS,
34  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35  * See the License for the specific language governing permissions and
36  * limitations under the License.
37  * ============LICENSE_END=========================================================
38  */
39
40 //
41 //package org.onap.aai.serialization.engines.query;
42 //
43 //import java.util.List;
44 //import java.util.regex.Matcher;
45 //import java.util.regex.Pattern;
46 //
47 //import org.apache.commons.collections.IteratorUtils;
48 //
49 //import org.onap.aai.db.AAIProperties;
50 //import org.onap.aai.query.builder.QueryBuilder;
51 //import org.onap.aai.serialization.engines.TransactionalGraphEngine;
52 //import com.tinkerpop.blueprints.Direction;
53 //import com.tinkerpop.blueprints.Graph;
54 //import com.tinkerpop.blueprints.TransactionalGraph;
55 //import com.tinkerpop.blueprints.Vertex;
56 //import com.tinkerpop.gremlin.groovy.Gremlin;
57 //import com.tinkerpop.gremlin.java.GremlinPipeline;
58 //import com.tinkerpop.pipes.Pipe;
59 //import com.tinkerpop.pipes.util.iterators.SingleIterator;
60 //
61 //public class GremlinQueryEngine extends QueryEngine {
62 //      
63 //      public GremlinQueryEngine (TransactionalGraphEngine engine) {
64 //              super(engine);
65 //
66 //      }
67 //      
68 //
69 //      @Override
70 //      public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
71 //              List<Vertex> result = null;
72 //              Vertex start = query.getStart();
73 //              if (start != null) {
74 //                      result = this.executeQuery(start, (String)query.getQuery());
75 //              } else {
76 //                      result = this.processGremlinQuery((String)query.getQuery());
77 //              }
78 //              return result;
79 //
80 //      }
81 //      
82 //      @Override
83 //      public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
84 //              
85 //              List<Vertex> result = null;
86 //              Vertex start = query.getStart();
87 //              if (start != null) {
88 //                      result = this.executeQuery(start, (String)query.getParentQuery());
89 //              } else {
90 //                      result = this.processGremlinQuery((String)query.getParentQuery());
91 //              }
92 //              return result;
93 //      }
94 //      
95 //      private List<Vertex> executeQuery(Vertex startVertex, String query) {
96 //              
97 //              return this.processGremlinQuery(startVertex, "_()" + query);
98 //
99 //      }
100 //
101 //      @Override
102 //      public List<Vertex> findParents(Vertex start) {
103 //              
104 //              String findAllParents = ".as('x').inE.has('isParent', true).outV"
105 //                              + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}";
106 //              
107 //              List<Vertex> results = this.executeQuery(start, findAllParents);
108 //              results.add(0, start);
109 //              return results;
110 //              
111 //      }
112 //      
113 //      @Override
114 //      public List<Vertex> findChildren(Vertex start) {
115 //              String findAllChildren = ".as('x').outE.has('isParent', true).inV"
116 //                              + ".loop('x'){it.object.outE.has('isParent', true).count() >= 1}{true}";
117 //              
118 //              List<Vertex> results = this.executeQuery(start, findAllChildren);
119 //              results.add(0, start);
120 //              return results;
121 //              
122 //      }
123 //      
124 //      @Override
125 //      public List<Vertex> findDeletable(Vertex start) {
126 //              String findAllChildren = ".as('x').outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).inV"
127 //                              + ".loop('x'){it.object.outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).count() >= 1}{true}";
128 //              
129 //              List<Vertex> results = this.executeQuery(start, findAllChildren);
130 //              results.add(0, start);
131 //              return results;
132 //      }
133 //      private List<Vertex> processGremlinQuery(String query) {
134 //              
135 //              Pattern firstHasSet = Pattern.compile("^(\\.has\\(.*?\\))(\\.has\\(.*?\\))*(?!\\.has)");
136 //              Pattern p = Pattern.compile("\\.has\\('(.*?)',\\s?'(.*?)'\\)");
137 //              Matcher m = firstHasSet.matcher(query);
138 //              List<Vertex> results = null;
139 //              GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<>(dbEngine.getGraph());
140 //              if (m.find()) {
141 //                      String hasSet = m.group();
142 //                      query = query.replace(m.group(0), "");
143 //                      m = p.matcher(hasSet);
144 //                      pipe.V();
145 //                      while (m.find()) {
146 //                              pipe.has(m.group(1), m.group(2));
147 //                      }
148 //                      results = processGremlinQuery(pipe.toList(), "_()" + query);
149 //              }
150 //              
151 //              return results;
152 //              
153 //      }
154 //      private List<Vertex> processGremlinQuery(Vertex startVertex, String query) {
155 //              
156 //              Pipe pipe = Gremlin.compile(query);
157 //              pipe.setStarts(new SingleIterator<Vertex>(startVertex));
158 //              
159 //              return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
160 //      }
161 //      private List<Vertex> processGremlinQuery(List<Vertex> list, String query) {
162 //              
163 //              Pipe pipe = Gremlin.compile(query);
164 //              
165 //              pipe.setStarts(list);
166 //              
167 //              return (List<Vertex>)IteratorUtils.toList(pipe.iterator());             
168 //      }
169 //
170 //
171 //      @Override
172 //      public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
173 //              String findRelatedVertices = "_()";
174 //              switch (direction) {
175 //                      case OUT:
176 //                              findRelatedVertices += ".out('" + label + "')";
177 //                              break;
178 //                      case IN:
179 //                              findRelatedVertices += ".in('" + label + "')";
180 //                              break;
181 //                      case BOTH:
182 //                              findRelatedVertices += ".both('" + label + "')";
183 //                              break;
184 //                       default:
185 //                              break;
186 //              }
187 //              findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()";
188 //              List<Vertex> results = this.executeQuery(start, findRelatedVertices);
189 //              results.add(0, start);
190 //              return results;
191 //      }
192 //      
193 //}
194 //