GetId() public method

public GetId ( object obj, bool &firstTime ) : long
obj object
firstTime bool
return long
Ejemplo n.º 1
0
        // Schedule an object for later serialization.
        protected virtual long Schedule(Object obj)
        {
            bool firstTime;
            long objID = m_idGenerator.GetId(obj, out firstTime);

            m_objectQueue.Enqueue(obj);
            return(objID);
        }
Ejemplo n.º 2
0
        protected virtual long Schedule(object obj)
        {
            if (obj == null)
            {
                return(0);
            }

            bool isNew;
            long id = m_idGenerator.GetId(obj, out isNew);

            if (isNew)
            {
                m_objectQueue.Enqueue(obj);
            }
            return(id);
        }
Ejemplo n.º 3
0
        protected virtual long Schedule(object obj)
        {
            if (obj == null)
            {
                return(0L);
            }

            bool FirstTime;
            long ID = m_idGenerator.GetId(obj, out FirstTime);

            if (FirstTime)
            {
                m_objectQueue.Enqueue(obj);
            }

            return(ID);
        }
Ejemplo n.º 4
0
        // Commences the process of serializing the entire graph.
        // initialize the graph walker.
        internal void Serialize(Object graph, Header[] inHeaders, SoapWriter serWriter)
        {
            InternalST.Soap( this, "Serialize Entry 2 ",graph,((headers == null)?" no headers ": "headers"));

            serializationPermission.Demand();

            if (graph == null)
                throw new ArgumentNullException("graph",SoapUtil.GetResourceString("ArgumentNull_Graph"));

            if (serWriter == null)
                throw new ArgumentNullException("serWriter",String.Format(SoapUtil.GetResourceString("ArgumentNull_WithParamName"), "serWriter"));

            serObjectInfoInit = new SerObjectInfoInit();        
            this.serWriter = serWriter;
            this.headers = inHeaders;

            if (graph is IMethodMessage)
            {
                bRemoting = true;
                MethodBase methodBase = ((IMethodMessage)graph).MethodBase;
                if (methodBase != null)
                    serWriter.WriteXsdVersion(ProcessTypeAttributes(methodBase.ReflectedType));
                else
                    serWriter.WriteXsdVersion(XsdVersion.V2001);
            }
            else
                serWriter.WriteXsdVersion(XsdVersion.V2001);

            m_idGenerator = new ObjectIDGenerator();
            m_objectQueue = new Queue();

            if (graph is ISoapMessage)
            {
                // Fake method call is to be written
                bRemoting = true;
                ISoapMessage ismc = (ISoapMessage)graph;
                graph = new InternalSoapMessage(ismc.MethodName, ismc.XmlNameSpace, ismc.ParamNames, ismc.ParamValues, ismc.ParamTypes);
                headers = ismc.Headers;
            }


            InternalST.Soap( this, "Serialize New SerializedTypeTable");
            m_serializedTypeTable = new Hashtable();

            serWriter.WriteBegin();

            long headerId = 0;
            Object obj;
            long objectId;
            bool isNew;

            topId = m_idGenerator.GetId(graph, out isNew);

            if (headers != null)
                headerId = m_idGenerator.GetId(headers, out isNew);
            else
                headerId = -1;

            WriteSerializedStreamHeader(topId, headerId);

            InternalST.Soap( this, "Serialize Schedule 0");

            // Write out SerializedStream header
            if (!((headers == null) || (headers.Length == 0)))
            {
                ProcessHeaders(headerId);
            }

            m_objectQueue.Enqueue(graph);

            while ((obj = GetNext(out objectId))!=null)
            {
                InternalST.Soap( this, "Serialize GetNext ",obj);
                WriteObjectInfo objectInfo = null;

                // GetNext will return either an object or a WriteObjectInfo. 
                // A WriteObjectInfo is returned if this object was member of another object
                if (obj is WriteObjectInfo)
                {
                    InternalST.Soap( this, "Serialize GetNext recognizes WriteObjectInfo");
                    objectInfo = (WriteObjectInfo)obj;
                }
                else
                {
                    objectInfo = WriteObjectInfo.Serialize(obj, m_surrogates, m_context, serObjectInfoInit, m_formatterConverter, null);
                    objectInfo.assemId = GetAssemblyId(objectInfo);
                }

                objectInfo.objectId = objectId;
                NameInfo typeNameInfo = TypeToNameInfo(objectInfo);
                typeNameInfo.NIisTopLevelObject = true;
                if (bRemoting && obj  == graph)
                    typeNameInfo.NIisRemoteRecord = true;
                Write(objectInfo, typeNameInfo, typeNameInfo);
                PutNameInfo(typeNameInfo);
                objectInfo.ObjectEnd();
            }

            serWriter.WriteSerializationHeaderEnd();
            serWriter.WriteEnd();

            m_idGenerator = new ObjectIDGenerator();
            InternalST.Soap( this, "*************Serialize New SerializedTypeTable 2");
            m_serializedTypeTable = new System.Collections.Hashtable();

            InternalST.Soap( this, "Serialize Exit ");
        }
