Пример #1
0
 public void testGetNonExistantVertices(final Graph graph) {
   try {
     assertNull(graph.getVertex("asbv"));
     assertNull(graph.getVertex(12.0d));
     assertNull(graph.getVertex(null));
   } catch (RuntimeException e) {
     assertTrue(true);
   }
 }
Пример #2
0
 public void testStringRepresentationOfVertexId() {
   final Graph graph = graphTest.generateGraph();
   final Vertex a = graph.addVertex(null);
   final Object id = a.getId();
   final Vertex b = graph.getVertex(id);
   final Vertex c = graph.getVertex(id.toString());
   assertEquals(a, b);
   assertEquals(b, c);
   assertEquals(c, a);
   graph.shutdown();
 }
Пример #3
0
  public void testVertexEquality(final Graph graph) {
    List<String> ids = generateIds(1);

    if (!config.ignoresSuppliedIds) {
      Vertex v = graph.addVertex(convertId(ids.get(0)));
      Vertex u = graph.getVertex(convertId(ids.get(0)));
      assertEquals(v, u);
    }

    this.stopWatch();
    Vertex v = graph.addVertex(null);
    Vertex u = graph.getVertex(v.getId());
    BaseTest.printPerformance(graph.toString(), 1, "vertex added and retrieved", this.stopWatch());

    assertEquals(v, u);
    assertEquals(graph.getVertex(u.getId()), graph.getVertex(u.getId()));
    assertEquals(graph.getVertex(v.getId()), graph.getVertex(u.getId()));
    assertEquals(graph.getVertex(v.getId()), graph.getVertex(v.getId()));

    graph.clear();

    if (!config.ignoresSuppliedIds) {
      v = graph.addVertex(convertId(ids.get(0)));
      u = graph.getVertex(convertId(ids.get(0)));
      Set<Vertex> set = new HashSet<Vertex>();
      set.add(v);
      set.add(v);
      set.add(u);
      set.add(u);
      set.add(graph.getVertex(convertId(ids.get(0))));
      set.add(graph.getVertex(convertId(ids.get(0))));
      if (config.supportsVertexIndex) set.add(graph.getVertices().iterator().next());
      assertEquals(1, set.size());
    }
  }
Пример #4
0
  private void saveVertexRelationships(
      List<VertexRelationship> vertexRelationships,
      List<TermMentionWithGraphVertex> termMentionsWithGraphVertices) {
    for (VertexRelationship vertexRelationship : vertexRelationships) {
      TermMentionWithGraphVertex sourceTermMentionsWithGraphVertex =
          findTermMentionWithGraphVertex(
              termMentionsWithGraphVertices, vertexRelationship.getSource());
      checkNotNull(sourceTermMentionsWithGraphVertex, "Could not find source");
      checkNotNull(sourceTermMentionsWithGraphVertex.getVertex(), "Could not find source vertex");

      Vertex targetVertex = graph.getVertex(vertexRelationship.getTargetId(), getAuthorizations());
      checkNotNull(sourceTermMentionsWithGraphVertex, "Could not find target vertex");

      String label = vertexRelationship.getLabel();
      checkNotNull(label, "label is required");

      Edge edge =
          trySingle(
              sourceTermMentionsWithGraphVertex
                  .getVertex()
                  .getEdges(targetVertex, Direction.OUT, label, getAuthorizations()));
      if (edge == null) {
        LOGGER.debug(
            "adding edge %s -> %s (%s)",
            sourceTermMentionsWithGraphVertex.getVertex().getId(), targetVertex.getId(), label);
        graph.addEdge(
            sourceTermMentionsWithGraphVertex.getVertex(),
            targetVertex,
            label,
            vertexRelationship.getVisibility(),
            getAuthorizations());
      }
    }
  }
