[AAI-178 Amsterdam] Make Edge Properties to be
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / engines / query / GremlinQueryEngine.java
1 package org.openecomp.aai.serialization.engines.query;/*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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
21 //
22 //package org.openecomp.aai.serialization.engines.query;
23 //
24 //import java.util.List;
25 //import java.util.regex.Matcher;
26 //import java.util.regex.Pattern;
27 //
28 //import org.apache.commons.collections.IteratorUtils;
29 //
30 //import org.openecomp.aai.db.AAIProperties;
31 //import org.openecomp.aai.query.builder.QueryBuilder;
32 //import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
33 //import com.tinkerpop.blueprints.Direction;
34 //import com.tinkerpop.blueprints.Graph;
35 //import com.tinkerpop.blueprints.TransactionalGraph;
36 //import com.tinkerpop.blueprints.Vertex;
37 //import com.tinkerpop.gremlin.groovy.Gremlin;
38 //import com.tinkerpop.gremlin.java.GremlinPipeline;
39 //import com.tinkerpop.pipes.Pipe;
40 //import com.tinkerpop.pipes.util.iterators.SingleIterator;
41 //
42 //public class GremlinQueryEngine extends QueryEngine {
43 //      
44 //      public GremlinQueryEngine (TransactionalGraphEngine engine) {
45 //              super(engine);
46 //
47 //      }
48 //      
49 //
50 //      @Override
51 //      public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
52 //              List<Vertex> result = null;
53 //              Vertex start = query.getStart();
54 //              if (start != null) {
55 //                      result = this.executeQuery(start, (String)query.getQuery());
56 //              } else {
57 //                      result = this.processGremlinQuery((String)query.getQuery());
58 //              }
59 //              return result;
60 //
61 //      }
62 //      
63 //      @Override
64 //      public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
65 //              
66 //              List<Vertex> result = null;
67 //              Vertex start = query.getStart();
68 //              if (start != null) {
69 //                      result = this.executeQuery(start, (String)query.getParentQuery());
70 //              } else {
71 //                      result = this.processGremlinQuery((String)query.getParentQuery());
72 //              }
73 //              return result;
74 //      }
75 //      
76 //      private List<Vertex> executeQuery(Vertex startVertex, String query) {
77 //              
78 //              return this.processGremlinQuery(startVertex, "_()" + query);
79 //
80 //      }
81 //
82 //      @Override
83 //      public List<Vertex> findParents(Vertex start) {
84 //              
85 //              String findAllParents = ".as('x').inE.has('isParent', true).outV"
86 //                              + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}";
87 //              
88 //              List<Vertex> results = this.executeQuery(start, findAllParents);
89 //              results.add(0, start);
90 //              return results;
91 //              
92 //      }
93 //      
94 //      @Override
95 //      public List<Vertex> findChildren(Vertex start) {
96 //              String findAllChildren = ".as('x').outE.has('isParent', true).inV"
97 //                              + ".loop('x'){it.object.outE.has('isParent', true).count() >= 1}{true}";
98 //              
99 //              List<Vertex> results = this.executeQuery(start, findAllChildren);
100 //              results.add(0, start);
101 //              return results;
102 //              
103 //      }
104 //      
105 //      @Override
106 //      public List<Vertex> findDeletable(Vertex start) {
107 //              String findAllChildren = ".as('x').outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).inV"
108 //                              + ".loop('x'){it.object.outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).count() >= 1}{true}";
109 //              
110 //              List<Vertex> results = this.executeQuery(start, findAllChildren);
111 //              results.add(0, start);
112 //              return results;
113 //      }
114 //      private List<Vertex> processGremlinQuery(String query) {
115 //              
116 //              Pattern firstHasSet = Pattern.compile("^(\\.has\\(.*?\\))(\\.has\\(.*?\\))*(?!\\.has)");
117 //              Pattern p = Pattern.compile("\\.has\\('(.*?)',\\s?'(.*?)'\\)");
118 //              Matcher m = firstHasSet.matcher(query);
119 //              List<Vertex> results = null;
120 //              GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<>(dbEngine.getGraph());
121 //              if (m.find()) {
122 //                      String hasSet = m.group();
123 //                      query = query.replace(m.group(0), "");
124 //                      m = p.matcher(hasSet);
125 //                      pipe.V();
126 //                      while (m.find()) {
127 //                              pipe.has(m.group(1), m.group(2));
128 //                      }
129 //                      results = processGremlinQuery(pipe.toList(), "_()" + query);
130 //              }
131 //              
132 //              return results;
133 //              
134 //      }
135 //      private List<Vertex> processGremlinQuery(Vertex startVertex, String query) {
136 //              
137 //              Pipe pipe = Gremlin.compile(query);
138 //              pipe.setStarts(new SingleIterator<Vertex>(startVertex));
139 //              
140 //              return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
141 //      }
142 //      private List<Vertex> processGremlinQuery(List<Vertex> list, String query) {
143 //              
144 //              Pipe pipe = Gremlin.compile(query);
145 //              
146 //              pipe.setStarts(list);
147 //              
148 //              return (List<Vertex>)IteratorUtils.toList(pipe.iterator());             
149 //      }
150 //
151 //
152 //      @Override
153 //      public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
154 //              String findRelatedVertices = "_()";
155 //              switch (direction) {
156 //                      case OUT:
157 //                              findRelatedVertices += ".out('" + label + "')";
158 //                              break;
159 //                      case IN:
160 //                              findRelatedVertices += ".in('" + label + "')";
161 //                              break;
162 //                      case BOTH:
163 //                              findRelatedVertices += ".both('" + label + "')";
164 //                              break;
165 //                       default:
166 //                              break;
167 //              }
168 //              findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()";
169 //              List<Vertex> results = this.executeQuery(start, findRelatedVertices);
170 //              results.add(0, start);
171 //              return results;
172 //      }
173 //      
174 //}
175 //