예제 #1
0
 private void writeEdges(Writer writer, List<Edge> edges, Map<Vertex, Integer> ids)
     throws IOException {
   for (Edge e : edges) {
     writeEdgeProperties(
         writer, e, ids.get(e.getVertex(Direction.OUT)), ids.get(e.getVertex(Direction.IN)));
   }
 }
  @Override
  public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain)
      throws Exception {
    final String graphEdgeId = getAttributeString(request, "graphEdgeId");
    final String visibilitySource = getRequiredParameter(request, "visibilitySource");

    User user = getUser(request);
    Authorizations authorizations = getAuthorizations(request, user);
    String workspaceId = getActiveWorkspaceId(request);

    Edge graphEdge = graph.getEdge(graphEdgeId, authorizations);
    if (graphEdge == null) {
      respondWithNotFound(response);
      return;
    }

    if (!graph.isVisibilityValid(new Visibility(visibilitySource), authorizations)) {
      LOGGER.warn(
          "%s is not a valid visibility for %s user", visibilitySource, user.getDisplayName());
      respondWithBadRequest(response, "visibilitySource", getString(request, "visibility.invalid"));
      chain.next(request, response);
      return;
    }

    LOGGER.info(
        "changing edge (%s) visibility source to %s",
        graphEdge.getId().toString(), visibilitySource);

    GraphUtil.VisibilityAndElementMutation<Edge> setPropertyResult =
        GraphUtil.updateElementVisibilitySource(
            visibilityTranslator,
            graphEdge,
            GraphUtil.getSandboxStatus(graphEdge, workspaceId),
            visibilitySource,
            workspaceId,
            authorizations);
    auditRepository.auditEdgeElementMutation(
        AuditAction.UPDATE,
        setPropertyResult.elementMutation,
        graphEdge,
        graphEdge.getVertex(Direction.OUT, authorizations),
        graphEdge.getVertex(Direction.IN, authorizations),
        "",
        user,
        setPropertyResult.visibility.getVisibility());

    this.graph.flush();

    this.workQueueRepository.pushGraphPropertyQueue(
        graphEdge,
        null,
        LumifyVisibilityProperties.VISIBILITY_JSON_PROPERTY.getPropertyName(),
        workspaceId,
        visibilitySource);

    JSONObject json = JsonSerializer.toJson(graphEdge, workspaceId, authorizations);
    respondWithJson(response, json);
  }
예제 #3
0
 public List<V> getInboundNeighbors(V vertex) {
   List<V> inboundList = new ArrayList<>();
   for (V to : neighbors.keySet()) {
     for (Edge<V> e : neighbors.get(to)) {
       if (e.getVertex().equals(vertex)) inboundList.add(to);
     }
   }
   return inboundList;
 }
예제 #4
0
  public void testRemoveSelfLoops() {
    Graph graph = graphTest.generateGraph();
    if (graph.getFeatures().supportsSelfLoops) {
      Vertex v1 = graph.addVertex(convertId(graph, "1"));
      Vertex v2 = graph.addVertex(convertId(graph, "2"));
      Vertex v3 = graph.addVertex(convertId(graph, "3"));
      Edge e1 = graph.addEdge(null, v1, v1, convertId(graph, "is_self"));
      Edge e2 = graph.addEdge(null, v2, v2, convertId(graph, "is_self"));
      Edge e3 = graph.addEdge(null, v3, v3, convertId(graph, "is_self"));

      if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices()));
      if (graph.getFeatures().supportsEdgeIteration) {
        assertEquals(3, count(graph.getEdges()));
        for (Edge edge : graph.getEdges()) {
          assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT));
          assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId());
        }
      }

      graph.removeVertex(v1);
      if (graph.getFeatures().supportsEdgeIteration) {
        assertEquals(2, count(graph.getEdges()));
        for (Edge edge : graph.getEdges()) {
          assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT));
          assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId());
        }
      }

      assertEquals(1, count(v2.getEdges(Direction.OUT)));
      assertEquals(1, count(v2.getEdges(Direction.IN)));
      graph.removeEdge(e2);
      assertEquals(0, count(v2.getEdges(Direction.OUT)));
      assertEquals(0, count(v2.getEdges(Direction.IN)));

      if (graph.getFeatures().supportsEdgeIteration) {
        assertEquals(count(graph.getEdges()), 1);
        for (Edge edge : graph.getEdges()) {
          assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT));
          assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId());
        }
      }
    }
    graph.shutdown();
  }
