Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1088] Move Element factories from Config to BaseFactory #1301

Merged
merged 13 commits into from Jul 3, 2019
Expand Up @@ -20,10 +20,7 @@
import org.gradoop.common.model.api.entities.Vertex;
import org.gradoop.common.model.impl.pojo.EPGMEdge;
import org.gradoop.common.model.impl.pojo.EPGMGraphHead;
import org.gradoop.common.model.impl.pojo.EPGMGraphHeadFactory;
import org.gradoop.common.model.impl.pojo.EPGMVertex;
import org.gradoop.common.model.impl.pojo.EPGMEdgeFactory;
import org.gradoop.common.model.impl.pojo.EPGMVertexFactory;

/**
* Basic Gradoop Configuration.
Expand All @@ -32,32 +29,7 @@
* @param <V> EPGM vertex type
* @param <E> EPGM edge type
*/
public class GradoopConfig
<G extends GraphHead, V extends Vertex, E extends Edge> {

/**
* Knows how to create {@link EPGMGraphHead}
*/
private final EPGMGraphHeadFactory graphHeadFactory;

/**
* Knows how to create {@link EPGMVertex}
*/
private final EPGMVertexFactory vertexFactory;

/**
* Knows how to create {@link EPGMEdge}
*/
private final EPGMEdgeFactory edgeFactory;

/**
* Creates a new Configuration.
*/
protected GradoopConfig() {
this.graphHeadFactory = new EPGMGraphHeadFactory();
this.vertexFactory = new EPGMVertexFactory();
this.edgeFactory = new EPGMEdgeFactory();
}
public class GradoopConfig<G extends GraphHead, V extends Vertex, E extends Edge> {

/**
* Creates a default Configuration using POJO handlers for vertices, edges
Expand All @@ -68,16 +40,4 @@ protected GradoopConfig() {
public static GradoopConfig<EPGMGraphHead, EPGMVertex, EPGMEdge> getDefaultConfig() {
return new GradoopConfig<>();
}

public EPGMGraphHeadFactory getGraphHeadFactory() {
return graphHeadFactory;
}

public EPGMVertexFactory getVertexFactory() {
return vertexFactory;
}

public EPGMEdgeFactory getEdgeFactory() {
return edgeFactory;
}
}
Expand Up @@ -19,11 +19,11 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.gradoop.common.model.api.entities.ElementFactoryProvider;
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.common.model.impl.id.GradoopId;
import org.gradoop.common.config.GradoopConfig;
import org.gradoop.common.model.impl.id.GradoopIdSet;
import org.gradoop.common.model.impl.properties.Properties;
import org.s1ck.gdl.GDLHandler;
Expand All @@ -40,16 +40,16 @@
*
* @see <a href="https://github.com/s1ck/gdl">GDL on GitHub</a>
*
* @param <G> EPGM graph head type
* @param <V> EPGM vertex type
* @param <E> EPGM edge type
* @param <G> graph head type
* @param <V> vertex type
* @param <E> edge type
*/
public class AsciiGraphLoader<G extends GraphHead, V extends Vertex, E extends Edge> {

/**
* Gradoop configuration
* Factory provider for graph elements.
*/
private final GradoopConfig<G, V, E> config;
private final ElementFactoryProvider<G, V, E> elementFactoryProvider;

/**
* Used to parse GDL scripts.
Expand Down Expand Up @@ -98,12 +98,12 @@ public class AsciiGraphLoader<G extends GraphHead, V extends Vertex, E extends E
* Creates a new AsciiGraphLoader.
*
* @param gdlHandler GDL Handler
* @param config Gradoop configuration
* @param elementFactoryProvider Factory provider for EPGM elements.
*/
private AsciiGraphLoader(GDLHandler gdlHandler,
GradoopConfig<G, V, E> config) {
ElementFactoryProvider<G, V, E> elementFactoryProvider) {
this.gdlHandler = gdlHandler;
this.config = config;
this.elementFactoryProvider = elementFactoryProvider;

this.graphHeads = Maps.newHashMap();
this.vertices = Maps.newHashMap();
Expand All @@ -123,72 +123,74 @@ private AsciiGraphLoader(GDLHandler gdlHandler,
/**
* Creates an AsciiGraphLoader from the given ASCII GDL string.
*
* @param asciiGraph GDL string
* @param config Gradoop configuration
* @param <G> EPGM graph head type
* @param <V> EPGM vertex type
* @param <E> EPGM edge type
* @param asciiGraph GDL string
* @param elementFactoryProvider Factory provider for graph elements.
* @param <G> graph head type
* @param <V> vertex type
* @param <E> edge type
*
* @return AsciiGraphLoader
*/
public static
<G extends GraphHead, V extends Vertex, E extends Edge>
AsciiGraphLoader<G, V, E> fromString(String asciiGraph,
GradoopConfig<G, V, E> config) {
ElementFactoryProvider<G, V, E> elementFactoryProvider) {
return new AsciiGraphLoader<>(new GDLHandler.Builder()
.setDefaultGraphLabel(GradoopConstants.DEFAULT_GRAPH_LABEL)
.setDefaultVertexLabel(GradoopConstants.DEFAULT_VERTEX_LABEL)
.setDefaultEdgeLabel(GradoopConstants.DEFAULT_EDGE_LABEL)
.buildFromString(asciiGraph),
config);
elementFactoryProvider);
}

/**
* Creates an AsciiGraphLoader from the given ASCII GDL file.
*
* @param fileName File that contains a GDL script
* @param config Gradoop configuration
* @param <G> EPGM graph head type
* @param <V> EPGM vertex type
* @param <E> EPGM edge type
* @param fileName File that contains a GDL script
* @param elementFactoryProvider Factory provider for graph elements.
* @param <G> graph head type
* @param <V> vertex type
* @param <E> edge type
*
* @return AsciiGraphLoader
* @throws IOException on failure
*/
public static
<G extends GraphHead, V extends Vertex, E extends Edge>
AsciiGraphLoader<G, V, E> fromFile(String fileName,
GradoopConfig<G, V, E> config) throws IOException {
ElementFactoryProvider<G, V, E> elementFactoryProvider)
throws IOException {
return new AsciiGraphLoader<>(new GDLHandler.Builder()
.setDefaultGraphLabel(GradoopConstants.DEFAULT_GRAPH_LABEL)
.setDefaultVertexLabel(GradoopConstants.DEFAULT_VERTEX_LABEL)
.setDefaultEdgeLabel(GradoopConstants.DEFAULT_EDGE_LABEL)
.buildFromFile(fileName),
config);
elementFactoryProvider);
}

/**
* Creates an AsciiGraphLoader from the given ASCII GDL file.
*
* @param inputStream File that contains a GDL script
* @param config Gradoop configuration
* @param <G> EPGM graph head type
* @param <V> EPGM vertex type
* @param <E> EPGM edge type
* @param inputStream File that contains a GDL script
* @param elementFactoryProvider Factory provider for graph elements.
* @param <G> graph head type
* @param <V> vertex type
* @param <E> edge type
*
* @return AsciiGraphLoader
* @throws IOException on failure
*/
public static
<G extends GraphHead, V extends Vertex, E extends Edge>
AsciiGraphLoader<G, V, E> fromStream(InputStream inputStream,
GradoopConfig<G, V, E> config) throws IOException {
ElementFactoryProvider<G, V, E> elementFactoryProvider)
throws IOException {
return new AsciiGraphLoader<>(new GDLHandler.Builder()
.setDefaultGraphLabel(GradoopConstants.DEFAULT_GRAPH_LABEL)
.setDefaultVertexLabel(GradoopConstants.DEFAULT_VERTEX_LABEL)
.setDefaultEdgeLabel(GradoopConstants.DEFAULT_EDGE_LABEL)
.buildFromStream(inputStream),
config);
elementFactoryProvider);
}

/**
Expand Down Expand Up @@ -473,13 +475,13 @@ private void initEdges() {
}

/**
* Creates a new EPGMGraph from the GDL Loader.
* Creates a new Graph from the GDL Loader.
*
* @param g graph from GDL Loader
* @return EPGM graph head
* @return graph head
*/
private G initGraphHead(Graph g) {
G graphHead = (G) config.getGraphHeadFactory().createGraphHead(
G graphHead = elementFactoryProvider.getGraphHeadFactory().createGraphHead(
g.getLabel(), Properties.createFromMap(g.getProperties()));
graphHeadIds.put(g.getId(), graphHead.getId());
graphHeads.put(graphHead.getId(), graphHead);
Expand All @@ -490,12 +492,12 @@ private G initGraphHead(Graph g) {
* Creates a new Vertex from the GDL Loader or updates an existing one.
*
* @param v vertex from GDL Loader
* @return EPGM vertex
* @return vertex
*/
private V initVertex(org.s1ck.gdl.model.Vertex v) {
V vertex;
if (!vertexIds.containsKey(v.getId())) {
vertex = (V) config.getVertexFactory().createVertex(
vertex = elementFactoryProvider.getVertexFactory().createVertex(
v.getLabel(),
Properties.createFromMap(v.getProperties()),
createGradoopIdSet(v));
Expand All @@ -512,12 +514,12 @@ private V initVertex(org.s1ck.gdl.model.Vertex v) {
* Creates a new Edge from the GDL Loader.
*
* @param e edge from GDL loader
* @return EPGM edge
* @return edge
*/
private E initEdge(org.s1ck.gdl.model.Edge e) {
E edge;
if (!edgeIds.containsKey(e.getId())) {
edge = (E) config.getEdgeFactory().createEdge(
edge = elementFactoryProvider.getEdgeFactory().createEdge(
e.getLabel(),
vertexIds.get(e.getSourceVertexId()),
vertexIds.get(e.getTargetVertexId()),
Expand Down
Expand Up @@ -23,15 +23,21 @@
import org.apache.flink.core.memory.DataOutputView;
import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
import org.apache.flink.types.Value;
import org.gradoop.common.config.GradoopConfig;
import org.gradoop.common.model.api.entities.EdgeFactory;
import org.gradoop.common.model.api.entities.Element;
import org.gradoop.common.model.api.entities.ElementFactoryProvider;
import org.gradoop.common.model.api.entities.GraphElement;
import org.gradoop.common.model.api.entities.GraphHeadFactory;
import org.gradoop.common.model.api.entities.Identifiable;
import org.gradoop.common.model.api.entities.VertexFactory;
import org.gradoop.common.model.impl.comparators.IdentifiableComparator;
import org.gradoop.common.model.impl.id.GradoopId;
import org.gradoop.common.model.impl.pojo.EPGMEdge;
import org.gradoop.common.model.impl.pojo.EPGMEdgeFactory;
import org.gradoop.common.model.impl.pojo.EPGMGraphHead;
import org.gradoop.common.model.impl.pojo.EPGMGraphHeadFactory;
import org.gradoop.common.model.impl.pojo.EPGMVertex;
import org.gradoop.common.model.impl.pojo.EPGMVertexFactory;
import org.gradoop.common.model.impl.properties.PropertyValue;
import org.gradoop.common.util.AsciiGraphLoader;

Expand Down Expand Up @@ -103,6 +109,12 @@ public class GradoopTestUtils {

private static Comparator<Identifiable> ID_COMPARATOR = new IdentifiableComparator();

/**
* Singleton instance of a EPGM ElementFactoryProvider.
*/
private static ElementFactoryProvider<EPGMGraphHead, EPGMVertex, EPGMEdge> epgmElementFactoryProvider =
null;

static {
MAP_VAL_9.put(PropertyValue.create(KEY_0), PropertyValue.create(NULL_VAL_0));
MAP_VAL_9.put(PropertyValue.create(KEY_1), PropertyValue.create(BOOL_VAL_1));
Expand Down Expand Up @@ -165,6 +177,37 @@ public class GradoopTestUtils {
SUPPORTED_PROPERTIES.put(KEY_f, SET_VAL_f);
}

/**
* Returns a {@link ElementFactoryProvider} able to create EPGM elements.
*
* @return ElementFactoryProvider for EPGM elements
*/
public static ElementFactoryProvider<EPGMGraphHead, EPGMVertex, EPGMEdge> getEPGMElementFactoryProvider() {
if (epgmElementFactoryProvider == null) {
epgmElementFactoryProvider = new ElementFactoryProvider<EPGMGraphHead, EPGMVertex, EPGMEdge>() {
GraphHeadFactory<EPGMGraphHead> graphHeadFactory = new EPGMGraphHeadFactory();
VertexFactory<EPGMVertex> vertexFactory = new EPGMVertexFactory();
EdgeFactory<EPGMEdge> edgeFactory = new EPGMEdgeFactory();

@Override
public GraphHeadFactory<EPGMGraphHead> getGraphHeadFactory() {
return graphHeadFactory;
}

@Override
public VertexFactory<EPGMVertex> getVertexFactory() {
return vertexFactory;
}

@Override
public EdgeFactory<EPGMEdge> getEdgeFactory() {
return edgeFactory;
}
};
}
return epgmElementFactoryProvider;
}

/**
* Creates a social network as a basis for tests.
* <p/>
Expand All @@ -176,11 +219,8 @@ public class GradoopTestUtils {
*/
public static AsciiGraphLoader<EPGMGraphHead, EPGMVertex, EPGMEdge> getSocialNetworkLoader()
throws IOException {

GradoopConfig<EPGMGraphHead, EPGMVertex, EPGMEdge> config = GradoopConfig.getDefaultConfig();

InputStream inputStream = GradoopTestUtils.class.getResourceAsStream(SOCIAL_NETWORK_GDL_FILE);
return AsciiGraphLoader.fromStream(inputStream, config);
return AsciiGraphLoader.fromStream(inputStream, getEPGMElementFactoryProvider());
}

/**
Expand Down