Пример #5
0
  public void testRemoveEdgesByRemovingVertex() {
    Graph graph = graphTest.generateGraph();

    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, v2, convertId(graph, "knows"));
    graph.addEdge(null, v2, v3, convertId(graph, "pets"));
    graph.addEdge(null, v2, v3, convertId(graph, "pets"));

    assertEquals(0, count(v1.getEdges(Direction.IN)));
    assertEquals(1, count(v1.getEdges(Direction.OUT)));
    assertEquals(1, count(v2.getEdges(Direction.IN)));
    assertEquals(0, count(v3.getEdges(Direction.OUT)));

    if (!graph.getFeatures().ignoresSuppliedIds) {
      v1 = graph.getVertex(convertId(graph, "1"));
      v2 = graph.getVertex(convertId(graph, "2"));
      v3 = graph.getVertex(convertId(graph, "3"));

      assertEquals(0, count(v1.getEdges(Direction.IN)));
      assertEquals(1, count(v1.getEdges(Direction.OUT)));
      assertEquals(1, count(v2.getEdges(Direction.IN)));
      assertEquals(0, count(v3.getEdges(Direction.OUT)));
    }

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

    graph.removeVertex(v1);

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

    if (graph.getFeatures().supportsDuplicateEdges)
      assertEquals(2, count(v2.getEdges(Direction.OUT)));
    else assertEquals(1, count(v2.getEdges(Direction.OUT)));

    assertEquals(0, count(v3.getEdges(Direction.OUT)));
    assertEquals(0, count(v2.getEdges(Direction.IN)));

    if (graph.getFeatures().supportsDuplicateEdges)
      assertEquals(2, count(v3.getEdges(Direction.IN)));
    else assertEquals(1, count(v3.getEdges(Direction.IN)));

    graph.shutdown();
  }
Пример #6
0
  public void testRemoveEdgesByRemovingVertex() {
    Graph graph = graphTest.getGraphInstance();
    List<String> ids = generateIds(3);

    Vertex v1 = graph.addVertex(convertId(ids.get(0)));
    Vertex v2 = graph.addVertex(convertId(ids.get(1)));
    Vertex v3 = graph.addVertex(convertId(ids.get(2)));
    graph.addEdge(null, v1, v2, convertId("knows"));
    graph.addEdge(null, v2, v3, convertId("pets"));
    graph.addEdge(null, v2, v3, convertId("pets"));

    assertEquals(0, count(v1.getInEdges()));
    assertEquals(1, count(v1.getOutEdges()));
    assertEquals(1, count(v2.getInEdges()));
    assertEquals(0, count(v3.getOutEdges()));

    if (!graphTest.ignoresSuppliedIds) {
      v1 = graph.getVertex(convertId(ids.get(0)));
      v2 = graph.getVertex(convertId(ids.get(1)));
      v3 = graph.getVertex(convertId(ids.get(2)));

      assertEquals(0, count(v1.getInEdges()));
      assertEquals(1, count(v1.getOutEdges()));
      assertEquals(1, count(v2.getInEdges()));
      assertEquals(0, count(v3.getOutEdges()));
    }

    if (graphTest.supportsVertexIteration) assertEquals(3, count(graph.getVertices()));

    graph.removeVertex(v1);

    if (graphTest.supportsVertexIteration) assertEquals(2, count(graph.getVertices()));

    if (graphTest.allowsDuplicateEdges) assertEquals(2, count(v2.getOutEdges()));
    else assertEquals(1, count(v2.getOutEdges()));

    assertEquals(0, count(v3.getOutEdges()));
    assertEquals(0, count(v2.getInEdges()));

    if (graphTest.allowsDuplicateEdges) assertEquals(2, count(v3.getInEdges()));
    else assertEquals(1, count(v3.getInEdges()));

    graph.shutdown();
  }