예제 #5
0
  public void testAddingSelfLoops() {
    Graph graph = graphTest.generateGraph();
    if (graph.getFeatures().supportsSelfLoops) {
      Vertex v1 = graph.addVertex(convertId(graph, "1"));
      Vertex v2 = graph.addVertex(convertId(graph, "2"));
      Vertex v3 = graph.addVertex(convertId(graph, "3"));
      graph.addEdge(null, v1, v1, convertId(graph, "is_self"));
      graph.addEdge(null, v2, v2, convertId(graph, "is_self"));
      graph.addEdge(null, v3, v3, convertId(graph, "is_self"));

      if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices()));
      if (graph.getFeatures().supportsEdgeIteration) {
        assertEquals(3, count(graph.getEdges()));
        int counter = 0;
        for (Edge edge : graph.getEdges()) {
          counter++;
          assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT));
          assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId());
        }
        assertEquals(counter, 3);
      }
    }
    graph.shutdown();
  }
예제 #6
0
  public void testEdgeEquality() {
    Graph graph = graphTest.generateGraph();

    Vertex v = graph.addVertex(convertId(graph, "1"));
    Vertex u = graph.addVertex(convertId(graph, "2"));
    Edge e = graph.addEdge(null, v, u, convertId(graph, "knows"));
    assertEquals(e.getLabel(), convertId(graph, "knows"));
    assertEquals(e.getVertex(Direction.IN), u);
    assertEquals(e.getVertex(Direction.OUT), v);
    assertEquals(e, v.getEdges(Direction.OUT).iterator().next());
    assertEquals(e, u.getEdges(Direction.IN).iterator().next());
    assertEquals(
        v.getEdges(Direction.OUT).iterator().next(), u.getEdges(Direction.IN).iterator().next());
    Set<Edge> set = new HashSet<Edge>();
    set.add(e);
    set.add(e);
    set.add(v.getEdges(Direction.OUT).iterator().next());
    set.add(v.getEdges(Direction.OUT).iterator().next());
    set.add(u.getEdges(Direction.IN).iterator().next());
    set.add(u.getEdges(Direction.IN).iterator().next());
    if (graph.getFeatures().supportsEdgeIteration) set.add(graph.getEdges().iterator().next());
    assertEquals(set.size(), 1);
    graph.shutdown();
  }
예제 #7
0
  public void testEdgeIterator() {
    Graph graph = graphTest.generateGraph();
    if (graph.getFeatures().supportsEdgeIteration) {
      Vertex v1 = graph.addVertex(convertId(graph, "1"));
      Vertex v2 = graph.addVertex(convertId(graph, "2"));
      Vertex v3 = graph.addVertex(convertId(graph, "3"));
      Edge e1 = graph.addEdge(null, v1, v2, convertId(graph, "test"));
      Edge e2 = graph.addEdge(null, v2, v3, convertId(graph, "test"));
      Edge e3 = graph.addEdge(null, v3, v1, convertId(graph, "test"));

      if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices()));
      if (graph.getFeatures().supportsEdgeIteration) assertEquals(3, count(graph.getEdges()));

      Set<String> edgeIds = new HashSet<String>();
      int count = 0;
      for (Edge e : graph.getEdges()) {
        count++;
        edgeIds.add(e.getId().toString());
        assertEquals(convertId(graph, "test"), e.getLabel());
        if (e.getId().toString().equals(e1.getId().toString())) {
          assertEquals(v1, e.getVertex(Direction.OUT));
          assertEquals(v2, e.getVertex(Direction.IN));
        } else if (e.getId().toString().equals(e2.getId().toString())) {
          assertEquals(v2, e.getVertex(Direction.OUT));
          assertEquals(v3, e.getVertex(Direction.IN));
        } else if (e.getId().toString().equals(e3.getId().toString())) {
          assertEquals(v3, e.getVertex(Direction.OUT));
          assertEquals(v1, e.getVertex(Direction.IN));
        } else {
          assertTrue(false);
        }
        // System.out.println(e);
      }
      assertEquals(3, count);
      assertEquals(3, edgeIds.size());
      assertTrue(edgeIds.contains(e1.getId().toString()));
      assertTrue(edgeIds.contains(e2.getId().toString()));
      assertTrue(edgeIds.contains(e3.getId().toString()));
    }
    graph.shutdown();
  }
