LGraphToEPGMMapper.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.model.impl.operators.layouting.functions;

  17. import org.apache.flink.api.java.DataSet;
  18. import org.gradoop.common.model.impl.id.GradoopId;
  19. import org.gradoop.common.model.impl.pojo.EPGMEdge;
  20. import org.gradoop.common.model.impl.pojo.EPGMVertex;
  21. import org.gradoop.common.model.impl.properties.Properties;
  22. import org.gradoop.flink.model.impl.epgm.LogicalGraph;
  23. import org.gradoop.flink.model.impl.operators.layouting.FusingFRLayouter;
  24. import org.gradoop.flink.model.impl.operators.layouting.util.LEdge;
  25. import org.gradoop.flink.model.impl.operators.layouting.util.LGraph;
  26. import org.gradoop.flink.model.impl.operators.layouting.util.LVertex;

  27. import java.util.List;
  28. import java.util.stream.Collectors;

  29. /**
  30.  * Converts an LGraph to an EPGM Graph
  31.  */
  32. public class LGraphToEPGMMapper {

  33.   /**
  34.    * Build a new EPGM-Graph from the given LGraph, by converting LVertices to vertices 1:1.
  35.    *
  36.    * @param input    The original EPGMGraph, to use it's factory
  37.    * @param layouted The LGraph to convert
  38.    * @return The converted LGraph
  39.    */
  40.   public LogicalGraph buildSimplifiedGraph(LogicalGraph input, LGraph layouted) {
  41.     DataSet<EPGMVertex> vertices = layouted.getVertices().map(LGraphToEPGMMapper::mapTpEPGMVertex);
  42.     DataSet<EPGMEdge> edges = layouted.getEdges().map(LGraphToEPGMMapper::mapToEPGMEdge);
  43.     return input.getFactory().fromDataSets(vertices, edges);
  44.   }

  45.   /**
  46.    * Map LVertex to EPGMVertex.
  47.    *
  48.    * @param lv Input LVertex
  49.    * @return Output EPGMVertex
  50.    */
  51.   protected static EPGMVertex mapTpEPGMVertex(LVertex lv) {
  52.     EPGMVertex v = new EPGMVertex(lv.getId(), "vertex", Properties.create(), null);
  53.     lv.getPosition().setVertexPosition(v);
  54.     v.setProperty(FusingFRLayouter.VERTEX_SIZE_PROPERTY, lv.getCount());
  55.     v.setProperty(FusingFRLayouter.SUB_ELEMENTS_PROPERTY,
  56.       getSubelementListValue(lv.getSubVertices()));
  57.     return v;
  58.   }

  59.   /**
  60.    * Map LEdge to EPGMEdge.
  61.    *
  62.    * @param le Input LEdge
  63.    * @return Output EPGMEdge
  64.    */
  65.   protected static EPGMEdge mapToEPGMEdge(LEdge le) {
  66.     EPGMEdge e = new EPGMEdge(le.getId(), "edge", le.getSourceId(), le.getTargetId(), Properties.create(),
  67.         null);
  68.     e.setProperty(FusingFRLayouter.VERTEX_SIZE_PROPERTY, le.getCount());
  69.     e.setProperty(FusingFRLayouter.SUB_ELEMENTS_PROPERTY, getSubelementListValue(le.getSubEdges()));
  70.     return e;
  71.   }

  72.   /**
  73.    * Helper function to convert the List of sub-elements into a comma seperated string
  74.    * Gradoop (especially the CSVDataSink) seems to have trouble with lists of PropertyValues, so
  75.    * this is the easies workaround
  76.    *
  77.    * @param ids List of GradoopIds
  78.    * @return A comma seperated string of ids
  79.    */
  80.   protected static String getSubelementListValue(List<GradoopId> ids) {
  81.     return ids.stream().map(GradoopId::toString).collect(Collectors.joining(","));
  82.   }
  83. }