Exemplo n.º 1
0
 private static void AddGraphAttributes(MainGraphExportContext mainExportContext, StreamWriter writer)
 {
     foreach (KeyValuePair <string, GraphExportContext> kvp in mainExportContext.nameToContext)
     {
         GraphExportContext context = kvp.Value;
         GRSExport.EmitSubgraphAttributes(mainExportContext, context, writer);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Exports the given graph to the file given by the stream writer in grs format.
        /// Any errors will be reported by exception.
        /// Returns the graph export context of the main graph.
        /// </summary>
        /// <param name="graph">The graph to export. Must be a named graph.</param>
        /// <param name="sw">The stream writer of the file to export into. The stream writer is not closed automatically.</param>
        /// <param name="modelPathPrefix">Path to the model.</param>
        /// <param name="nonewgraph">If true, the new graph command is not emitted.</param>
        /// <param name="typesToAttributesToSkip">Gives a dictionary with type names containing a dictionary with attribute names that are not to be emitted</param>
        public static MainGraphExportContext ExportYouMustCloseStreamWriter(INamedGraph graph, StreamWriter sw, string modelPathPrefix, bool noNewGraph, Dictionary <String, Dictionary <String, String> > typesToAttributesToSkip)
        {
            MainGraphExportContext mainGraphContext = new MainGraphExportContext(graph);

            mainGraphContext.graphToContext[mainGraphContext.graph] = mainGraphContext;
            mainGraphContext.nameToContext[mainGraphContext.name]   = mainGraphContext;
            mainGraphContext.modelPathPrefix = modelPathPrefix;
            mainGraphContext.noNewGraph      = noNewGraph;
            if (typesToAttributesToSkip != null)
            {
                mainGraphContext.typesToAttributesToSkip = new Dictionary <string, Dictionary <string, string> >();
                foreach (KeyValuePair <String, Dictionary <String, String> > typeContainingAttributeToSkip in typesToAttributesToSkip)
                {
                    mainGraphContext.typesToAttributesToSkip.Add(typeContainingAttributeToSkip.Key, new Dictionary <string, string>());
                    foreach (KeyValuePair <String, String> attributeToSkip in typeContainingAttributeToSkip.Value)
                    {
                        mainGraphContext.typesToAttributesToSkip[typeContainingAttributeToSkip.Key].Add(attributeToSkip.Key, null);
                    }
                }
            }

            sw.WriteLine("# begin of graph \"{0}\" saved by GrsExport", mainGraphContext.name);
            sw.WriteLine();

            bool subgraphAdded = ExportSingleGraph(mainGraphContext, mainGraphContext, sw);

            if (subgraphAdded)
            {
restart:
                foreach (KeyValuePair <string, GraphExportContext> kvp in mainGraphContext.nameToContext)
                {
                    GraphExportContext context = kvp.Value;
                    if (!context.isExported)
                    {
                        subgraphAdded = ExportSingleGraph(mainGraphContext, context, sw);
                        if (subgraphAdded)
                        {
                            goto restart;
                        }
                    }
                }

                foreach (KeyValuePair <string, GraphExportContext> kvp in mainGraphContext.nameToContext)
                {
                    GraphExportContext context = kvp.Value;
                    EmitSubgraphAttributes(mainGraphContext, context, sw);
                }

                sw.WriteLine("in \"" + mainGraphContext.name + "\""); // after import in main graph
            }

            sw.WriteLine("# end of graph \"{0}\" saved by GrsExport", mainGraphContext.name);
            sw.WriteLine();

            return(mainGraphContext);
        }
Exemplo n.º 3
0
        public static void EmitSubgraphAttributes(MainGraphExportContext mainGraphContext,
                                                  GraphExportContext context, StreamWriter sw)
        {
            if (context.areGraphAttributesExported)
            {
                return;
            }

            sw.WriteLine("in \"" + context.name + "\"");

            foreach (INode node in context.graph.Nodes)
            {
                foreach (AttributeType attrType in node.Type.AttributeTypes)
                {
                    if (IsAttributeToBeSkipped(mainGraphContext, attrType))
                    {
                        continue;
                    }
                    if (!IsGraphUsedInAttribute(attrType))
                    {
                        continue;
                    }

                    object value = node.GetAttribute(attrType.Name);
                    sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(node), attrType.Name);
                    EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                    sw.Write("\n");
                }

                foreach (IEdge edge in node.Outgoing)
                {
                    foreach (AttributeType attrType in edge.Type.AttributeTypes)
                    {
                        if (IsAttributeToBeSkipped(mainGraphContext, attrType))
                        {
                            continue;
                        }
                        if (!IsGraphUsedInAttribute(attrType))
                        {
                            continue;
                        }

                        object value = edge.GetAttribute(attrType.Name);
                        sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(edge), attrType.Name);
                        EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                        sw.Write("\n");
                    }
                }
            }

            context.areGraphAttributesExported = true;
        }
