Exemplo n.º 1
0
 /// <summary>
 /// Enable runtime
 /// </summary>
 /// <param name="control">Control</param>
 /// <param name="enable">Enable</param>
 /// <param name="calculationReason">Calculation reason</param>
 /// <param name="list">List of updates</param>
 public static void EnableRuntime(this Control control, bool enable, string calculationReason = null,
                                  List <IRealtimeUpdate> list = null)
 {
     foreach (Control c in control.Controls)
     {
         c.EnableRuntime(enable, calculationReason, list);
     }
     if (control is IShowForm)
     {
         Control f = (control as IShowForm).Form as Control;
         if (f != null)
         {
             if (!f.IsDisposed)
             {
                 f.EnableRuntime(enable, calculationReason, list);
             }
         }
     }
     if (calculationReason != null)
     {
         if (control is ICalculationReason)
         {
             (control as ICalculationReason).CalculationReason = calculationReason;
         }
     }
     if (control is IRealTimeStartStop)
     {
         IRealTimeStartStop rt = control as IRealTimeStartStop;
         if (enable)
         {
             control.InvokeIfNeeded(() => rt.Start());
         }
         else
         {
             rt.Stop();
         }
     }
     if (list != null)
     {
         if (control is IRealtimeUpdate)
         {
             IRealtimeUpdate up = control as IRealtimeUpdate;
             if (!list.Contains(up))
             {
                 list.Add(up);
             }
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="collection">Collection of components</param>
        /// <param name="reason">Reason</param>
        /// <param name="priority">Priority</param>
        /// <param name="dataConsumer">Data consumer</param>
        /// <param name="realtimeStep">Realtime step</param>
        /// <param name="realtime">Realtime provider</param>
        public DataRuntime(IComponentCollection collection, string reason, int priority,
                           IDataConsumer dataConsumer            = null,
                           IAsynchronousCalculation realtimeStep = null,
                           ITimeMeasureProvider realtime         = null)
        {
            this.collection   = collection;
            this.priority     = priority;
            this.reason       = reason;
            this.realtimeStep = realtimeStep;
            Prepare();
            if (realtime != null & priority == 0)
            {
                if (reason == StaticExtensionEventInterfaces.Realtime |
                    reason.IsRealtimeAnalysis())
                {
                    List <IRealtimeUpdate> lr = new List <IRealtimeUpdate>();
                    realTimeData =
                        new Dictionary <IComponentCollection, Tuple <IDataRuntime, double[],
                                                                     IDifferentialEquationProcessor, Action> >();
                    this.realtime = realtime as RealtimeProvider;
                    provider      = realtime;
                    dCollection   = new Dictionary <object, IComponentCollection>();

                    /*!!!! * DECOMPOSITION THINK AFTER
                     * collections = collection.Decompose(dCollection);
                     */
                    //========== REPLACED========
                    collections = new List <IComponentCollection>()
                    {
                        collection
                    };
                    collection.ForEach((IEvent ev) => { dCollection[ev] = collection; });
                    //===========================
                    IEventLog log = null;
                    if (reason.Equals(StaticExtensionEventInterfaces.Realtime))
                    {
                        log = StaticExtensionEventInterfaces.NewLog;
                    }
                    Action logupd = null;
                    if (log != null)
                    {
                        Dictionary <IMeasurement, string> namem = new Dictionary <IMeasurement, string>();
                        List <IMeasurement> lim = new List <IMeasurement>();
                        collection.ForEach((IMeasurements mm) =>
                        {
                            string name = (dataConsumer).GetMeasurementsName(mm) + ".";
                            for (int i = 0; i < mm.Count; i++)
                            {
                                IMeasurement mea = mm[i];
                                if (mea is IReplacedMeasurementParameter)
                                {
                                    lim.Add(mea);
                                    namem[mea] = name + mea.Name;
                                }
                            }
                        });
                        if (lim.Count > 0)
                        {
                            if (reason == "Realtime")
                            {
                                logupd = () =>
                                {
                                    logDictionary = new Dictionary <string, object>();
                                    foreach (IMeasurement mea in namem.Keys)
                                    {
                                        string n = namem[mea];
                                        logDictionary[n] = mea.Parameter();
                                    }
                                    log.Write(logDictionary, DateTime.Now);
                                };
                            }
                        }
                    }

                    if (collections.Count == 1)
                    {
                        IDifferentialEquationProcessor pr = CreateProcessor(collection);
                        double[] dt   = new double[1];
                        bool     find = (reason.Equals(StaticExtensionEventInterfaces.Realtime) |
                                         reason.Equals(StaticExtensionEventInterfaces.RealtimeLogAnalysis));
                        collection.ForEach((IRealtimeUpdate ru) => { lr.Add(ru); }, find);
                        collection.ForEach((IEvent ev) =>
                        {
                            IComponentCollection cc = CreateCollection(ev);
                            if (!realTimeData.ContainsKey(cc))
                            {
                                List <IRealtimeUpdate> lru = new List <IRealtimeUpdate>();
                                cc.ForEach((IRealtimeUpdate rud) => { lru.Add(rud); }, find);
                                IDataRuntime drc = Copy(cc);
                                double[] dtt     = new double[1];
                                Action actp      = CreateAction(drc, dtt, pr, logupd, lru.ToArray());
                                realTimeData[cc] =
                                    new Tuple <IDataRuntime, double[], IDifferentialEquationProcessor, Action>
                                        (drc, dtt, pr, actp);
                            }
                        });
                    }
                    else
                    {
                        foreach (IComponentCollection coll in collections)
                        {
                            List <IRealtimeUpdate> lu = new List <IRealtimeUpdate>();
                            foreach (object o in coll.AllComponents)
                            {
                                if (o is IObjectLabel)
                                {
                                    IRealtimeUpdate ru = (o as IObjectLabel).Object.GetObject <IRealtimeUpdate>();
                                    if (ru != null)
                                    {
                                        lu.Add(ru);
                                    }
                                }
                            }
                            IDataRuntime rt = Copy(coll);
                            IDifferentialEquationProcessor pr = CreateProcessor(collection);
                            double[] dt  = new double[1];
                            Action   act = CreateAction(rt, dt, pr, null, lr.ToArray());
                            realTimeData[coll] = new Tuple <IDataRuntime, double[], IDifferentialEquationProcessor, Action>
                                                     (rt, dt, pr, act);
                        }
                    }
                    (realtime as RealtimeProvider).Update();
                    startRuntime = provider.Time;
                    foreach (Tuple <IDataRuntime, double[], IDifferentialEquationProcessor, Action> t in realTimeData.Values)
                    {
                        t.Item2[0] = startRuntime;
                    }
                    collection.ForEach <IStarted>((IStarted st) => { st.Start(startRuntime); });
                }
            }
        }