Github user vasia commented on a diff in the pull request:

    https://github.com/apache/flink/pull/452#discussion_r26487919
  
    --- Diff: 
flink-staging/flink-gelly/src/main/java/org/apache/flink/graph/example/EuclideanGraphExample.java
 ---
    @@ -0,0 +1,231 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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.apache.flink.graph.example;
    +
    +import org.apache.flink.api.common.ProgramDescription;
    +import org.apache.flink.api.common.functions.MapFunction;
    +import org.apache.flink.api.java.DataSet;
    +import org.apache.flink.api.java.ExecutionEnvironment;
    +import org.apache.flink.api.java.tuple.Tuple3;
    +import org.apache.flink.graph.Edge;
    +import org.apache.flink.graph.Graph;
    +import org.apache.flink.graph.Triplet;
    +import org.apache.flink.graph.Vertex;
    +import org.apache.flink.graph.example.utils.EuclideanGraphData;
    +
    +import java.io.Serializable;
    +
    +/**
    + * Given a directed, unweighted graph, with vertex values representing 
points in a plan,
    + * return a weighted graph where the edge weights are equal to the 
Euclidean distance between the
    + * src and the trg vertex values.
    + *
    + * <p>
    + * Input files are plain text files and must be formatted as follows:
    + * <ul>
    + *         <li> Vertices are represented by their vertexIds and vertex 
values and are separated by newlines,
    + *         the value being formed of two doubles separated by a comma.
    + *         For example: <code>1,1.0,1.0\n2,2.0,2.0\n3,3.0,3.0\n</code> 
defines a data set of three vertices
    + *         <li> Edges are represented by triples of srcVertexId, 
srcEdgeId, weight which are
    + *         separated by commas. Edges themselves are separated by 
newlines. The initial edge value will be overwritten.
    + *         For example: <code>1,2,13.0\n1,3,6.0\n</code> defines two edges 
1-2 and 1-3 having
    + *         weights 13 and 6 respectively.
    + * </ul>
    + * </p>
    + *
    + * Usage <code>EuclideanDistanceExample &lt;vertex path&gt; &lt;edge 
path&gt; &lt;result path&gt;</code><br>
    + * If no parameters are provided, the program is ran with default data from
    + * {@link org.apache.flink.graph.example.utils.EuclideanGraphData}
    + */
    +@SuppressWarnings("serial")
    +public class EuclideanGraphExample implements ProgramDescription {
    +
    +   public static void main(String[] args) throws Exception {
    +
    +           if (!parseParameters(args)) {
    +                   return;
    +           }
    +
    +           ExecutionEnvironment env = 
ExecutionEnvironment.getExecutionEnvironment();
    +
    +           DataSet<Vertex<Long, Point>> vertices = getVerticesDataSet(env);
    +
    +           DataSet<Edge<Long, Double>> edges = getEdgesDataSet(env);
    +
    +           Graph<Long, Point, Double> graph = Graph.fromDataSet(vertices, 
edges, env);
    +
    +           DataSet<Triplet<Long, Point, Double>> triplets = 
graph.getTriplets();
    +
    +           // the edge value will be the Euclidean distance between its 
src and trg vertex
    +           DataSet<Triplet<Long, Point, Double>> resultedTriplets = 
triplets
    +                           .map(new MapFunction<Triplet<Long, Point, 
Double>, Triplet<Long, Point, Double>>() {
    +
    +                                   @Override
    +                                   public Triplet<Long, Point, Double> 
map(Triplet<Long, Point, Double> triplet) throws Exception {
    +
    +                                           Vertex<Long, Point> srcVertex = 
triplet.getSrcVertex();
    +                                           Vertex<Long, Point> trgVertex = 
triplet.getTrgVertex();
    +                                           Edge<Long, Double> edge = 
triplet.getEdge();
    +
    +                                           
edge.setValue(srcVertex.getValue().euclideanDistance(trgVertex.getValue()));
    +
    +                                           return new Triplet<Long, Point, 
Double>(srcVertex, trgVertex, edge);
    +                                   }
    +                           });
    +
    +           // retrieve the edges from the final result
    +           DataSet<Edge<Long, Double>> result = resultedTriplets
    +                           .map(new MapFunction<Triplet<Long, Point, 
Double>, Edge<Long, Double>>() {
    +
    +                                   @Override
    +                                   public Edge<Long, Double> 
map(Triplet<Long, Point, Double> triplet) throws Exception {
    +                                           return triplet.getEdge();
    +                                   }
    +                           });
    +
    +           // emit result
    +           if (fileOutput) {
    +                   result.writeAsCsv(outputPath, "\n", ",");
    +           } else {
    +                   result.print();
    +           }
    +
    +           env.execute("Euclidean Graph Example");
    +   }
    +
    +   @Override
    +   public String getDescription() {
    +           return "Weighing a graph by computing the Euclidean distance " +
    +                           "between its vertices";
    +   }
    +
    +   // 
*************************************************************************
    +   //     DATA TYPES
    +   // 
*************************************************************************
    +
    +   /**
    +    * A simple two-dimensional point.
    +    */
    +   public static class Point implements Serializable {
    +
    +           public double x, y;
    +
    +           public Point() {}
    +
    +           public Point(double x, double y) {
    +                   this.x = x;
    +                   this.y = y;
    +           }
    +
    +           public Point add(Point other) {
    +                   x += other.x;
    +                   y += other.y;
    +                   return this;
    +           }
    +
    +           public Point div(long val) {
    +                   x /= val;
    +                   y /= val;
    +                   return this;
    +           }
    +
    +           public double euclideanDistance(Point other) {
    +                   return Math.sqrt((x-other.x)*(x-other.x) + 
(y-other.y)*(y-other.y));
    +           }
    +
    +           public void clear() {
    +                   x = y = 0.0;
    +           }
    +
    +           @Override
    +           public String toString() {
    +                   return x + " " + y;
    +           }
    +   }
    +
    +   // 
******************************************************************************************************************
    +   // UTIL METHODS
    +   // 
******************************************************************************************************************
    +
    +   private static boolean fileOutput = false;
    +
    +   private static String verticesInputPath = null;
    +
    +   private static String edgesInputPath = null;
    +
    +   private static String outputPath = null;
    +
    +   private static boolean parseParameters(String[] args) {
    +
    +           if (args.length > 0) {
    +                   if (args.length == 3) {
    +                           fileOutput = true;
    +                           verticesInputPath = args[0];
    +                           edgesInputPath = args[1];
    +                           outputPath = args[2];
    +                   } else {
    +                           System.out.println("Executing Euclidean Graph 
example with default parameters and built-in default data.");
    +                           System.out.println("Provide parameters to read 
input data from files.");
    +                           System.out.println("See the documentation for 
the correct format of input files.");
    +                           System.err.println("Usage: 
EuclideanGraphExample <input vertices path> <input edges path>" +
    +                                           " <output path>");
    +                           return false;
    +                   }
    +           }
    +           return true;
    +   }
    +
    +   private static DataSet<Vertex<Long, Point>> 
getVerticesDataSet(ExecutionEnvironment env) {
    +           if (fileOutput) {
    +                   return env.readCsvFile(verticesInputPath)
    +                                   .lineDelimiter("\n")
    +                                   .types(Long.class, Double.class, 
Double.class)
    +                                   .map(new MapFunction<Tuple3<Long, 
Double, Double>, Vertex<Long, Point>>() {
    +
    +                                           @Override
    +                                           public Vertex<Long, Point> 
map(Tuple3<Long, Double, Double> value) throws Exception {
    +                                                   return new Vertex<Long, 
Point>(value.f0, new Point(value.f1, value.f2));
    +                                           }
    +                                   });
    +           } else {
    +                   System.err.println("Usage: EuclideanGraphExample <input 
vertices path> <input edges path>" +
    +                                   " <output path>");
    +                   return EuclideanGraphData.getDefaultVertexDataSet(env);
    +           }
    +   }
    +
    +   private static DataSet<Edge<Long, Double>> 
getEdgesDataSet(ExecutionEnvironment env) {
    +           if (fileOutput) {
    +                   return env.readCsvFile(edgesInputPath)
    +                                   .lineDelimiter("\n")
    +                                   .types(Long.class, Long.class, 
Double.class)
    +                                   .map(new MapFunction<Tuple3<Long, Long, 
Double>, Edge<Long, Double>>() {
    +
    +                                           @Override
    +                                           public Edge<Long, Double> 
map(Tuple3<Long, Long, Double> tuple3) throws Exception {
    +                                                   return new Edge<Long, 
Double>(tuple3.f0, tuple3.f1, tuple3.f2);
    +                                           }
    +                                   });
    +           } else {
    +                   System.err.println("Usage: EuclideanGraphExample <input 
vertices path> <input edges path>" +
    --- End diff --
    
    same :-)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to