Ejemplo n.º 5
0
 public CGraphFields(GraphControl control, ObjectIDGenerator idGen)
 {
     Dictionary<String, CNodeItemFields> idFieldMap = new Dictionary<String, CNodeItemFields>();
     Dictionary<NodeItem, String> idItemMap = new Dictionary<NodeItem, String>();
     bool first = false;
     id = idGen.GetId(control, out first).ToString();
     name = control.Name;
     Nodes = new List<CNodeFields>();
     List<Node> nodes = (List<Node>)control.Nodes;
     foreach (Node node in nodes)
     {
         CNodeFields nFields = new CNodeFields();
         nFields.Title = node.Title;
         nFields.name = getNodeName(node);
         nFields.Location = node.Location;
         nFields.Items = new List<CNodeItemFields>();
         bool nfirst = false;
         nFields.id = idGen.GetId(nFields, out nfirst).ToString();
         foreach(NodeItem item in node.Items)
         {
             CNodeItemFields iFields = new CNodeItemFields();
             bool ifirst = false;
             iFields.id = idGen.GetId(iFields, out ifirst).ToString();
             String fName = nFields.name + ":" + (item.Name == null ? iFields.id : item.Name);
             idFieldMap.Add(fName, iFields);
             idItemMap.Add(item, fName);
             iFields.Input = new List<CConnectionFields>();
             iFields.Output = new List<CConnectionFields>();
             iFields.ItemParts = new List<CItemPartFields>();
             iFields.name = fName;
             iFields.ParentNode = nFields.id;
             if(item.Tag != null)
                 iFields.Tag = item.Tag.GetType().ToString();
             iFields.IOMode = getIOMode(item);
             iFields.ItemType = item.GetType().ToString();
             switch(iFields.ItemType)
             {
                 case "Graph.Items.NodeTextBoxItem":
                     {
                         NodeTextBoxItem temp = item as NodeTextBoxItem;
                         iFields.Text = temp.Text;
                     }
                     break;
                 case "Graph.Items.NodeLabelItem":
                     {
                         NodeLabelItem temp = item as NodeLabelItem;
                         iFields.Text = temp.Text;
                     }
                     break;
                 case "Graph.Items.NodeCompositeItem":
                     {
                         NodeCompositeItem temp = item as NodeCompositeItem;
                         foreach(ItemPart part in temp.Parts)
                         {
                             CItemPartFields pFields = new CItemPartFields();
                             bool pfirst = false;
                             pFields.id = idGen.GetId(pFields, out pfirst).ToString();
                             pFields.name = part.Name;
                             pFields.PartType = part.GetType().ToString();
                             switch(pFields.PartType)
                             {
                                 case "Graph.Items.ItemTextBoxPart":
                                     {
                                         ItemTextBoxPart tempPart = part as ItemTextBoxPart;
                                         pFields.Text = tempPart.Text;
                                     }
                                     break;
                                 default:
                                     break;
                             }
                             iFields.ItemParts.Add(pFields);
                         }
                     }
                     break;
             }
             nFields.Items.Add(iFields);
         }
         foreach(NodeItem item in node.Items)
         {
             // now get connection info
             String iid = idItemMap[item];
             if (item.Input.Enabled && item.Input.HasConnection)
             {
                 foreach (NodeConnection conn in item.Input.Connectors)
                 {
                     CConnectionFields cFields = new CConnectionFields();
                     bool cfirst = false;
                     cFields.id = idGen.GetId(cFields, out cfirst).ToString();
                     Node nFrom = conn.From.Node;
                     String fromName = "";
                     foreach(NodeItem nItem in nFrom.Items)
                     {
                         if(nItem.Name == "NodeName")
                         {
                             if(nItem.GetType().ToString() == "Graph.Items.NodeTextBoxItem")
                             {
                                 NodeTextBoxItem i = nItem as NodeTextBoxItem;
                                 fromName = i.Text;
                             }
                             if (nItem.GetType().ToString() == "Graph.Items.NodeLabelItem")
                             {
                                 NodeLabelItem i = nItem as NodeLabelItem;
                                 fromName = i.Text;
                             }
                         }
                     }
                     Node nTo = conn.To.Node;
                     String toName = "";
                     foreach (NodeItem nItem in nTo.Items)
                     {
                         if (nItem.Name == "NodeName")
                         {
                             if (nItem.GetType().ToString() == "Graph.Items.NodeTextBoxItem")
                             {
                                 NodeTextBoxItem i = nItem as NodeTextBoxItem;
                                 toName = i.Text;
                             }
                             if (nItem.GetType().ToString() == "Graph.Items.NodeLabelItem")
                             {
                                 NodeLabelItem i = nItem as NodeLabelItem;
                                 toName = i.Text;
                             }
                         }
                     }
                     if (conn.Tag != null)
                         cFields.Tag = conn.Tag.GetType().ToString();
                     else if (item.Tag != null)
                         cFields.Tag = item.Tag.GetType().ToString();
                     cFields.name = (conn.Name == null ? cFields.id : conn.Name);
                     cFields.From = fromName + ":" + conn.From.Item.Name;
                     cFields.To = toName + ":" + conn.To.Item.Name;
                     idFieldMap[iid].Input.Add(cFields);
                 }
             }
             if (item.Output.Enabled && item.Output.HasConnection)
             {
                 foreach (NodeConnection conn in item.Output.Connectors)
                 {
                     CConnectionFields cFields = new CConnectionFields();
                     bool cfirst = false;
                     cFields.id = idGen.GetId(cFields, out cfirst).ToString();
                     Node nFrom = conn.From.Node;
                     String fromName = "";
                     foreach (NodeItem nItem in nFrom.Items)
                     {
                         if (nItem.Name == "NodeName")
                         {
                             if (nItem.GetType().ToString() == "Graph.Items.NodeTextBoxItem")
                             {
                                 NodeTextBoxItem i = nItem as NodeTextBoxItem;
                                 fromName = i.Text;
                             }
                             if (nItem.GetType().ToString() == "Graph.Items.NodeLabelItem")
                             {
                                 NodeLabelItem i = nItem as NodeLabelItem;
                                 fromName = i.Text;
                             }
                         }
                     }
                     Node nTo = conn.To.Node;
                     String toName = "";
                     foreach (NodeItem nItem in nTo.Items)
                     {
                         if (nItem.Name == "NodeName")
                         {
                             if (nItem.GetType().ToString() == "Graph.Items.NodeTextBoxItem")
                             {
                                 NodeTextBoxItem i = nItem as NodeTextBoxItem;
                                 toName = i.Text;
                             }
                             if (nItem.GetType().ToString() == "Graph.Items.NodeLabelItem")
                             {
                                 NodeLabelItem i = nItem as NodeLabelItem;
                                 toName = i.Text;
                             }
                         }
                     }
                     if (conn.Tag != null)
                         cFields.Tag = conn.Tag.GetType().ToString();
                     else if (item.Tag != null)
                         cFields.Tag = item.Tag.GetType().ToString();
                     cFields.name = (conn.Name == null ? cFields.id : conn.Name);
                     cFields.From = fromName + ":" + conn.From.Item.Name;
                     cFields.To = toName + ":" + conn.To.Item.Name;
                     idFieldMap[iid].Input.Add(cFields);
                 }
             }
         }
         Nodes.Add(nFields);
     }
 }
