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=========================================================
20 package org.onap.aai.serialization.engines.query;/*-
21 * ============LICENSE_START=======================================================
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
30 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
41 //package org.onap.aai.serialization.engines.query;
43 //import java.util.List;
44 //import java.util.regex.Matcher;
45 //import java.util.regex.Pattern;
47 //import org.apache.commons.collections.IteratorUtils;
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;
61 //public class GremlinQueryEngine extends QueryEngine {
63 // public GremlinQueryEngine (TransactionalGraphEngine engine) {
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());
76 // result = this.processGremlinQuery((String)query.getQuery());
83 // public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
85 // List<Vertex> result = null;
86 // Vertex start = query.getStart();
87 // if (start != null) {
88 // result = this.executeQuery(start, (String)query.getParentQuery());
90 // result = this.processGremlinQuery((String)query.getParentQuery());
95 // private List<Vertex> executeQuery(Vertex startVertex, String query) {
97 // return this.processGremlinQuery(startVertex, "_()" + query);
102 // public List<Vertex> findParents(Vertex start) {
104 // String findAllParents = ".as('x').inE.has('isParent', true).outV"
105 // + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}";
107 // List<Vertex> results = this.executeQuery(start, findAllParents);
108 // results.add(0, start);
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}";
118 // List<Vertex> results = this.executeQuery(start, findAllChildren);
119 // results.add(0, start);
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}";
129 // List<Vertex> results = this.executeQuery(start, findAllChildren);
130 // results.add(0, start);
133 // private List<Vertex> processGremlinQuery(String query) {
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());
141 // String hasSet = m.group();
142 // query = query.replace(m.group(0), "");
143 // m = p.matcher(hasSet);
145 // while (m.find()) {
146 // pipe.has(m.group(1), m.group(2));
148 // results = processGremlinQuery(pipe.toList(), "_()" + query);
154 // private List<Vertex> processGremlinQuery(Vertex startVertex, String query) {
156 // Pipe pipe = Gremlin.compile(query);
157 // pipe.setStarts(new SingleIterator<Vertex>(startVertex));
159 // return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
161 // private List<Vertex> processGremlinQuery(List<Vertex> list, String query) {
163 // Pipe pipe = Gremlin.compile(query);
165 // pipe.setStarts(list);
167 // return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
172 // public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
173 // String findRelatedVertices = "_()";
174 // switch (direction) {
176 // findRelatedVertices += ".out('" + label + "')";
179 // findRelatedVertices += ".in('" + label + "')";
182 // findRelatedVertices += ".both('" + label + "')";
187 // findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()";
188 // List<Vertex> results = this.executeQuery(start, findRelatedVertices);
189 // results.add(0, start);