Exemplo n.º 4
0
 public static bool AddSubgraphAsNeeded(MainGraphExportContext mainGraphContext,
                                        INamedGraph graph)
 {
     if (graph != null && !mainGraphContext.graphToContext.ContainsKey(graph))
     {
         GraphExportContext subgraphContext = new GraphExportContext(mainGraphContext, graph);
         mainGraphContext.graphToContext[graph] = subgraphContext;
         mainGraphContext.nameToContext[subgraphContext.name] = subgraphContext;
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Exports the given graph to the file given by the stream writer in grs format.
        /// Any errors will be reported by exception.
        /// Returns the graph export context of the main graph.
        /// </summary>
        /// <param name="graph">The graph to export. Must be a named graph.</param>
        /// <param name="sw">The stream writer of the file to export into. The stream writer is not closed automatically.</param>
        /// <param name="modelPathPrefix">Path to the model.</param>
        public static MainGraphExportContext ExportYouMustCloseStreamWriter(INamedGraph graph, StreamWriter sw, string modelPathPrefix)
        {
            MainGraphExportContext mainGraphContext = new MainGraphExportContext(graph);

            mainGraphContext.graphToContext[mainGraphContext.graph] = mainGraphContext;
            mainGraphContext.nameToContext[mainGraphContext.name]   = mainGraphContext;
            mainGraphContext.modelPathPrefix = modelPathPrefix;

            sw.WriteLine("# begin of graph \"{0}\" saved by GrsExport", mainGraphContext.name);
            sw.WriteLine();

            bool subgraphAdded = ExportSingleGraph(mainGraphContext, mainGraphContext, sw);

            if (subgraphAdded)
            {
restart:
                foreach (KeyValuePair <string, GraphExportContext> kvp in mainGraphContext.nameToContext)
                {
                    GraphExportContext context = kvp.Value;
                    if (!context.isExported)
                    {
                        subgraphAdded = ExportSingleGraph(mainGraphContext, context, sw);
                        if (subgraphAdded)
                        {
                            goto restart;
                        }
                    }
                }

                foreach (KeyValuePair <string, GraphExportContext> kvp in mainGraphContext.nameToContext)
                {
                    GraphExportContext context = kvp.Value;
                    EmitSubgraphAttributes(mainGraphContext, context, sw);
                }

                sw.WriteLine("in \"" + mainGraphContext.name + "\""); // after import in main graph
            }

            sw.WriteLine("# end of graph \"{0}\" saved by GrsExport", mainGraphContext.name);
            sw.WriteLine();

            return(mainGraphContext);
        }
Exemplo n.º 6
0
        private bool AddSubgraphsAsNeeded(MainGraphExportContext mainExportContext,
                                          IGraphElement element, AttributeType attrType, Object value, StreamWriter writer)
        {
            if (!GRSExport.IsGraphUsedInAttribute(attrType))
            {
                return(false);
            }

            if (value == null)
            {
                return(false);
            }

            if (!(value is INamedGraph))
            {
                return(false);
            }

            bool wasAdded = GRSExport.AddSubgraphAsNeeded(mainExportContext, (INamedGraph)value);

            if (wasAdded)
            {
restart:
                foreach (KeyValuePair <string, GraphExportContext> kvp in mainExportContext.nameToContext)
                {
                    GraphExportContext context = kvp.Value;
                    if (!context.isExported)
                    {
                        wasAdded = GRSExport.ExportSingleGraph(mainExportContext, context, writer);
                        if (wasAdded)
                        {
                            goto restart;
                        }
                    }
                }
                AddGraphAttributes(mainExportContext, writer);
                writer.WriteLine("in \"" + mainExportContext.graphToContext[graph].name + "\" # after emitting new subgraph for attribute");
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        private static bool AddSubgraphsAsNeeded(INamedGraph potentialNewGraph, RecordingState recordingState)
        {
            bool wasAdded = GRSExport.AddSubgraphAsNeeded(recordingState.mainExportContext, potentialNewGraph);

            if (wasAdded)
            {
restart:
                foreach (KeyValuePair <string, GraphExportContext> kvp in recordingState.mainExportContext.nameToContext)
                {
                    GraphExportContext context = kvp.Value;
                    if (!context.isExported)
                    {
                        wasAdded = GRSExport.ExportSingleGraph(recordingState.mainExportContext, context, recordingState.writer);
                        if (wasAdded)
                        {
                            goto restart;
                        }
                    }
                }
                AddGraphAttributes(recordingState.mainExportContext, recordingState.writer);
                return(true);
            }
            return(false);
        }
Exemplo n.º 8
0
        public static bool ExportSingleGraph(MainGraphExportContext mainGraphContext,
                                             GraphExportContext context, StreamWriter sw)
        {
            bool subgraphAdded = false;

            if (context.modelPathPrefix != null)
            {
                sw.WriteLine("new graph \"" + context.modelPathPrefix + context.graph.Model.ModelName + "\" \"" + context.name + "\"");
            }
            else
            {
                sw.WriteLine("add new graph \"" + context.name + "\"");
            }

            // emit nodes
            int numNodes = 0;

            foreach (INode node in context.graph.Nodes)
            {
                sw.Write("new :{0}($ = \"{1}\"", node.Type.PackagePrefixedName, context.graph.GetElementName(node));
                foreach (AttributeType attrType in node.Type.AttributeTypes)
                {
                    if (IsNodeOrEdgeUsedInAttribute(attrType))
                    {
                        context.nodeOrEdgeUsedInAttribute = true;
                        continue;
                    }
                    if (IsGraphUsedInAttribute(attrType))
                    {
                        context.subgraphUsedInAttribute = true;
                        subgraphAdded |= AddSubgraphAsNeeded(mainGraphContext, node, attrType);
                        continue;
                    }

                    object value = node.GetAttribute(attrType.Name);
                    EmitAttributeInitialization(mainGraphContext, attrType, value, context.graph, sw);
                }
                sw.WriteLine(")");
                numNodes++;
            }
            if (context.modelPathPrefix != null)
            {
                sw.WriteLine("# total number of nodes: {0}", numNodes);
            }
            else
            {
                sw.WriteLine("# total number of nodes in subgraph {0}: {1}", context.name, numNodes);
            }
            sw.WriteLine();

            // emit edges
            int numEdges = 0;

            foreach (INode node in context.graph.Nodes)
            {
                foreach (IEdge edge in node.Outgoing)
                {
                    sw.Write("new @(\"{0}\") - :{1}($ = \"{2}\"", context.graph.GetElementName(node),
                             edge.Type.PackagePrefixedName, context.graph.GetElementName(edge));
                    foreach (AttributeType attrType in edge.Type.AttributeTypes)
                    {
                        if (IsNodeOrEdgeUsedInAttribute(attrType))
                        {
                            context.nodeOrEdgeUsedInAttribute = true;
                            continue;
                        }
                        if (IsGraphUsedInAttribute(attrType))
                        {
                            context.subgraphUsedInAttribute = true;
                            subgraphAdded |= AddSubgraphAsNeeded(mainGraphContext, edge, attrType);
                            continue;
                        }

                        object value = edge.GetAttribute(attrType.Name);
                        // TODO: Add support for null values, as the default initializers could assign non-null values!
                        if (value != null)
                        {
                            EmitAttributeInitialization(mainGraphContext, attrType, value, context.graph, sw);
                        }
                    }
                    sw.WriteLine(") -> @(\"{0}\")", context.graph.GetElementName(edge.Target));
                    numEdges++;
                }
            }
            if (context.modelPathPrefix != null)
            {
                sw.WriteLine("# total number of edges: {0}", numEdges);
            }
            else
            {
                sw.WriteLine("# total number of edges in subgraph {0}: {1}", context.name, numEdges);
            }
            sw.WriteLine();

            // emit node/edge valued attributes
            if (context.nodeOrEdgeUsedInAttribute)
            {
                foreach (INode node in context.graph.Nodes)
                {
                    foreach (AttributeType attrType in node.Type.AttributeTypes)
                    {
                        if (!IsNodeOrEdgeUsedInAttribute(attrType))
                        {
                            continue;
                        }
                        if (IsGraphUsedInAttribute(attrType))
                        {
                            continue;
                        }

                        object value = node.GetAttribute(attrType.Name);
                        sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(node), attrType.Name);
                        EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                        sw.Write("\n");
                    }

                    foreach (IEdge edge in node.Outgoing)
                    {
                        foreach (AttributeType attrType in edge.Type.AttributeTypes)
                        {
                            if (!IsNodeOrEdgeUsedInAttribute(attrType))
                            {
                                continue;
                            }
                            if (IsGraphUsedInAttribute(attrType))
                            {
                                continue;
                            }

                            object value = edge.GetAttribute(attrType.Name);
                            sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(edge), attrType.Name);
                            EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                            sw.Write("\n");
                        }
                    }
                }
            }

            if (!context.subgraphUsedInAttribute)
            {
                context.areGraphAttributesExported = true;
            }
            context.isExported = true;
            return(subgraphAdded);
        }
Exemplo n.º 9
0
        public static void EmitSubgraphAttributes(MainGraphExportContext mainGraphContext,
            GraphExportContext context, StreamWriter sw)
        {
            if(context.areGraphAttributesExported)
                return;

            sw.WriteLine("in \"" + context.name + "\"");

            foreach(INode node in context.graph.Nodes)
            {
                foreach(AttributeType attrType in node.Type.AttributeTypes)
                {
                    if(!IsGraphUsedInAttribute(attrType))
                        continue;

                    object value = node.GetAttribute(attrType.Name);
                    sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(node), attrType.Name);
                    EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                    sw.Write("\n");
                }

                foreach(IEdge edge in node.Outgoing)
                {
                    foreach(AttributeType attrType in edge.Type.AttributeTypes)
                    {
                        if(!IsGraphUsedInAttribute(attrType))
                            continue;

                        object value = edge.GetAttribute(attrType.Name);
                        sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(edge), attrType.Name);
                        EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                        sw.Write("\n");
                    }
                }
            }

            context.areGraphAttributesExported = true;
        }
