/// <summary>
 /// Returns a string representation of the given Deque
 /// after the given operation with the given parameters was applied
 /// </summary>
 /// <param name="deque">The base Deque of the operation</param>
 /// <param name="changeType">The type of the change operation</param>
 /// <param name="newValue">The new value to be inserted/added if changeType==PutElement on deque.</param>
 /// <param name="type">The type as string, e.g deque<int></param>
 /// <param name="content">The content as string, e.g. ] 42, 43 [ </param>
 /// <param name="attrType">The attribute type of the Deque</param>
 /// <param name="graph">The graph with the model and the element names</param>
 public static void ToString(IDeque deque,
                             AttributeChangeType changeType, object newValue,
                             out string type, out string content,
                             AttributeType attrType, IGraph graph)
 {
     if (changeType == AttributeChangeType.PutElement)
     {
         Type valueType;
         ContainerHelper.GetDequeType(deque, out valueType);
         ToString(deque, out type, out content, attrType, graph, false, null, null);
         content += ".add(" + ToString(newValue, attrType.ValueType, graph, false, null, null) + ")";
     }
     else if (changeType == AttributeChangeType.RemoveElement)
     {
         Type valueType;
         ContainerHelper.GetDequeType(deque, out valueType);
         ToString(deque, out type, out content, attrType, graph, false, null, null);
         content += ".rem()";
     }
     else // changeType==AttributeChangeType.Assign
     {
         ToString((IDeque)newValue, out type, out content, attrType, graph, false, null, null);
     }
 }
Exemple #2
0
        /// <summary>
        /// Event handler for IGraph.OnChangingNodeAttribute and IGraph.OnChangingEdgeAttribute.
        /// </summary>
        /// <param name="element">The node or edge 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(IGraphElement element, AttributeType attrType,
                               AttributeChangeType changeType, Object newValue, Object keyValue)
        {
            foreach (RecordingState recordingState in recordings.Values)
            {
                MainGraphExportContext mainExportContext = recordingState.mainExportContext;
                AddSubgraphsAsNeeded(mainExportContext, element, attrType, newValue, recordingState.writer);
                AddSubgraphsAsNeeded(mainExportContext, element, attrType, keyValue, recordingState.writer);
                switch (changeType)
                {
                case AttributeChangeType.Assign:
                    recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name + " = ");
                    GRSExport.EmitAttribute(mainExportContext, attrType, newValue, graph, recordingState.writer);
                    recordingState.writer.WriteLine();
                    break;

                case AttributeChangeType.PutElement:
                    recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name);
                    switch (attrType.Kind)
                    {
                    case AttributeKind.SetAttr:
                        recordingState.writer.Write(".add(");
                        recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                        recordingState.writer.WriteLine(")");
                        break;

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

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

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

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

                case AttributeChangeType.RemoveElement:
                    recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name);
                    switch (attrType.Kind)
                    {
                    case AttributeKind.SetAttr:
                        recordingState.writer.Write(".rem(");
                        recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                        recordingState.writer.WriteLine(")");
                        break;

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

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

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

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

                case AttributeChangeType.AssignElement:
                    recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name);
                    switch (attrType.Kind)
                    {
                    case AttributeKind.ArrayAttr:
                        recordingState.writer.Write("[");
                        recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                        recordingState.writer.Write("] = ");
                        recordingState.writer.WriteLine(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                        break;

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

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

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

                default:
                    throw new Exception("Unknown attribute change type");
                }
            }
        }
