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.HashSet;
44 import java.util.List;
47 import org.onap.aai.db.AAIProperties;
48 import org.onap.aai.query.builder.QueryBuilder;
49 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
50 import com.tinkerpop.blueprints.Direction;
51 import com.tinkerpop.blueprints.TransactionalGraph;
52 import com.tinkerpop.blueprints.Vertex;
53 import com.tinkerpop.gremlin.java.GremlinPipeline;
54 import com.tinkerpop.pipes.IdentityPipe;
55 import com.tinkerpop.pipes.PipeFunction;
56 import com.tinkerpop.pipes.branch.LoopPipe;
58 public class GremlinPipelineQueryEngine extends QueryEngine {
60 public GremlinPipelineQueryEngine(TransactionalGraphEngine graphEngine) {
65 public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
66 List<Vertex> results = null;
67 Vertex start = query.getStart();
69 results = ((GremlinPipeline)query.getQuery()).cast(Vertex.class).toList();
71 GremlinPipeline pipe = new GremlinPipeline(g);
72 results = process(pipe, (GremlinPipeline)query.getQuery());
79 public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
80 List<Vertex> results = null;
81 Vertex start = query.getStart();
83 results = ((GremlinPipeline)query.getParentQuery()).cast(Vertex.class).toList();
85 GremlinPipeline pipe = new GremlinPipeline(g);
86 results = process(pipe, (GremlinPipeline)query.getParentQuery());
93 public List<Vertex> findParents(Vertex start) {
94 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").inE()
95 .has("isParent", true).outV().loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
98 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
99 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
100 return pipe.inE().has("isParent", true).count() == 1 || argument.getLoops() < 100;
103 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
106 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
112 List<Vertex> results = pipe.toList();
113 results.add(0, start);
118 public List<Vertex> findChildren(Vertex start) {
119 Set<Vertex> seen = new HashSet<>();
121 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").outE().has("isParent", true).inV()
122 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
125 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
126 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
127 return pipe.outE().has("isParent", true).count() >= 1 || argument.getLoops() < 100;
130 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
133 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
139 List<Vertex> results = pipe.toList();
140 results.add(0, start);
145 public List<Vertex> findDeletable(Vertex start) {
146 Set<Vertex> seen = new HashSet<>();
148 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start).as("x").outE().or(
149 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
150 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).inV()
151 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
154 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
155 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
156 return pipe.outE().or(
157 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
158 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).count() >= 1 || argument.getLoops() < 100;
161 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
164 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
169 List<Vertex> results = pipe.toList();
170 results.add(0, start);
175 private List<Vertex> process(GremlinPipeline start, GremlinPipeline pipe) {
178 return start.add(pipe).cast(Vertex.class).toList();
182 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
183 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start);
198 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
199 List<Vertex> result = pipe.toList();