void PerformExternal(IBufferData data) { string reason = StaticExtensionDataPerformerInterfaces.Calculation; IEnumerable <object> en = PureEnumerable(data); IEnumerator <object> enumerator = en.GetEnumerator(); using (TimeProviderBackup backup = new TimeProviderBackup(externalComponentCollection, this, 0, reason)) { enumerator.MoveNext(); IDataRuntime runtime = backup.Runtime; IStep st = null; if (runtime is IStep) { st = runtime as IStep; st.Step = 0; } externalComponentCollection.ForEach((IStarted started) => { started.Start(time); }); double last = time; Action <double, double, long> act = runtime.Step(null, (double timer) => { }, reason, null); int i = 0; while (enumerator.MoveNext()) { act(last, time, i); ++i; if (st != null) { st.Step = i; } last = time; } } }
/// <summary> /// Construtor /// </summary> /// <param name="collection">Collection</param> protected AbstractDoubleTransformer(IObjectCollection collection) { runtime = StaticExtensionDataPerformerPortable.Factory.Create(collection, 0); this.collection = collection; SetEvents(); type = new ArrayReturnType(a, new int[] { 1 }, true); }
/// <summary> /// Sets consumers /// </summary> /// <param name="collection">Consumers</param> /// <returns>Lists of parameters</returns> public virtual void Set(object collection) { IComponentCollection cc = collection as IComponentCollection; IDataRuntime rt = StaticExtensionDataPerformerPortable.Factory.Create(cc, 0); Clear(); variablesStr.Clear(); /* IEnumerable<IDataConsumer> consumers = cc.GetAll<IDataConsumer>(); * foreach (IDataConsumer c in consumers) * { * c.GetMeasurements(measurements); * }*/ List <object> l = new List <object>(); cc.ForEach <IDifferentialEquationSolver>((IDifferentialEquationSolver solver) => { if (solver is IMeasurements) { if (!l.Contains(solver)) { l.Insert(0, solver); } } if (solver is IDataConsumer) { (solver as IDataConsumer).GetDependentObjects(l); } } ); foreach (object o in l) { if (o is IMeasurements) { measurements.Add(o as IMeasurements); } } foreach (IMeasurements m in measurements) { IDataRuntimeFactory s = StaticExtensionDataPerformerPortable.Factory; if (m is IDifferentialEquationSolver) { IDifferentialEquationSolver ds = m as IDifferentialEquationSolver; Add(ds); } else if (s != null) { IDifferentialEquationSolver ds = rt.GetDifferentialEquationSolver(m); if (ds != null) { Add(ds); } } if (m is INormalizable) { Add(m as INormalizable); } } measurements.SortMeasurements(); }
/// <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); }
/// <summary> /// Creates runtime /// </summary> protected void CreateRuntime() { Portable.Interfaces.IDataRuntimeFactory factory = StaticExtensionDataPerformerPortable.Factory; // Diagram.UI.Interfaces.IComponentCollection collection = // factory.CreateCollection(this, 0, StaticExtensionDataPerformerInterfaces.Calculation); // runtime = factory.Create(collection, 0, StaticExtensionDataPerformerInterfaces.Calculation); runtime = this.CreateRuntime(StaticExtensionDataPerformerInterfaces.Calculation); }
/// <summary> /// Prepares itself /// </summary> protected virtual void Prepare() { runtime = StaticExtensionDataPerformerPortable.Factory.Create(collection, 0); List <string[]> l = collection.GetDoubleVariables(); type = new ArrayReturnType(a, new int[] { l.Count }, false); outbuffer = new double[l.Count]; }
void IDataConsumer.Add(IMeasurements measurements) { this.measurements.Add(measurements); // this.measurements.GetDependent(l, dependent); if (!isSerialized) { runtime = this.CreateRuntime(StaticExtensionDataPerformerInterfaces.Calculation); } }
/// <summary> /// Constructor /// </summary> /// <param name="collection">Collection</param> /// <param name="priority">Priority</param> /// <param name="reason">Reason</param> public TimeProviderBackup(IComponentCollection collection, int priority, string reason) { this.collection = collection; CreateMeasurements(priority, reason); runtime = StaticExtensionDataPerformerPortable.Factory.Create(collection, priority); SetTimeProvider(collection, StaticExtensionDataPerformerPortable.Factory.TimeProvider, dictionary); processor = DifferentialEquationProcessors.DifferentialEquationProcessor.Processor; processor.Set(collection); }
/// <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); }
/// <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 } }
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); }
/// <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; } }
/// <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); } } }
IEnumerable <byte[]> Transform(IIterator iterator, Func <bool> stop) { string reason = StaticExtensionEventInterfaces.RealtimeLogAnalysis; using (TimeProviderBackup backup = new TimeProviderBackup(consumer, iterator as ITimeMeasureProvider, null, reason, 0)) { IDataRuntime runtime = backup.Runtime; IStep st = null; IMeasurement time = (iterator as ITimeMeasureProvider).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; while (iterator.Next()) //!!! Unifinished? { t = (double)time.Parameter(); act(last, t, i); ++i; DateTime dt = new DateTime(); dt += TimeSpan.FromSeconds(t); object tuple = new Tuple <DateTime, Dictionary <string, object> >(dt, measurementsWrite.WriteParameters()); yield return(objectToBytes(tuple)); if (stop()) { break; } } } }
/// <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); } }
/// <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; } }
/// <summary> /// Initialaztion /// </summary> public void Init() { selection.Selections = selectedSelections; method = new StructuredGLM(selection, this); runtime = this.CreateRuntime(StaticExtensionDataPerformerInterfaces.Calculation); }
/// <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]); } }); } }
/// <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); }); } } }
/// <summary> /// Sets positions /// </summary> protected void SetPositions() { positions.Clear(); if (measures.Count < 3) { return; } measuresData.Clear(); IDataConsumer c = this; foreach (string ms in measures) { for (int i = 0; i < c.Count; i++) { IMeasurements m = c[i]; IAssociatedObject ao = m as IAssociatedObject; string on = this.GetRelativeName(ao) + "."; for (int j = 0; j < m.Count; j++) { IMeasurement mea = m[j]; string s = on + mea.Name; if (s.Equals(ms)) { measuresData.Add(mea); } } } } List <IIterator> iterators = new List <IIterator>(); c.GetIterators(iterators); foreach (IIterator it in iterators) { it.Reset(); } IDataConsumer consumer = this; IDataRuntime rt = consumer.CreateRuntime(StaticExtensionDataPerformerInterfaces.Calculation); while (true) { rt.UpdateAll(); object[] ob = new object[measuresData.Count]; for (int i = 0; i < measuresData.Count; i++) { object o = measuresData[i].Parameter(); if (o is DBNull | o == null) { goto iterate; } ob[i] = o; } IPosition position = factory.Create(ob); if (position != null) { positions.Add(position); } iterate: foreach (IIterator it in iterators) { if (!it.Next()) { goto fin; } } } fin: Parent = parent; return; }