/** * Drop edge properties. * * @param current current edge * @param transformed copy of current except label and properties * @return current edge without properties */ private static Edge dropEdgeProperties(Edge current, Edge transformed) { transformed.setLabel(current.getLabel()); return transformed; }
/** * Returns a mapping between the edge GradoopId and the GDL variable name. * * @param edges The graph edges. * @return Mapping between edge and GDL variable name. */ private Map<GradoopId, String> getEdgeNameMapping(List<Edge> edges) { Map<GradoopId, String> idToEdgeName = new HashMap<>(edges.size()); for (int i = 0; i < edges.size(); i++) { Edge e = edges.get(i); String eName = String.format("%s_%s_%s", EDGE_VARIABLE_PREFIX, e.getLabel(), i); idToEdgeName.put(e.getId(), eName); } return idToEdgeName; }
@Override public void flatMap(GraphTransaction graphTransaction, Collector<String> collector) throws Exception { for (Edge edge : graphTransaction.getEdges()) { collector.collect(edge.getLabel()); } } }
@Override public void flatMap(GraphTransaction graphTransaction, Collector<String> collector) throws Exception { for (Edge edge : graphTransaction.getEdges()) { collector.collect(edge.getLabel()); } } }
@Override public void flatMap(GraphTransaction graph, Collector<WithCount<String>> out) throws Exception { Set<String> edgeLabels = Sets.newHashSet(); for (Edge edge : graph.getEdges()) { edgeLabels.add(edge.getLabel()); } for (String label : edgeLabels) { reuseTuple.setObject(label); out.collect(reuseTuple); } } }
@Override public void flatMap(GraphTransaction graph, Collector<WithCount<String>> out) throws Exception { Set<String> edgeLabels = Sets.newHashSet(); for (Edge edge : graph.getEdges()) { edgeLabels.add(edge.getLabel()); } for (String label : edgeLabels) { reuseTuple.setObject(label); out.collect(reuseTuple); } } }
@Override public IdWithLabel map(E edge) throws Exception { reuseTuple.setId(edge.getSourceId()); reuseTuple.setLabel(edge.getLabel()); return reuseTuple; } }
@Override public IdWithLabel map(E edge) throws Exception { reuseTuple.setId(edge.getTargetId()); reuseTuple.setLabel(edge.getLabel()); return reuseTuple; } }
@Override public IdWithLabel map(E edge) throws Exception { reuseTuple.setId(edge.getTargetId()); reuseTuple.setLabel(edge.getLabel()); return reuseTuple; } }
@Override public IdWithLabel map(E edge) throws Exception { reuseTuple.setId(edge.getSourceId()); reuseTuple.setLabel(edge.getLabel()); return reuseTuple; } }
/** * The actual computation. * * @param socialNetwork social network graph * @return summarized, aggregated graph */ private static LogicalGraph execute(LogicalGraph socialNetwork) { return socialNetwork .subgraph( vertex -> vertex.getLabel().equals("Person"), edge -> edge.getLabel().equals("knows")) .groupBy(Arrays.asList("gender", "city")) .aggregate(new VertexCount(), new EdgeCount()); }
@Override public Edge apply(Edge current, Edge transformed) { if (current.getLabel().equals(forEdgeLabel)) { GradoopId source = current.getSourceId(); GradoopId target = current.getTargetId(); current.setSourceId(target); current.setTargetId(source); current.setLabel(newLabel); } return current; } }
@Test public void getEdgesByLabel() throws Exception { TxCollectionLayout layout = new TxCollectionLayout( getExecutionEnvironment().fromElements(tx0, tx1)); GradoopTestUtils.validateEPGMGraphElementCollections( tx0.getEdges().stream().filter(e -> e.getLabel().equals("a")).collect(Collectors.toList()), layout.getEdgesByLabel("a").collect()); } }
@Test public void testEdgeInducedSubgraph() throws Exception { FlinkAsciiGraphLoader loader = getSocialNetworkLoader(); loader.appendToDatabaseFromString("expected[" + "(databases)<-[ghtd]-(gdbs)-[ghtg1]->(graphs)" + "(graphs)<-[ghtg2]-(gps)-[ghth]->(hadoop)" + "]"); LogicalGraph input = loader.getLogicalGraph(); LogicalGraph expected = loader.getLogicalGraphByVariable("expected"); LogicalGraph output = input.edgeInducedSubgraph( e -> e.getLabel().equals("hasTag")); collectAndAssertTrue(output.equalsByElementData(expected)); }
@Test public void testEdgeInducedSubgraphProjectFirst() throws Exception { FlinkAsciiGraphLoader loader = getSocialNetworkLoader(); loader.appendToDatabaseFromString("expected[" + "(databases)<-[ghtd]-(gdbs)-[ghtg1]->(graphs)" + "(graphs)<-[ghtg2]-(gps)-[ghth]->(hadoop)" + "]"); LogicalGraph input = loader.getLogicalGraph(); LogicalGraph expected = loader.getLogicalGraphByVariable("expected"); LogicalGraph output = input.subgraph(null, e -> e.getLabel().equals("hasTag"), Subgraph.Strategy.EDGE_INDUCED_PROJECT_FIRST); collectAndAssertTrue(output.equalsByElementData(expected)); }
@Test public void testGetEdges() throws Exception { AsciiGraphLoader<GraphHead, Vertex, Edge> asciiGraphLoader = AsciiGraphLoader.fromString("[()-->()]", config); validateCollections(asciiGraphLoader, 1, 2, 1); validateCaches(asciiGraphLoader, 0, 0, 0); for (Edge edge : asciiGraphLoader.getEdges()) { assertEquals("Edge has wrong label", GradoopConstants.DEFAULT_EDGE_LABEL, edge.getLabel()); } }
@Override public void join(Edge edge, Vertex vertex, Collector<Edge> collector) throws Exception { if (vertex == null) { collector.collect(edge); } else if (edge != null) { REUSABLE_EDGE.setId(GradoopId.get()); REUSABLE_EDGE.setSourceId(isSourceNow ? vId : edge.getSourceId()); REUSABLE_EDGE.setTargetId(isSourceNow ? edge.getTargetId() : vId); REUSABLE_EDGE.setProperties(edge.getProperties()); REUSABLE_EDGE.setLabel(edge.getLabel()); REUSABLE_EDGE.setGraphIds(edge.getGraphIds()); collector.collect(REUSABLE_EDGE); } } }
@Override public void join(Edge edge, Vertex vertex, Collector<Edge> collector) throws Exception { if (vertex == null) { collector.collect(edge); } else if (edge != null) { REUSABLE_EDGE.setId(GradoopId.get()); REUSABLE_EDGE.setSourceId(isSourceNow ? vId : edge.getSourceId()); REUSABLE_EDGE.setTargetId(isSourceNow ? edge.getTargetId() : vId); REUSABLE_EDGE.setProperties(edge.getProperties()); REUSABLE_EDGE.setLabel(edge.getLabel()); REUSABLE_EDGE.setGraphIds(edge.getGraphIds()); collector.collect(REUSABLE_EDGE); } } }
@Override public CSVEdge map(Edge edge) throws Exception { csvEdge.setId(edge.getId().toString()); csvEdge.setGradoopIds(collectionToCsvString(edge.getGraphIds())); csvEdge.setSourceId(edge.getSourceId().toString()); csvEdge.setTargetId(edge.getTargetId().toString()); csvEdge.setLabel(StringEscaper.escape(edge.getLabel(), CSVConstants.ESCAPED_CHARACTERS)); csvEdge.setProperties(getPropertyString(edge, MetaDataSource.EDGE_TYPE)); return csvEdge; } }
@Override public CSVEdge map(Edge edge) throws Exception { csvEdge.setId(edge.getId().toString()); csvEdge.setGradoopIds(collectionToCsvString(edge.getGraphIds())); csvEdge.setSourceId(edge.getSourceId().toString()); csvEdge.setTargetId(edge.getTargetId().toString()); csvEdge.setLabel(StringEscaper.escape(edge.getLabel(), CSVConstants.ESCAPED_CHARACTERS)); csvEdge.setProperties(getPropertyString(edge, MetaDataSource.EDGE_TYPE)); return csvEdge; } }