Example #1
0
 private static void SetTimeProvider(IEnumerable <object> c, ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
 {
     foreach (object o in c)
     {
         SetTimeProvider(o, provider, dictionary);
     }
 }
Example #2
0
 /// <summary>
 /// Sets time provider to data consumer and all dependent objects
 /// </summary>
 /// <param name="consumer">Data consumer</param>
 /// <param name="provider">Data provider</param>
 /// <param name="dictionary">Backup dictionary</param>
 private static void SetTimeProvider(IDataConsumer consumer,
                                     ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
 {
     if (consumer is ITimeMeasureConsumer)
     {
         ITimeMeasureConsumer tc = consumer as ITimeMeasureConsumer;
         IMeasurement         timeMeasurement = provider.TimeMeasurement;
         if (!(timeMeasurement is TimeMeasurement))
         {
             throw new Exception();
         }
         Func <object> f = provider.TimeMeasurement.Parameter;
         if (dictionary.ContainsKey(tc))
         {
             if (tc.Time != provider.TimeMeasurement)
             {
                 dictionary[tc] = tc.Time;
                 (tc.Time as TimeMeasurement).TimeParameter = provider.TimeMeasurement.Parameter;
             }
         }
         else
         {
             dictionary[tc] = tc.Time;
             (tc.Time as TimeMeasurement).TimeParameter = provider.TimeMeasurement.Parameter;
         }
     }
 }
Example #3
0
        static void SetTimeProvider(object o, ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
        {
            ITimeMeasureConsumer tc = o.GetLabelObject <ITimeMeasureConsumer>();

            if (tc != null)
            {
                if (dictionary.ContainsKey(tc))
                {
                    if (tc.Time != provider.TimeMeasurement)
                    {
                        dictionary[tc] = tc.Time;
                        tc.Time        = provider.TimeMeasurement;
                    }
                }
                else
                {
                    dictionary[tc] = tc.Time;
                    tc.Time        = provider.TimeMeasurement;
                }
            }
            IChildrenObject co = o.GetLabelObject <IChildrenObject>();

            if (co != null)
            {
                IAssociatedObject[] ch = co.Children;
                if (ch != null)
                {
                    foreach (object ob in ch)
                    {
                        SetTimeProvider(ob, provider, dictionary);
                    }
                }
            }
        }
Example #4
0
 /// <summary>
 /// Construtor
 /// </summary>
 /// <param name="collection">Colletion of objects</param>
 /// <param name="processor">Differential equation proessor</param>
 public StateTransformer(IObjectCollection collection,
                         IDifferentialEquationProcessor processor)
     : this(collection, processor, null)
 {
     provider = this;
     processor.TimeProvider = provider;
 }
Example #5
0
 static void SetTimeProvider(IMeasurements m, ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
 {
     if (m is ITimeMeasureConsumer)
     {
         ITimeMeasureConsumer mc = m as ITimeMeasureConsumer;
         if (dictionary.ContainsKey(mc))
         {
             if (mc.Time != provider.TimeMeasurement)
             {
                 dictionary[mc] = mc.Time;
                 mc.Time        = provider.TimeMeasurement;
             }
         }
         else
         {
             dictionary[mc] = mc.Time;
             mc.Time        = provider.TimeMeasurement;
         }
     }
     if (m is IDataConsumer)
     {
         IDataConsumer dc = m as IDataConsumer;
         SetTimeProvider(dc, provider, dictionary);
     }
     if (m is MeasurementsWrapper)
     {
         MeasurementsWrapper mw = m as MeasurementsWrapper;
         int n = mw.Count;
         for (int i = 0; i < n; i++)
         {
             SetTimeProvider(mw[i], provider, dictionary);
         }
     }
 }
Example #6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="collection">Collection</param>
 /// <param name="provider">Time provider</param>
 /// <param name="priority">Priority</param>
 /// <param name="reason">Reason</param>
 public TimeProviderBackup(IComponentCollection collection, ITimeMeasureProvider provider,
                           int priority, string reason)
 {
     this.collection = collection;
     CreateMeasurements(priority, reason);
     runtime = StaticExtensionDataPerformerPortable.Factory.Create(collection, priority, reason);
     SetTimeProvider(collection, provider, dictionary);
 }
Example #7
0
        private void Update()
        {
            this.ForEach <IStack>(delegate(IStack s)
            {
                s.Push();
            });

            ITimeMeasureProvider old = processor.TimeProvider;

            processor.TimeProvider = this;
            if (block)
            {
                //return;
            }
            block = true;
            using (TimeProviderBackup tb = new TimeProviderBackup(this, this, processor, StaticExtensionDataPerformerInterfaces.Calculation, 0))
            {
                runtime.StartAll(0);
                IDataConsumer th = this;
                double        last;
                if (arg == null)
                {
                    return;
                }
                if (IsUpdated)
                {
                    return;
                }
                double st = arg[0];
                last = st;
                foreach (IStep s in steps)
                {
                    s.Step = -1;
                }
                Action <double, double, long> act =
                    runtime.Step(processor, SetTime, StaticExtensionDataPerformerInterfaces.Calculation);
                for (int i = 0; i < arg.Length; i++)
                {
                    double time = arg[i];
                    act(last, time, i);
                    //th.UpdateChildrenData(); //TEST!!! Comment this string for artificial bug of orbit determination
                    foreach (IMeasurement m in functions.Keys)
                    {
                        object       o = m.Parameter();
                        TimeFunction f = functions[m];
                        f.Set(i, time, o);
                    }
                    last = time;
                }
            }
            this.ForEach((IStack s) =>
            {
                s.Pop();
            });
            processor.TimeProvider = old;
            block = false;
        }
Example #8
0
        private static void SetTimeProvider(IComponentCollection collection,
                                            ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
        {
            IEnumerable <object> c = collection.AllComponents;

            foreach (object o in c)
            {
                SetTimeProvider(o, provider, dictionary);
            }
        }
Example #9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="collection">Collection</param>
 /// <param name="provider">Time provider</param>
 /// <param name="processor">Differential equation processor</param>
 /// <param name="priority">Priority</param>
 /// <param name="reason">Reason</param>
 public TimeProviderBackup(IComponentCollection collection, ITimeMeasureProvider provider,
                           IDifferentialEquationProcessor processor, int priority, string reason)
 {
     this.collection = collection;
     SetCollectionHolders();
     CreateMeasurements(priority, reason);
     runtime = StaticExtensionDataPerformerPortable.Factory.Create(collection, priority);
     SetTimeProvider(collection.AllComponents, provider, dictionary);
     this.processor = processor;
     processor.Set(collection);
 }
Example #10
0
        static void SetTimeProvider(object o, ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
        {
            ITimeMeasureConsumer tc = o.GetLabelObject <ITimeMeasureConsumer>();
            IMeasurement         timeMeasurement = provider.TimeMeasurement;

            if (!(timeMeasurement is TimeMeasurement))
            {
                throw new Exception();
            }
            Func <object> f = timeMeasurement.Parameter;

            if (tc != null)
            {
                TimeMeasurement tmr = tc.Time as TimeMeasurement;
                if (tmr == null)
                {
                    tc.Time = new TimeMeasurement(() => { return((double)0); });
                }
                tmr = tc.Time as TimeMeasurement;
                if (dictionary.ContainsKey(tc))
                {
                    if (tc.Time != provider.TimeMeasurement)
                    {
                        dictionary[tc] = tc.Time;
                        if (tmr != null)
                        {
                            tmr.TimeParameter = f;
                        }
                    }
                }
                else
                {
                    dictionary[tc] = tc.Time;
                    if (tmr != null)
                    {
                        tmr.TimeParameter = f;
                    }
                }
            }
            IChildrenObject co = o.GetLabelObject <IChildrenObject>();

            if (co != null)
            {
                IAssociatedObject[] ch = co.Children;
                if (ch != null)
                {
                    foreach (object ob in ch)
                    {
                        SetTimeProvider(ob, provider, dictionary);
                    }
                }
            }
        }
Example #11
0
 static private void Check(DigraphLoop loop)
 {
     ITimeMeasureProvider[] p = new ITimeMeasureProvider[] { GetProvider(loop[0]), GetProvider(loop[1]) };
     if (p[0] == null | p[1] == null)
     {
         return;
     }
     if (p[0] != p[1])
     {
         throw new Exception(illegal);
     }
 }
Example #12
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)
     : base(collection, reason, priority, dataConsumer, realtimeStep, realtime)
 {
     updateInternal = base.UpdateAll;
     foreach (IPosition p in collection.GetObjectsAndArrows <IPosition>())
     {
         updateInternal = UpdateWithFrames;
         break;
     }
 }
Example #13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="consumer">Data consumer</param>
 /// <param name="provider">Time provider</param>
 /// <param name="processor">Differential equation processor</param>
 /// <param name="reason">Reason</param>
 /// <param name="priority">Priority</param>
 public TimeProviderBackup(IDataConsumer consumer, ITimeMeasureProvider provider, IDifferentialEquationProcessor processor, string reason, int priority)
 {
     this.consumer = consumer;
     collection    = consumer.GetDependentCollection(priority);
     SetTimeProvider(collection, provider, dictionary);
     CreateMeasurements(priority, null);
     runtime        = consumer.CreateRuntime(reason, priority);
     this.processor = processor;
     if (processor != null)
     {
         processor.Set(collection); // !!! added to allow buffer processing, as no IDifferentialEquationProcessoris required there
     }
 }
Example #14
0
        /// <summary>
        /// Start
        /// </summary>
        /// <param name="collection">Components</param>
        /// <param name="timeUnit">Time unit</param>
        /// <param name="isAbsoluteTime">The "is absolute time" sign</param>
        /// <param name="stepAction">Step Action</param>
        /// <param name="dataConsumer">Data Consumer</param>
        /// <param name="log">log</param>
        /// <param name="reason">Reason</param>
        IRealtime IRealtime.Start(IComponentCollection collection,
                                  TimeType timeUnit, bool isAbsoluteTime, IAsynchronousCalculation stepAction,
                                  IDataConsumer dataConsumer, IEventLog log, string reason)
        {
            this.reason = reason;
            StaticExtensionEventPortable.StartRealtime(collection);
            StandardEventRuntime rt = new StandardEventRuntime();

            rt.collection = collection;
            try
            {
                ITimeMeasureProvider realTime = RealtimeProvider.Create(isAbsoluteTime, timeUnit, reason);
                Tuple <IDataConsumer, IComponentCollection, ITimeMeasureProvider, IAsynchronousCalculation> tuple =
                    new Tuple <IDataConsumer, IComponentCollection,
                               ITimeMeasureProvider, IAsynchronousCalculation>
                        (dataConsumer, collection, realTime, stepAction);
                rt.timeProvider = realTime;
                rt.timeBackup   =
                    new DataPerformer.TimeProviderBackup(dataConsumer, realTime, null,
                                                         reason, 0);
                IEvent[] events = (dataConsumer as IEventHandler).Events.EnumerableToArray();
                if (log != null)
                {
                    foreach (IEvent ev in events)
                    {
                        string name = (dataConsumer as
                                       IAssociatedObject).GetRelativeName(ev as IAssociatedObject);
                        ev.ConnectLog(log, name);
                    }
                    collection.ForEach((IEventReader reader) =>
                    {
                        string name = (dataConsumer as
                                       IAssociatedObject).GetRelativeName(reader as IAssociatedObject);
                        reader.ConnectLog(log, name);
                    });
                }
                collection.ForEach((ICalculationReason cr) => { cr.CalculationReason = reason; });
                collection.ForEach((IRealTimeStartStop ss) =>
                {
                    ss.Start();
                });
                rt.eventBackup = new EventBackup(tuple, events, reason);
            }
            catch (Exception exception)
            {
                onError(exception);
                (rt as IRealtime).Stop();
                rt = null;
            }
            return(rt);
        }
Example #15
0
        static private ITimeMeasureProvider GetProvider(DigraphPath path)
        {
            ITimeMeasureProvider p = null;

            for (int i = 0; i < path.Count - 1; i++)
            {
                ICategoryObject ob = path[i].CategoryObject;
                if (ob is ITimeMeasureProvider)
                {
                    p = ob as ITimeMeasureProvider;
                }
            }
            return(p);
        }
Example #16
0
        /// <summary>
        /// Calculates dictionary of sounds
        /// </summary>
        /// <param name="collection">Collection of objects</param>
        /// <returns>Dictionary of sounds</returns>
        Dictionary <double, string> GetSounds(IComponentCollection collection)
        {
            IDesktop desktop = collection as IDesktop;
            Dictionary <double, string> d    = new Dictionary <double, string>();
            SoundCollection             coll = desktop.GetObject(name) as SoundCollection;
            IDataConsumer        dc          = coll;
            ITimeMeasureProvider p           = DataPerformer.Portable.StaticExtensionDataPerformerPortable.Factory.TimeProvider;
            Action <string>      act         = (string s) => { d[p.Time] = s; };

            coll.PlaySound += act;
            dc.PerformFixed(start, step, stepCount, p,
                            DataPerformer.Portable.DifferentialEquationProcessors.DifferentialEquationProcessor.Processor, "Animation", 0, () => { });
            coll.PlaySound -= act;
            return(d);
        }
        public static bool PerformIterator(this IDataConsumer consumer, IIterator iterator,
                                           ITimeMeasureProvider timeProvider, string reason, Func <bool> stop)
        {
            try
            {
                using (TimeProviderBackup backup = new TimeProviderBackup(consumer, timeProvider, null, reason, 0))
                {
                    IDataRuntime runtime = backup.Runtime;
                    IStep        st      = null;
                    IMeasurement time    = timeProvider.TimeMeasurement;
                    if (runtime is IStep)
                    {
                        st      = runtime as IStep;
                        st.Step = 0;
                    }
                    iterator.Reset();
                    double t    = (double)time.Parameter();
                    double last = t;
                    Action <double, double, long> act = runtime.Step(null,
                                                                     (double timer) => { }, reason, null);
                    int i = 0;
                    IEnumerable <object> enu = runtime.AllComponents;

                    while (iterator.Next())
                    {
                        t = (double)time.Parameter();
                        act(last, t, i);
                        ++i;
                        if (st != null)
                        {
                            st.Step = i;
                        }
                        last = t;
                        if (stop())
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
            catch (Exception ex)
            {
                ex.ShowError(10);
            }
            return(true);
        }
Example #18
0
        /// <summary>
        /// Performs ation with array of arguments
        /// </summary>
        /// <param name="consumer">Data consumer</param>
        /// <param name="array">Array of arguments</param>
        /// <param name="collection">Desktop</param>
        /// <param name="provider">Provider of time measure</param>
        /// <param name="processor">Differential equation processor</param>
        /// <param name="priority">Priority</param>
        /// <param name="action">Additional action</param>
        /// <param name="reason">Reason</param>
        static public void PerformArray(this IDataConsumer consumer, Array array, IComponentCollection collection, ITimeMeasureProvider provider,
                                        IDifferentialEquationProcessor processor, int priority, Action action, string reason)
        {
            using (TimeProviderBackup backup = new TimeProviderBackup(collection, provider, processor, priority, reason))
            {
                IDataRuntime         runtime = backup.Runtime;
                ITimeMeasureProvider old     = processor.TimeProvider;
                processor.TimeProvider = provider;
                IStep st = null;
                if (runtime is IStep)
                {
                    st = runtime as IStep;
                }
                int    n    = array.Length;
                double t    = (double)array.GetValue(0);
                double last = t;
                Action <double, double, long> act =
                    runtime.Step(processor, (double time) =>
                                 { provider.Time = time; }, reason);
                for (int i = 0; i < n; i++)
                {
                    t = (double)array.GetValue(i);
                    act(last, t, (long)i);

                    /* last = t;
                     * if (i == 0)
                     * {
                     *   runtime.StartAll((double)array.GetValue(0));
                     * }
                     * if (st != null)
                     * {
                     *   st.Step = i;
                     * }
                     * runtime.UpdateAll();
                     * provider.Time = (double)array.GetValue(i);
                     * if (i > 0 & processor != null)
                     * {
                     *   processor.Step((double)array.GetValue(i - 1), (double)array.GetValue(i));
                     * }
                     * provider.Time = (double)array.GetValue(i);*/
                    action();
                    //collection.ResetUpdatedMeasurements();
                }
                processor.TimeProvider = old;
            }
        }
Example #19
0
 /// <summary>
 /// Performs action with fixed step
 /// </summary>
 /// <param name="consumer">Data consumer</param>
 /// <param name="start">Start</param>
 /// <param name="step">Step</param>
 /// <param name="count">Count of steps</param>
 /// <param name="provider">Provider of time measure</param>
 /// <param name="processor">Differential equation processor</param>
 /// <param name="reason">Reason</param>
 /// <param name="priority">Priority</param>
 /// <param name="action">Additional action</param>
 /// <param name="errorHandler">Error handler</param>
 /// <param name="asynchronousCalculation">Asynchronous calculation</param>
 static public void PerformFixed(this IDataConsumer consumer, double start, double step, int count,
                                 ITimeMeasureProvider provider,
                                 IDifferentialEquationProcessor processor, string reason,
                                 int priority, Action action, IAsynchronousCalculation asynchronousCalculation = null,
                                 IErrorHandler errorHandler = null)
 {
     try
     {
         using (TimeProviderBackup backup = new TimeProviderBackup(consumer, provider, processor, reason, priority))
         {
             IDataRuntime         runtime = backup.Runtime;
             ITimeMeasureProvider old     = processor.TimeProvider;
             processor.TimeProvider = provider;
             IStep st = null;
             if (runtime is IStep)
             {
                 st = runtime as IStep;
             }
             provider.Time = start;
             double t    = start;
             double last = t;
             Action <double, double, long> act = runtime.Step(processor,
                                                              (double time) => { provider.Time = time; }, reason, asynchronousCalculation);
             for (int i = 0; i < count; i++)
             {
                 t = start + i * step;
                 act(last, t, i);
                 last = t;
                 action();
             }
             processor.TimeProvider = old;
         }
     }
     catch (Exception ex)
     {
         if (errorHandler != null)
         {
             errorHandler.ShowError(ex, 10);
         }
         else
         {
             ex.ShowError(10);
         }
     }
 }
Example #20
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="consumer">Data consumer</param>
        /// <param name="provider">Time provider</param>
        /// <param name="processor">Differential equation processor</param>
        /// <param name="reason">Reason</param>
        /// <param name="priority">Priority</param>
        public TimeProviderBackup(IDataConsumer consumer, ITimeMeasureProvider provider,
                                  IDifferentialEquationProcessor processor, string reason, int priority)
        {
            this.consumer = consumer;
            collection    = consumer.GetDependentCollection(priority);
            SetCollectionHolders();
            List <object> l = new List <object>(collection.AllComponents);

            if (!l.Contains(consumer))
            {
                l.Add(consumer);
            }
            SetTimeProvider(l, provider, dictionary);
            CreateMeasurements(priority, null);
            runtime = consumer.CreateRuntime(reason, priority);
            if (processor != null)
            {
                this.processor = processor;
                processor.Set(collection);
            }
        }
Example #21
0
 static void SetTimeProvider(IChildrenObject co, ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
 {
     IAssociatedObject[] ao = co.Children;
     foreach (object o in ao)
     {
         if (o is IDataConsumer)
         {
             IDataConsumer dc = o as IDataConsumer;
             SetTimeProvider(dc, provider, dictionary);
         }
         else if (o is IMeasurements)
         {
             IMeasurements mea = o as IMeasurements;
             SetTimeProvider(mea, provider, dictionary);
         }
         if (o is IChildrenObject)
         {
             IChildrenObject cho = o as IChildrenObject;
             SetTimeProvider(cho, provider, dictionary);
         }
     }
 }
Example #22
0
 /// <summary>
 /// Sets time provider to data consumer and all dependent objects
 /// </summary>
 /// <param name="consumer">Data consumer</param>
 /// <param name="provider">Data provider</param>
 /// <param name="dictionary">Backup dictionary</param>
 private static void SetTimeProvider(IDataConsumer consumer,
                                     ITimeMeasureProvider provider, IDictionary <ITimeMeasureConsumer, IMeasurement> dictionary)
 {
     if (consumer is ITimeMeasureConsumer)
     {
         ITimeMeasureConsumer tc = consumer as ITimeMeasureConsumer;
         if (dictionary.ContainsKey(tc))
         {
             if (tc.Time != provider.TimeMeasurement)
             {
                 dictionary[tc] = tc.Time;
                 tc.Time        = provider.TimeMeasurement;
             }
         }
         else
         {
             dictionary[tc] = tc.Time;
             tc.Time        = provider.TimeMeasurement;
         }
     }
     // IDataRuntime dr = consumer.CreateRuntime();
 }
Example #23
0
        /// <summary>
        /// Calculation
        /// </summary>
        /// <param name="input">Input</param>
        /// <param name="output">Output</param>
        public override void Calculate(object[] input, object[] output)
        {
            ITimeMeasureProvider old = processor.TimeProvider;

            try
            {
                using (new TimeProviderBackup(collection, provider, DifferentialEquationProcessor.Processor, 0, null))
                {
                    using (new ComponentCollectionBackup(collection))
                    {
                        processor.TimeProvider = provider;

                        // Input
                        double[] inp = input[0] as double[];

                        // Sets state vector
                        collection.SetStateVector(inp);
                        double start = provider.Time;
                        double step  = provider.Step;
                        runtime.StartAll(start);
                        runtime.UpdateAll();

                        // Solution of differential equations
                        processor.Step(start, start + step);
                        provider.Time = start + step;
                        collection.GetStateVector(outbuffer);

                        // Sets final vector
                        output[0] = outbuffer;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ShowError(10);
            }
            processor.TimeProvider = old;
        }
Example #24
0
 /// <summary>
 /// Performs action with fixed step
 /// </summary>
 /// <param name="collection">Desktop</param>
 /// <param name="start">Start</param>
 /// <param name="step">Step</param>
 /// <param name="count">Count of steps</param>
 /// <param name="provider">Provider of time measure</param>
 /// <param name="processor">Differential equation processor</param>
 /// <param name="priority">Priority</param>
 /// <param name="action">Additional action</param>
 /// <param name="reason">Reason</param>
 static public void PerformFixed(this IComponentCollection collection, double start, double step, int count, ITimeMeasureProvider provider,
                                 IDifferentialEquationProcessor processor, int priority, Action action, string reason)
 {
     using (TimeProviderBackup backup = new TimeProviderBackup(collection, provider, processor, priority, reason))
     {
         List <IMeasurements> measurements = backup.Measurements;
         IDataRuntime         runtime      = backup.Runtime;
         ITimeMeasureProvider old          = processor.TimeProvider;
         processor.TimeProvider = provider;
         Action <double, double, long> act = runtime.Step(processor,
                                                          (double time) => { provider.Time = time; }, reason);
         double last = start;
         double t    = start;
         for (int i = 0; i < count; i++)
         {
             t = start + i * step;
             act(last, t, (long)i);
             last = t;
             action();
         }
         processor.TimeProvider = old;
     }
 }
Example #25
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); });
                }
            }
        }
Example #26
0
        /// <summary>
        /// Creates action
        /// </summary>
        /// <param name="runtime">Runtime</param>
        /// <param name="time">Time</param>
        /// <param name="processor">processor</param>
        /// <param name="action">Action</param>
        /// <param name="update">Update objects</param>
        /// <returns>Action</returns>
        protected virtual Action CreateAction(IDataRuntime runtime, double[] time,
                                              IDifferentialEquationProcessor processor, Action action, IRealtimeUpdate[] update)
        {
            ITimeMeasureProvider rt = realtime;

            double[]        t                 = new double[1];
            Action <double> setTime           = (double a) => { };
            Action <double, double, long> act = runtime.Step(processor, setTime,
                                                             StaticExtensionEventInterfaces.Realtime, null);

            long[] st = new long[] { 0 };

            Action updList = null;

            foreach (IRealtimeUpdate upd in update)
            {
                Action actp = upd.Update;
                if (actp == null)
                {
                    continue;
                }
                if (act != null)
                {
                    if (updList == null)
                    {
                        updList = actp;
                        continue;
                    }
                    updList = updList + actp;
                }
            }
            if (action != null)
            {
                updList = updList + action;
            }
            if (updList == null)
            {
                updList = action;
            }
            if (updList == null)
            {
                updList = () => { };
            }

            //  Action[] updateActions = updlist.ToArray();
            object loc = new object();

            if (realtimeStep == null)
            {
                return(() =>
                {
                    // !!! REPLACED WITH GLOBAL LOCKER  lock(locker)
                    // {
                    realtime.Update();
                    t[0] = rt.Time;
                    if (time[0] == t[0])
                    {
                        return;
                    }
                    act(time[0], t[0], st[0]);
                    time[0] = t[0];
                    st[0] += 1;
                    updList();
                    //}
                });
            }
            else
            {
                Action <double> acti;

                acti = (double arg) =>
                {
                    realtimeStep.Start(arg);
                    acti = realtimeStep.Step;
                };


                return(() =>
                {
                    lock (realtime)
                    {
                        realtime.Update();
                        t[0] = rt.Time;
                        act(time[0], t[0], st[0]);
                        time[0] = t[0];
                        st[0] += 1;
                        updList();
                        acti(t[0]);
                    }
                });
            }
        }
Example #27
0
 /// <summary>
 /// Construtor
 /// </summary>
 /// <param name="collection">Colletion of objects</param>
 /// <param name="processor">Differential equation proessor</param>
 /// <param name="provider">Provider of time</param>
 protected StateTransformer(IObjectCollection collection, IDifferentialEquationProcessor processor,
                            ITimeMeasureProvider provider) : base(collection)
 {
     this.processor = processor;
     this.provider  = provider;
 }