AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / GremlinPipelineQueryEngine.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.engines.query;/*-
22                                                  * ============LICENSE_START=======================================================
23                                                  * org.onap.aai
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
30                                                  * 
31                                                  *      http://www.apache.org/licenses/LICENSE-2.0
32                                                  * 
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=========================================================
39                                                  */
40
41 /*
42  * package org.onap.aai.serialization.engines.query;
43  * 
44  * import java.util.HashSet;
45  * import java.util.List;
46  * import java.util.Set;
47  * 
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;
58  * 
59  * public class GremlinPipelineQueryEngine extends QueryEngine {
60  * 
61  * public GremlinPipelineQueryEngine(TransactionalGraphEngine graphEngine) {
62  * super(graphEngine);
63  * }
64  * 
65  * @Override
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();
71  * } else {
72  * GremlinPipeline pipe = new GremlinPipeline(g);
73  * results = process(pipe, (GremlinPipeline)query.getQuery());
74  * 
75  * }
76  * return results;
77  * }
78  * 
79  * @Override
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();
85  * } else {
86  * GremlinPipeline pipe = new GremlinPipeline(g);
87  * results = process(pipe, (GremlinPipeline)query.getParentQuery());
88  * 
89  * }
90  * return results;
91  * }
92  * 
93  * @Override
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>() {
97  * 
98  * @Override
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;
102  * }
103  * 
104  * }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
105  * 
106  * @Override
107  * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
108  * return true;
109  * }
110  * 
111  * });
112  * 
113  * List<Vertex> results = pipe.toList();
114  * results.add(0, start);
115  * return results;
116  * }
117  * 
118  * @Override
119  * public List<Vertex> findChildren(Vertex start) {
120  * Set<Vertex> seen = new HashSet<>();
121  * seen.add(start);
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>() {
124  * 
125  * @Override
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;
129  * }
130  * 
131  * }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
132  * 
133  * @Override
134  * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
135  * return true;
136  * }
137  * 
138  * });
139  * 
140  * List<Vertex> results = pipe.toList();
141  * results.add(0, start);
142  * return results;
143  * }
144  * 
145  * @Override
146  * public List<Vertex> findDeletable(Vertex start) {
147  * Set<Vertex> seen = new HashSet<>();
148  * seen.add(start);
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>() {
153  * 
154  * @Override
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;
160  * }
161  * 
162  * }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
163  * 
164  * @Override
165  * public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
166  * return true;
167  * }
168  * 
169  * });
170  * List<Vertex> results = pipe.toList();
171  * results.add(0, start);
172  * 
173  * return results;
174  * }
175  * 
176  * private List<Vertex> process(GremlinPipeline start, GremlinPipeline pipe) {
177  * 
178  * 
179  * return start.add(pipe).cast(Vertex.class).toList();
180  * }
181  * 
182  * @Override
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) {
186  * case OUT:
187  * pipe.out(label);
188  * break;
189  * case IN:
190  * pipe.in(label);
191  * break;
192  * case BOTH:
193  * pipe.both(label);
194  * break;
195  * default:
196  * break;
197  * }
198  * 
199  * pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
200  * List<Vertex> result = pipe.toList();
201  * return result;
202  * }
203  * 
204  * }
205  */