CountGraphHeads.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.distinction.functions;

  17. import org.apache.flink.api.java.tuple.Tuple2;
  18. import org.apache.flink.util.Collector;
  19. import org.gradoop.common.model.api.entities.GraphHead;
  20. import org.gradoop.flink.model.api.functions.GraphHeadReduceFunction;

  21. import java.util.Iterator;

  22. /**
  23.  * Distinction function that just selects the first graph head of an isomorphic group.
  24.  *
  25.  * @param <G> graph head type
  26.  */
  27. public class CountGraphHeads<G extends GraphHead> implements GraphHeadReduceFunction<G> {

  28.   /**
  29.    * property key to store graph count.
  30.    */
  31.   private final String propertyKey;

  32.   /**
  33.    * Constructor.
  34.    *
  35.    * @param propertyKey property key to store graph count.
  36.    */
  37.   public CountGraphHeads(String propertyKey) {
  38.     this.propertyKey = propertyKey;
  39.   }

  40.   @Override
  41.   public void reduce(Iterable<Tuple2<String, G>> iterable,
  42.     Collector<G> collector) throws Exception {
  43.     Iterator<Tuple2<String, G>> iterator = iterable.iterator();

  44.     G graphHead = iterator.next().f1;
  45.     int count = 1;

  46.     while (iterator.hasNext()) {
  47.       iterator.next();
  48.       count++;
  49.     }

  50.     graphHead.setProperty(propertyKey, count);

  51.     collector.collect(graphHead);
  52.   }
  53. }