Skip to content

Commit

Permalink
DynamicGraphInput working.
Browse files Browse the repository at this point in the history
Persist working.
  • Loading branch information
fsaintpreux committed Apr 27, 2024
1 parent 8994282 commit 63c8e99
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public DynamicGraph readGraph() throws AdapterException {
String edgeDate = line.get(0);
String edgeAction = "+";
Edge edge = graph.addEdge(UUID.randomUUID().toString(), sourceNode, targetNode);
graph.addDynamicInteraction(new DynamicInteraction(edge, edgeDate, edgeAction));
graph.addDynamicInteraction(edge, edgeDate, edgeAction);
line = Adapters.readLine(reader);
}
} else if (line.size() == 4) {
Expand All @@ -107,15 +107,15 @@ public DynamicGraph readGraph() throws AdapterException {
String edgeAction = line.get(1);
if (edgeAction.equals("+")) {
Edge edge = graph.addEdge(UUID.randomUUID().toString(), sourceNode, targetNode);
graph.addDynamicInteraction(new DynamicInteraction(edge,edgeDate,edgeAction));
graph.addDynamicInteraction(edge, edgeDate, edgeAction);
}else if(edgeAction.equals("-")) {
try {
Edge edge = graph.removeEdge(sourceNode,targetNode);
graph.addDynamicInteraction(new DynamicInteraction(edge,edgeDate,edgeAction));
graph.addDynamicInteraction(edge, edgeDate, edgeAction);
} catch (ElementNotFoundException e1) {
try {
Edge edge = graph.removeEdge(targetNode, sourceNode);
graph.addDynamicInteraction(new DynamicInteraction(edge,edgeDate,edgeAction));
graph.addDynamicInteraction(edge, edgeDate, edgeAction);
} catch (ElementNotFoundException e2) {
System.out.println(e1 + " and/or " + e2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ protected CustomEdge(CustomEdge customEdge) {
public int getId() {
return id;
}

public CustomGraph getGraph() {
return this.graph;
}

public void setGraph(CustomGraph graph) {
this.graph = graph;
}

/**
* Getter for the key.
* @return The key.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package i5.las2peer.services.ocd.graphs;

import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.model.DocumentCreateOptions;
import com.arangodb.model.DocumentUpdateOptions;
import i5.las2peer.services.ocd.metrics.OcdMetricLog;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.AbstractGraph;
Expand All @@ -12,6 +18,8 @@ public class DynamicGraph extends CustomGraph{
* Dynamic graph extension
* Extends CustomGraph's by a list of interactions
*/

public static final String dynInKeysColumnName = "DYNAMICINTERACTION_KEYS";
private List<DynamicInteraction> dynamicInteractions = new ArrayList<>();

public DynamicGraph() {
Expand Down Expand Up @@ -58,77 +66,32 @@ public boolean isDynamic() {
return isOfType(GraphType.DYNAMIC);
}

public void addDynamicInteraction(DynamicInteraction dynamicInteraction) {
public void addDynamicInteraction(Edge edge, String date, String action) {
CustomNode source = this.getCustomNode(edge.getSourceNode());
CustomNode target = this.getCustomNode(edge.getTargetNode());
DynamicInteraction dynamicInteraction = new DynamicInteraction(source, target, date, action);
this.dynamicInteractions.add(dynamicInteraction);
}

/*@Override
protected void copyMappings(Map<Integer, CustomNode> customNodes, Map<Integer, CustomEdge> customEdges, Map<MultiNode, Integer> nodeIds, Map<Edge, Integer> edgeIds) {
copyDynamicMappings(customNodes, customEdges, nodeIds, edgeIds);
}*/

/*@Override
protected void addCustomEdge(Edge edge) {
DynamicInteraction dynamicInteraction = new DynamicInteraction();
this.addDynamicInteraction(edge, dynamicInteraction);
}*/

/**
* Getter for the edge date of a certain edge.
*
* @param edge
* The edge.
* @return The edge date.
*//*
public String getEdgeDate(Edge edge) {
if(getCustomEdge(edge) instanceof DynamicInteraction) {
DynamicInteraction result = (DynamicInteraction) getCustomEdge(edge);
return result.getDate();
@Override
public void persist(ArangoDatabase db, String transId) throws InterruptedException {
super.persist(db, transId);
for(DynamicInteraction dynIn: dynamicInteractions) {
dynIn.update(this);
}
return "no date";
}
*//**
* Setter for the edge date of a certain edge.
*
* @param edge
* The edge.
* @param date
* The edge date.
*//*
public void setEdgeDate(Edge edge, String date) {
if(getCustomEdge(edge) instanceof DynamicInteraction) {
DynamicInteraction result = (DynamicInteraction) getCustomEdge(edge);
result.setDate(date);
ArangoCollection collection = db.collection(collectionName);
BaseDocument bd = new BaseDocument();
DocumentUpdateOptions updateOptions = new DocumentUpdateOptions().streamTransactionId(transId);
DocumentCreateOptions createOptions = new DocumentCreateOptions().streamTransactionId(transId);

List<String> dynInteractionKeyList = new ArrayList<String>();
for(DynamicInteraction di : this.dynamicInteractions) {
di.persist(db, createOptions);
dynInteractionKeyList.add(di.getKey());
}
}

*//**
* Getter for the edge action of a certain edge.
*
* @param edge
* The edge.
* @return The edge action.
*//*
public String getEdgeAction(Edge edge) {
if(getCustomEdge(edge) instanceof DynamicInteraction) {
DynamicInteraction result = (DynamicInteraction) getCustomEdge(edge);
return result.getAction();
}
return "no action";
}
bd.addAttribute(dynInKeysColumnName, dynInteractionKeyList);

*//**
* Setter for the edge action of a certain edge.
*
* @param edge
* The edge.
* @param action
* The edge action.
*//*
public void setEdgeAction(Edge edge, String action) {
if(getCustomEdge(edge) instanceof DynamicInteraction) {
((DynamicInteraction) getCustomEdge(edge)).setAction(action);
}
}*/
collection.updateDocument(this.getKey(), bd, updateOptions);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package i5.las2peer.services.ocd.graphs;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseEdgeDocument;
import com.arangodb.model.DocumentCreateOptions;
import com.arangodb.model.DocumentReadOptions;
import org.graphstream.graph.Node;
import org.graphstream.graph.Edge;

Expand All @@ -7,9 +12,12 @@
*
* @author fsaintpreux
*/
public class DynamicInteraction {
private Node source;
private Node target;
public class DynamicInteraction extends CustomEdge{

private static final String dateColumnName = "DATE";
private static final String actionColumnName = "ACTION";

public static final String collectionName = "dynamicinteraction";

/**
* The edge date as a string
Expand All @@ -24,36 +32,13 @@ public class DynamicInteraction {
public DynamicInteraction() {
}


public DynamicInteraction(DynamicInteraction dynamicInteraction) {
this.source = dynamicInteraction.source;
this.target = dynamicInteraction.target;
this.action = dynamicInteraction.action;
this.date = dynamicInteraction.date;
}

public DynamicInteraction(Edge edge, String date, String action) {
this.source = edge.getSourceNode();
this.target = edge.getTargetNode();
public DynamicInteraction(CustomNode source, CustomNode target, String date, String action) {
this.setSource(source);
this.setTarget(target);
this.date = date;
this.action = action;
}

public Node getSource() {
return source;
}

public void setSource(Node source) {
this.source = source;
}

public Node getTarget() {
return target;
}

public void setTarget(Node target) {
this.target = target;
}

/**
* Getter for the date.
Expand Down Expand Up @@ -87,11 +72,50 @@ public void setAction(String action) {
this.action = action;
}

public void update(DynamicGraph graph) {
this.setGraph(graph);
}
@Override
public void persist(ArangoDatabase db, DocumentCreateOptions opt) {
ArangoCollection collection = db.collection(collectionName);
BaseEdgeDocument bed = new BaseEdgeDocument();
bed.addAttribute(graphKeyColumnName, this.getGraph().getKey());
bed.addAttribute(dateColumnName, this.date);
bed.addAttribute(actionColumnName, this.action);
bed.setFrom(CustomNode.collectionName + "/" + this.getSource().getKey());
bed.setTo(CustomNode.collectionName + "/" + this.getTarget().getKey());

collection.insertDocument(bed, opt);
this.key = bed.getKey();
}

public static DynamicInteraction load(BaseEdgeDocument bed, CustomNode source, CustomNode target, DynamicGraph graph, ArangoDatabase db) {
DynamicInteraction dynamicInteraction = new DynamicInteraction();

if (bed != null) {
dynamicInteraction.key = bed.getKey();
dynamicInteraction.setGraph(graph);
if(bed.getAttribute(dateColumnName)!=null) {
dynamicInteraction.date = bed.getAttribute(dateColumnName).toString();
}
if(bed.getAttribute(actionColumnName) != null) {
dynamicInteraction.action = bed.getAttribute(actionColumnName).toString();
}
dynamicInteraction.setSource(source);
dynamicInteraction.setTarget(target);
}
else {
System.out.println("Empty Document");
}
return dynamicInteraction;

}

@Override
public String toString() {
return "DynamicInteraction{" +
"source=" + source +
", target=" + target +
"source=" + this.getSource() +
", target=" + this.getTarget() +
", date='" + date + '\'' +
", action='" + action + '\'' +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void determineGraphTypes(CustomGraph graph) {
}

}

if (graph instanceof DynamicGraph) {
graph.addType(GraphType.DYNAMIC);
}

if (graph.getPath() != "" && graph.getPath() != null) {
if (graph.getEdgeCount() == 0) {
graph.addType(GraphType.CONTENT_UNLINKED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public void createCollections() {
if(!collection.exists()) {
collection.create();
}
collectionNames.add(DynamicInteraction.collectionName); //13
collection = db.collection(DynamicInteraction.collectionName);
if(!collection.exists()) {
collection.create();
}

}

Expand All @@ -199,6 +204,9 @@ public void createCollections() {
*/
public String storeGraph(CustomGraph graph) {
String transId = getTransactionId(CustomGraph.class, true);
if(graph instanceof DynamicGraph) {
transId = getTransactionId(DynamicGraph.class, true);
}
try {
graph.persist(db, transId);
db.commitStreamTransaction(transId);
Expand Down Expand Up @@ -1762,7 +1770,7 @@ else if(c == SimulationSeriesGroup.class){
collections = collectionNames.subList(11,13).toArray(new String[1]);
}
else {
collections = collectionNames.subList(0, 13).toArray(new String[10]);
collections = collectionNames.subList(0, 14).toArray(new String[10]);
}
StreamTransactionEntity tx;
if(write) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
package i5.las2peer.services.ocd.graphs;

import i5.las2peer.services.ocd.utils.Database;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class DynamicGraphDatabaseTest {
}

private static final String userName1 = "testUser1";
private static final String graphName1 = "persistenceTestGraph1";
private static final String invalidGraphName = "invalidGraphName";
private static Database database;

@BeforeClass
public static void clearDatabase() {
database = new Database(true);
}

//@AfterClass
// public static void deleteDatabase() {
// database.deleteDatabase();
// }

@Test
public void testPersist() {
DynamicGraph graph = new DynamicGraph();
graph.setUserName(userName1);
graph.setName(graphName1);
Node nodeA = graph.addNode("A");
Node nodeB = graph.addNode("B");
Node nodeC = graph.addNode("C");
graph.setNodeName(nodeA, "A");
graph.setNodeName(nodeB, "B");
graph.setNodeName(nodeC, "C");
Edge edgeAB = graph.addEdge(UUID.randomUUID().toString(), nodeA, nodeB);
graph.addDynamicInteraction(edgeAB,"0","+");
graph.setEdgeWeight(edgeAB, 5);
Edge edgeBC = graph.addEdge(UUID.randomUUID().toString(), nodeB, nodeC);
graph.addDynamicInteraction(edgeBC, "1","-");
graph.setEdgeWeight(edgeBC, 2.5);
graph.addType(GraphType.DYNAMIC);

database.storeGraph(graph);


}

}
Loading

0 comments on commit 63c8e99

Please sign in to comment.