Exemplo n.º 10
0
 public static bool AddSubgraphAsNeeded(MainGraphExportContext mainGraphContext, 
     INamedGraph graph)
 {
     if(graph!=null && !mainGraphContext.graphToContext.ContainsKey(graph))
     {
         GraphExportContext subgraphContext = new GraphExportContext(mainGraphContext, graph);
         mainGraphContext.graphToContext[graph] = subgraphContext;
         mainGraphContext.nameToContext[subgraphContext.name] = subgraphContext;
         return true;
     }
     else
         return false;
 }
Exemplo n.º 11
0
        public static bool ExportSingleGraph(MainGraphExportContext mainGraphContext, 
            GraphExportContext context, StreamWriter sw)
        {
            bool subgraphAdded = false;

            if(context.modelPathPrefix != null)
                sw.WriteLine("new graph \"" + context.modelPathPrefix + context.graph.Model.ModelName + "\" \"" + context.name + "\"");
            else
                sw.WriteLine("add new graph \"" + context.name + "\"");

            // emit nodes
            int numNodes = 0;
            foreach(INode node in context.graph.Nodes)
            {
                sw.Write("new :{0}($ = \"{1}\"", node.Type.PackagePrefixedName, context.graph.GetElementName(node));
                foreach(AttributeType attrType in node.Type.AttributeTypes)
                {
                    if(IsNodeOrEdgeUsedInAttribute(attrType))
                    {
                        context.nodeOrEdgeUsedInAttribute = true;
                        continue;
                    }
                    if(IsGraphUsedInAttribute(attrType))
                    {
                        context.subgraphUsedInAttribute = true;
                        subgraphAdded |= AddSubgraphAsNeeded(mainGraphContext, node, attrType);
                        continue;
                    }

                    object value = node.GetAttribute(attrType.Name);
                    EmitAttributeInitialization(mainGraphContext, attrType, value, context.graph, sw);
                }
                sw.WriteLine(")");
                numNodes++;
            }
            if(context.modelPathPrefix != null)
                sw.WriteLine("# total number of nodes: {0}", numNodes);
            else
                sw.WriteLine("# total number of nodes in subgraph {0}: {1}", context.name, numNodes);
            sw.WriteLine();

            // emit edges
            int numEdges = 0;
            foreach(INode node in context.graph.Nodes)
            {
                foreach(IEdge edge in node.Outgoing)
                {
                    sw.Write("new @(\"{0}\") - :{1}($ = \"{2}\"", context.graph.GetElementName(node),
                        edge.Type.PackagePrefixedName, context.graph.GetElementName(edge));
                    foreach(AttributeType attrType in edge.Type.AttributeTypes)
                    {
                        if(IsNodeOrEdgeUsedInAttribute(attrType))
                        {
                            context.nodeOrEdgeUsedInAttribute = true;
                            continue;
                        }
                        if(IsGraphUsedInAttribute(attrType))
                        {
                            context.subgraphUsedInAttribute = true;
                            subgraphAdded |= AddSubgraphAsNeeded(mainGraphContext, edge, attrType);
                            continue;
                        }

                        object value = edge.GetAttribute(attrType.Name);
                        // TODO: Add support for null values, as the default initializers could assign non-null values!
                        if(value != null)
                        {
                            EmitAttributeInitialization(mainGraphContext, attrType, value, context.graph, sw);
                        }
                    }
                    sw.WriteLine(") -> @(\"{0}\")", context.graph.GetElementName(edge.Target));
                    numEdges++;
                }
            }
            if(context.modelPathPrefix != null)
                sw.WriteLine("# total number of edges: {0}", numEdges);
            else
                sw.WriteLine("# total number of edges in subgraph {0}: {1}", context.name, numEdges);
            sw.WriteLine();

            // emit node/edge valued attributes
            if(context.nodeOrEdgeUsedInAttribute)
            {
                foreach(INode node in context.graph.Nodes)
                {
                    foreach(AttributeType attrType in node.Type.AttributeTypes)
                    {
                        if(!IsNodeOrEdgeUsedInAttribute(attrType))
                            continue;
                        if(IsGraphUsedInAttribute(attrType))
                            continue;

                        object value = node.GetAttribute(attrType.Name);
                        sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(node), attrType.Name);
                        EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                        sw.Write("\n");
                    }

                    foreach(IEdge edge in node.Outgoing)
                    {
                        foreach(AttributeType attrType in edge.Type.AttributeTypes)
                        {
                            if(!IsNodeOrEdgeUsedInAttribute(attrType))
                                continue;
                            if(IsGraphUsedInAttribute(attrType))
                                continue;

                            object value = edge.GetAttribute(attrType.Name);
                            sw.Write("@(\"{0}\").{1} = ", context.graph.GetElementName(edge), attrType.Name);
                            EmitAttribute(mainGraphContext, attrType, value, context.graph, sw);
                            sw.Write("\n");
                        }
                    }
                }
            }

            if(!context.subgraphUsedInAttribute)
                context.areGraphAttributesExported = true;
            context.isExported = true;
            return subgraphAdded;
        }