Пример #7
0
  public static void main(String[] args) {
    String[] vertices = {
      "Seattle",
      "San Francisco",
      "Los Angeles",
      "Denver",
      "Kansas City",
      "Chicago",
      "Boston",
      "New York",
      "Atlanta",
      "Miami",
      "Dallas",
      "Houston"
    };

    int[][] edges = {
      {0, 1}, {0, 3}, {0, 5}, {1, 0}, {1, 2}, {1, 3}, {2, 1}, {2, 3}, {2, 4}, {2, 10}, {3, 0},
      {3, 1}, {3, 2}, {3, 4}, {3, 5}, {4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10}, {5, 0},
      {5, 3}, {5, 4}, {5, 6}, {5, 7}, {6, 5}, {6, 7}, {7, 4}, {7, 5}, {7, 6}, {7, 8}, {8, 4},
      {8, 7}, {8, 9}, {8, 10}, {8, 11}, {9, 8}, {9, 11}, {10, 2}, {10, 4}, {10, 8}, {10, 11},
      {11, 8}, {11, 9}, {11, 10}
    };

    Graph<String> graph = new UnweightedGraph<String>(edges, vertices);
    AbstractGraph<String>.Tree dfs = graph.dfs(5); // 5 is Chicago

    java.util.List<Integer> searchOrders = dfs.getSearchOrders();
    System.out.println(
        dfs.getNumberOfVerticesFound() + " vertices are searched in this DFS order:");
    for (int i = 0; i < searchOrders.size(); i++)
      System.out.print(graph.getVertex(searchOrders.get(i)) + " ");
    System.out.println();

    for (int i = 0; i < searchOrders.size(); i++)
      if (dfs.getParent(i) != -1)
        System.out.println(
            "parent of " + graph.getVertex(i) + " is " + graph.getVertex(dfs.getParent(i)));
  }
Пример #8
0
  @Override
  public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain)
      throws Exception {
    User user = getUser(request);
    Authorizations authorizations = getAuthorizations(request, user);
    String workspaceId = getWorkspaceId(request);

    String graphVertexId = (String) request.getAttribute("graphVertexId");
    long offset = getOptionalParameterLong(request, "offset", 0);
    long size = getOptionalParameterLong(request, "size", 25);

    Vertex vertex = graph.getVertex(graphVertexId, authorizations);
    if (vertex == null) {
      respondWithNotFound(response);
      return;
    }

    Iterable<Edge> edges = vertex.getEdges(Direction.BOTH, authorizations);

    JSONObject json = new JSONObject();
    JSONArray relationshipsJson = new JSONArray();
    long referencesAdded = 0, skipped = 0, totalReferences = 0;
    for (Edge edge : edges) {
      Vertex otherVertex = edge.getOtherVertex(vertex.getId(), authorizations);
      if (otherVertex == null) { // user doesn't have access to other side of edge
        continue;
      }

      if (edge.getLabel().equals("http://lumify.io/dev#rawHasEntity")) {
        totalReferences++;
        if (referencesAdded >= size) continue;
        if (skipped < offset) {
          skipped++;
          continue;
        }

        referencesAdded++;
      }

      JSONObject relationshipJson = new JSONObject();
      relationshipJson.put("relationship", toJson(edge, workspaceId));
      relationshipJson.put("vertex", toJson(otherVertex, workspaceId));
      relationshipsJson.put(relationshipJson);
    }
    json.put("totalReferences", totalReferences);
    json.put("relationships", relationshipsJson);

    respondWithJson(response, json);
  }
  public void testReadingTinkerGraph(Graph graph) throws Exception {
    if (!config.ignoresSuppliedIds) {

      this.stopWatch();
      GraphMLReader.inputGraph(
          graph, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
      BaseTest.printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());

      assertEquals(count(graph.getVertex("1").getOutEdges()), 3);
      assertEquals(count(graph.getVertex("1").getInEdges()), 0);
      Vertex marko = graph.getVertex("1");
      assertEquals(marko.getProperty("name"), "marko");
      assertEquals(marko.getProperty("age"), 29);
      int counter = 0;
      for (Edge e : graph.getVertex("1").getOutEdges()) {
        if (e.getInVertex().getId().equals("2")) {
          // assertEquals(e.getProperty("weight"), 0.5);
          assertEquals(e.getLabel(), "knows");
          assertEquals(e.getId(), "7");
          counter++;
        } else if (e.getInVertex().getId().equals("3")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 0);
          assertEquals(e.getLabel(), "created");
          assertEquals(e.getId(), "9");
          counter++;
        } else if (e.getInVertex().getId().equals("4")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 1);
          assertEquals(e.getLabel(), "knows");
          assertEquals(e.getId(), "8");
          counter++;
        }
      }

      assertEquals(count(graph.getVertex("4").getOutEdges()), 2);
      assertEquals(count(graph.getVertex("4").getInEdges()), 1);
      Vertex josh = graph.getVertex("4");
      assertEquals(josh.getProperty("name"), "josh");
      assertEquals(josh.getProperty("age"), 32);
      for (Edge e : graph.getVertex("4").getOutEdges()) {
        if (e.getInVertex().getId().equals("3")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 0);
          assertEquals(e.getLabel(), "created");
          assertEquals(e.getId(), "11");
          counter++;
        } else if (e.getInVertex().getId().equals("5")) {
          assertEquals(Math.round((Float) e.getProperty("weight")), 1);
          assertEquals(e.getLabel(), "created");
          assertEquals(e.getId(), "10");
          counter++;
        }
      }

      assertEquals(counter, 5);
    }
  }
