2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.engines.query;/*-
23 * ============LICENSE_START=======================================================
25 * ================================================================================
26 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
27 * ================================================================================
28 * Licensed under the Apache License, Version 2.0 (the "License");
29 * you may not use this file except in compliance with the License.
30 * You may obtain a copy of the License at
32 * http://www.apache.org/licenses/LICENSE-2.0
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS,
36 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 * See the License for the specific language governing permissions and
38 * limitations under the License.
39 * ============LICENSE_END=========================================================
43 package org.onap.aai.serialization.engines.query;
45 import java.util.HashSet;
46 import java.util.List;
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.TransactionalGraph;
54 import com.tinkerpop.blueprints.Vertex;
55 import com.tinkerpop.gremlin.java.GremlinPipeline;
56 import com.tinkerpop.pipes.IdentityPipe;
57 import com.tinkerpop.pipes.PipeFunction;
58 import com.tinkerpop.pipes.branch.LoopPipe;
60 public class GremlinPipelineQueryEngine extends QueryEngine {
62 public GremlinPipelineQueryEngine(TransactionalGraphEngine graphEngine) {
67 public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
68 List<Vertex> results = null;
69 Vertex start = query.getStart();
71 results = ((GremlinPipeline)query.getQuery()).cast(Vertex.class).toList();
73 GremlinPipeline pipe = new GremlinPipeline(g);
74 results = process(pipe, (GremlinPipeline)query.getQuery());
81 public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
82 List<Vertex> results = null;
83 Vertex start = query.getStart();
85 results = ((GremlinPipeline)query.getParentQuery()).cast(Vertex.class).toList();
87 GremlinPipeline pipe = new GremlinPipeline(g);
88 results = process(pipe, (GremlinPipeline)query.getParentQuery());
95 public List<Vertex> findParents(Vertex start) {
96 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").inE()
97 .has("isParent", true).outV().loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
100 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
101 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
102 return pipe.inE().has("isParent", true).count() == 1 || argument.getLoops() < 100;
105 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
108 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
114 List<Vertex> results = pipe.toList();
115 results.add(0, start);
120 public List<Vertex> findChildren(Vertex start) {
121 Set<Vertex> seen = new HashSet<>();
123 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").outE().has("isParent", true).inV()
124 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
127 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
128 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
129 return pipe.outE().has("isParent", true).count() >= 1 || argument.getLoops() < 100;
132 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
135 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
141 List<Vertex> results = pipe.toList();
142 results.add(0, start);
147 public List<Vertex> findDeletable(Vertex start) {
148 Set<Vertex> seen = new HashSet<>();
150 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start).as("x").outE().or(
151 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
152 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).inV()
153 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
156 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
157 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
158 return pipe.outE().or(
159 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
160 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).count() >= 1 || argument.getLoops() < 100;
163 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
166 public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
171 List<Vertex> results = pipe.toList();
172 results.add(0, start);
177 private List<Vertex> process(GremlinPipeline start, GremlinPipeline pipe) {
180 return start.add(pipe).cast(Vertex.class).toList();
184 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
185 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start);
200 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
201 List<Vertex> result = pipe.toList();