Example #1
0
        /// <summary>
        /// Event handler for IGraph.OnChangingNodeAttribute and IGraph.OnChangingEdgeAttribute and IGraph.OnChangingObjectAttribute.
        /// </summary>
        /// <param name="owner">The node or edge or object whose attribute is changed.</param>
        /// <param name="attrType">The type of the attribute to be changed.</param>
        /// <param name="changeType">The type of the change which will be made.</param>
        /// <param name="newValue">The new value of the attribute, if changeType==Assign.
        ///                        Or the value to be inserted/removed if changeType==PutElement/RemoveElement on set.
        ///                        Or the new map pair value to be inserted if changeType==PutElement on map.
        ///                        Or the new value to be inserted/added if changeType==PutElement on array.
        ///                        Or the new value to be assigned to the given position if changeType==AssignElement on array.</param>
        /// <param name="keyValue">The map pair key to be inserted/removed if changeType==PutElement/RemoveElement on map.
        ///                        The array index to be removed/written to if changeType==RemoveElement/AssignElement on array.</param>
        void ChangingAttribute(IAttributeBearer owner, AttributeType attrType,
                               AttributeChangeType changeType, object newValue, object keyValue)
        {
            foreach (RecordingState recordingState in recordings.Values)
            {
                MainGraphExportContext mainExportContext = recordingState.mainExportContext;
                StringBuilder          deferredInits     = new StringBuilder();
                AddSubgraphsAsNeeded(mainExportContext, owner, attrType, newValue, recordingState.writer);
                AddSubgraphsAsNeeded(mainExportContext, owner, attrType, keyValue, recordingState.writer);
                switch (changeType)
                {
                case AttributeChangeType.Assign:
                    if (owner is IGraphElement)
                    {
                        recordingState.writer.Write("@(\"" + graph.GetElementName((IGraphElement)owner) + "\")." + attrType.Name + " = ");
                    }
                    else
                    {
                        EmitObjectAttributeAssignmentCreatingAsNeeded((IObject)owner, attrType, mainExportContext, recordingState);
                    }

                    StringBuilder sb = new StringBuilder();
                    GRSExport.EmitAttribute(mainExportContext, attrType, newValue, graph, recordingState.writer, sb);
                    recordingState.writer.WriteLine();
                    recordingState.writer.Write(sb.ToString());
                    break;

                case AttributeChangeType.PutElement:
                    if (owner is IGraphElement)
                    {
                        recordingState.writer.Write("@(\"" + graph.GetElementName((IGraphElement)owner) + "\")." + attrType.Name);
                    }
                    else
                    {
                        EmitObjectAttributeAssignmentCreatingAsNeeded((IObject)owner, attrType, mainExportContext, recordingState);
                    }
                    switch (attrType.Kind)
                    {
                    case AttributeKind.SetAttr:
                        recordingState.writer.Write(".add(");
                        GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine(")");
                        break;

                    case AttributeKind.MapAttr:
                        recordingState.writer.Write(".add(");
                        GRSExport.EmitAttributeValue(mainExportContext, keyValue, attrType.KeyType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.Write(", ");
                        GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine(")");
                        break;

                    case AttributeKind.ArrayAttr:
                        if (keyValue == null)
                        {
                            recordingState.writer.Write(".add(");
                            GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                            recordingState.writer.WriteLine(")");
                        }
                        else
                        {
                            recordingState.writer.Write(".add(");
                            GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                            recordingState.writer.Write(", ");
                            GRSExport.EmitAttributeValue(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph, recordingState.writer, deferredInits);
                            recordingState.writer.WriteLine(")");
                        }
                        break;

                    case AttributeKind.DequeAttr:
                        if (keyValue == null)
                        {
                            recordingState.writer.Write(".add(");
                            GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                            recordingState.writer.WriteLine(")");
                        }
                        else
                        {
                            recordingState.writer.Write(".add(");
                            GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                            recordingState.writer.Write(", ");
                            GRSExport.EmitAttributeValue(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph, recordingState.writer, deferredInits);
                            recordingState.writer.WriteLine(")");
                        }
                        break;

                    default:
                        throw new Exception("Wrong attribute type for attribute change type");
                    }
                    break;

                case AttributeChangeType.RemoveElement:
                    if (owner is IGraphElement)
                    {
                        recordingState.writer.Write("@(\"" + graph.GetElementName((IGraphElement)owner) + "\")." + attrType.Name);
                    }
                    else
                    {
                        EmitObjectAttributeAssignmentCreatingAsNeeded((IObject)owner, attrType, mainExportContext, recordingState);
                    }
                    switch (attrType.Kind)
                    {
                    case AttributeKind.SetAttr:
                        recordingState.writer.Write(".rem(");
                        GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine(")");
                        break;

                    case AttributeKind.MapAttr:
                        recordingState.writer.Write(".rem(");
                        GRSExport.EmitAttributeValue(mainExportContext, keyValue, attrType.KeyType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine(")");
                        break;

                    case AttributeKind.ArrayAttr:
                        recordingState.writer.Write(".rem(");
                        if (keyValue != null)
                        {
                            GRSExport.EmitAttributeValue(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph, recordingState.writer, deferredInits);
                        }
                        recordingState.writer.WriteLine(")");
                        break;

                    case AttributeKind.DequeAttr:
                        recordingState.writer.Write(".rem(");
                        if (keyValue != null)
                        {
                            GRSExport.EmitAttributeValue(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph, recordingState.writer, deferredInits);
                        }
                        recordingState.writer.WriteLine(")");
                        break;

                    default:
                        throw new Exception("Wrong attribute type for attribute change type");
                    }
                    break;

                case AttributeChangeType.AssignElement:
                    if (owner is IGraphElement)
                    {
                        recordingState.writer.Write("@(\"" + graph.GetElementName((IGraphElement)owner) + "\")." + attrType.Name);
                    }
                    else
                    {
                        EmitObjectAttributeAssignmentCreatingAsNeeded((IObject)owner, attrType, mainExportContext, recordingState);
                    }
                    switch (attrType.Kind)
                    {
                    case AttributeKind.ArrayAttr:
                        recordingState.writer.Write("[");
                        GRSExport.EmitAttributeValue(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph, recordingState.writer, deferredInits);
                        recordingState.writer.Write("] = ");
                        GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine();
                        break;

                    case AttributeKind.DequeAttr:
                        recordingState.writer.Write("[");
                        GRSExport.EmitAttributeValue(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph, recordingState.writer, deferredInits);
                        recordingState.writer.Write("] = ");
                        GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine();
                        break;

                    case AttributeKind.MapAttr:
                        recordingState.writer.Write("[");
                        GRSExport.EmitAttributeValue(mainExportContext, keyValue, attrType.KeyType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.Write("] = ");
                        GRSExport.EmitAttributeValue(mainExportContext, newValue, attrType.ValueType, graph, recordingState.writer, deferredInits);
                        recordingState.writer.WriteLine();
                        break;

                    default:
                        throw new Exception("Wrong attribute type for attribute change type");
                    }
                    break;

                default:
                    throw new Exception("Unknown attribute change type");
                }
                recordingState.writer.Write(deferredInits.ToString());
            }
        }