ToDirectedAdjacencyList.java

  1. /*
  2.  * Copyright © 2014 - 2021 Leipzig University (Database Research Group)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gradoop.flink.algorithms.fsm.transactional.common.functions;

  17. import com.google.common.collect.Maps;
  18. import org.apache.flink.api.common.functions.MapFunction;
  19. import org.gradoop.common.model.impl.id.GradoopId;
  20. import org.gradoop.common.model.impl.pojo.EPGMVertex;
  21. import org.gradoop.common.model.impl.pojo.EPGMEdge;
  22. import org.gradoop.flink.model.impl.tuples.IdWithLabel;
  23. import org.gradoop.flink.representation.common.adjacencylist.AdjacencyListCell;
  24. import org.gradoop.flink.representation.common.adjacencylist.AdjacencyListRow;
  25. import org.gradoop.flink.representation.transactional.AdjacencyList;
  26. import org.gradoop.flink.model.impl.layouts.transactional.tuples.GraphTransaction;

  27. import java.util.Map;
  28. import java.util.Set;

  29. /**
  30.  * {@code (G, V, E) => adjacencyList}
  31.  * for directed graph
  32.  */
  33. public class ToDirectedAdjacencyList implements
  34.   MapFunction<GraphTransaction, AdjacencyList<GradoopId, String, IdWithLabel, IdWithLabel>> {

  35.   @Override
  36.   public AdjacencyList<GradoopId, String, IdWithLabel, IdWithLabel> map(
  37.     GraphTransaction transaction) throws Exception {

  38.     Set<EPGMVertex> vertices = transaction.getVertices();
  39.     Set<EPGMEdge> edges = transaction.getEdges();

  40.     int vertexCount = vertices.size();

  41.     Map<GradoopId, AdjacencyListRow<IdWithLabel, IdWithLabel>> outgoingRows =
  42.       Maps.newHashMapWithExpectedSize(vertexCount);

  43.     Map<GradoopId, AdjacencyListRow<IdWithLabel, IdWithLabel>> incomingRows =
  44.       Maps.newHashMapWithExpectedSize(vertexCount);

  45.     Map<GradoopId, String> labels = Maps.newHashMapWithExpectedSize(vertexCount);

  46.     // VERTICES
  47.     for (EPGMVertex vertex : vertices) {
  48.       labels.put(vertex.getId(), vertex.getLabel());
  49.     }

  50.     // EDGES

  51.     for (EPGMEdge edge : edges) {
  52.       GradoopId sourceId = edge.getSourceId();

  53.       AdjacencyListRow<IdWithLabel, IdWithLabel> outgoingRow =
  54.         outgoingRows.computeIfAbsent(sourceId, k -> new AdjacencyListRow<>());

  55.       IdWithLabel sourceData = new IdWithLabel(sourceId, labels.get(sourceId));

  56.       GradoopId targetId = edge.getTargetId();
  57.       AdjacencyListRow<IdWithLabel, IdWithLabel> incomingRow =
  58.         incomingRows.computeIfAbsent(targetId, k -> new AdjacencyListRow<>());

  59.       IdWithLabel targetData = new IdWithLabel(targetId, labels.get(targetId));

  60.       IdWithLabel edgeData = new IdWithLabel(edge.getId(), edge.getLabel());

  61.       outgoingRow.getCells().add(new AdjacencyListCell<>(edgeData, targetData));
  62.       incomingRow.getCells().add(new AdjacencyListCell<>(edgeData, sourceData));
  63.     }

  64.     return new AdjacencyList<>(
  65.       transaction.getGraphHead(), labels, null, outgoingRows, incomingRows);
  66.   }
  67. }