Java Code Examples for com.tinkerpop.blueprints.Vertex#getEdges()

The following examples show how to use com.tinkerpop.blueprints.Vertex#getEdges() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TeacherStudentTraversal.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private void benchmarkTeacherThroughSectionBlueprint() {
    logger.log(Level.INFO, "Starting...");
    StopWatch bigWatch = new StopWatch();
    for (int i = 0; i < ITERATIONS; ++i) {
        StopWatch subWatch = new StopWatch();
        for (Vertex teacher : graph.getVertices("mongoid", "2012wl-f126bd7d-cfba-11e1-a172-024224a39f1b")) {
            for(Edge  e : teacher.getEdges(Direction.IN, "teacherSection")) {
                for (Edge ss : e.getVertex(Direction.OUT).getEdges(Direction.OUT, "studentSection")) {
                    ss.getVertex(Direction.IN);
                }
            }
        }
        subWatch.stop();
        times.add(subWatch.getEndTime());
    }
    bigWatch.stop();
    logger.log(Level.INFO, "Total {0} \t Avg {1} ms", new String[] { bigWatch.inSeconds(), "" + averageTime() });
    
}
 
Example 2
Source File: IncidenceAnnotationHandler.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
public Object processVertex(final Incidence incidence, final Method method, final Object[] arguments, final FramedGraph framedGraph, final Vertex element) {
    if (ClassUtilities.isGetMethod(method)) {
        return new FramedEdgeIterable(framedGraph, element.getEdges(incidence.direction(), incidence.label()), incidence.direction(), ClassUtilities.getGenericClass(method));
    } else if (ClassUtilities.isAddMethod(method)) {
        
        switch(incidence.direction()) {
        case OUT:
            return framedGraph.addEdge(null, element, ((VertexFrame) arguments[0]).asVertex(), incidence.label(), Direction.OUT, method.getReturnType());
        case IN:
            return framedGraph.addEdge(null, ((VertexFrame) arguments[0]).asVertex(), element, incidence.label(), Direction.IN, method.getReturnType());
        case BOTH:
            throw new UnsupportedOperationException("Direction.BOTH it not supported on 'add' or 'set' methods");
        }
            
    } else if (ClassUtilities.isRemoveMethod(method)) {
        framedGraph.removeEdge(((EdgeFrame) arguments[0]).asEdge());
        return null;
    }

    return null;
}
 
Example 3
Source File: TraitRelation.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
    Vertex v = vWrapper.getVertex();
    Collection<VertexWrapper> vertices = new ArrayList<>();
    for (Edge e : v.getEdges(Direction.OUT)) {
        if (e.getLabel().startsWith(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY))) {
            VertexWrapper trait = new TermVertexWrapper(e.getVertex(Direction.IN));
            if (! trait.getPropertyKeys().contains("available_as_tag") && ! isDeleted(trait.getVertex())) {
                vertices.add(trait);
            }
        }
    }
    return Collections.singletonList(new RelationSet("traits", vertices));
}
 
Example 4
Source File: TagRelation.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
    Vertex v = vWrapper.getVertex();
    Collection<VertexWrapper> vertices = new ArrayList<>();
    for (Edge e : v.getEdges(Direction.OUT)) {
        if (e.getLabel().startsWith(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY))) {
            VertexWrapper trait = new TermVertexWrapper(e.getVertex(Direction.IN));
            if (trait.getPropertyKeys().contains("available_as_tag") && ! isDeleted(trait.getVertex())) {
                vertices.add(trait);
            }
        }
    }
    return Collections.singletonList(new RelationSet("tags", vertices));
}
 