Exemple #3
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());
            }
        }
        public readonly IGraph _graph; // for ToString only

        public LGSPUndoAttributeChanged(IGraphElement elem, AttributeType attrType,
                                        AttributeChangeType changeType, Object newValue, Object keyValue,
                                        LGSPGraphProcessingEnvironment procEnv)
        {
            _elem     = elem;
            _attrType = attrType;
            if (procEnv.graph is LGSPNamedGraph)
            {
                _name = ((LGSPNamedGraph)procEnv.graph).GetElementName(_elem);
            }
            else
            {
                _name = "?";
            }
            _graph = procEnv.graph;

            if (_attrType.Kind == AttributeKind.SetAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(newValue))
                    {
                        _undoOperation = UndoOperation.None;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.RemoveElement;
                        _value         = newValue;
                    }
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(newValue))
                    {
                        _undoOperation = UndoOperation.PutElement;
                        _value         = newValue;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.None;
                    }
                }
                else // Assign
                {
                    Type        keyType;
                    Type        valueType;
                    IDictionary dict = ContainerHelper.GetDictionaryTypes(
                        _elem.GetAttribute(_attrType.Name), out keyType, out valueType);
                    IDictionary clonedDict = ContainerHelper.NewDictionary(keyType, valueType, dict);
                    _undoOperation = UndoOperation.Assign;
                    _value         = clonedDict;
                }
            }
            else if (_attrType.Kind == AttributeKind.ArrayAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IList array = (IList)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.RemoveElement;
                    _keyOfValue    = keyValue;
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IList array = (IList)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.PutElement;
                    if (keyValue == null)
                    {
                        _value = array[array.Count - 1];
                    }
                    else
                    {
                        _value      = array[(int)keyValue];
                        _keyOfValue = keyValue;
                    }
                }
                else if (changeType == AttributeChangeType.AssignElement)
                {
                    IList array = (IList)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.AssignElement;
                    _value         = array[(int)keyValue];
                    _keyOfValue    = keyValue;
                }
                else // Assign
                {
                    Type  valueType;
                    IList array = ContainerHelper.GetListType(
                        _elem.GetAttribute(_attrType.Name), out valueType);
                    IList clonedArray = ContainerHelper.NewList(valueType, array);
                    _undoOperation = UndoOperation.Assign;
                    _value         = clonedArray;
                }
            }
            else if (_attrType.Kind == AttributeKind.DequeAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IDeque deque = (IDeque)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.RemoveElement;
                    _keyOfValue    = keyValue;
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IDeque deque = (IDeque)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.PutElement;
                    if (keyValue == null)
                    {
                        _value = deque.Front;
                    }
                    else
                    {
                        _value      = deque[(int)keyValue];
                        _keyOfValue = keyValue;
                    }
                }
                else if (changeType == AttributeChangeType.AssignElement)
                {
                    IDeque deque = (IDeque)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.AssignElement;
                    _value         = deque[(int)keyValue];
                    _keyOfValue    = keyValue;
                }
                else // Assign
                {
                    Type   valueType;
                    IDeque deque = ContainerHelper.GetDequeType(
                        _elem.GetAttribute(_attrType.Name), out valueType);
                    IDeque clonedDeque = ContainerHelper.NewDeque(valueType, deque);
                    _undoOperation = UndoOperation.Assign;
                    _value         = clonedDeque;
                }
            }
            else if (_attrType.Kind == AttributeKind.MapAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(keyValue))
                    {
                        if (dict[keyValue] == newValue)
                        {
                            _undoOperation = UndoOperation.None;
                        }
                        else
                        {
                            _undoOperation = UndoOperation.PutElement;
                            _value         = dict[keyValue];
                            _keyOfValue    = keyValue;
                        }
                    }
                    else
                    {
                        _undoOperation = UndoOperation.RemoveElement;
                        _value         = newValue;
                        _keyOfValue    = keyValue;
                    }
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(keyValue))
                    {
                        _undoOperation = UndoOperation.PutElement;
                        _value         = dict[keyValue];
                        _keyOfValue    = keyValue;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.None;
                    }
                }
                else if (changeType == AttributeChangeType.AssignElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict[keyValue] == newValue)
                    {
                        _undoOperation = UndoOperation.None;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.AssignElement;
                        _value         = dict[keyValue];
                        _keyOfValue    = keyValue;
                    }
                }
                else // Assign
                {
                    Type        keyType, valueType;
                    IDictionary dict = ContainerHelper.GetDictionaryTypes(
                        _elem.GetAttribute(_attrType.Name), out keyType, out valueType);
                    IDictionary clonedDict = ContainerHelper.NewDictionary(keyType, valueType, dict);
                    _undoOperation = UndoOperation.Assign;
                    _value         = clonedDict;
                }
            }
            else // Primitve Type Assign
            {
                _undoOperation = UndoOperation.Assign;
                _value         = _elem.GetAttribute(_attrType.Name);
            }
        }
        public IGraph _graph; // for ToString only

        public LGSPUndoAttributeChanged(IGraphElement elem, AttributeType attrType,
                AttributeChangeType changeType, Object newValue, Object keyValue, 
                LGSPGraphProcessingEnvironment procEnv)
        {
            _elem = elem;
            _attrType = attrType;
            if(procEnv.graph is LGSPNamedGraph) _name = ((LGSPNamedGraph)procEnv.graph).GetElementName(_elem);
            else _name = "?";
            _graph = procEnv.graph;

            if (_attrType.Kind == AttributeKind.SetAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(newValue))
                    {
                        _undoOperation = UndoOperation.None;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.RemoveElement;
                        _value = newValue;
                    }
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(newValue))
                    {
                        _undoOperation = UndoOperation.PutElement;
                        _value = newValue;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.None;
                    }
                }
                else // Assign
                {
                    Type keyType, valueType;
                    IDictionary dict = ContainerHelper.GetDictionaryTypes(
                        _elem.GetAttribute(_attrType.Name), out keyType, out valueType);
                    IDictionary clonedDict = ContainerHelper.NewDictionary(keyType, valueType, dict);
                    _undoOperation = UndoOperation.Assign;
                    _value = clonedDict;
                }
            }
            else if (_attrType.Kind == AttributeKind.ArrayAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IList array = (IList)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.RemoveElement;
                    _keyOfValue = keyValue;
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IList array = (IList)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.PutElement;
                    if(keyValue == null)
                    {
                        _value = array[array.Count-1];
                    }
                    else
                    {
                        _value = array[(int)keyValue];
                        _keyOfValue = keyValue;
                    }
                }
                else if(changeType == AttributeChangeType.AssignElement)
                {
                    IList array = (IList)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.AssignElement;
                    _value = array[(int)keyValue];
                    _keyOfValue = keyValue;
                }
                else // Assign
                {
                    Type valueType;
                    IList array = ContainerHelper.GetListType(
                        _elem.GetAttribute(_attrType.Name), out valueType);
                    IList clonedArray = ContainerHelper.NewList(valueType, array);
                    _undoOperation = UndoOperation.Assign;
                    _value = clonedArray;
                }
            }
            else if(_attrType.Kind == AttributeKind.DequeAttr)
            {
                if(changeType == AttributeChangeType.PutElement)
                {
                    IDeque deque = (IDeque)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.RemoveElement;
                    _keyOfValue = keyValue;
                }
                else if(changeType == AttributeChangeType.RemoveElement)
                {
                    IDeque deque = (IDeque)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.PutElement;
                    if(keyValue == null)
                    {
                        _value = deque.Front;
                    }
                    else
                    {
                        _value = deque[(int)keyValue];
                        _keyOfValue = keyValue;
                    }
                }
                else if(changeType == AttributeChangeType.AssignElement)
                {
                    IDeque deque = (IDeque)_elem.GetAttribute(_attrType.Name);
                    _undoOperation = UndoOperation.AssignElement;
                    _value = deque[(int)keyValue];
                    _keyOfValue = keyValue;
                }
                else // Assign
                {
                    Type valueType;
                    IDeque deque = ContainerHelper.GetDequeType(
                        _elem.GetAttribute(_attrType.Name), out valueType);
                    IDeque clonedDeque = ContainerHelper.NewDeque(valueType, deque);
                    _undoOperation = UndoOperation.Assign;
                    _value = clonedDeque;
                }
            }
            else if(_attrType.Kind == AttributeKind.MapAttr)
            {
                if (changeType == AttributeChangeType.PutElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(keyValue))
                    {
                        if (dict[keyValue] == newValue)
                        {
                            _undoOperation = UndoOperation.None;
                        }
                        else
                        {
                            _undoOperation = UndoOperation.PutElement;
                            _value = dict[keyValue];
                            _keyOfValue = keyValue;
                        }
                    }
                    else
                    {
                        _undoOperation = UndoOperation.RemoveElement;
                        _value = newValue;
                        _keyOfValue = keyValue;
                    }
                }
                else if (changeType == AttributeChangeType.RemoveElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if (dict.Contains(keyValue))
                    {
                        _undoOperation = UndoOperation.PutElement;
                        _value = dict[keyValue];
                        _keyOfValue = keyValue;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.None;
                    }
                }
                else if(changeType == AttributeChangeType.AssignElement)
                {
                    IDictionary dict = (IDictionary)_elem.GetAttribute(_attrType.Name);
                    if(dict[keyValue] == newValue)
                    {
                        _undoOperation = UndoOperation.None;
                    }
                    else
                    {
                        _undoOperation = UndoOperation.AssignElement;
                        _value = dict[keyValue];
                        _keyOfValue = keyValue;
                    }
                }
                else // Assign
                {
                    Type keyType, valueType;
                    IDictionary dict = ContainerHelper.GetDictionaryTypes(
                        _elem.GetAttribute(_attrType.Name), out keyType, out valueType);
                    IDictionary clonedDict = ContainerHelper.NewDictionary(keyType, valueType, dict);
                    _undoOperation = UndoOperation.Assign;
                    _value = clonedDict;
                }
            }
            else // Primitve Type Assign
            {
                _undoOperation = UndoOperation.Assign;
                _value = _elem.GetAttribute(_attrType.Name);
            }
        }
