public static void Serialize(string path, gexfcontent graph) { using (var file = XmlWriter.Create(path)) { var serializer = new XmlSerializer(typeof(gexfcontent)); serializer.Serialize(file, graph); } }
public void Convert() { JsonInput deserialized; using (var sr = File.OpenText(_opts.Input)) using (var jr = new JsonTextReader(sr)) deserialized = JsonSerializer.CreateDefault().Deserialize <JsonInput>(jr); var gc = new graphcontent(); var nodes = new List <nodecontent>(); var edges = new List <edgecontent>(); for (var cIdx = 0; cIdx < deserialized.Clusters.Count; cIdx++) { var cluster = deserialized.Clusters[cIdx]; foreach (var node in cluster.Nodes.Values) { var attVals = new attvaluescontent { attvalue = new [] { new attvalue { @for = "Cluster", value = cIdx.ToString() }, new attvalue { @for = "descr", value = node.Descr }, } }; nodes.Add(new nodecontent { id = node.Id.ToString(), label = node.Code, Items = new object[] { attVals } }); } foreach (var edge in cluster.Edges) { edges.Add(new edgecontent { source = edge.Source.ToString(), target = edge.Target.ToString(), weight = edge.Weight, weightSpecified = true, }); } } gc.Items = new object[] { new attributescontent { mode = modetype.@static, attribute = new [] { new attributecontent { id = "Cluster", type = attrtypetype.integer, title = "Cluster" }, new attributecontent { id = "descr", type = attrtypetype.@string, title = "Description" }, } }, new nodescontent { node = nodes.ToArray() }, new edgescontent { edge = edges.ToArray() }, }; var gexf = new gexfcontent { graph = gc }; using (var writer = File.Create(_opts.Out)) { new XmlSerializer(typeof(gexfcontent)) .Serialize(writer, gexf); } Console.Write("Done"); }
public void Execute() { _traceWriter.Output("Loading graph from database..."); var graph = _graphDatabase.Load(); _traceWriter.Output("Transforming nodes..."); var nodes = graph.Nodes.Where(x => x.NodeTypeID == NodeTypes.Solution || x.NodeTypeID == NodeTypes.Project).ToList(); var gexfNodes = nodes .Select(x => new nodecontent { id = x.NodeID.ToString(), label = x.Name, Items = new object[] { new attvaluescontent { attvalue = new[] { new attvalue { @for = "0", value = Enum.GetName(typeof(NodeTypes), x.NodeTypeID) } } } } }).ToArray(); _traceWriter.Output("Transforming edges..."); var gexfEdges = nodes.SelectMany( x => x.Out .Where(y => y.To.NodeTypeID == NodeTypes.Solution || y.To.NodeTypeID == NodeTypes.Project) .Select( r => new edgecontent { id = r.RelationID.ToString(), source = r.FromNodeID.ToString(), target = r.ToNodeID.ToString(), weight = (float)r.Weight, weightSpecified = true })).ToArray(); _traceWriter.Output("Constructing gexf format"); var gexf = new gexfcontent { meta = new metacontent { lastmodifieddate = DateTime.UtcNow, Items = new[] { _creator, _description }, ItemsElementName = new[] { ItemsChoiceType1.creator, ItemsChoiceType1.description, } }, graph = new graphcontent { mode = modetype.@static, defaultedgetype = defaultedgetypetype.directed, Items = new object[] { new attributescontent { @class = classtype.node, attribute = new[] { new attributecontent() { id = "0", title = "NodeType", type = attrtypetype.@string } } }, new nodescontent { node = gexfNodes }, new edgescontent { edge = gexfEdges } } } }; _traceWriter.Output("Writing to gexf file..."); var writer = new XmlSerializer(typeof(gexfcontent)); using (var file = File.Create("repository.gexf")) { writer.Serialize(file, gexf); file.Close(); } }
public void Serialize(string path) { var foo = new gexfcontent(); foo.version = gexfcontentVersion.Item12; foo.graph = new graphcontent(); string NodeTypeId = "NodeTypeAttribute"; string NodeTypeFullContent = "NodeFullContent"; var nodeAttributes = new attributescontent { @class = classtype.node, attribute = new[] { new attributecontent { id = NodeTypeId, title = "NodeType", type = attrtypetype.@string }, new attributecontent { id = NodeTypeFullContent, title = "FullContent", type = attrtypetype.@string }, } }; string EdgeTypeId = "EdgeTypeAttribute"; var edgeAttributes = new attributescontent { @class = classtype.edge, attribute = new[] { new attributecontent { id = EdgeTypeId, title = "EdgeType", type = attrtypetype.@string } } }; var nodesToStore = new nodescontent(); { List <nodecontent> convertedNodes = new List <nodecontent>(); foreach (var nc in this.Nodes) { var node = new nodecontent(); node.id = nc.Id; node.label = nc.Type + " : " + nc.Id; if (nc.Type == Node.NodeType.Syntax) { node.label = nc.Type + " : " + nc.Content; } node.Items = new object[] { new attvaluescontent { attvalue = new[] { new attvalue { @for = NodeTypeId, value = (nc.Type != Node.NodeType.Syntax) ? nc.Type.ToString() : nc.Type.ToString() + " " + nc.SyntaxType }, new attvalue { @for = NodeTypeFullContent, value = (nc.Type != Node.NodeType.Syntax) ? nc.FullContent : nc.getSyntaxFullContent(), }, } } }; convertedNodes.Add(node); } nodesToStore.node = convertedNodes.ToArray(); nodesToStore.count = convertedNodes.Count.ToString(); } int i = 0; var edgesToSTore = new edgescontent(); { List <edgecontent> convertedEdge = new List <edgecontent>(); foreach (var e in this.Edges) { var blub = new edgecontent(); blub.id = i.ToString(); i++; blub.source = e.from.Id; blub.target = e.to.Id; blub.Items = new object[] { new attvaluescontent { attvalue = new[] { new attvalue { @for = EdgeTypeId, value = e.type.ToString() } } } }; convertedEdge.Add(blub); } edgesToSTore.edge = convertedEdge.ToArray(); edgesToSTore.count = convertedEdge.Count.ToString(); } foo.graph.Items = new object[4]; foo.graph.Items[0] = nodeAttributes; foo.graph.Items[1] = edgeAttributes; foo.graph.Items[2] = nodesToStore; foo.graph.Items[3] = edgesToSTore; using (var file = XmlWriter.Create(path)) { var serializer = new XmlSerializer(typeof(gexfcontent)); serializer.Serialize(file, foo); } }