Exemplo n.º 1
0
 /// <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
 }
Exemplo n.º 2
0
        /// <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();
        }
Exemplo n.º 3
0
 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));
 }
        /// <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>
        /// <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(IList array, out string type, out string content,
                                    AttributeType attrType, IGraph graph, bool firstLevelObjectEmitted,
                                    IDictionary <string, IObject> nameToObject, IGraphProcessingEnvironment procEnv)
        {
            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, firstLevelObjectEmitted, nameToObject, procEnv);
            }
            else
            {
                type = "<INVALID>";
            }

            sb.Append("]");
            content = sb.ToString();
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
        /// <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);
        }
Exemplo n.º 7
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);
     }
 }
Exemplo n.º 8
0
        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));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a new dictionary representing a set containing all values from the given list or deque.
        /// </summary>
        public static IDictionary ArrayOrDequeAsSet(object obj)
        {
            if (obj is IList)
            {
                IList a = (IList)obj;

                Type valueType;
                ContainerHelper.GetListType(a, out valueType);
                IDictionary newDict = NewDictionary(valueType, typeof(SetValueType));

                for (int i = 0; i < a.Count; ++i)
                {
                    newDict[a[i]] = null;
                }

                return(newDict);
            }
            else if (obj is IDeque)
            {
                IDeque a = (IDeque)obj;

                Type valueType;
                ContainerHelper.GetDequeType(a, out valueType);
                IDictionary newDict = NewDictionary(valueType, typeof(SetValueType));

                for (int i = 0; i < a.Count; ++i)
                {
                    newDict[a[i]] = null;
                }

                return(newDict);
            }
            else
            {
                throw new Exception("asSet() can only be used on array or deque");
            }
        }
Exemplo n.º 10
0
        /// <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 + ">";
                bool first = true;
                foreach (Object entry in array)
                {
                    if (first)
                    {
                        sb.Append(ToString(entry, attrValueType, graph)); first = false;
                    }
                    else
                    {
                        sb.Append(","); sb.Append(ToString(entry, attrValueType, graph));
                    }
                }
            }
            else
            {
                type = "<INVALID>";
            }

            sb.Append("]");
            content = sb.ToString();
        }
Exemplo n.º 11
0
 /// <summary>
 /// If the attribute of the given name of the given element is a container attribute
 /// then return a clone of the given container value, otherwise just return the original value;
 /// additionally returns the AttributeType of the attribute of the element.
 /// </summary>
 public static object IfAttributeOfElementIsContainerThenCloneContainer(
     IObject element, String AttributeName, object value, out AttributeType attrType)
 {
     attrType = element.Type.GetAttributeType(AttributeName);
     if (attrType.Kind == AttributeKind.SetAttr || attrType.Kind == AttributeKind.MapAttr)
     {
         Type keyType, valueType;
         ContainerHelper.GetDictionaryTypes(element.GetAttribute(AttributeName), out keyType, out valueType);
         return(ContainerHelper.NewDictionary(keyType, valueType, value)); // by-value-semantics -> clone dictionary
     }
     else if (attrType.Kind == AttributeKind.ArrayAttr)
     {
         Type valueType;
         ContainerHelper.GetListType(element.GetAttribute(AttributeName), out valueType);
         return(ContainerHelper.NewList(valueType, value)); // by-value-semantics -> clone array
     }
     else if (attrType.Kind == AttributeKind.DequeAttr)
     {
         Type valueType;
         ContainerHelper.GetDequeType(element.GetAttribute(AttributeName), out valueType);
         return(ContainerHelper.NewDeque(valueType, value)); // by-value-semantics -> clone deque
     }
     return(value);
 }