Exemple #6
0
 public void ChangingEdgeAttribute(IEdge edge, AttributeType attrType,
     AttributeChangeType changeType, Object newValue, Object keyValue)
 {
     ChangingEdgeAttributeHandler changingElemAttr = OnChangingEdgeAttribute;
     if(changingElemAttr != null)
         changingElemAttr(edge, attrType, changeType, newValue, keyValue);
 }
        public void ChangingElementAttribute(IGraphElement elem, AttributeType attrType,
                AttributeChangeType changeType, Object newValue, Object keyValue)
        {
#if LOG_TRANSACTION_HANDLING
            writer.WriteLine((paused ? "" : new String(' ', transactionLevel)) + "ChangingElementAttribute: " + ((LGSPNamedGraph)procEnv.graph).GetElementName(elem) + ":" + elem.Type.Name + "." + attrType.Name);
#endif
            if(recording && !paused && !undoing)
                undoItems.Add(new LGSPUndoAttributeChanged(elem, attrType, changeType, newValue, keyValue, procEnv));
        }
Exemple #8
0
 /// <summary>
 /// Event handler for IGraph.OnChangingNodeAttribute and IGraph.OnChangingEdgeAttribute.
 /// </summary>
 /// <param name="element">The node or edge 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(IGraphElement element, AttributeType attrType,
         AttributeChangeType changeType, Object newValue, Object keyValue)
 {
     foreach(RecordingState recordingState in recordings.Values)
     {
         MainGraphExportContext mainExportContext = recordingState.mainExportContext;
         AddSubgraphsAsNeeded(mainExportContext, element, attrType, newValue, recordingState.writer);
         AddSubgraphsAsNeeded(mainExportContext, element, attrType, keyValue, recordingState.writer);
         switch(changeType)
         {
         case AttributeChangeType.Assign:
             recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name + " = ");
             GRSExport.EmitAttribute(mainExportContext, attrType, newValue, graph, recordingState.writer);
             recordingState.writer.WriteLine();
             break;
         case AttributeChangeType.PutElement:
             recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name);
             switch(attrType.Kind)
             {
             case AttributeKind.SetAttr:
                 recordingState.writer.Write(".add(");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                 recordingState.writer.WriteLine(")");
                 break;
             case AttributeKind.MapAttr:
                 recordingState.writer.Write(".add(");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, attrType.KeyType, graph));
                 recordingState.writer.Write(", ");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                 recordingState.writer.WriteLine(")");
                 break;
             case AttributeKind.ArrayAttr:
                 if(keyValue == null)
                 {
                     recordingState.writer.Write(".add(");
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                     recordingState.writer.WriteLine(")");
                 }
                 else
                 {
                     recordingState.writer.Write(".add(");
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                     recordingState.writer.Write(", ");
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                     recordingState.writer.WriteLine(")");
                 }
                 break;
             case AttributeKind.DequeAttr:
                 if(keyValue == null)
                 {
                     recordingState.writer.Write(".add(");
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                     recordingState.writer.WriteLine(")");
                 }
                 else
                 {
                     recordingState.writer.Write(".add(");
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                     recordingState.writer.Write(", ");
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                     recordingState.writer.WriteLine(")");
                 }
                 break;
             default:
                  throw new Exception("Wrong attribute type for attribute change type");
             }
             break;
         case AttributeChangeType.RemoveElement:
             recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name);
             switch(attrType.Kind)
             {
             case AttributeKind.SetAttr:
                 recordingState.writer.Write(".rem(");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                 recordingState.writer.WriteLine(")");
                 break;
             case AttributeKind.MapAttr:
                 recordingState.writer.Write(".rem(");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, attrType.KeyType, graph));
                 recordingState.writer.WriteLine(")");
                 break;
             case AttributeKind.ArrayAttr:
                 recordingState.writer.Write(".rem(");
                 if(keyValue!=null)
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                 recordingState.writer.WriteLine(")");
                 break;
             case AttributeKind.DequeAttr:
                 recordingState.writer.Write(".rem(");
                 if(keyValue != null)
                     recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                 recordingState.writer.WriteLine(")");
                 break;
             default:
                  throw new Exception("Wrong attribute type for attribute change type");
             }
             break;
         case AttributeChangeType.AssignElement:
             recordingState.writer.Write("@(\"" + graph.GetElementName(element) + "\")." + attrType.Name);
             switch(attrType.Kind)
             {
             case AttributeKind.ArrayAttr:
                 recordingState.writer.Write("[");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                 recordingState.writer.Write("] = ");
                 recordingState.writer.WriteLine(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                 break;
             case AttributeKind.DequeAttr:
                 recordingState.writer.Write("[");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, new AttributeType(null, null, AttributeKind.IntegerAttr, null, null, null, null, null, null, typeof(int)), graph));
                 recordingState.writer.Write("] = ");
                 recordingState.writer.WriteLine(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                 break;
             case AttributeKind.MapAttr:
                 recordingState.writer.Write("[");
                 recordingState.writer.Write(GRSExport.ToString(mainExportContext, keyValue, attrType.KeyType, graph));
                 recordingState.writer.Write("] = ");
                 recordingState.writer.WriteLine(GRSExport.ToString(mainExportContext, newValue, attrType.ValueType, graph));
                 break;
             default:
                  throw new Exception("Wrong attribute type for attribute change type");
             }
             break;
         default:
             throw new Exception("Unknown attribute change type");
         }
     }
 }
