Skip to content

Commit

Permalink
Added DynamicGraphs with a List of DynamicInteraction's
Browse files Browse the repository at this point in the history
Added TimestampedEdgeListInputAdapter.
And the respective test classes.
  • Loading branch information
fsaintpreux committed Apr 24, 2024
1 parent 681802f commit 8994282
Show file tree
Hide file tree
Showing 15 changed files with 613 additions and 4,546 deletions.
4,547 changes: 7 additions & 4,540 deletions ocd/mea/network.paj

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions ocd/test/input/TimestampedEdgeListWithAction.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
0 + 1 2
1 + 2 7
1 + 1 7
2 + 7 3
3 - 7 2
4 + 6 3
5 + 7 5
6 + 5 4
6 + 6 7
6 + 4 7
7 - 1 7
8 + 3 4
9 - 3 4
7 changes: 7 additions & 0 deletions ocd/test/input/TimestampedEdgeListWithoutAction.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0 1 2
1 2 3
2 1 3
3 2 4
4 3 4
5 2 5
6 4 5
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ public enum GraphInputFormat implements EnumDisplayNames {
/**
* Format corresponding to the LmsTripleStoreGraphInputAdapter.
*/
LMS_TRIPLESTORE ("Fetched from LMS Triplestore", LmsTripleStoreGraphInputAdapter.class, 9);

LMS_TRIPLESTORE ("Fetched from LMS Triplestore", LmsTripleStoreGraphInputAdapter.class, 9),
/**
* Format corresponding to the TimestampedEdgeListInputAdapter.
*/
TIMESTAMPED_EDGE_LIST("Timestamped Edge List", TimestampedEdgeListInputAdapter.class, 10);
/**
* The adapter class corresponding to the format.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package i5.las2peer.services.ocd.adapters.graphInput;

import i5.las2peer.services.ocd.adapters.AdapterException;
import i5.las2peer.services.ocd.adapters.Adapters;
import i5.las2peer.services.ocd.cooperation.simulation.dynamic.Dynamic;
import i5.las2peer.services.ocd.graphs.CustomGraph;

import java.io.Reader;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import i5.las2peer.services.ocd.graphs.DynamicGraph;
import i5.las2peer.services.ocd.graphs.DynamicInteraction;
import org.checkerframework.checker.units.qual.A;
import org.graphstream.graph.Edge;
import org.graphstream.graph.ElementNotFoundException;
import org.graphstream.graph.Node;

/**
* A graph input adapter for timestamped edge list format.
* Each line of input contains either a timestamp, node1 and node2 or a timestamp, action, node1 and node2.
* They are each separated by a space delimiter and in the first case an edge addition "+" is given as an attribute
* for the edge
* @author Fabien
*
*/
public class TimestampedEdgeListInputAdapter extends AbstractGraphInputAdapter {

/**
* Creates a new instance setting the reader attribute.
* @param reader The reader to receive input from.
*/
public TimestampedEdgeListInputAdapter(Reader reader) {
this.setReader(reader);
}

/**
* Creates a new instance.
*/
public TimestampedEdgeListInputAdapter() {
}

@Override
public void setParameter(Map<String,String> param) throws IllegalArgumentException, ParseException{

}

@Override
public DynamicGraph readGraph() throws AdapterException {
DynamicGraph graph = new DynamicGraph();
try {
Map<String, Node> reverseNodeNames = new HashMap<String, Node>();
List<String> line = Adapters.readLine(reader);
/*
* Reads edges
*/
if(line.size() == 3) {
while (line.size() == 3) {
String sourceNodeName = line.get(1);
Node sourceNode;
if (!reverseNodeNames.containsKey(sourceNodeName)) {
sourceNode = graph.addNode(sourceNodeName);
reverseNodeNames.put(sourceNodeName, sourceNode);
graph.setNodeName(sourceNode, sourceNodeName);
} else {
sourceNode = reverseNodeNames.get(sourceNodeName);
}
String targetNodeName = line.get(2);
Node targetNode;
if (!reverseNodeNames.containsKey(targetNodeName)) {
targetNode = graph.addNode(targetNodeName);
reverseNodeNames.put(targetNodeName, targetNode);
graph.setNodeName(targetNode, targetNodeName);
} else {
targetNode = reverseNodeNames.get(targetNodeName);
}
String edgeDate = line.get(0);
String edgeAction = "+";
Edge edge = graph.addEdge(UUID.randomUUID().toString(), sourceNode, targetNode);
graph.addDynamicInteraction(new DynamicInteraction(edge, edgeDate, edgeAction));
line = Adapters.readLine(reader);
}
} else if (line.size() == 4) {
while (line.size() == 4) {
String sourceNodeName = line.get(2);
Node sourceNode;
if (!reverseNodeNames.containsKey(sourceNodeName)) {
sourceNode = graph.addNode(sourceNodeName);
reverseNodeNames.put(sourceNodeName, sourceNode);
graph.setNodeName(sourceNode, sourceNodeName);
} else {
sourceNode = reverseNodeNames.get(sourceNodeName);
}
String targetNodeName = line.get(3);
Node targetNode;
if (!reverseNodeNames.containsKey(targetNodeName)) {
targetNode = graph.addNode(targetNodeName);
reverseNodeNames.put(targetNodeName, targetNode);
graph.setNodeName(targetNode, targetNodeName);
} else {
targetNode = reverseNodeNames.get(targetNodeName);
}
String edgeDate = line.get(0);
String edgeAction = line.get(1);
if (edgeAction.equals("+")) {
Edge edge = graph.addEdge(UUID.randomUUID().toString(), sourceNode, targetNode);
graph.addDynamicInteraction(new DynamicInteraction(edge,edgeDate,edgeAction));
}else if(edgeAction.equals("-")) {
try {
Edge edge = graph.removeEdge(sourceNode,targetNode);
graph.addDynamicInteraction(new DynamicInteraction(edge,edgeDate,edgeAction));
} catch (ElementNotFoundException e1) {
try {
Edge edge = graph.removeEdge(targetNode, sourceNode);
graph.addDynamicInteraction(new DynamicInteraction(edge,edgeDate,edgeAction));
} catch (ElementNotFoundException e2) {
System.out.println(e1 + " and/or " + e2);
}
}
}else {
throw new AdapterException("Invalid action");
}
line = Adapters.readLine(reader);
}
}
if(line.size() > 0) {
throw new AdapterException("Invalid input format");
}
return graph;
}
catch (Exception e) {
throw new AdapterException(e);
}
finally {
try {
reader.close();
}
catch (Exception e) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public CustomGraph(CustomGraph graph) {
graph.creationMethod.getParameters());
this.creationMethod.setStatus(graph.creationMethod.getStatus());
this.customNodes = new HashMap<Integer, CustomNode>();
System.out.println("Into Copy");
copyMappings(graph.customNodes, graph.customEdges, graph.nodeIds, graph.edgeIds);
this.userName = new String(graph.userName);
this.name = new String(graph.name);
Expand Down Expand Up @@ -1141,7 +1142,7 @@ public Set<Edge> getPositiveEdgesAboveZero(Node node) throws InterruptedExceptio

}
return positiveEdges;
}
}

/**
* Returns the set of all positive incoming edges for a given node.
Expand Down Expand Up @@ -1448,6 +1449,36 @@ protected void copyMappings(Map<Integer, CustomNode> customNodes, Map<Integer, C
}
}

/**
* Extension for DynamicGraphs, copy Mappings with edges as Dynamic Interaction
* @param customNodes
* @param customEdges
* @param nodeIds
* @param edgeIds
*//*
protected void copyDynamicMappings(Map<Integer, CustomNode> customNodes, Map<Integer, CustomEdge> customEdges,
Map<MultiNode, Integer> nodeIds, Map<Edge, Integer> edgeIds) {
for (Map.Entry<Integer, CustomNode> entry : customNodes.entrySet()) {
this.customNodes.put(entry.getKey(), new CustomNode(entry.getValue()));
}
for (Map.Entry<Integer, CustomEdge> entry : customEdges.entrySet()) {
this.customEdges.put(entry.getKey(), new DynamicInteraction((DynamicInteraction) entry.getValue()));
}
Node[] nodeArr = this.nodes().toArray(Node[]::new);
for (Map.Entry<MultiNode, Integer> entry : nodeIds.entrySet()) {
this.nodeIds.put((MultiNode) nodeArr[entry.getKey().getIndex()], entry.getValue());
}
Node[] nodes = this.nodes().toArray(Node[]::new);
for (Node node : nodes) {
this.reverseNodeMap.put(this.getCustomNode(node), (MultiNode) node);
}
Edge[] edgeArr = this.edges().toArray(Edge[]::new);
for (Map.Entry<Edge, Integer> entry : edgeIds.entrySet()) {
this.edgeIds.put(edgeArr[entry.getKey().getIndex()], entry.getValue());
}
}
*/

/**
* Returns the custom edge object corresponding to an edge.
*
Expand Down Expand Up @@ -1551,6 +1582,16 @@ protected void removeCustomEdge(Edge edge) {
this.customEdges.remove(id);
}

/* *//**
* This method is used in DynamicGraphs to give access to CustomGraph Maps
* @param edge the added edge
* @param customEdge the newly created dynamic interaction
*//*
protected void addDynamicInteraction(Edge edge, CustomEdge customEdge) {
this.edgeIds.put(edge, this.edgeIndexer);
this.customEdges.put(edgeIndexer, customEdge);
edgeIndexer++;
}*/
/////////////////////////// PERSISTENCE CALLBACK METHODS

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package i5.las2peer.services.ocd.graphs;

import org.graphstream.graph.Edge;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.AbstractGraph;
import org.graphstream.graph.implementations.MultiNode;

import java.util.*;

public class DynamicGraph extends CustomGraph{
/**
* Dynamic graph extension
* Extends CustomGraph's by a list of interactions
*/
private List<DynamicInteraction> dynamicInteractions = new ArrayList<>();

public DynamicGraph() {
}

public DynamicGraph(CustomGraph graph, List<DynamicInteraction> dynamicInteractions) {
super(graph);
this.dynamicInteractions = dynamicInteractions;
}

public DynamicGraph(String key) {
super(key);
}

public DynamicGraph(AbstractGraph graph) {
super(graph);
}

public DynamicGraph(DynamicGraph graph) {
super(graph);
this.dynamicInteractions = graph.dynamicInteractions;
}

/**
* The getter for the list of dynamic interactions
* @return list
*/
public List<DynamicInteraction> getDynamicInteractions() {
return dynamicInteractions;
}

/**
* The setter for the list of dynamic interactions
* @param dynamicInteractions
*/
public void setDynamicInteractions(List<DynamicInteraction> dynamicInteractions) {
this.dynamicInteractions = dynamicInteractions;
}

/**
* @return true if the graph is dynamic
*/
public boolean isDynamic() {
return isOfType(GraphType.DYNAMIC);
}

public void addDynamicInteraction(DynamicInteraction dynamicInteraction) {
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();
}
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);
}
}
*//**
* 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";
}
*//**
* 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);
}
}*/
}
Loading

0 comments on commit 8994282

Please sign in to comment.