/*
 * Copyright 2015 Delft University of Technology
 *
 * 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 science.atlarge.graphalytics.neo4j;

import science.atlarge.graphalytics.validation.GraphStructure;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.test.TestGraphDatabaseFactory;

import java.util.HashMap;
import java.util.Map;

import static science.atlarge.graphalytics.neo4j.Neo4jConfiguration.EDGE;
import static science.atlarge.graphalytics.neo4j.Neo4jConfiguration.ID_PROPERTY;
import static science.atlarge.graphalytics.neo4j.Neo4jConfiguration.VertexLabelEnum.Vertex;

/**
 * Utility class for loading a validation graph into a Neo4j in-memory database.
 *
 * @author Tim Hegeman
 */
public final class ValidationGraphLoader {

	private ValidationGraphLoader() {
	}

	public static GraphDatabaseService loadValidationGraphToDatabase(GraphStructure validationGraph) {
		GraphDatabaseService graphDatabase = new TestGraphDatabaseFactory().newImpermanentDatabase();
		try (Transaction tx = graphDatabase.beginTx()) {
			Map<Long, Node> vertexToNode = new HashMap<>();
			for (long vertexId : validationGraph.getVertices()) {
				Node newVertex = graphDatabase.createNode((Label)Vertex);
				newVertex.setProperty(ID_PROPERTY, vertexId);
				vertexToNode.put(vertexId, newVertex);
			}

			for (long vertexId : validationGraph.getVertices()) {
				Node sourceVertex = vertexToNode.get(vertexId);
				for (long neighbourId : validationGraph.getEdgesForVertex(vertexId)) {
					sourceVertex.createRelationshipTo(vertexToNode.get(neighbourId), EDGE);
				}
			}

			tx.success();
		}
		return graphDatabase;
	}

}

@@ -656,16 +656,117 @@
 neo4j;%0A%0A
+import org.neo4j.graphdb.Relationship;%0Aimport science.atlarge.graphalytics.util.graph.PropertyGraph;%0A
 import s
@@ -820,16 +820,16 @@
 ucture;%0A
-
 import o
@@ -1312,16 +1312,101 @@
 .Vertex;
+%0Aimport static science.atlarge.graphalytics.neo4j.Neo4jConfiguration.WEIGHT_PROPERTY;
 %0A%0A/**%0A *
@@ -2293,24 +2293,24 @@
 ertexId)) %7B%0A
-
 %09%09%09%09%09sourceV
@@ -2435,10 +2435,1053 @@
 se;%0A%09%7D%0A%0A
+%0A%09public static %3CV, E%3E GraphDatabaseService loadValidationGraphToDatabase(PropertyGraph%3CV, E%3E graph) %7B%0A%09%09GraphDatabaseService graphDatabase = new TestGraphDatabaseFactory().newImpermanentDatabase();%0A%09%09try (Transaction tx = graphDatabase.beginTx()) %7B%0A%09%09%09Map%3CPropertyGraph%3CV, E%3E.Vertex, Node%3E vertexToNode = new HashMap%3C%3E();%0A%09%09%09for (PropertyGraph%3CV, E%3E.Vertex vertex : graph.getVertices()) %7B%0A%09%09%09%09Node newVertex = graphDatabase.createNode((Label)Vertex);%0A%09%09%09%09newVertex.setProperty(ID_PROPERTY, vertex.getId());%0A%09%09%09%09vertexToNode.put(vertex, newVertex);%0A%09%09%09%7D%0A%0A%09%09%09for (PropertyGraph%3CV, E%3E.Vertex vertex : graph.getVertices()) %7B%0A%09%09%09%09Node sourceVertex = vertexToNode.get(vertex);%0A%0A%09%09%09%09for (PropertyGraph%3CV, E%3E.Edge edge : vertex.getOutgoingEdges()) %7B%0A%09%09%09%09%09PropertyGraph%3CV, E%3E.Vertex destinationVertex = edge.getDestinationVertex();%0A%09%09%09%09%09Relationship rel = sourceVertex.createRelationshipTo(vertexToNode.get(destinationVertex), EDGE);%0A%09%09%09%09%09rel.setProperty(WEIGHT_PROPERTY, edge.getValue());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09tx.success();%0A%09%09%7D%0A%09%09return graphDatabase;%0A%09%7D%0A%0A
 %7D%0A

Add property graph loader