Пример #10
0
  public static void main(String[] args) {
    String[] vertices = {
      "Seattle",
      "San Francisco",
      "Los Angeles",
      "Denver",
      "Atlanta",
      "Kansas City",
      "Chicago",
      "Boston",
      "New York",
      "Miami",
      "Dallas",
      "Houston"
    };

    int[][] edges = {
      {0, 1}, {0, 3}, {0, 5}, {1, 0}, {1, 2}, {1, 3}, {2, 1}, {2, 3}, {2, 4}, {2, 10}, {3, 0},
      {3, 1}, {3, 2}, {3, 4}, {3, 5}, {4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10}, {5, 0},
      {5, 3}, {5, 4}, {5, 6}, {5, 7}, {6, 5}, {6, 7}, {7, 4}, {7, 5}, {7, 6}, {7, 8}, {8, 4},
      {8, 7}, {8, 9}, {8, 10}, {8, 11}, {9, 8}, {9, 11}, {10, 2}, {10, 4}, {10, 8}, {10, 11},
      {11, 8}, {11, 9}, {11, 10}
    };

    Graph<String> graph1 = new UnweightedGraph<>(vertices, edges);
    System.out.println("The number of vertices in graph1: " + graph1.getSize());
    System.out.println("The vertex with index 1 is " + graph1.getVertex(1));
    System.out.println("The index for Miami is " + graph1.getIndex("Miami"));
    System.out.println("The edges for graph1:");
    graph1.printEdges();

    String names[] = {"Peter", "Jane", "Mark", "Cindy", "Wendy"};

    java.uil.ArrayList<AbstractGraph.Edge> edgeList = new java.util.ArrayList<>();
    edgeList.add(new AbstractGraph.Edge(0, 2));
    edgeList.add(new AbstractGraph.Edge(1, 2));
    edgeList.add(new AbstractGraph.Edge(2, 4));
    edgeList.add(new AbstractGraph.Edge(3, 4));

    Graph<String> graph2 = new UnweightedGraph<>(java.util.Arrays.asList(names), edgeList);

    System.out.println("\nThe number of vertices in graph2: " + graph2.getSie());
    System.out.println("The edges for graph2:");
    graph2.printEdges();
  }
  /**
   * @brief assigns latitude and longitude cordinates to each Vertex
   * @param Graph graph which expects the graph that is to have its Vertexes' cordinates set
   * @param File cordFile which expects the file containing the cordinates for the Vertexes
   * @return Graph which is the newly created graph with cordinates
   */
  public Graph setCordinates(Graph graph, File cordFile) {
    Scanner input = null;
    Scanner lineScan = null;

    try {
      input = new Scanner(cordFile);

    } catch (FileNotFoundException e) {
      System.out.println(" File not found");
    }

    try {

      while (input.hasNextLine()) {
        String line = input.nextLine();
        lineScan = new Scanner(line);

        String lineDefiner = lineScan.next();

        if (lineDefiner.equals("v")) {
          while (lineScan.hasNext()) {

            String vertex = lineScan.next();

            int latitude = Integer.parseInt(lineScan.next());

            int longitude = Integer.parseInt(lineScan.next());

            Vertex vert = graph.getVertex(vertex);
            vert.setLatitude(latitude);
            vert.setLongitude(longitude);
          }
        }
      }

    } catch (NullPointerException e) {

      System.out.println("Error: File either not found or not formatted correctly");
    }
    return graph;
  }
