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.tinkerpop;
22 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
23 import org.apache.tinkerpop.gremlin.structure.*;
24 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
32 * Represents a {@link Vertex} that is disconnected from a {@link Graph} however,
33 * traversals are supported as they are backed by a Tree with saturated {@link Vertex} and {@link Edge} objects.
34 * These objects are not mutable and can only be used to read information out.
38 public class TreeBackedVertex extends DetachedVertex implements Vertex {
40 private static final long serialVersionUID = -976854460992756953L;
41 private final Tree<Element> tree;
42 private final Vertex self;
43 public TreeBackedVertex (Vertex v, Tree<Element> tree) {
50 public Iterator<Edge> edges(final Direction direction, final String... edgeLabels) {
51 final List<Element> edges = tree.getObjectsAtDepth(2);
52 final List<Tree<Element>> trees = tree.getTreesAtDepth(2);
53 final List<Tree<Element>> vTrees = tree.getTreesAtDepth(3);
54 return edges.stream().map( ele -> (Edge)ele).filter(e -> {
55 if (Direction.IN.equals(direction)) {
56 return e.inVertex().equals(self);
57 } else if (Direction.OUT.equals(direction)) {
58 return e.outVertex().equals(self);
63 boolean result = false;
64 if (edgeLabels.length == 0) {
67 for (String label : edgeLabels) {
68 if (label.equals(e.label())) {
75 Tree<Element> eTree = new Tree<>();
76 for (Tree<Element> tree : trees) {
77 if (tree.keySet().contains(e)) {
82 TreeBackedVertex in = null;
83 TreeBackedVertex out = null;
84 if (e.inVertex().equals(self)) {
86 out = this.createForVertex(e.outVertex(), vTrees);
87 } else if (e.outVertex().equals(self)) {
89 in = this.createForVertex(e.inVertex(), vTrees);
91 return (Edge)new TreeBackedEdge(e, in, out);
96 private TreeBackedVertex createForVertex(Vertex v, List<Tree<Element>> trees) {
97 Tree<Element> vTree = new Tree<>();
98 for (Tree<Element> tree : trees) {
99 if (tree.keySet().contains(v)) {
105 return new TreeBackedVertex((Vertex)vTree.keySet().iterator().next(), vTree);
108 public Iterator<Vertex> vertices(final Direction direction, final String... labels) {
109 final List<Tree<Element>> vertexElements = tree.getTreesAtDepth(3);
110 final List<Element> edgeElements = tree.getObjectsAtDepth(2);
111 return edgeElements.stream().map( ele -> (Edge)ele).filter(e -> {
112 boolean result = false;
113 if (labels.length == 0) {
116 for (String label : labels) {
117 if (label.equals(e.label())) {
124 if (Direction.IN.equals(direction) && e.inVertex().equals(self)) {
126 } else if (Direction.OUT.equals(direction) && e.outVertex().equals(self)) {
128 } else if (Direction.BOTH.equals(direction)){
134 final List<Vertex> list;
135 if (Direction.IN.equals(direction)) {
136 list = Collections.singletonList(e.outVertex());
137 } else if (Direction.OUT.equals(direction)){
138 list = Collections.singletonList(e.inVertex());
140 list = new ArrayList<>();
141 Iterator<Vertex> itr = e.bothVertices();
142 while (itr.hasNext()) {
143 list.add(itr.next());
148 }).flatMap(list -> list.stream()).map(v -> {
149 Tree<Element> vTree = new Tree<Element>();
150 for (Tree<Element> tree : vertexElements) {
151 if (tree.keySet().contains(v)) {
157 return (Vertex)new TreeBackedVertex(v, vTree);