Example 5
Source File: GenericRelation.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
    Collection<RelationSet> relations = new ArrayList<>();
    Vertex v = vWrapper.getVertex();
    String vertexType = v.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
    Map<String, Collection<VertexWrapper>> vertexMap = new HashMap<>();
    for (Edge e : v.getEdges(Direction.OUT)) {
        String edgeLabel = e.getLabel();
        String edgePrefix = String.format("%s%s.", Constants.INTERNAL_PROPERTY_KEY_PREFIX, vertexType);
        if (edgeLabel.startsWith(edgePrefix)) {
            Vertex adjacentVertex = e.getVertex(Direction.IN);
            if (! isDeleted(adjacentVertex)) {
                VertexWrapper relationVertex = new VertexWrapper(adjacentVertex, resourceDefinition);
                String relationName = edgeLabel.substring(edgePrefix.length());
                Collection<VertexWrapper> vertices = vertexMap.get(relationName);
                if (vertices == null) {
                    vertices = new ArrayList<>();
                    vertexMap.put(relationName, vertices);
                }
                vertices.add(relationVertex);
            }
        }
    }
    for (Map.Entry<String, Collection<VertexWrapper>> entry : vertexMap.entrySet()) {
        relations.add(new RelationSet(entry.getKey(), entry.getValue()));
    }
    return relations;
}
 
Example 6
Source File: GraphUtils.java    From antiquity with GNU General Public License v3.0 5 votes vote down vote up
public static String getVerticesAsString(Iterable<Vertex> vertices, boolean withInternals) {
    StringBuilder graphStr = new StringBuilder();
    VersionedElementUtils utils = new VersionedElementUtils();

    for (Vertex v : vertices) {
        if (!withInternals && utils.isInternal(v)) {
            continue;
        }

        graphStr.append("Vertex [").append(v.getId()).append("]\n");
        graphStr.append("\tProps:\n");

        Vertex vr = v;
        if (withInternals && v instanceof HistoricVersionedVertex) {
            vr = ((HistoricVersionedVertex) v).getBaseElement();
        }

        for (String key : vr.getPropertyKeys()) {
            graphStr.append("\t\tProp [").append(key).append("] with value [").append(v.getProperty(key))
                    .append("]\n");
        }

        graphStr.append("\n\tEdges:\n");

        Iterable<Edge> edges;
        if (v instanceof HistoricVersionedVertex) {
            edges = ((HistoricVersionedVertex) v).getEdges(Direction.BOTH, withInternals);
        } else {
            edges = v.getEdges(Direction.BOTH);
        }

        for (Edge e : edges) {
            graphStr.append("\t\tEdge [").append(e.getLabel()).append("]\n");
        }

        graphStr.append("\n");
    }

    return graphStr.toString();
}
 
Example 7
Source File: AdjacencyAnnotationHandler.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
private void removeEdges(final Direction direction, final String label, final Vertex element, final Vertex otherVertex, final FramedGraph framedGraph) {
    for (final Edge edge : element.getEdges(direction, label)) {
        if (null == otherVertex || edge.getVertex(direction.opposite()).equals(otherVertex)) {
            framedGraph.removeEdge(edge);
        }
    }
}
 
Example 8
Source File: AbstractEdgeHelper.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public Set<? extends Edge> getEdges(final Vertex vertex) {
	//		System.out.println("DEBUG: Getting edges...");
	Set<Edge> results = null;

	if (sameTypes_) {
		//			System.out.println("DEBUG: Both directions are " + inType_.getSimpleName() + " in helper " + getLabel());
		results = (Set<Edge>) vertex.getEdges(Direction.IN, getLabel());
		if (results.size() == 0) {
			results = (Set<Edge>) vertex.getEdges(Direction.OUT, getLabel());
			//				System.out.println("DEBUG: Returning " + results.size() + " outs");
		} else {
			//				System.out.println("DEBUG: Returning " + results.size() + " ins");
		}
	} else {
		//			System.out.println("DEBUG: Analyzing directions for helper " + getLabel() + " with a vertex of "
		//					+ vertex.getClass().getSimpleName());

		if (getInType().equals(vertex.getClass())) {
			results = (Set<Edge>) vertex.getEdges(Direction.IN, getLabel());
			//				System.out.println("DEBUG: " + vertex.getClass().getSimpleName() + " is the IN type");
		} else if (getOutType().equals(vertex.getClass())) {
			results = (Set<Edge>) vertex.getEdges(Direction.OUT, getLabel());
			//				System.out.println("DEBUG: " + vertex.getClass().getSimpleName() + " is the OUT type");
		} else if (getInType().isAssignableFrom(vertex.getClass())) {
			results = (Set<Edge>) vertex.getEdges(Direction.IN, getLabel());
			//				System.out.println("DEBUG: " + vertex.getClass().getSimpleName() + " can be assigned as the IN type");
		} else if (getOutType().isAssignableFrom(vertex.getClass())) {
			results = (Set<Edge>) vertex.getEdges(Direction.OUT, getLabel());
			//				System.out.println("DEBUG: " + vertex.getClass().getSimpleName() + " can be assigned as the OUT type");
		}
	}
	if (results == null) {
		//			System.out.println("DEBUG: Exception " + vertex.getClass().getName() + " is not a participating type in edge " + getLabel());
		throw new EdgeHelperException(vertex.getClass().getName() + " is not a participating type in edge " + getLabel());
	} else {
		//			System.out.println("DEBUG: Returning " + results.size() + " edges for helper " + getLabel());
		return Collections.unmodifiableSet(results);
	}
}
 
