private StudioGraph CreateStudioGraph(dynamic dag)
{
StudioGraph graph = new StudioGraph();
InsertNodesIntoGraph(dag, graph, "Graph");
InsertNodesIntoGraph(dag, graph, "WebService");
// dataset nodes are treated differently because they don't show in the EdgesInternal section.
Dictionary<string, string> datasetNodes = new Dictionary<string, string>();
foreach (var moduleNode in dag["Graph"]["ModuleNodes"])
{
string nodeId = moduleNode["Id"];
foreach (var inputPort in moduleNode["InputPortsInternal"])
if (inputPort["DataSourceId"] != null && !datasetNodes.Keys.Contains(nodeId)) // this is a dataset node
datasetNodes.Add(nodeId, inputPort["DataSourceId"].ToString());
}
// normal edges
foreach (dynamic edge in dag["Graph"]["EdgesInternal"])
{
string sourceOutputPort = edge["SourceOutputPortId"].ToString();
string destInputPort = edge["DestinationInputPortId"].ToString();
string sourceNode = (sourceOutputPort.Split(':')[0]);
string destNode = (destInputPort.Split(':')[0]);
graph.Edges.Add(new StudioGraphEdge
{
DestinationNode = graph.Nodes.Single(n => n.Id == destNode),
SourceNode = graph.Nodes.Single(n => n.Id == sourceNode)
});
}
// dataset edges
foreach (string nodeId in datasetNodes.Keys)
graph.Edges.Add(new StudioGraphEdge {
DestinationNode = graph.Nodes.Single(n => n.Id == nodeId),
SourceNode = graph.Nodes.Single(n => n.Id == datasetNodes[nodeId])
}
);
if (dag["WebService"] != null)
{
// web service input edges
if (dag["WebService"]["Inputs"] != null)
foreach (var webSvcInput in dag["WebService"]["Inputs"])
{
if (webSvcInput["PortId"] != null)
{
string webSvcModuleId = webSvcInput["Id"].ToString();
string connectedModuleId = webSvcInput["PortId"].ToString().Split(':')[0];
graph.Edges.Add(new StudioGraphEdge
{
DestinationNode = graph.Nodes.Single(n => n.Id == connectedModuleId),
SourceNode = graph.Nodes.Single(n => n.Id == webSvcModuleId)
});
}
}
// web service output edges
if (dag["WebService"]["Outputs"] != null)
foreach (var webSvcOutput in dag["WebService"]["Outputs"])
{
if (webSvcOutput["PortId"] != null)
{
string webSvcModuleId = webSvcOutput["Id"].ToString();
string connectedModuleId = webSvcOutput["PortId"].ToString().Split(':')[0];
graph.Edges.Add(new StudioGraphEdge
{
DestinationNode = graph.Nodes.Single(n => n.Id == webSvcModuleId),
SourceNode = graph.Nodes.Single(n => n.Id == connectedModuleId)
});
}
}
}
return graph;
}