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.HashSet;
25 import java.util.List;
28 import org.openecomp.aai.db.props.AAIProperties;
29 import org.openecomp.aai.query.builder.QueryBuilder;
30 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
31 import com.tinkerpop.blueprints.Direction;
32 import com.tinkerpop.blueprints.TransactionalGraph;
33 import com.tinkerpop.blueprints.Vertex;
34 import com.tinkerpop.gremlin.java.GremlinPipeline;
35 import com.tinkerpop.pipes.IdentityPipe;
36 import com.tinkerpop.pipes.PipeFunction;
37 import com.tinkerpop.pipes.branch.LoopPipe;
39 public class GremlinPipelineQueryEngine extends QueryEngine {
41 public GremlinPipelineQueryEngine(TransactionalGraphEngine graphEngine) {
46 public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
47 List<Vertex> results = null;
48 Vertex start = query.getStart();
50 results = ((GremlinPipeline)query.getQuery()).cast(Vertex.class).toList();
52 GremlinPipeline pipe = new GremlinPipeline(g);
53 results = process(pipe, (GremlinPipeline)query.getQuery());
60 public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
61 List<Vertex> results = null;
62 Vertex start = query.getStart();
64 results = ((GremlinPipeline)query.getParentQuery()).cast(Vertex.class).toList();
66 GremlinPipeline pipe = new GremlinPipeline(g);
67 results = process(pipe, (GremlinPipeline)query.getParentQuery());
74 public List<Vertex> findParents(Vertex start) {
75 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").inE()
76 .has("isParent", true).outV().loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
79 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
80 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
81 return pipe.inE().has("isParent", true).count() == 1 || argument.getLoops() < 100;
84 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
87 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
93 List<Vertex> results = pipe.toList();
94 results.add(0, start);
99 public List<Vertex> findChildren(Vertex start) {
100 Set<Vertex> seen = new HashSet<>();
102 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").outE().has("isParent", true).inV()
103 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
106 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
107 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
108 return pipe.outE().has("isParent", true).count() >= 1 || argument.getLoops() < 100;
111 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
114 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
120 List<Vertex> results = pipe.toList();
121 results.add(0, start);
126 public List<Vertex> findDeletable(Vertex start) {
127 Set<Vertex> seen = new HashSet<>();
129 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start).as("x").outE().or(
130 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
131 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).inV()
132 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
135 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
136 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
137 return pipe.outE().or(
138 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
139 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).count() >= 1 || argument.getLoops() < 100;
142 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
145 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
150 List<Vertex> results = pipe.toList();
151 results.add(0, start);
156 private List<Vertex> process(GremlinPipeline start, GremlinPipeline pipe) {
159 return start.add(pipe).cast(Vertex.class).toList();
163 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
164 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start);
179 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
180 List<Vertex> result = pipe.toList();