Beispiel #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
 }
Beispiel #2
0
        /// <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();
        }
 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 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();
        }
Beispiel #5
0
        /// <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);
        }
Beispiel #6
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");
            }
        }
Beispiel #7
0
        /// <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 + ">";
                bool first = true;
                foreach (Object entry in deque)
                {
                    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();
        }
Beispiel #8
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);
     }
 }
Beispiel #9
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);
 }