forked from dbs-leipzig/gradoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dbs-leipzig#1559] change average return value to double
- Loading branch information
Showing
3 changed files
with
124 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...doop/temporal/model/impl/operators/metric/functions/GroupDegreeTreesToAverageDegrees.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright © 2014 - 2021 Leipzig University (Database Research Group) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.gradoop.temporal.model.impl.operators.metric.functions; | ||
|
||
import org.apache.flink.api.common.functions.GroupReduceFunction; | ||
import org.apache.flink.api.java.tuple.Tuple2; | ||
import org.apache.flink.util.Collector; | ||
import org.gradoop.common.model.impl.id.GradoopId; | ||
|
||
import java.util.TreeSet; | ||
import java.util.TreeMap; | ||
import java.util.SortedSet; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* A group reduce function that merges all Tuples (vId, degreeTree) to a dataset of tuples (time, aggDegree) | ||
* that represents the aggregated degree value for the whole graph at the given time. | ||
*/ | ||
public class GroupDegreeTreesToAverageDegrees | ||
implements GroupReduceFunction<Tuple2<GradoopId, TreeMap<Long, Integer>>, Tuple2<Long, Double>> { | ||
|
||
/** | ||
* Creates an instance of this group reduce function. | ||
* | ||
*/ | ||
public GroupDegreeTreesToAverageDegrees() { | ||
} | ||
|
||
@Override | ||
public void reduce(Iterable<Tuple2<GradoopId, TreeMap<Long, Integer>>> iterable, | ||
Collector<Tuple2<Long, Double>> collector) throws Exception { | ||
|
||
// init necessary maps and set | ||
HashMap<GradoopId, TreeMap<Long, Integer>> degreeTrees = new HashMap<>(); | ||
HashMap<GradoopId, Integer> vertexDegrees = new HashMap<>(); | ||
SortedSet<Long> timePoints = new TreeSet<>(); | ||
|
||
// convert the iterables to a hashmap and remember all possible timestamps | ||
for (Tuple2<GradoopId, TreeMap<Long, Integer>> tuple : iterable) { | ||
degreeTrees.put(tuple.f0, tuple.f1); | ||
timePoints.addAll(tuple.f1.keySet()); | ||
} | ||
|
||
int numberOfVertices = degreeTrees.size(); | ||
|
||
// Add default times | ||
timePoints.add(Long.MIN_VALUE); | ||
|
||
for (Long timePoint : timePoints) { | ||
// skip last default time | ||
if (Long.MAX_VALUE == timePoint) { | ||
continue; | ||
} | ||
// Iterate over all vertices | ||
for (Map.Entry<GradoopId, TreeMap<Long, Integer>> entry : degreeTrees.entrySet()) { | ||
// Make sure the vertex is registered in the current vertexDegrees capture | ||
if (!vertexDegrees.containsKey(entry.getKey())) { | ||
vertexDegrees.put(entry.getKey(), 0); | ||
} | ||
|
||
// Check if timestamp is in tree, if not, take the lower key | ||
if (entry.getValue().containsKey(timePoint)) { | ||
vertexDegrees.put(entry.getKey(), entry.getValue().get(timePoint)); | ||
} else { | ||
Long lowerKey = entry.getValue().lowerKey(timePoint); | ||
if (lowerKey != null) { | ||
vertexDegrees.put(entry.getKey(), entry.getValue().get(lowerKey)); | ||
} | ||
} | ||
} | ||
|
||
// Here, every tree with this time point is iterated. Now we need to aggregate for the current time. | ||
Optional<Integer> opt; | ||
opt = vertexDegrees.values().stream().reduce(Math::addExact); | ||
opt.ifPresent(integer -> collector.collect( | ||
new Tuple2<>(timePoint, (double) integer / (double) numberOfVertices))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters