Skip to content

Commit

Permalink
[#1366] Move generified operators to BaseGraphOperators. (#1420)
Browse files Browse the repository at this point in the history
fixes #1366
  • Loading branch information
timo95 authored and Kevin Gómez committed Oct 17, 2019
1 parent 5057ebe commit 50b81ab
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
*/
package org.gradoop.flink.model.api.epgm;

import org.apache.flink.api.java.DataSet;
import org.gradoop.common.model.api.entities.GraphHead;
import org.gradoop.common.model.api.entities.Edge;
import org.gradoop.common.model.api.entities.Vertex;
import org.gradoop.flink.model.api.layouts.LogicalGraphLayout;
import org.gradoop.flink.model.impl.functions.bool.Not;
import org.gradoop.flink.model.impl.functions.bool.Or;
import org.gradoop.flink.model.impl.functions.bool.True;
import org.gradoop.flink.util.GradoopFlinkConfig;

/**
Expand Down Expand Up @@ -58,4 +62,18 @@ public interface BaseGraph<
* @return a factory that can be used to create a {@link GC} instance.
*/
BaseGraphCollectionFactory<G, V, E, LG, GC> getCollectionFactory();

//----------------------------------------------------------------------------
// Utility methods
//----------------------------------------------------------------------------

@Override
default DataSet<Boolean> isEmpty() {
return getVertices()
.map(new True<>())
.distinct()
.union(getConfig().getExecutionEnvironment().fromElements(false))
.reduce(new Or())
.map(new Not());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.gradoop.common.model.impl.id.GradoopId;
import org.gradoop.common.model.impl.id.GradoopIdSet;
import org.gradoop.flink.model.api.layouts.GraphCollectionLayout;
import org.gradoop.flink.model.impl.functions.bool.Not;
import org.gradoop.flink.model.impl.functions.bool.Or;
import org.gradoop.flink.model.impl.functions.bool.True;
import org.gradoop.flink.model.impl.functions.epgm.BySameId;
import org.gradoop.flink.model.impl.functions.graphcontainment.InAnyGraph;
import org.gradoop.flink.model.impl.functions.graphcontainment.InGraph;
Expand Down Expand Up @@ -98,4 +101,18 @@ default GC getGraphs(final GradoopIdSet identifiers) {

return getFactory().fromDataSets(newGraphHeads, vertices, edges);
}

//----------------------------------------------------------------------------
// Utility methods
//----------------------------------------------------------------------------

@Override
default DataSet<Boolean> isEmpty() {
return getGraphHeads()
.map(new True<>())
.distinct()
.union(getConfig().getExecutionEnvironment().fromElements(false))
.reduce(new Or())
.map(new Not());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ default DataSet<Boolean> equalsByGraphData(GC otherCollection) {
// Utility methods
//----------------------------------------------------------------------------

/**
* Returns a 1-element dataset containing a {@code boolean} value which indicates if the collection
* is empty.
*
* A collection is considered empty, if it contains no logical graphs.
*
* @return 1-element dataset containing {@code true}, if the collection is empty or {@code false} if not
*/
DataSet<Boolean> isEmpty();

/**
* Returns a distinct collection of base graphs. Graph equality is based on graph identifiers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.gradoop.flink.model.api.epgm;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.java.DataSet;
import org.gradoop.common.model.api.entities.Edge;
import org.gradoop.common.model.api.entities.GraphHead;
import org.gradoop.common.model.api.entities.Vertex;
Expand All @@ -34,6 +35,9 @@
import org.gradoop.flink.model.impl.operators.exclusion.Exclusion;
import org.gradoop.flink.model.impl.operators.grouping.Grouping;
import org.gradoop.flink.model.impl.operators.grouping.GroupingStrategy;
import org.gradoop.flink.model.impl.operators.matching.common.MatchStrategy;
import org.gradoop.flink.model.impl.operators.matching.common.statistics.GraphStatistics;
import org.gradoop.flink.model.impl.operators.matching.single.cypher.CypherPatternMatching;
import org.gradoop.flink.model.impl.operators.neighborhood.Neighborhood;
import org.gradoop.flink.model.impl.operators.neighborhood.ReduceEdgeNeighborhood;
import org.gradoop.flink.model.impl.operators.neighborhood.ReduceVertexNeighborhood;
Expand Down Expand Up @@ -66,6 +70,118 @@ public interface BaseGraphOperators<
// Unary Operators
//----------------------------------------------------------------------------

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism strategies,
* which is vertex homomorphism and edge isomorphism. The vertex and edge data of the data graph elements
* is attached to the resulting vertices.
* <p>
* Note, that this method used no statistics about the data graph which may result in bad runtime
* performance. Use {@link #query(String, GraphStatistics)} to provide statistics for the query planner.
*
* @param query Cypher query
* @return graph collection containing matching subgraphs
*/
default GC query(String query) {
return query(query, new GraphStatistics(1, 1, 1, 1));
}

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism strategies,
* which is vertex homomorphism and edge isomorphism. The vertex and edge data of the data graph elements
* is attached to the resulting vertices.
* <p>
* Note, that this method used no statistics about the data graph which may result in bad runtime
* performance. Use {@link #query(String, String, GraphStatistics)} to provide statistics for the query planner.
* <p>
* In addition, the operator can be supplied with a construction pattern allowing the creation of new graph
* elements based on variable bindings of the match pattern. Consider the following example:
* <br>
* {@code graph.query(
* "MATCH (a:Author)-[:WROTE]->(:Paper)<-[:WROTE]-(b:Author) WHERE a <> b",
* "(a)-[:CO_AUTHOR]->(b)")}
* <p>
* The query pattern is looking for pairs of authors that worked on the same paper.
* The construction pattern defines a new edge of type CO_AUTHOR between the two entities.
*
* @param query Cypher query string
* @param constructionPattern Construction pattern
* @return graph collection containing the output of the construct pattern
*/
default GC query(String query, String constructionPattern) {
return query(query, constructionPattern, new GraphStatistics(1, 1, 1, 1));
}

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism strategies,
* which is vertex homomorphism and edge isomorphism. The vertex and edge data of the data graph elements
* is attached to the resulting vertices.
*
* @param query Cypher query
* @param graphStatistics statistics about the data graph
* @return graph collection containing matching subgraphs
*/
default GC query(String query, GraphStatistics graphStatistics) {
return query(query, true, MatchStrategy.HOMOMORPHISM, MatchStrategy.ISOMORPHISM,
graphStatistics);
}

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism strategies,
* which is vertex homomorphism and edge isomorphism. The vertex and edge data of the data graph elements
* is attached to the resulting vertices.
* <p>
* In addition, the operator can be supplied with a construction pattern allowing the creation of new graph
* elements based on variable bindings of the match pattern. Consider the following example:
* <br>
* {@code graph.query(
* "MATCH (a:Author)-[:WROTE]->(:Paper)<-[:WROTE]-(b:Author) WHERE a <> b",
* "(a)-[:CO_AUTHOR]->(b)")}
* <p>
* The query pattern is looking for pairs of authors that worked on the same paper.
* The construction pattern defines a new edge of type CO_AUTHOR between the two entities.
*
* @param query Cypher query
* @param constructionPattern Construction pattern
* @param graphStatistics statistics about the data graph
* @return graph collection containing the output of the construct pattern
*/
default GC query(String query, String constructionPattern, GraphStatistics graphStatistics) {
return query(query, constructionPattern, true, MatchStrategy.HOMOMORPHISM,
MatchStrategy.ISOMORPHISM, graphStatistics);
}

/**
* Evaluates the given query using the Cypher query engine.
*
* @param query Cypher query
* @param attachData attach original vertex and edge data to the result
* @param vertexStrategy morphism setting for vertex mapping
* @param edgeStrategy morphism setting for edge mapping
* @param graphStatistics statistics about the data graph
* @return graph collection containing matching subgraphs
*/
default GC query(String query, boolean attachData, MatchStrategy vertexStrategy,
MatchStrategy edgeStrategy, GraphStatistics graphStatistics) {
return query(query, null, attachData, vertexStrategy, edgeStrategy, graphStatistics);
}

/**
* Evaluates the given query using the Cypher query engine.
*
* @param query Cypher query
* @param constructionPattern Construction pattern
* @param attachData attach original vertex and edge data to the result
* @param vertexStrategy morphism setting for vertex mapping
* @param edgeStrategy morphism setting for edge mapping
* @param graphStatistics statistics about the data graph
* @return graph collection containing matching subgraphs
*/
default GC query(String query, String constructionPattern, boolean attachData, MatchStrategy vertexStrategy,
MatchStrategy edgeStrategy, GraphStatistics graphStatistics) {
return callForCollection(new CypherPatternMatching<>(query, constructionPattern, attachData,
vertexStrategy, edgeStrategy, graphStatistics));
}

/**
* Creates a copy of the base graph.
* <p>
Expand Down Expand Up @@ -376,6 +492,20 @@ default LG exclude(LG otherGraph) {
return callForGraph(new Exclusion<>(), otherGraph);
}

//----------------------------------------------------------------------------
// Utility methods
//----------------------------------------------------------------------------

/**
* Returns a 1-element dataset containing a {@code boolean} value which indicates if the graph is empty.
*
* A graph is considered empty, if it contains no vertices.
*
* @return 1-element dataset containing {@code true}, if the collection is
* empty or {@code false} if not
*/
DataSet<Boolean> isEmpty();

//----------------------------------------------------------------------------
// Call for Operators
//----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.gradoop.flink.model.api.epgm;

import org.apache.flink.api.java.DataSet;
import org.gradoop.common.model.impl.pojo.EPGMEdge;
import org.gradoop.common.model.impl.pojo.EPGMGraphHead;
import org.gradoop.common.model.impl.pojo.EPGMVertex;
Expand All @@ -35,17 +34,6 @@ public interface GraphCollectionOperators
// Auxiliary operators
//----------------------------------------------------------------------------

/**
* Returns a 1-element dataset containing a {@code boolean} value which
* indicates if the collection is empty.
*
* A collection is considered empty, if it contains no logical graphs.
*
* @return 1-element dataset containing {@code true}, if the collection is
* empty or {@code false} if not
*/
DataSet<Boolean> isEmpty();

/**
* Writes the graph collection to the given data sink.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.gradoop.flink.model.impl.epgm.GraphCollection;
import org.gradoop.flink.model.impl.epgm.LogicalGraph;
import org.gradoop.flink.model.impl.operators.cypher.capf.result.CAPFQueryResult;
import org.gradoop.flink.model.impl.operators.matching.common.MatchStrategy;
import org.gradoop.flink.model.impl.operators.matching.common.statistics.GraphStatistics;
import org.gradoop.flink.model.impl.operators.sampling.SamplingAlgorithm;

import java.io.IOException;
Expand Down Expand Up @@ -71,106 +69,6 @@ public interface LogicalGraphOperators
*/
CAPFQueryResult cypher(String query) throws Exception;

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism
* strategies, which is vertex homomorphism and edge isomorphism. The vertex and edge data of
* the data graph elements is attached to the resulting vertices.
* <p>
* Note, that this method used no statistics about the data graph which may result in bad
* runtime performance. Use {@link LogicalGraphOperators#query(String, GraphStatistics)} to
* provide statistics for the query planner.
*
* @param query Cypher query
* @return graph collection containing matching subgraphs
*/
GraphCollection query(String query);

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism
* strategies, which is vertex homomorphism and edge isomorphism. The vertex and edge data of
* the data graph elements is attached to the resulting vertices.
* <p>
* Note, that this method used no statistics about the data graph which may result in bad
* runtime performance. Use {@link LogicalGraphOperators#query(String, GraphStatistics)} to
* provide statistics for the query planner.
* <p>
* In addition, the operator can be supplied with a construction pattern allowing the creation
* of new graph elements based on variable bindings of the match pattern. Consider the following example:
* <p>
* {@code graph.query(
* "MATCH (a:Author)-[:WROTE]->(:Paper)<-[:WROTE]-(b:Author) WHERE a <> b",
* "(a)-[:CO_AUTHOR]->(b)")}
* <p>
* The query pattern is looking for pairs of authors that worked on the same paper.
* The construction pattern defines a new edge of type CO_AUTHOR between the two entities.
*
* @param query Cypher query string
* @param constructionPattern Construction pattern
* @return graph collection containing the output of the construct pattern
*/
GraphCollection query(String query, String constructionPattern);

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism
* strategies, which is vertex homomorphism and edge isomorphism. The vertex and edge data of
* the data graph elements is attached to the resulting vertices.
*
* @param query Cypher query
* @param graphStatistics statistics about the data graph
* @return graph collection containing matching subgraphs
*/
GraphCollection query(String query, GraphStatistics graphStatistics);

/**
* Evaluates the given query using the Cypher query engine. The engine uses default morphism
* strategies, which is vertex homomorphism and edge isomorphism. The vertex and edge data of
* the data graph elements is attached to the resulting vertices.
* <p>
* In addition, the operator can be supplied with a construction pattern allowing the creation
* of new graph elements based on variable bindings of the match pattern. Consider the following example:
* <p>
* {@code graph.query(
* "MATCH (a:Author)-[:WROTE]->(:Paper)<-[:WROTE]-(b:Author) WHERE a <> b",
* "(a)-[:CO_AUTHOR]->(b)")}
* <p>
* The query pattern is looking for pairs of authors that worked on the same paper.
* The construction pattern defines a new edge of type CO_AUTHOR between the two entities.
*
* @param query Cypher query
* @param constructionPattern Construction pattern
* @param graphStatistics statistics about the data graph
* @return graph collection containing the output of the construct pattern
*/
GraphCollection query(String query, String constructionPattern, GraphStatistics graphStatistics);

/**
* Evaluates the given query using the Cypher query engine.
*
* @param query Cypher query
* @param attachData attach original vertex and edge data to the result
* @param vertexStrategy morphism setting for vertex mapping
* @param edgeStrategy morphism setting for edge mapping
* @param graphStatistics statistics about the data graph
* @return graph collection containing matching subgraphs
*/
GraphCollection query(String query, boolean attachData, MatchStrategy vertexStrategy,
MatchStrategy edgeStrategy, GraphStatistics graphStatistics);

/**
* Evaluates the given query using the Cypher query engine.
*
* @param query Cypher query
* @param constructionPattern Construction pattern
* @param attachData attach original vertex and edge data to the result
* @param vertexStrategy morphism setting for vertex mapping
* @param edgeStrategy morphism setting for edge mapping
* @param graphStatistics statistics about the data graph
* @return graph collection containing matching subgraphs
*/
GraphCollection query(String query, String constructionPattern, boolean attachData,
MatchStrategy vertexStrategy, MatchStrategy edgeStrategy,
GraphStatistics graphStatistics);

/**
* Creates a new graph from a randomly chosen subset of nodes and their associated edges.
*
Expand Down Expand Up @@ -260,16 +158,6 @@ GraphCollection groupEdgesByRollUp(
*/
LogicalGraph callForGraph(GraphsToGraphOperator operator, LogicalGraph... otherGraphs);

/**
* Returns a 1-element dataset containing a {@code boolean} value which indicates if the graph is empty.
*
* A graph is considered empty, if it contains no vertices.
*
* @return 1-element dataset containing {@code true}, if the collection is
* empty or {@code false} if not
*/
DataSet<Boolean> isEmpty();

/**
* Writes the graph to given data sink.
*
Expand Down
Loading

0 comments on commit 50b81ab

Please sign in to comment.