Ejemplo n.º 6
0
        public static IEnumerable<ITask> TryToMakeTasksComply(this IEnumerable<ITask> taskCollection, double minLevel, double maxLevel, double initialValue)
        {
            //Check if initial value is inside allowed range
            if ((initialValue > maxLevel) || (initialValue < minLevel))
                throw new ArgumentException(string.Format("InitialValue: {0}, must be between MinLevel: {1} and MaxLevel: {2}", initialValue, minLevel, maxLevel));

            //Check if final quantity cannot be inside allowed range
            double finalQuantity = initialValue + taskCollection.Sum(task => task.Quantity);
            if ((finalQuantity > maxLevel) || (finalQuantity < minLevel))
                return null;

            var dataPoints = taskCollection.GetDataPoints(initialValue);

            //Find the first point that is outside of allowed range
            KeyValuePair<DateTime, double> outOfRangePoint;
            if (!FindOutOfRangePoint(dataPoints, minLevel, maxLevel, out outOfRangePoint))
                return taskCollection;

            //Calculate how much quantity overshoot/undershoot we have
            double diffQ = CalcQuantityDifference(outOfRangePoint, minLevel, maxLevel);

            //Find where to move the end time of the tasks
            DateTime moveToTime;
            if (!FindMoveToTime(dataPoints, outOfRangePoint, minLevel, maxLevel, out moveToTime))
                return null;

            Collection<ITask> newTaskCollection = new Collection<ITask>(taskCollection.ToList());

            //Cache indexes of the collection for fast retrieval
            ObjectIDGenerator oidGen = new ObjectIDGenerator();
            bool firstTime;

            Dictionary<long, int> indexDict = new Dictionary<long, int>();
            for (int i = 0; i < newTaskCollection.Count; i++)
                indexDict.Add(oidGen.GetId(newTaskCollection[i], out firstTime), i);

            int iterations = 0;

            //Loop until the overshoot/undershoot quantity is zero
            while (diffQ != 0)
            {
                //check for infinite iterations
                if (iterations >= MAX_ITER)
                    return null;

                int sign = diffQ > 0 ? 1 : -1;

                //Find the tasks that should be moved
                var tasksToMove = newTaskCollection.Where(task => (task.Quantity * sign > 0) && (task.DateRange.StartTime < outOfRangePoint.Key))
                                                                                    .OrderByDescending(task => task.DateRange.StartTime);

                if (!tasksToMove.Any())
                    return null;

                //Loop through found tasks to decrease the overshoot/undershoot quantity
                foreach (Task task in tasksToMove)
                {
                    //Last task cannot be moved
                    if (indexDict[oidGen.GetId(task, out firstTime)] == newTaskCollection.Count - 1)
                        continue;

                    double oldQuantity = task.GetQuantity(outOfRangePoint.Key);
                    Task newTask;

                    DateTime newStartTime;

                    if (task.DateRange.EndTime < moveToTime)
                        newStartTime = moveToTime.AddHours(-1 * task.DateRange.Duration.TotalHours);
                    else
                        if (oldQuantity * sign <= diffQ * sign)
                            newStartTime = outOfRangePoint.Key;
                        else
                            newStartTime = task.DateRange.StartTime.AddHours(diffQ / task.QuantityPerHour);

                    //Do not move task further than the next task of the same type in sequence
                    DateTime maxAllowedStartTime = GetMaxAllowedStartTime(task, newTaskCollection, oidGen, indexDict);
                    if (newStartTime > maxAllowedStartTime)
                        newStartTime = maxAllowedStartTime;

                    if (newStartTime > task.DateRange.StartTime)
                    {
                        newTask = task.ChangeTimes(newStartTime);
                        diffQ -= oldQuantity - newTask.GetQuantity(outOfRangePoint.Key);

                        //Replace the old task with the new one
                        long oldTaskId = oidGen.GetId(task, out firstTime);
                        int index = indexDict[oldTaskId];
                        newTaskCollection[index] = newTask;
                        indexDict.Remove(oldTaskId);
                        indexDict.Add(oidGen.GetId(newTask, out firstTime), index);

                        //If overshoot/undershoot quantity is zero then exit loop
                        if (diffQ * sign <= 0)
                            break;
                    }
                }

                //Recalculate new graph and prepare for next iteration
                dataPoints = newTaskCollection.GetDataPoints(initialValue);

                if (!FindOutOfRangePoint(dataPoints, minLevel, maxLevel, out outOfRangePoint))
                    return newTaskCollection;

                diffQ = CalcQuantityDifference(outOfRangePoint, minLevel, maxLevel);

                if (!FindMoveToTime(dataPoints, outOfRangePoint, minLevel, maxLevel, out moveToTime))
                    return null;

                iterations++;
            }

            return newTaskCollection;
        }