Exemple #9
0
 /// <summary>
 /// Returns a string representation of the given Deque
 /// after the given operation with the given parameters was applied
 /// </summary>
 /// <param name="deque">The base Deque of the operation</param>
 /// <param name="changeType">The type of the change operation</param>
 /// <param name="newValue">The new value to be inserted/added if changeType==PutElement on deque.</param>
 /// <param name="type">The type as string, e.g deque<int></param>
 /// <param name="content">The content as string, e.g. ] 42, 43 [ </param>
 /// <param name="attrType">The attribute type of the Deque</param>
 /// <param name="graph">The graph with the model and the element names</param>
 public static void ToString(IDeque deque,
     AttributeChangeType changeType, Object newValue,
     out string type, out string content,
     AttributeType attrType, IGraph graph)
 {
     if(changeType == AttributeChangeType.PutElement)
     {
         Type valueType;
         ContainerHelper.GetDequeType(deque, out valueType);
         ToString(deque, out type, out content, attrType, graph);
         content += ".add(" + ToString(newValue, attrType.ValueType, graph) + ")";
     }
     else if(changeType == AttributeChangeType.RemoveElement)
     {
         Type valueType;
         ContainerHelper.GetDequeType(deque, out valueType);
         ToString(deque, out type, out content, attrType, graph);
         content += ".rem()";
     }
     else // changeType==AttributeChangeType.Assign
     {
         ToString((IDeque)newValue, out type, out content, attrType, graph);
     }
 }
