-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DynamicGraphs with a List of DynamicInteraction's
Added TimestampedEdgeListInputAdapter. And the respective test classes.
- Loading branch information
1 parent
681802f
commit 8994282
Showing
15 changed files
with
613 additions
and
4,546 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...in/java/i5/las2peer/services/ocd/adapters/graphInput/TimestampedEdgeListInputAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
rest_ocd_services/src/main/java/i5/las2peer/services/ocd/graphs/DynamicGraph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}*/ | ||
} |
Oops, something went wrong.