Ejemplo n.º 7
0
        private static DateTime GetMaxAllowedStartTime(ITask task, Collection<ITask> taskCollection, ObjectIDGenerator oidGen, IDictionary<long, int> indexDict)
        {
            bool firstTime;
            int taskIndex = indexDict[oidGen.GetId(task, out firstTime)];

            for (int i = taskIndex + 1; i < taskCollection.Count; i++)
            {
                if (taskCollection[i].TaskType == task.TaskType)
                    return taskCollection[i].DateRange.StartTime;
            }

            return DateTime.MaxValue;
        }
Ejemplo n.º 8
0
        private static void InternalDump(int indentationLevel, string name, object value, TextWriter writer,
            ObjectIDGenerator idGenerator, bool recursiveDump)
        {
            var indentation = new string(' ', indentationLevel*3);

            if (value == null)
            {
                writer.WriteLine("{0}{1} = <null>", indentation, name);
                return;
            }

            Type type = value.GetType();

            // figure out if this is an object that has already been dumped, or is currently being dumped
            string keyRef = string.Empty;
            string keyPrefix = string.Empty;
            if (!type.IsValueType)
            {
                bool firstTime;
                long key = idGenerator.GetId(value, out firstTime);
                if (!firstTime)
                    keyRef = string.Format(CultureInfo.InvariantCulture, " (see #{0})", key);
                else
                {
                    keyPrefix = string.Format(CultureInfo.InvariantCulture, "#{0}: ", key);
                }
            }

            // work out how a simple dump of the value should be done
            bool isString = value is string;
            string typeName = value.GetType().FullName;
            string formattedValue = value.ToString();

            var exception = value as Exception;
            if (exception != null)
            {
                formattedValue = exception.GetType().Name + ": " + exception.Message;
            }

            if (formattedValue == typeName)
                formattedValue = string.Empty;
            else
            {
                // escape tabs and line feeds
                formattedValue = formattedValue.Replace("\t", "\\t").Replace("\n", "\\n").Replace("\r", "\\r");

                // chop at 80 characters
                int length = formattedValue.Length;
                if (length > 80)
                    formattedValue = formattedValue.Substring(0, 80);
                if (isString)
                    formattedValue = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", formattedValue);
                if (length > 80)
                    formattedValue += " (+" + (length - 80) + " chars)";
                formattedValue = " = " + formattedValue;
            }

            writer.WriteLine("{0}{1}{2}{3} [{4}]{5}", indentation, keyPrefix, name, formattedValue, value.GetType(),
                keyRef);

            // Avoid dumping objects we've already dumped, or is already in the process of dumping
            if (keyRef.Length > 0)
                return;

            // don't dump strings, we already got at around 80 characters of those dumped
            if (isString)
                return;

            // don't dump value-types in the System namespace
            if (type.IsValueType && type.FullName == "System." + type.Name)
                return;

            // Avoid certain types that will result in endless recursion
            if (type.FullName == "System.Reflection." + type.Name)
                return;

            if (value is SecurityIdentifier)
                return;

            if (!recursiveDump)
                return;

            PropertyInfo[] properties =
                (from property in
                    type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                    where property.GetIndexParameters().Length == 0
                          && property.CanRead
                    select property).ToArray();
            FieldInfo[] fields =
                type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToArray();

            if (properties.Length == 0 && fields.Length == 0)
                return;

            writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}{{", indentation));
            if (properties.Length > 0)
            {
                writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}   properties {{", indentation));
                foreach (PropertyInfo pi in properties)
                {
                    try
                    {
                        object propertyValue = pi.GetValue(value, null);
                        InternalDump(indentationLevel + 2, pi.Name, propertyValue, writer, idGenerator, true);
                    }
                    catch (TargetInvocationException ex)
                    {
                        InternalDump(indentationLevel + 2, pi.Name, ex, writer, idGenerator, false);
                    }
                    catch (ArgumentException ex)
                    {
                        InternalDump(indentationLevel + 2, pi.Name, ex, writer, idGenerator, false);
                    }
                    catch (RemotingException ex)
                    {
                        InternalDump(indentationLevel + 2, pi.Name, ex, writer, idGenerator, false);
                    }
                }
                writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}   }}", indentation));
            }
            if (fields.Length > 0)
            {
                writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}   fields {{", indentation));
                foreach (FieldInfo field in fields)
                {
                    try
                    {
                        object fieldValue = field.GetValue(value);
                        InternalDump(indentationLevel + 2, field.Name, fieldValue, writer, idGenerator, true);
                    }
                    catch (TargetInvocationException ex)
                    {
                        InternalDump(indentationLevel + 2, field.Name, ex, writer, idGenerator, false);
                    }
                }
                writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}   }}", indentation));
            }
            writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}}}", indentation));
        }