2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 //package org.openecomp.aai.serialization.engines.query;
24 //import java.util.List;
25 //import java.util.regex.Matcher;
26 //import java.util.regex.Pattern;
28 //import org.apache.commons.collections.IteratorUtils;
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;
42 //public class GremlinQueryEngine extends QueryEngine {
44 // public GremlinQueryEngine (TransactionalGraphEngine engine) {
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());
57 // result = this.processGremlinQuery((String)query.getQuery());
64 // public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
66 // List<Vertex> result = null;
67 // Vertex start = query.getStart();
68 // if (start != null) {
69 // result = this.executeQuery(start, (String)query.getParentQuery());
71 // result = this.processGremlinQuery((String)query.getParentQuery());
76 // private List<Vertex> executeQuery(Vertex startVertex, String query) {
78 // return this.processGremlinQuery(startVertex, "_()" + query);
83 // public List<Vertex> findParents(Vertex start) {
85 // String findAllParents = ".as('x').inE.has('isParent', true).outV"
86 // + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}";
88 // List<Vertex> results = this.executeQuery(start, findAllParents);
89 // results.add(0, start);
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}";
99 // List<Vertex> results = this.executeQuery(start, findAllChildren);
100 // results.add(0, start);
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}";
110 // List<Vertex> results = this.executeQuery(start, findAllChildren);
111 // results.add(0, start);
114 // private List<Vertex> processGremlinQuery(String query) {
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());
122 // String hasSet = m.group();
123 // query = query.replace(m.group(0), "");
124 // m = p.matcher(hasSet);
126 // while (m.find()) {
127 // pipe.has(m.group(1), m.group(2));
129 // results = processGremlinQuery(pipe.toList(), "_()" + query);
135 // private List<Vertex> processGremlinQuery(Vertex startVertex, String query) {
137 // Pipe pipe = Gremlin.compile(query);
138 // pipe.setStarts(new SingleIterator<Vertex>(startVertex));
140 // return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
142 // private List<Vertex> processGremlinQuery(List<Vertex> list, String query) {
144 // Pipe pipe = Gremlin.compile(query);
146 // pipe.setStarts(list);
148 // return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
153 // public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
154 // String findRelatedVertices = "_()";
155 // switch (direction) {
157 // findRelatedVertices += ".out('" + label + "')";
160 // findRelatedVertices += ".in('" + label + "')";
163 // findRelatedVertices += ".both('" + label + "')";
168 // findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()";
169 // List<Vertex> results = this.executeQuery(start, findRelatedVertices);
170 // results.add(0, start);