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.HashSet;
45 * import java.util.List;
46 * import java.util.Set;
48 * import org.onap.aai.db.AAIProperties;
49 * import org.onap.aai.query.builder.QueryBuilder;
50 * import org.onap.aai.serialization.engines.TransactionalGraphEngine;
51 * import com.tinkerpop.blueprints.Direction;
52 * import com.tinkerpop.blueprints.TransactionalGraph;
53 * import com.tinkerpop.blueprints.Vertex;
54 * import com.tinkerpop.gremlin.java.GremlinPipeline;
55 * import com.tinkerpop.pipes.IdentityPipe;
56 * import com.tinkerpop.pipes.PipeFunction;
57 * import com.tinkerpop.pipes.branch.LoopPipe;
59 * public class GremlinPipelineQueryEngine extends QueryEngine {
61 * public GremlinPipelineQueryEngine(TransactionalGraphEngine graphEngine) {
66 * public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
67 * List<Vertex> results = null;
68 * Vertex start = query.getStart();
69 * if (start != null) {
70 * results = ((GremlinPipeline)query.getQuery()).cast(Vertex.class).toList();
72 * GremlinPipeline pipe = new GremlinPipeline(g);
73 * results = process(pipe, (GremlinPipeline)query.getQuery());
80 * public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
81 * List<Vertex> results = null;
82 * Vertex start = query.getStart();
83 * if (start != null) {
84 * results = ((GremlinPipeline)query.getParentQuery()).cast(Vertex.class).toList();
86 * GremlinPipeline pipe = new GremlinPipeline(g);
87 * results = process(pipe, (GremlinPipeline)query.getParentQuery());
94 * public List<Vertex> findParents(Vertex start) {
95 * GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").inE()
96 * .has("isParent", true).outV().loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
99 * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
100 * GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
101 * return pipe.inE().has("isParent", true).count() == 1 || argument.getLoops() < 100;
104 * }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
107 * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
113 * List<Vertex> results = pipe.toList();
114 * results.add(0, start);
119 * public List<Vertex> findChildren(Vertex start) {
120 * Set<Vertex> seen = new HashSet<>();
122 * GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").outE().has("isParent", true).inV()
123 * .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
126 * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
127 * GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
128 * return pipe.outE().has("isParent", true).count() >= 1 || argument.getLoops() < 100;
131 * }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
134 * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
140 * List<Vertex> results = pipe.toList();
141 * results.add(0, start);
146 * public List<Vertex> findDeletable(Vertex start) {
147 * Set<Vertex> seen = new HashSet<>();
149 * GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start).as("x").outE().or(
150 * new GremlinPipeline(new IdentityPipe()).has("isParent", true),
151 * new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).inV()
152 * .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
155 * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
156 * GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
157 * return pipe.outE().or(
158 * new GremlinPipeline(new IdentityPipe()).has("isParent", true),
159 * new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).count() >= 1 || argument.getLoops() < 100;
162 * }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
165 * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
170 * List<Vertex> results = pipe.toList();
171 * results.add(0, start);
176 * private List<Vertex> process(GremlinPipeline start, GremlinPipeline pipe) {
179 * return start.add(pipe).cast(Vertex.class).toList();
183 * public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
184 * GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start);
185 * switch (direction) {
199 * pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
200 * List<Vertex> result = pipe.toList();