2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.serialization.engines.query;/*-
22 * ============LICENSE_START=======================================================
24 * ================================================================================
25 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
26 * ================================================================================
27 * Licensed under the Apache License, Version 2.0 (the "License");
28 * you may not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
31 * http://www.apache.org/licenses/LICENSE-2.0
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS,
35 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
38 * ============LICENSE_END=========================================================
42 // package org.onap.aai.serialization.engines.query;
44 // import java.util.List;
45 // import java.util.regex.Matcher;
46 // import java.util.regex.Pattern;
48 // import org.apache.commons3.collections.IteratorUtils;
50 // import org.onap.aai.db.AAIProperties;
51 // import org.onap.aai.query.builder.QueryBuilder;
52 // import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 // import com.tinkerpop.blueprints.Direction;
54 // import com.tinkerpop.blueprints.Graph;
55 // import com.tinkerpop.blueprints.TransactionalGraph;
56 // import com.tinkerpop.blueprints.Vertex;
57 // import com.tinkerpop.gremlin.groovy.Gremlin;
58 // import com.tinkerpop.gremlin.java.GremlinPipeline;
59 // import com.tinkerpop.pipes.Pipe;
60 // import com.tinkerpop.pipes.util.iterators.SingleIterator;
62 // public class GremlinQueryEngine extends QueryEngine {
64 // public GremlinQueryEngine (TransactionalGraphEngine engine) {
71 // public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
72 // List<Vertex> result = null;
73 // Vertex start = query.getStart();
74 // if (start != null) {
75 // result = this.executeQuery(start, (String)query.getQuery());
77 // result = this.processGremlinQuery((String)query.getQuery());
84 // public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
86 // List<Vertex> result = null;
87 // Vertex start = query.getStart();
88 // if (start != null) {
89 // result = this.executeQuery(start, (String)query.getParentQuery());
91 // result = this.processGremlinQuery((String)query.getParentQuery());
96 // private List<Vertex> executeQuery(Vertex startVertex, String query) {
98 // return this.processGremlinQuery(startVertex, "_()" + query);
103 // public List<Vertex> findParents(Vertex start) {
105 // String findAllParents = ".as('x').inE.has('isParent', true).outV"
106 // + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}";
108 // List<Vertex> results = this.executeQuery(start, findAllParents);
109 // results.add(0, start);
115 // public List<Vertex> findChildren(Vertex start) {
116 // String findAllChildren = ".as('x').outE.has('isParent', true).inV"
117 // + ".loop('x'){it.object.outE.has('isParent', true).count() >= 1}{true}";
119 // List<Vertex> results = this.executeQuery(start, findAllChildren);
120 // results.add(0, start);
126 // public List<Vertex> findDeletable(Vertex start) {
127 // String findAllChildren = ".as('x').outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).inV"
128 // + ".loop('x'){it.object.outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).count() >= 1}{true}";
130 // List<Vertex> results = this.executeQuery(start, findAllChildren);
131 // results.add(0, start);
134 // private List<Vertex> processGremlinQuery(String query) {
136 // Pattern firstHasSet = Pattern.compile("^(\\.has\\(.*?\\))(\\.has\\(.*?\\))*(?!\\.has)");
137 // Pattern p = Pattern.compile("\\.has\\('(.*?)',\\s?'(.*?)'\\)");
138 // Matcher m = firstHasSet.matcher(query);
139 // List<Vertex> results = null;
140 // GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<>(dbEngine.getGraph());
142 // String hasSet = m.group();
143 // query = query.replace(m.group(0), "");
144 // m = p.matcher(hasSet);
146 // while (m.find()) {
147 // pipe.has(m.group(1), m.group(2));
149 // results = processGremlinQuery(pipe.toList(), "_()" + query);
155 // private List<Vertex> processGremlinQuery(Vertex startVertex, String query) {
157 // Pipe pipe = Gremlin.compile(query);
158 // pipe.setStarts(new SingleIterator<Vertex>(startVertex));
160 // return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
162 // private List<Vertex> processGremlinQuery(List<Vertex> list, String query) {
164 // Pipe pipe = Gremlin.compile(query);
166 // pipe.setStarts(list);
168 // return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
173 // public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
174 // String findRelatedVertices = "_()";
175 // switch (direction) {
177 // findRelatedVertices += ".out('" + label + "')";
180 // findRelatedVertices += ".in('" + label + "')";
183 // findRelatedVertices += ".both('" + label + "')";
188 // findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()";
189 // List<Vertex> results = this.executeQuery(start, findRelatedVertices);
190 // results.add(0, start);