Exemple #10
0
 /// <summary>
 /// Returns a string representation of the given List
 /// after the given operation with the given parameters was applied
 /// </summary>
 /// <param name="array">The base List of the operation</param>
 /// <param name="changeType">The type of the change operation</param>
 /// <param name="newValue">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 array index to be removed/written to if changeType==RemoveElement/AssignElement on array.</param>
 /// <param name="type">The type as string, e.g array<int></param>
 /// <param name="content">The content as string, e.g. [ 42, 43 ] </param>
 /// <param name="attrType">The attribute type of the List</param>
 /// <param name="graph">The graph with the model and the element names</param>
 public static void ToString(IList array,
     AttributeChangeType changeType, Object newValue, Object keyValue,
     out string type, out string content,
     AttributeType attrType, IGraph graph)
 {
     if(changeType == AttributeChangeType.PutElement)
     {
         Type valueType;
         ContainerHelper.GetListType(array, out valueType);
         ToString(array, out type, out content, attrType, graph);
         content += ".add(" + ToString(newValue, attrType.ValueType, graph);
         if(keyValue != null) content += ", " + keyValue.ToString() + ")";
         else content += ")";
     }
     else if(changeType == AttributeChangeType.RemoveElement)
     {
         Type valueType;
         ContainerHelper.GetListType(array, out valueType);
         ToString(array, out type, out content, attrType, graph);
         content += ".rem(";
         if(keyValue != null) content += keyValue.ToString() + ")";
         else content += ")";
     }
     else if(changeType == AttributeChangeType.AssignElement)
     {
         Type valueType;
         ContainerHelper.GetListType(array, out valueType);
         ToString(array, out type, out content, attrType, graph);
         content += "[" + keyValue.ToString() + "] = " + ToString(newValue, attrType.ValueType, graph);
     }
     else // changeType==AttributeChangeType.Assign
     {
         ToString((IList)newValue, out type, out content, attrType, graph);
     }
 }
