public static object GetAttributeOrElementOfMatch(object source, string attributeOrElementName) { if (source is IMatch) { IMatch match = (IMatch)source; object value = match.getNode(attributeOrElementName); if (value != null) { return(value); } value = match.getEdge(attributeOrElementName); if (value != null) { return(value); } value = match.getVariable(attributeOrElementName); return(value); } else { IAttributeBearer attributeBearer = (IAttributeBearer)source; object value = attributeBearer.GetAttribute(attributeOrElementName); value = attributeBearer is ITransientObject ? value : ContainerHelper.IfAttributeOfElementIsContainerThenCloneContainer( attributeBearer, attributeOrElementName, value); return(value); } }
// convenience helper function for firing the changing node/edge/object attribute event (for an attribute of map type, contains type dispatching and fixes the change type to element removal) public static void ChangingMapAttributeRemoveElement(IGraph graph, IAttributeBearer owner, AttributeType attrType, object keyToRemove) { if (owner is INode) { graph.ChangingNodeAttribute((INode)owner, attrType, AttributeChangeType.RemoveElement, null, keyToRemove); } else if (owner is IEdge) { graph.ChangingEdgeAttribute((IEdge)owner, attrType, AttributeChangeType.RemoveElement, null, keyToRemove); } else { graph.ChangingObjectAttribute((IObject)owner, attrType, AttributeChangeType.RemoveElement, null, keyToRemove); } }
// convenience helper function for firing the changing node/edge/object attribute event (for an attribute of map type, contains type dispatching and fixes the change type to element addition) public static void ChangingMapAttributePutElement(IGraph graph, IAttributeBearer owner, AttributeType attrType, object key, object value) { if (owner is INode) { graph.ChangingNodeAttribute((INode)owner, attrType, AttributeChangeType.PutElement, value, key); } else if (owner is IEdge) { graph.ChangingEdgeAttribute((IEdge)owner, attrType, AttributeChangeType.PutElement, value, key); } else { graph.ChangingObjectAttribute((IObject)owner, attrType, AttributeChangeType.PutElement, value, key); } }
// convenience helper function for firing the changing node/edge/object attribute event (contains type dispatching and fixes the change type to assignment) public static void ChangingAttributeAssign(IGraph graph, IAttributeBearer owner, AttributeType attrType, object value) { if (owner is INode) { graph.ChangingNodeAttribute((INode)owner, attrType, AttributeChangeType.Assign, value, null); } else if (owner is IEdge) { graph.ChangingEdgeAttribute((IEdge)owner, attrType, AttributeChangeType.Assign, value, null); } else { graph.ChangingObjectAttribute((IObject)owner, attrType, AttributeChangeType.Assign, value, null); } }
public void ChangingElementAttribute(IAttributeBearer owner, AttributeType attrType, AttributeChangeType changeType, object newValue, object keyValue) { #if LOG_TRANSACTION_HANDLING if (owner is IGraphElement) { writer.WriteLine((paused ? "" : new String(' ', transactionLevel)) + "ChangingElementAttribute: " + ((LGSPNamedGraph)procEnv.graph).GetElementName((IGraphElement)owner) + ":" + owner.Type.Name + "." + attrType.Name); } else { writer.WriteLine((paused ? "" : new String(' ', transactionLevel)) + "ChangingElementAttribute: hash" + ((IObject)owner).GetHashCode() + ":" + owner.Type.Name + "." + attrType.Name); } #endif if (recording && !paused && !undoing) { undoItems.Add(new LGSPUndoAttributeChanged(owner, attrType, changeType, newValue, keyValue, procEnv)); } }
/// <summary> /// If the attribute of the given name of the given element is a conatiner attribute /// then return a clone of the given container value, otherwise just return the original value /// </summary> public static object IfAttributeOfElementIsContainerThenCloneContainer( IAttributeBearer attributeBearer, String AttributeName, object value) { AttributeType attrType; if (attributeBearer is IGraphElement) { return(IfAttributeOfElementIsContainerThenCloneContainer( (IGraphElement)attributeBearer, AttributeName, value, out attrType)); } else if (attributeBearer is IObject) { return(IfAttributeOfElementIsContainerThenCloneContainer( (IObject)attributeBearer, AttributeName, value, out attrType)); } else //if(attributeBearer is ITransientObject) { return(value); } }
private bool AddSubgraphsAsNeeded(MainGraphExportContext mainExportContext, IAttributeBearer owner, 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); }
/// <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 bool AreAttributesEqual(IAttributeBearer that) { throw new NotImplementedException("The method or operation is not implemented."); }
/// <summary> /// Returns whether the attributes of this element and that are equal. /// If types are unequal the result is false, otherwise the conjunction of equality comparison of the attributes. /// </summary> public abstract bool AreAttributesEqual(IAttributeBearer that);
public readonly IGraph _graph; // for ToString only public LGSPUndoAttributeChanged(IAttributeBearer elem, AttributeType attrType, AttributeChangeType changeType, object newValue, object keyValue, LGSPGraphProcessingEnvironment procEnv) { _elem = elem; _attrType = attrType; if (procEnv.graph is LGSPNamedGraph) { if (_elem is IGraphElement) { _name = ((LGSPNamedGraph)procEnv.graph).GetElementName((IGraphElement)_elem); } else { _name = "hash" + _elem.GetHashCode(); } } 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); } }
/// <summary> /// Emits the node/edge/internal class object attribute initialization code in graph exporting /// for an attribute of internal class object type with the given value into the stream writer. /// </summary> private static void EmitAttributeInitialization(MainGraphExportContext mainGraphContext, IAttributeBearer owner, AttributeType attrType, object value, INamedGraph graph, StreamWriter sw) { if (owner is IGraphElement) { String persistentName = graph.GetElementName((IGraphElement)owner); sw.Write("@(\"{0}\").{1} = ", persistentName, attrType.Name); } else { String persistentName = mainGraphContext.GetOrAssignPersistentName((IObject)owner); sw.Write("@@(\"{0}\").{1} = ", persistentName, attrType.Name); } StringBuilder deferredInits = new StringBuilder(); if (value == null) { sw.Write("null"); } else if (value is IObject) { IObject obj = (IObject)value; EmitObjectFetchingOrCreation(mainGraphContext, obj.Type, obj, graph, sw, deferredInits); } else // container { EmitAttribute(mainGraphContext, attrType, value, graph, sw, deferredInits); } sw.WriteLine(); sw.Write(deferredInits); }