Пример #12
0
  public void testVertexPropertyInconsistency(final Graph graph) {
    if (!config.isRDFModel) {
      List<String> ids = generateIds(1);
      Vertex v1 = graph.addVertex(convertId(ids.get(0)));
      v1.setProperty("key1", "value1");
      if (config.supportsVertexIteration) {
        assertEquals(count(graph.getVertices()), 1);
      }
      assertEquals("value1", v1.getProperty("key1"));

      Vertex v2 = graph.getVertex(v1.getId());
      assertEquals("value1", v2.getProperty("key1"));

      v1.setProperty("key1", "value111");
      assertEquals("value111", v1.getProperty("key1"));
      assertEquals("value111", v2.getProperty("key1"));

      assertEquals("value111", v2.removeProperty("key1"));
      assertNull(v2.getProperty("key1"));
      assertNull(v1.getProperty("key1"));
    }
  }
Пример #13
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();
  }
Пример #14
0
  private TermMentionWithGraphVertex saveTermMention(
      Vertex artifactGraphVertex,
      TermMention termMention,
      String workspaceId,
      String visibilitySource) {
    LOGGER.debug(
        "Saving term mention '%s':%s:%s (%d:%d)",
        termMention.getSign(),
        termMention.getOntologyClassUri(),
        termMention.getPropertyKey(),
        termMention.getStart(),
        termMention.getEnd());
    Vertex vertex = null;
    if (visibilitySource == null) {
      visibilitySource = termMention.getVisibility().getVisibilityString();
    }

    JSONObject visibilityJson = new JSONObject();
    visibilityJson.put(VisibilityTranslator.JSON_SOURCE, visibilitySource);
    Visibility visibility = visibilityTranslator.toVisibility(visibilityJson).getVisibility();

    Visibility visibilityWithWorkspaceId;
    JSONObject visibilityJsonWithWorkspaceId;
    if (!workspaceId.equals("")) {
      visibilityJsonWithWorkspaceId =
          GraphUtil.updateVisibilitySourceAndAddWorkspaceId(null, visibilitySource, workspaceId);
      visibilityWithWorkspaceId =
          visibilityTranslator.toVisibility(visibilityJsonWithWorkspaceId).getVisibility();
    } else {
      visibilityWithWorkspaceId = visibility;
      visibilityJsonWithWorkspaceId = visibilityJson;
    }

    TermMentionRowKey termMentionRowKey =
        new TermMentionRowKey(
            artifactGraphVertex.getId().toString(),
            termMention.getPropertyKey(),
            termMention.getStart(),
            termMention.getEnd());
    TermMentionModel termMentionModel = new TermMentionModel(termMentionRowKey);
    termMentionModel.getMetadata().setSign(termMention.getSign(), visibility);
    termMentionModel
        .getMetadata()
        .setOntologyClassUri(termMention.getOntologyClassUri(), visibility);
    if (termMention.getProcess() != null && !termMention.getProcess().equals("")) {
      termMentionModel.getMetadata().setAnalyticProcess(termMention.getProcess(), visibility);
    }

    Concept concept = ontologyRepository.getConceptByIRI(termMention.getOntologyClassUri());
    if (concept == null) {
      LOGGER.error("Could not find ontology graph vertex '%s'", termMention.getOntologyClassUri());
      return null;
    }
    termMentionModel.getMetadata().setConceptGraphVertexId(concept.getTitle(), visibility);

    if (termMention.isResolved()) {
      String title = termMention.getSign();
      ElementMutation<Vertex> vertexElementMutation;
      if (termMention.getUseExisting()) {
        graph.flush(); // make sure the previous term mentions have made it into the graph
        if (termMention.getId() != null) {
          vertex = graph.getVertex(termMention.getId(), getAuthorizations());
        } else {
          vertex =
              singleOrDefault(
                  graph
                      .query(getAuthorizations())
                      .has(LumifyProperties.TITLE.getPropertyName(), title)
                      .has(LumifyProperties.CONCEPT_TYPE.getPropertyName(), concept.getTitle())
                      .vertices(),
                  null);
        }
      }

      Map<String, Object> metadata = new HashMap<String, Object>();
      LumifyVisibilityProperties.VISIBILITY_JSON_PROPERTY.setMetadata(
          metadata, visibilityJsonWithWorkspaceId);

      if (vertex == null) {
        if (termMention.getId() != null) {
          vertexElementMutation =
              graph.prepareVertex(termMention.getId(), visibilityWithWorkspaceId);
        } else {
          vertexElementMutation = graph.prepareVertex(visibilityWithWorkspaceId);
        }
        LumifyProperties.TITLE.setProperty(
            vertexElementMutation, title, metadata, visibilityWithWorkspaceId);
        LumifyProperties.CONCEPT_TYPE.setProperty(
            vertexElementMutation, concept.getTitle(), metadata, visibilityWithWorkspaceId);
      } else {
        vertexElementMutation = vertex.prepareMutation();
      }

      for (TermMention.TermMentionProperty termMentionProperty : termMention.getNewProperties()) {
        vertexElementMutation.addPropertyValue(
            termMentionProperty.getKey(),
            termMentionProperty.getName(),
            termMentionProperty.getValue(),
            metadata,
            visibilityWithWorkspaceId);
      }

      LumifyVisibilityProperties.VISIBILITY_JSON_PROPERTY.setProperty(
          vertexElementMutation,
          visibilityJsonWithWorkspaceId,
          metadata,
          visibilityWithWorkspaceId);
      vertexElementMutation.addPropertyValue(
          graph.getIdGenerator().nextId(),
          LumifyProperties.ROW_KEY.getPropertyName(),
          termMentionRowKey.toString(),
          metadata,
          visibilityWithWorkspaceId);

      if (!(vertexElementMutation instanceof ExistingElementMutation)) {
        vertex = vertexElementMutation.save(getAuthorizations());
        auditRepository.auditVertexElementMutation(
            AuditAction.UPDATE,
            vertexElementMutation,
            vertex,
            termMention.getProcess(),
            getUser(),
            visibilityWithWorkspaceId);
      } else {
        auditRepository.auditVertexElementMutation(
            AuditAction.UPDATE,
            vertexElementMutation,
            vertex,
            termMention.getProcess(),
            getUser(),
            visibilityWithWorkspaceId);
        vertex = vertexElementMutation.save(getAuthorizations());
      }

      // TODO: a better way to check if the same edge exists instead of looking it up every time?
      Edge edge =
          singleOrDefault(
              artifactGraphVertex.getEdges(
                  vertex, Direction.OUT, artifactHasEntityIri, getAuthorizations()),
              null);
      if (edge == null) {
        edge =
            graph.addEdge(
                artifactGraphVertex, vertex, artifactHasEntityIri, visibility, getAuthorizations());
        LumifyVisibilityProperties.VISIBILITY_JSON_PROPERTY.setProperty(
            edge,
            visibilityJsonWithWorkspaceId,
            metadata,
            visibilityWithWorkspaceId,
            getAuthorizations());
        auditRepository.auditRelationship(
            AuditAction.CREATE,
            artifactGraphVertex,
            vertex,
            edge,
            termMention.getProcess(),
            "",
            getUser(),
            visibilityWithWorkspaceId);
      }

      graph.flush();
      if (workspaceId != null && !workspaceId.equals("")) {
        Workspace workspace = workspaceRepository.findById(workspaceId, getUser());
        workspaceRepository.updateEntityOnWorkspace(
            workspace, vertex.getId(), null, null, null, getUser());
      }

      termMentionModel
          .getMetadata()
          .setVertexId(vertex.getId().toString(), visibility)
          .setEdgeId(edge.getId().toString(), visibility);
    }

    getTermMentionRepository().save(termMentionModel, FlushFlag.NO_FLUSH);
    return new TermMentionWithGraphVertex(termMentionModel, vertex);
  }