예제 #8
0
  public void testTreeConnectivity() {
    Graph graph = graphTest.generateGraph();
    this.stopWatch();
    int branchSize = 11;
    Vertex start = graph.addVertex(null);
    for (int i = 0; i < branchSize; i++) {
      Vertex a = graph.addVertex(null);
      graph.addEdge(null, start, a, convertId(graph, "test1"));
      for (int j = 0; j < branchSize; j++) {
        Vertex b = graph.addVertex(null);
        graph.addEdge(null, a, b, convertId(graph, "test2"));
        for (int k = 0; k < branchSize; k++) {
          Vertex c = graph.addVertex(null);
          graph.addEdge(null, b, c, convertId(graph, "test3"));
        }
      }
    }

    assertEquals(0, count(start.getEdges(Direction.IN)));
    assertEquals(branchSize, count(start.getEdges(Direction.OUT)));
    for (Edge e : start.getEdges(Direction.OUT)) {
      assertEquals(convertId(graph, "test1"), e.getLabel());
      assertEquals(branchSize, count(e.getVertex(Direction.IN).getEdges(Direction.OUT)));
      assertEquals(1, count(e.getVertex(Direction.IN).getEdges(Direction.IN)));
      for (Edge f : e.getVertex(Direction.IN).getEdges(Direction.OUT)) {
        assertEquals(convertId(graph, "test2"), f.getLabel());
        assertEquals(branchSize, count(f.getVertex(Direction.IN).getEdges(Direction.OUT)));
        assertEquals(1, count(f.getVertex(Direction.IN).getEdges(Direction.IN)));
        for (Edge g : f.getVertex(Direction.IN).getEdges(Direction.OUT)) {
          assertEquals(convertId(graph, "test3"), g.getLabel());
          assertEquals(0, count(g.getVertex(Direction.IN).getEdges(Direction.OUT)));
          assertEquals(1, count(g.getVertex(Direction.IN).getEdges(Direction.IN)));
        }
      }
    }

    int totalVertices = 0;
    for (int i = 0; i < 4; i++) {
      totalVertices = totalVertices + (int) Math.pow(branchSize, i);
    }
    printPerformance(
        graph.toString(), totalVertices, "vertices added in a tree structure", this.stopWatch());

    if (graph.getFeatures().supportsVertexIteration) {
      this.stopWatch();
      Set<Vertex> vertices = new HashSet<Vertex>();
      for (Vertex v : graph.getVertices()) {
        vertices.add(v);
      }
      assertEquals(totalVertices, vertices.size());
      printPerformance(graph.toString(), totalVertices, "vertices iterated", this.stopWatch());
    }

    if (graph.getFeatures().supportsEdgeIteration) {
      this.stopWatch();
      Set<Edge> edges = new HashSet<Edge>();
      for (Edge e : graph.getEdges()) {
        edges.add(e);
      }
      assertEquals(totalVertices - 1, edges.size());
      printPerformance(graph.toString(), totalVertices - 1, "edges iterated", this.stopWatch());
    }
    graph.shutdown();
  }
