* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
* ============LICENSE_END=========================================================
*/
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal.Admin;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
import org.apache.tinkerpop.gremlin.structure.Direction;
+import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
-
import org.openecomp.aai.db.props.AAIProperties;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Introspector;
/**
* The Class GraphTraversalBuilder.
*/
-public abstract class GraphTraversalBuilder extends QueryBuilder {
+public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> {
- protected GraphTraversal<Vertex, Vertex> traversal = null;
- protected Admin<Vertex, Vertex> completeTraversal = null;
+ protected GraphTraversal<Vertex, E> traversal = null;
+ protected Admin<Vertex, E> completeTraversal = null;
private EdgeRules edgeRules = EdgeRules.getInstance();
protected int parentStepIndex = 0;
public GraphTraversalBuilder(Loader loader, GraphTraversalSource source) {
super(loader, source);
- traversal = __.start();
+ traversal = new DefaultGraphTraversal<>();
}
public GraphTraversalBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
super(loader, source, start);
- traversal = __.start();
+ traversal = new DefaultGraphTraversal<>();
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByIndexedProperty(String key, Object value) {
+ public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
return this.getVerticesByProperty(key, value);
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByIndexedProperty(String key, List<?> values) {
+ public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
return this.getVerticesByProperty(key, values);
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByProperty(String key, Object value) {
+ public QueryBuilder<Vertex> getVerticesByProperty(String key, Object value) {
//this is because the index is registered as an Integer
value = this.correctObjectType(value);
traversal.has(key, value);
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getVerticesByProperty(final String key, final List<?> values) {
+ public QueryBuilder<Vertex> getVerticesByProperty(final String key, final List<?> values) {
//this is because the index is registered as an Integer
List<Object> correctedValues = new ArrayList<>();
traversal.has(key, P.within(correctedValues));
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
+ public QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
traversal.has(parentKey, parentValue).has(AAIProperties.NODE_TYPE, childType);
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getTypedVerticesByMap(String type, LinkedHashMap<String, String> map) {
+ public QueryBuilder<Vertex> getTypedVerticesByMap(String type, LinkedHashMap<String, String> map) {
for (String key : map.keySet()) {
traversal.has(key, map.get(key));
}
traversal.has(AAIProperties.NODE_TYPE, type);
stepIndex++;
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder createDBQuery(Introspector obj) {
+ public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
this.createKeyQuery(obj);
this.createContainerQuery(obj);
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder createKeyQuery(Introspector obj) {
+ public QueryBuilder<Vertex> createKeyQuery(Introspector obj) {
Set<String> keys = obj.getKeys();
Object val;
for (String key : keys) {
stepIndex++;
}
}
- return this;
+ return (QueryBuilder<Vertex>) this;
}
@Override
- public QueryBuilder exactMatchQuery(Introspector obj) {
+ public QueryBuilder<Vertex> exactMatchQuery(Introspector obj) {
this.createKeyQuery(obj);
allPropertiesQuery(obj);
this.createContainerQuery(obj);
- return this;
+ return (QueryBuilder<Vertex>) this;
}
private void allPropertiesQuery(Introspector obj) {
*/
@Override
- public QueryBuilder createContainerQuery(Introspector obj) {
+ public QueryBuilder<Vertex> createContainerQuery(Introspector obj) {
String type = obj.getChildDBName();
String abstractType = obj.getMetadata(ObjectMetadata.ABSTRACT);
if (abstractType != null) {
}
stepIndex++;
markContainer();
- return this;
+ return (QueryBuilder<Vertex>) this;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException, NoEdgeRuleFoundException {
+ public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException, NoEdgeRuleFoundException {
String isAbstractType = parent.getMetadata(ObjectMetadata.ABSTRACT);
if ("true".equals(isAbstractType)) {
markParentBoundary();
traversal.union(handleAbstractEdge(type, parent, child));
stepIndex += 1;
} else {
- this.edgeQuery(type, parent, child);
+ this.edgeQueryToVertex(type, parent, child);
}
- return this;
+ return (QueryBuilder<Vertex>) this;
}
* @{inheritDoc}
*/
@Override
- public QueryBuilder createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException, NoEdgeRuleFoundException {
+ public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException, NoEdgeRuleFoundException {
String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
Introspector parentObj = loader.introspectorFromName(nodeType);
- this.edgeQuery(type, parentObj, child);
- return this;
+ this.edgeQueryToVertex(type, parentObj, child);
+ return (QueryBuilder<Vertex>) this;
}
+ @Override
+ public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
+ Introspector outObj = loader.introspectorFromName(outNodeType);
+ Introspector inObj = loader.introspectorFromName(inNodeType);
+ this.edgeQuery(type, outObj, inObj);
+
+ return (QueryBuilder<Edge>)this;
+
+ }
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder union(QueryBuilder... builder) {
+ public QueryBuilder<E> union(QueryBuilder... builder) {
GraphTraversal<Vertex, Vertex>[] traversals = new GraphTraversal[builder.length];
for (int i = 0; i < builder.length; i++) {
traversals[i] = (GraphTraversal<Vertex, Vertex>)builder[i].getQuery();
* @{inheritDoc}
*/
@Override
- public QueryBuilder where(QueryBuilder... builder) {
+ public QueryBuilder<E> where(QueryBuilder... builder) {
GraphTraversal<Vertex, Vertex>[] traversals = new GraphTraversal[builder.length];
for (int i = 0; i < builder.length; i++) {
this.traversal.where((GraphTraversal<Vertex, Vertex>)builder[i].getQuery());
return this;
}
+ @Override
+ public QueryBuilder<E> store(String name) {
+
+ this.traversal.store(name);
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> cap(String name) {
+ this.traversal.cap(name);
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> unfold() {
+ this.traversal.unfold();
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> dedup() {
+
+ this.traversal.dedup();
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> emit() {
+
+ this.traversal.emit();
+ stepIndex++;
+
+ return this;
+
+ }
+
+ @Override
+ public QueryBuilder<E> repeat(QueryBuilder<E> builder) {
+
+ this.traversal.repeat((GraphTraversal<Vertex, E>)builder.getQuery());
+ stepIndex++;
+
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<Edge> outE() {
+ this.traversal.outE();
+ stepIndex++;
+ return (QueryBuilder<Edge>)this;
+ }
+
+ @Override
+ public QueryBuilder<Edge> inE() {
+ this.traversal.inE();
+ stepIndex++;
+ return (QueryBuilder<Edge>)this;
+ }
+
+ @Override
+ public QueryBuilder<Vertex> outV() {
+ this.traversal.outV();
+ stepIndex++;
+ return (QueryBuilder<Vertex>)this;
+ }
+
+ @Override
+ public QueryBuilder<Vertex> inV() {
+ this.traversal.inV();
+ stepIndex++;
+ return (QueryBuilder<Vertex>)this;
+ }
+
+ @Override
+ public QueryBuilder<E> as(String name) {
+ this.traversal.as(name);
+
+ stepIndex++;
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> not(QueryBuilder<E> builder) {
+ this.traversal.not(builder.getQuery());
+
+ stepIndex++;
+ return this;
+ }
+
+ @Override
+ public QueryBuilder<E> select(String name) {
+ this.traversal.select(name);
+
+ stepIndex++;
+
+ return this;
+ }
+
/**
* Edge query.
*
* @throws NoEdgeRuleFoundException
* @throws AAIException
*/
- private void edgeQuery(EdgeType type, Introspector outObj, Introspector inObj) throws AAIException, NoEdgeRuleFoundException {
+ private void edgeQueryToVertex(EdgeType type, Introspector outObj, Introspector inObj) throws AAIException, NoEdgeRuleFoundException {
String outType = outObj.getDbName();
String inType = inObj.getDbName();
}
stepIndex++;
this.createContainerQuery(inObj);
+
+ }
+
+ /**
+ * Edge query.
+ *
+ * @param outType the out type
+ * @param inType the in type
+ * @throws NoEdgeRuleFoundException
+ * @throws AAIException
+ */
+ private void edgeQuery(EdgeType type, Introspector outObj, Introspector inObj) throws AAIException, NoEdgeRuleFoundException {
+ String outType = outObj.getDbName();
+ String inType = inObj.getDbName();
+
+ if (outObj.isContainer()) {
+ outType = outObj.getChildDBName();
+ }
+ if (inObj.isContainer()) {
+ inType = inObj.getChildDBName();
+ }
+ markParentBoundary();
+ EdgeRule rule = edgeRules.getEdgeRule(type, outType, inType);
+ if (rule.getDirection().equals(Direction.OUT)) {
+ traversal.outE(rule.getLabel());
+ } else {
+ traversal.inE(rule.getLabel());
+ }
+ stepIndex++;
+
}
@Override
- public QueryBuilder limit(long amount) {
+ public QueryBuilder<E> limit(long amount) {
traversal.limit(amount);
return this;
}
* @{inheritDoc}
*/
@Override
- public <T> T getQuery() {
- return (T)this.traversal;
+ public <E2> E2 getQuery() {
+ return (E2)this.traversal;
}
/**
* @{inheritDoc}
*/
@Override
- public QueryBuilder getParentQuery() {
+ public QueryBuilder<E> getParentQuery() {
return cloneQueryAtStep(parentStepIndex);
}
@Override
- public QueryBuilder getContainerQuery() {
+ public QueryBuilder<E> getContainerQuery() {
if (this.parentStepIndex == 0) {
return removeQueryStepsBetween(0, containerStepIndex);
return stepIndex;
}
- protected abstract QueryBuilder cloneQueryAtStep(int index);
+ protected abstract QueryBuilder<E> cloneQueryAtStep(int index);
/**
* end is exclusive
*
* @param end
* @return
*/
- protected abstract QueryBuilder removeQueryStepsBetween(int start, int end);
+ protected abstract QueryBuilder<E> removeQueryStepsBetween(int start, int end);
private void executeQuery() {
TraversalHelper.insertTraversal(admin.getEndStep(), traversal.asAdmin(), admin);
- this.completeTraversal = admin;
+ this.completeTraversal = (Admin<Vertex, E>) admin;
}
@Override
}
@Override
- public Vertex next() {
+ public E next() {
if (this.completeTraversal == null) {
executeQuery();
}
}
@Override
- public List<Vertex> toList() {
+ public List<E> toList() {
if (this.completeTraversal == null) {
executeQuery();
}