Пример #15
0
  public void testRemoveVertexProperties(final Graph graph) {

    if (!config.isRDFModel) {
      List<String> ids = generateIds(2);

      Vertex v1 = graph.addVertex(ids.get(0));
      Vertex v2 = graph.addVertex(ids.get(1));
      v1.setProperty("key1", "value1");
      v1.setProperty("key2", 10);
      v2.setProperty("key2", 20);

      assertEquals("value1", v1.removeProperty("key1"));
      assertEquals(10, v1.removeProperty("key2"));
      assertEquals(20, v2.removeProperty("key2"));

      assertNull(v1.removeProperty("key1"));
      assertNull(v1.removeProperty("key2"));
      assertNull(v2.removeProperty("key2"));

      v1.setProperty("key1", "value1");
      v1.setProperty("key2", 10);
      v2.setProperty("key2", 20);

      if (!config.ignoresSuppliedIds) {
        v1 = graph.getVertex(ids.get(0));
        v2 = graph.getVertex(ids.get(1));

        assertEquals("value1", v1.removeProperty("key1"));
        assertEquals(10, v1.removeProperty("key2"));
        assertEquals(20, v2.removeProperty("key2"));

        assertNull(v1.removeProperty("key1"));
        assertNull(v1.removeProperty("key2"));
        assertNull(v2.removeProperty("key2"));

        v1 = graph.getVertex(ids.get(0));
        v2 = graph.getVertex(ids.get(1));

        v1.setProperty("key1", "value2");
        v1.setProperty("key2", 20);
        v2.setProperty("key2", 30);

        assertEquals("value2", v1.removeProperty("key1"));
        assertEquals(20, v1.removeProperty("key2"));
        assertEquals(30, v2.removeProperty("key2"));

        assertNull(v1.removeProperty("key1"));
        assertNull(v1.removeProperty("key2"));
        assertNull(v2.removeProperty("key2"));
      }

    } else {
      Vertex v1 = graph.addVertex("\"1\"^^<http://www.w3.org/2001/XMLSchema#int>");
      assertEquals("http://www.w3.org/2001/XMLSchema#int", v1.removeProperty("type"));
      assertEquals("1", v1.getProperty("value"));
      assertNull(v1.getProperty("lang"));
      assertNull(v1.getProperty("random something"));

      Vertex v2 = graph.addVertex("\"hello\"@en");
      assertEquals("en", v2.removeProperty("lang"));
      assertEquals("hello", v2.getProperty("value"));
      assertNull(v2.getProperty("type"));
      assertNull(v2.getProperty("random something"));
    }
  }