Example 9
Source File: AddDependencyRule.java    From light with Apache License 2.0 4 votes vote down vote up
public boolean execute (Object ...objects) throws Exception {
    Map<String, Object> inputMap = (Map<String, Object>)objects[0];
    Map<String, Object> data = (Map<String, Object>)inputMap.get("data");
    String host = (String)data.get("host");
    String source = (String)data.get("source");
    String dest = (String)data.get("desc");
    String error = null;
    Map<String, Object> user = (Map<String, Object>) inputMap.get("user");
    String userHost = (String)user.get("host");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        Vertex sourceRule = DbService.getVertexByRid(graph, source);
        Vertex destRule = DbService.getVertexByRid(graph, dest);
        if(sourceRule == null || destRule == null) {
            error = "source rule or destination rule doesn't exist";
            inputMap.put("responseCode", 400);
        } else {
            String sourceRuleClass = sourceRule.getProperty("ruleClass");
            String destRuleClass = destRule.getProperty("ruleClass");
            if(userHost != null) {
                if (!userHost.equals(host)) {
                    error = "You can only add dependency from host: " + host;
                    inputMap.put("responseCode", 403);
                } else {
                    // make sure dest ruleClass contains host.
                    if(!destRuleClass.contains(host)) {
                        error = "Destination rule doesn't belong to the host " + host;
                        inputMap.put("responseCode", 403);
                    } else {
                        // check if there is an depend edge from source to dest
                        boolean hasEdge = false;
                        for (Edge edge : (Iterable<Edge>) sourceRule.getEdges(Direction.OUT, "Own")) {
                            if(edge.getVertex(Direction.IN) == destRule) hasEdge = true;
                        }
                        if(hasEdge) {
                            error = "There is depend edge between source rule and dest rule";
                            inputMap.put("responseCode", 400);
                        } else {
                            Map eventMap = getEventMap(inputMap);
                            Map<String, Object> eventData = (Map<String, Object>)eventMap.get("data");
                            inputMap.put("eventMap", eventMap);
                            eventData.put("sourceRuleClass", sourceRuleClass);
                            eventData.put("destRuleClass", destRuleClass);
                            eventData.put("content", data.get("content"));
                            eventData.put("createDate", new java.util.Date());
                            eventData.put("createUserId", user.get("userId"));
                        }
                    }
                }
            }

        }
    } catch (Exception e) {
        logger.error("Exception:", e);
        throw e;
    } finally {
        graph.shutdown();
    }
    if(error != null) {
        inputMap.put("result", error);
        return false;
    } else {
        return true;
    }
}
 
Example 10
Source File: VerticesFromEdgesIterable.java    From org.openntf.domino with Apache License 2.0 4 votes vote down vote up
public VerticesFromEdgesIterable(final Vertex vertex, final Direction direction, final String... labels) {
    this.direction = direction;
    this.vertex = vertex;
    this.iterable = vertex.getEdges(direction, labels);
}