예제 #9
0
  public void testConnectivityPatterns() {
    Graph graph = graphTest.generateGraph();

    Vertex a = graph.addVertex(convertId(graph, "1"));
    Vertex b = graph.addVertex(convertId(graph, "2"));
    Vertex c = graph.addVertex(convertId(graph, "3"));
    Vertex d = graph.addVertex(convertId(graph, "4"));

    if (graph.getFeatures().supportsVertexIteration) assertEquals(4, count(graph.getVertices()));

    Edge e = graph.addEdge(null, a, b, convertId(graph, "knows"));
    Edge f = graph.addEdge(null, b, c, convertId(graph, "knows"));
    Edge g = graph.addEdge(null, c, d, convertId(graph, "knows"));
    Edge h = graph.addEdge(null, d, a, convertId(graph, "knows"));

    if (graph.getFeatures().supportsEdgeIteration) assertEquals(4, count(graph.getEdges()));

    if (graph.getFeatures().supportsVertexIteration) {
      for (Vertex v : graph.getVertices()) {
        assertEquals(1, count(v.getEdges(Direction.OUT)));
        assertEquals(1, count(v.getEdges(Direction.IN)));
      }
    }

    if (graph.getFeatures().supportsEdgeIteration) {
      for (Edge x : graph.getEdges()) {
        assertEquals(convertId(graph, "knows"), x.getLabel());
      }
    }
    if (!graph.getFeatures().ignoresSuppliedIds) {
      a = graph.getVertex(convertId(graph, "1"));
      b = graph.getVertex(convertId(graph, "2"));
      c = graph.getVertex(convertId(graph, "3"));
      d = graph.getVertex(convertId(graph, "4"));

      assertEquals(1, count(a.getEdges(Direction.IN)));
      assertEquals(1, count(a.getEdges(Direction.OUT)));
      assertEquals(1, count(b.getEdges(Direction.IN)));
      assertEquals(1, count(b.getEdges(Direction.OUT)));
      assertEquals(1, count(c.getEdges(Direction.IN)));
      assertEquals(1, count(c.getEdges(Direction.OUT)));
      assertEquals(1, count(d.getEdges(Direction.IN)));
      assertEquals(1, count(d.getEdges(Direction.OUT)));

      Edge i = graph.addEdge(null, a, b, convertId(graph, "hates"));

      assertEquals(1, count(a.getEdges(Direction.IN)));
      assertEquals(2, count(a.getEdges(Direction.OUT)));
      assertEquals(2, count(b.getEdges(Direction.IN)));
      assertEquals(1, count(b.getEdges(Direction.OUT)));
      assertEquals(1, count(c.getEdges(Direction.IN)));
      assertEquals(1, count(c.getEdges(Direction.OUT)));
      assertEquals(1, count(d.getEdges(Direction.IN)));
      assertEquals(1, count(d.getEdges(Direction.OUT)));

      assertEquals(1, count(a.getEdges(Direction.IN)));
      assertEquals(2, count(a.getEdges(Direction.OUT)));
      for (Edge x : a.getEdges(Direction.OUT)) {
        assertTrue(
            x.getLabel().equals(convertId(graph, "knows"))
                || x.getLabel().equals(convertId(graph, "hates")));
      }
      assertEquals(convertId(graph, "hates"), i.getLabel());
      assertEquals(i.getVertex(Direction.IN).getId().toString(), convertId(graph, "2"));
      assertEquals(i.getVertex(Direction.OUT).getId().toString(), convertId(graph, "1"));
    }

    Set<Object> vertexIds = new HashSet<Object>();
    vertexIds.add(a.getId());
    vertexIds.add(a.getId());
    vertexIds.add(b.getId());
    vertexIds.add(b.getId());
    vertexIds.add(c.getId());
    vertexIds.add(d.getId());
    vertexIds.add(d.getId());
    vertexIds.add(d.getId());
    assertEquals(4, vertexIds.size());
    graph.shutdown();
  }
예제 #10
0
 public boolean isEdgeExists(V from, V to) {
   for (Edge<V> e : neighbors.get(from)) if (e.getVertex().equals(e)) return true;
   return false;
 }
예제 #11
0
 public List<V> getOutboundNeighbors(V vertex) {
   List<V> outboundList = new ArrayList<>();
   for (Edge<V> e : neighbors.get(vertex)) outboundList.add(e.getVertex());
   return outboundList;
 }