public static IDictionary Intersect(IDictionary a, IDictionary b) { Type keyType; Type valueType; IDictionary dict = ContainerHelper.GetDictionaryTypes(a, out keyType, out valueType); // Create empty dictionary. IDictionary newDict = NewDictionary(keyType, valueType); // Add all elements of a also contained in b. if (a.Count <= b.Count) { foreach (DictionaryEntry entry in a) { if (b.Contains(entry.Key)) { newDict.Add(entry.Key, entry.Value); } } } else { foreach (DictionaryEntry entry in b) { if (a.Contains(entry.Key)) { newDict.Add(entry.Key, a[entry.Key]); } } } return(newDict); }
/// <summary> /// Returns a string representation of the given Deque /// </summary> /// <param name="deque">The Deque of which to get the string representation</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 if available, otherwise null</param> /// <param name="graph">The graph with the model and the element names if available, otherwise null</param> /// <param name="firstLevelObjectEmitted">Prevents emitting of further objects and thus infinite regressions</param> /// <param name="nameToObject">If not null, the names of visited objects are added</param> /// <param name="procEnv">If not null, the processing environment is used for transient object unique id emitting and fetching</param> public static void ToString(IDeque deque, out string type, out string content, AttributeType attrType, IGraph graph, bool firstLevelObjectEmitted, IDictionary <string, IObject> nameToObject, IGraphProcessingEnvironment procEnv) { Type valueType; ContainerHelper.GetDequeType(deque, out valueType); StringBuilder sb = new StringBuilder(256); sb.Append("]"); AttributeType attrValueType = attrType != null ? attrType.ValueType : null; if (deque != null) { type = "deque<" + valueType.Name + ">"; AppendDeque(sb, deque, attrValueType, graph, firstLevelObjectEmitted, nameToObject, procEnv); } else { type = "<INVALID>"; } sb.Append("["); content = sb.ToString(); }
public static bool LessOrEqualIDictionary(IDictionary a, IDictionary b) { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(a, out keyType, out valueType); if (valueType.Name == "SetValueType") { foreach (DictionaryEntry entry in a) { if (!b.Contains(entry.Key)) { return(false); } } } else { foreach (DictionaryEntry entry in a) { if (!b.Contains(entry.Key)) { return(false); } if (entry.Value != null ? !entry.Value.Equals(b[entry.Key]) : b[entry.Key] != null) { return(false); } } } return(true); }
public static String DotNetTypeToXgrsType(Type type) { if (type.IsGenericType) { if (type.Name == "Dictionary`2") { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(type, out keyType, out valueType); if (valueType.Name == "SetValueType") { return("set<" + DotNetTypeToXgrsType(keyType.Name, keyType.FullName) + ">"); } else { return("map<" + DotNetTypeToXgrsType(keyType.Name, keyType.FullName) + "," + DotNetTypeToXgrsType(valueType.Name, valueType.FullName) + ">"); } } else if (type.Name == "List`1") { Type valueType; ContainerHelper.GetListType(type, out valueType); return("array<" + DotNetTypeToXgrsType(valueType.Name, valueType.FullName) + ">"); } else if (type.Name == "Deque`1") { Type valueType; ContainerHelper.GetDequeType(type, out valueType); return("deque<" + DotNetTypeToXgrsType(valueType.Name, valueType.FullName) + ">"); } } return(DotNetTypeToXgrsType(type.Name, type.FullName)); }
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); } }
///////////////////////////////////////////////////////////////////////////////// public static void AssignAttribute(object target, object value, string attributeName, IGraph graph) { if (target is IGraphElement) { IGraphElement elem = (IGraphElement)target; AttributeType attrType; value = ContainerHelper.IfAttributeOfElementIsContainerThenCloneContainer( elem, attributeName, value, out attrType); BaseGraph.ChangingAttributeAssign(graph, elem, attrType, value); elem.SetAttribute(attributeName, value); BaseGraph.ChangedAttribute(graph, elem, attrType); } else if (target is IObject) { IObject elem = (IObject)target; AttributeType attrType = elem.Type.GetAttributeType(attributeName); BaseGraph.ChangingAttributeAssign(graph, elem, attrType, value); elem.SetAttribute(attributeName, value); } else //if(target is ITransientObject) { ITransientObject elem = (ITransientObject)target; elem.SetAttribute(attributeName, value); } }
/// <summary> /// Creates a shallow clone of the given container. /// </summary> /// <param name="oldContainer">The container to clone.</param> /// <returns>A shallow clone of the container</returns> public static object Clone(object oldContainer) { if (oldContainer is IDictionary) { Type keyType, valueType; IDictionary dict = ContainerHelper.GetDictionaryTypes( oldContainer, out keyType, out valueType); return(NewDictionary(keyType, valueType, oldContainer)); } else if (oldContainer is IList) { Type valueType; IList array = ContainerHelper.GetListType( oldContainer, out valueType); return(NewList(valueType, oldContainer)); } else if (oldContainer is IDeque) { Type valueType; IDeque deque = ContainerHelper.GetDequeType( oldContainer, out valueType); return(NewDeque(valueType, oldContainer)); } return(null); // no known container type }
public static object GetGraphElementAttributeOrElementOfMatch(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 { IGraphElement elem = (IGraphElement)source; object value = elem.GetAttribute(attributeOrElementName); value = ContainerHelper.IfAttributeOfElementIsContainerThenCloneContainer( elem, attributeOrElementName, value); return(value); } }
/// <summary> /// Creates a new list containing all values from the given dictionary representing a set. /// </summary> public static IList AsArray(object container, IGraphModel model) { if (container is IList) { return((IList)container); } else if (container is IDictionary) { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(container, out keyType, out valueType); if (valueType.Name == "SetValueType") { return(SetAsArray((IDictionary)container)); } else { return(MapAsArray((IDictionary)container, model)); } } else if (container is IDeque) { return(DequeAsArray((IDeque)container)); } return(null); }
/// <summary> /// Returns a string representation of the given Deque /// </summary> /// <param name="deque">The Deque of which to get the string representation</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 if available, otherwise null</param> /// <param name="graph">The graph with the model and the element names if available, otherwise null</param> public static void ToString(IDeque deque, out string type, out string content, AttributeType attrType, IGraph graph) { Type valueType; ContainerHelper.GetDequeType(deque, out valueType); StringBuilder sb = new StringBuilder(256); sb.Append("]"); AttributeType attrValueType = attrType != null ? attrType.ValueType : null; if (deque != null) { type = "deque<" + valueType.Name + ">"; AppendDeque(sb, deque, attrValueType, graph); } else { type = "<INVALID>"; } sb.Append("["); content = sb.ToString(); }
protected static void FillReturnVariablesFromValues(SequenceVariable[] ReturnVars, IAction Action, IGraphProcessingEnvironment procEnv, List <object[]> retElemsList) { IList[] returnVars = null; if (ReturnVars.Length > 0) { returnVars = new IList[ReturnVars.Length]; for (int i = 0; i < ReturnVars.Length; ++i) { returnVars[i] = ReturnVars[i].GetVariableValue(procEnv) as IList; if (returnVars[i] == null) { string returnType = TypesHelper.DotNetTypeToXgrsType(Action.RulePattern.Outputs[i]); Type valueType = TypesHelper.GetType(returnType, procEnv.Graph.Model); returnVars[i] = ContainerHelper.NewList(valueType); ReturnVars[i].SetVariableValue(returnVars[i], procEnv); } else { returnVars[i].Clear(); } } } for (int curRetElemNum = 0; curRetElemNum < retElemsList.Count; ++curRetElemNum) { object[] retElems = retElemsList[curRetElemNum]; for (int i = 0; i < ReturnVars.Length; ++i) { returnVars[i].Add(retElems[i]); } } }
public override void Assign(object value, IGraphProcessingEnvironment procEnv) { IGraphElement elem = (IGraphElement)DestVar.GetVariableValue(procEnv); AttributeType attrType; value = ContainerHelper.IfAttributeOfElementIsContainerThenCloneContainer( elem, AttributeName, value, out attrType); AttributeChangeType changeType = AttributeChangeType.Assign; if (elem is INode) { procEnv.Graph.ChangingNodeAttribute((INode)elem, attrType, changeType, value, null); } else { procEnv.Graph.ChangingEdgeAttribute((IEdge)elem, attrType, changeType, value, null); } elem.SetAttribute(AttributeName, value); if (elem is INode) { procEnv.Graph.ChangedNodeAttribute((INode)elem, attrType); } else { procEnv.Graph.ChangedEdgeAttribute((IEdge)elem, attrType); } }
/// <summary> /// Returns a string representation of the given List /// </summary> /// <param name="array">The List of which to get the string representation</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 array if available, otherwise null</param> /// <param name="graph">The graph with the model and the element names if available, otherwise null</param> public static void ToString(IList array, out string type, out string content, AttributeType attrType, IGraph graph) { Type valueType; ContainerHelper.GetListType(array, out valueType); StringBuilder sb = new StringBuilder(256); sb.Append("["); AttributeType attrValueType = attrType != null ? attrType.ValueType : null; if (array != null) { type = "array<" + valueType.Name + ">"; AppendArray(sb, array, attrValueType, graph); } else { type = "<INVALID>"; } sb.Append("]"); content = sb.ToString(); }
/// <summary> /// Creates a new list representing an array, /// containing all values from the given dictionary representing a map <paramref name="map"/> from int to some values. /// </summary> /// <param name="map">A dictionary representing a map.</param> /// <returns>A new list containing all values from <paramref name="map"/>.</returns> public static IList MapAsArray(IDictionary map, IGraphModel model) { int max = 0; foreach (int i in map.Keys) { if (i < 0) { throw new Exception("MapAsArray does not support negative indices"); } max = Math.Max(max, i); } Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(map, out keyType, out valueType); IList newList = NewList(valueType, max + 1); // yep, if the dict contains max int, contiguous 8GB are needed // Add all values of dictionary representing map to new dictionary representing set for (int i = 0; i < max + 1; ++i) { if (map.Contains(i)) { newList.Add(map[i]); } else { newList.Add(TypesHelper.DefaultValue(valueType.Name, model)); } } return(newList); }
/// <summary> /// Returns a string representation of the given dictionary /// </summary> /// <param name="setmap">The dictionary of which to get the string representation</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 if available, otherwise null</param> /// <param name="graph">The graph with the model and the element names if available, otherwise null</param> public static void ToString(IDictionary setmap, out string type, out string content, AttributeType attrType, IGraph graph) { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(setmap, out keyType, out valueType); StringBuilder sb = new StringBuilder(256); sb.Append("{"); AttributeType attrValueType = attrType != null ? attrType.ValueType : null; AttributeType attrKeyType = attrType != null ? attrType.KeyType : null; if (setmap != null) { if (valueType == typeof(SetValueType)) { type = "set<" + keyType.Name + ">"; bool first = true; foreach (DictionaryEntry entry in setmap) { if (first) { sb.Append(ToString(entry.Key, attrValueType, graph)); first = false; } else { sb.Append(","); sb.Append(ToString(entry.Key, attrValueType, graph)); } } } else { type = "map<" + keyType.Name + "," + valueType.Name + ">"; bool first = true; foreach (DictionaryEntry entry in setmap) { if (first) { sb.Append(ToString(entry.Key, attrKeyType, graph)); sb.Append("->"); sb.Append(ToString(entry.Value, attrValueType, graph)); first = false; } else { sb.Append(","); sb.Append(ToString(entry.Key, attrKeyType, graph)); sb.Append("->"); sb.Append(ToString(entry.Value, attrValueType, graph)); } } } } else { type = "<INVALID>"; } sb.Append("}"); content = sb.ToString(); }
public override void Assign(object value, IGraphProcessingEnvironment procEnv) { IGraphElement elem = (IGraphElement)DestVar.GetVariableValue(procEnv); AttributeType attrType; value = ContainerHelper.IfAttributeOfElementIsContainerThenCloneContainer( elem, AttributeName, value, out attrType); BaseGraph.ChangingAttributeAssign(procEnv.Graph, elem, attrType, value); elem.SetAttribute(AttributeName, value); BaseGraph.ChangedAttribute(procEnv.Graph, elem, attrType); }
/// <summary> /// Creates a new deque containing all values from the given list. /// </summary> public static IDeque ArrayAsDeque(IList a) { Type valueType; ContainerHelper.GetListType(a, out valueType); IDeque newDeque = NewDeque(valueType); for (int i = 0; i < a.Count; ++i) { newDeque.Add(a[i]); } return(newDeque); }
/// <summary> /// Creates a new dictionary representing a map containing all values from the given list, mapped to by their index. /// </summary> public static IDictionary ArrayAsMap(IList a) { Type valueType; ContainerHelper.GetListType(a, out valueType); IDictionary newDict = NewDictionary(typeof(int), valueType); for (int i = 0; i < a.Count; ++i) { newDict[i] = a[i]; } return(newDict); }
/// <summary> /// Creates a new list representing an array containing all values from the given deque. /// </summary> public static IList DequeAsArray(IDeque a) { Type valueType; ContainerHelper.GetDequeType(a, out valueType); IList newArray = NewList(valueType); for (int i = 0; i < a.Count; ++i) { newArray.Add(a[i]); } return(newArray); }
public static IDictionary Domain(IDictionary map) { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(map, out keyType, out valueType); IDictionary newDict = NewDictionary(keyType, typeof(SetValueType)); foreach (object key in map.Keys) { newDict[key] = null; } return(newDict); }
public static IDictionary Range(IDictionary map) { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(map, out keyType, out valueType); IDictionary newDict = NewDictionary(valueType, typeof(SetValueType)); foreach (object value in map.Values) { newDict[value] = null; } return(newDict); }
/// <summary> /// Returns a clone of either a graph or a match or a container /// </summary> /// <param name="toBeCloned">The graph or match or container to be cloned</param> /// <returns>The cloned graph or match or container</returns> public static object Clone(object toBeCloned) { if (toBeCloned is IGraph) { return(GraphHelper.Copy((IGraph)toBeCloned)); } else if (toBeCloned is IMatch) { return(((IMatch)toBeCloned).Clone()); } else { return(ContainerHelper.Clone(toBeCloned)); } }
/// <summary> /// Creates a new list containing all values from the given dictionary representing a set. /// </summary> public static IList SetAsArray(IDictionary a) { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(a, out keyType, out valueType); IList newList = NewList(keyType); foreach (DictionaryEntry entry in a) { newList.Add(entry.Key); } return(newList); }
/// <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); } }
public static IDictionary Except(IDictionary a, IDictionary b) { Type keyType; Type valueType; IDictionary dict = ContainerHelper.GetDictionaryTypes(a, out keyType, out valueType); // Fill new dictionary with all elements from a. IDictionary newDict = NewDictionary(keyType, valueType, a); // Remove all elements contained in b. foreach (DictionaryEntry entry in b) { newDict.Remove(entry.Key); } return(newDict); }
public static IDictionary Union(IDictionary a, IDictionary b) { Type keyType; Type valueType; IDictionary dict = ContainerHelper.GetDictionaryTypes(a, out keyType, out valueType); // Fill new dictionary with all elements from a. IDictionary newDict = NewDictionary(keyType, valueType, a); // Add all elements from b, potentially overwriting those of a. foreach (DictionaryEntry entry in b) { newDict[entry.Key] = entry.Value; } return(newDict); }
/// <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); } }
public static String XgrsTypeOfConstant(object constant, IGraphModel model) { if (constant == null) { return(""); } if (constant.GetType().IsGenericType) { if (constant.GetType().Name == "Dictionary`2") { Type keyType; Type valueType; ContainerHelper.GetDictionaryTypes(constant, out keyType, out valueType); if (valueType == typeof(de.unika.ipd.grGen.libGr.SetValueType)) { return("set<" + DotNetTypeToXgrsType(keyType.Name, keyType.FullName) + ">"); } else { return("map<" + DotNetTypeToXgrsType(keyType.Name, keyType.FullName) + "," + DotNetTypeToXgrsType(valueType.Name, valueType.FullName) + ">"); } } else if (constant.GetType().Name == "List`1") { Type valueType; ContainerHelper.GetListType(constant.GetType(), out valueType); return("array<" + DotNetTypeToXgrsType(valueType.Name, valueType.FullName) + ">"); } else //if(constant.GetType().Name == "Deque`1") { Type valueType; ContainerHelper.GetListType(constant.GetType(), out valueType); return("deque<" + DotNetTypeToXgrsType(valueType.Name, valueType.FullName) + ">"); } } object typeinsteadofobject = NodeOrEdgeTypeIfNodeOrEdge(constant); return(DotNetTypeToXgrsType(typeinsteadofobject.GetType().Name, typeinsteadofobject.GetType().FullName)); }
/// <summary> /// Returns a shallow clone of either a graph or a match or a class object or a transient class object or a container /// </summary> /// <param name="toBeCloned">The graph or match or (transient) class object or container to be cloned</param> /// <returns>The clone</returns> public static object Clone(object toBeCloned, IGraph graph) { if (toBeCloned is IGraph) { return(GraphHelper.Copy((IGraph)toBeCloned)); } else if (toBeCloned is IMatch) { return(((IMatch)toBeCloned).Clone()); } else if (toBeCloned is IObject) { return(((IObject)toBeCloned).Clone(graph)); } else if (toBeCloned is ITransientObject) { return(((ITransientObject)toBeCloned).Clone()); } else { return(ContainerHelper.Clone(toBeCloned)); } }
/// <summary> /// Returns a deep copy of either a graph or a match or a class object or a transient class object or a container /// </summary> /// <param name="toBeCopied">The graph or match or (transient) class object or container to be copied</param> /// <returns>The copy</returns> public static object Copy(object toBeCopied, IGraph graph) { if (toBeCopied is IGraph) { return(GraphHelper.Copy((IGraph)toBeCopied)); } else if (toBeCopied is IMatch) { return(((IMatch)toBeCopied).Clone()); } else if (toBeCopied is IObject) { return(((IObject)toBeCopied).Copy(graph, new Dictionary <IBaseObject, IBaseObject>())); } else if (toBeCopied is ITransientObject) { return(((ITransientObject)toBeCopied).Copy(graph, new Dictionary <IBaseObject, IBaseObject>())); } else { return(ContainerHelper.Copy(toBeCopied, graph, new Dictionary <IBaseObject, IBaseObject>())); } }