FilterVerticesWithDegreeOtherThanGiven.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.sampling.functions;

  17. import org.apache.flink.api.java.DataSet;
  18. import org.gradoop.common.model.api.entities.Edge;
  19. import org.gradoop.common.model.api.entities.GraphHead;
  20. import org.gradoop.common.model.api.entities.Vertex;
  21. import org.gradoop.flink.algorithms.gelly.vertexdegrees.DistinctVertexDegrees;
  22. import org.gradoop.flink.model.api.epgm.BaseGraph;
  23. import org.gradoop.flink.model.api.epgm.BaseGraphCollection;
  24. import org.gradoop.flink.model.api.operators.UnaryBaseGraphToBaseGraphOperator;
  25. import org.gradoop.flink.model.impl.functions.epgm.PropertyRemover;
  26. import org.gradoop.flink.model.impl.operators.sampling.common.SamplingConstants;

  27. /**
  28.  * Retains all vertices which do not have the given degree.
  29.  *
  30.  * @param <G>  The graph head type.
  31.  * @param <V>  The vertex type.
  32.  * @param <E>  The edge type.
  33.  * @param <LG> The type of the graph.
  34.  * @param <GC> The type of the graph collection.
  35.  */
  36. public class FilterVerticesWithDegreeOtherThanGiven<
  37.   G extends GraphHead,
  38.   V extends Vertex,
  39.   E extends Edge,
  40.   LG extends BaseGraph<G, V, E, LG, GC>,
  41.   GC extends BaseGraphCollection<G, V, E, LG, GC>> implements UnaryBaseGraphToBaseGraphOperator<LG> {

  42.   /**
  43.    * the given degree
  44.    */
  45.   private long degree;

  46.   /**
  47.    * Constructor
  48.    *
  49.    * @param degree the given degree
  50.    */
  51.   public FilterVerticesWithDegreeOtherThanGiven(long degree) {
  52.     this.degree = degree;
  53.   }

  54.   @Override
  55.   public LG execute(LG graph) {

  56.     DistinctVertexDegrees<G, V, E, LG, GC> distinctVertexDegrees = new DistinctVertexDegrees<>(
  57.       SamplingConstants.DEGREE_PROPERTY_KEY,
  58.       SamplingConstants.IN_DEGREE_PROPERTY_KEY,
  59.       SamplingConstants.OUT_DEGREE_PROPERTY_KEY,
  60.       true);

  61.     DataSet<V> newVertices = distinctVertexDegrees.execute(graph).getVertices()
  62.       .filter(new VertexWithDegreeFilter<>(degree, SamplingConstants.DEGREE_PROPERTY_KEY))
  63.       .map(new PropertyRemover<>(SamplingConstants.DEGREE_PROPERTY_KEY))
  64.       .map(new PropertyRemover<>(SamplingConstants.IN_DEGREE_PROPERTY_KEY))
  65.       .map(new PropertyRemover<>(SamplingConstants.OUT_DEGREE_PROPERTY_KEY));

  66.     return graph.getFactory().fromDataSets(
  67.       graph.getGraphHead(), newVertices, graph.getEdges());
  68.   }
  69. }