Exemple #11
0
        /// <summary>
        /// Returns a string representation of the given dictionary
        /// after the given operation with the given parameters was applied
        /// </summary>
        /// <param name="setmap">The base dictionary of the operation</param>
        /// <param name="changeType">The type of the change operation</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.</param>
        /// <param name="keyValue">The map pair key to be inserted/removed if changeType==PutElement/RemoveElement on map.</param>
        /// <param name="type">The type as string, e.g set<int> or map<string,boolean> </param>
        /// <param name="content">The content as string, e.g. { 42, 43 } or { "foo"->true, "bar"->false } </param>
        /// <param name="attrType">The attribute type of the dictionary</param>
        /// <param name="graph">The graph with the model and the element names</param>
        public static void ToString(IDictionary setmap,
            AttributeChangeType changeType, Object newValue, Object keyValue,
            out string type, out string content,
            AttributeType attrType, IGraph graph)
        {
            if(changeType==AttributeChangeType.PutElement)
            {
                Type keyType;
                Type valueType;
                ContainerHelper.GetDictionaryTypes(setmap, out keyType, out valueType);

                if(valueType==typeof(SetValueType))
                {
                    ToString(setmap, out type, out content, attrType, graph);
                    content += "|" + ToString(newValue, attrType.ValueType, graph);
                }
                else
                {
                    ToString(setmap, out type, out content, attrType, graph);
                    content += "|" + ToString(keyValue, attrType.KeyType, graph) + "->" + ToString(newValue, attrType.ValueType, graph);
                }
            }
            else if(changeType==AttributeChangeType.RemoveElement)
            {
                Type keyType;
                Type valueType;
                ContainerHelper.GetDictionaryTypes(setmap, out keyType, out valueType);

                if(valueType==typeof(SetValueType))
                {
                    ToString(setmap, out type, out content, attrType, graph);
                    content += "\\" + ToString(newValue, attrType.ValueType, graph);
                }
                else
                {
                    ToString(setmap, out type, out content, attrType, graph);
                    content += "\\" + ToString(keyValue, attrType.KeyType, graph) + "->.";
                }
            }
            else // changeType==AttributeChangeType.Assign
            {
                ToString((IDictionary)newValue, out type, out content, attrType, graph);
            }
        }