/// <summary> /// Adds a link /// </summary> /// <param name="NewLink">The link</param> public virtual void AddLink (ILink NewLink) { if (NewLink.SourceComponent==this) { for (int iNewDO = 0; iNewDO < NewLink.DataOperationsCount; iNewDO++) { IDataOperation newDataOperation = NewLink.GetDataOperation(iNewDO); foreach (ILink link in _providingLinks) { for (int iExistingDO = 0; iExistingDO < link.DataOperationsCount; iExistingDO++) { IDataOperation existingDataOperation = link.GetDataOperation(iExistingDO); if (newDataOperation == existingDataOperation) { Event warning = new Event(EventType.Warning); warning.Description = "DataOperation " + newDataOperation.ID + " has already been used. " + "It's argument values will overrule the values set previously for this operation."; warning.Sender = this; SendEvent(warning); } } } } _providingLinks.Add(NewLink); } if (NewLink.TargetComponent==this) { _acceptingLinks.Add(NewLink); } }
static CompositionManager() { _simulationFinishedEvent = new Event(EventType.GlobalProgress); _simulationFinishedEvent.Description = "Simulation finished successfuly..."; _simulationFailedEvent = new Event(EventType.GlobalProgress); _simulationFailedEvent.Description = "Simulation FAILED..."; }
private void SendEvent(EventType eventType) { Oatc.OpenMI.Sdk.Backbone.Event eventD = new Oatc.OpenMI.Sdk.Backbone.Event(eventType); eventD.Description = eventType.ToString(); eventD.Sender = this; eventD.SimulationTime = TimeToTimeStamp(_engineApiAccess.GetCurrentTime()); SendEvent(eventD); }
static CompositionManager() { _simulationFinishedEvent = new Event(EventType.GlobalProgress); _simulationFinishedEvent.Description = "Simulation Complete"; _simulationFailedEvent = new Event(EventType.GlobalProgress); _simulationFailedEvent.Description = "Simulation Failed"; }
public void Init() { e = new Event(EventType.Informative); e.Description = "Description"; e.SimulationTime = new TimeStamp(44060.0); e.SetAttribute("key1","value1"); e.SetAttribute("key2","value2"); e.SetAttribute("key3","value3"); }
private void SendSourceAfterGetValuesCallEvent(ITime time, string LinkID) { Oatc.OpenMI.Sdk.Backbone.Event eventA = new Oatc.OpenMI.Sdk.Backbone.Event(EventType.SourceAfterGetValuesCall); eventA.Description = "GetValues(t = " + ITimeToString(time) + ", "; eventA.Description += "LinkID: " + LinkID; //TODO: QS = " + _smartOutputLinkSet.GetLink(LinkID).SourceQuantity.ID + " ,QT = " + _smartOutputLinkSet.GetLink(LinkID).TargetQuantity.ID; eventA.Description += ") <<<==="; eventA.Sender = this; eventA.SimulationTime = TimeToTimeStamp(_engineApiAccess.GetCurrentTime()); eventA.SetAttribute("GetValues time argument : ", ITimeToString(time)); SendEvent(eventA); }
public void EqualsAttribute() { Event e1 = new Event(EventType.Informative); e1.Description = "Description"; e1.SimulationTime = new TimeStamp(44060.0); e1.SetAttribute("key1","value1"); e1.SetAttribute("key2","value2"); Assert.IsFalse(e1.Equals(e)); e1.SetAttribute("key3","value4"); Assert.IsFalse(e1.Equals(e)); }
public void RunParallel(ITime triggerTime, ParallelizeMode mode) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Parallel execution feature enabled, with " + mode + " mode."; _runListener.OnEvent(theEvent); switch (mode) { case ParallelizeMode.Standard: runStandardParallel(triggerTime); break; case ParallelizeMode.Aggressive: runAggressiveParallel(triggerTime); break; } }
/// <summary> /// Send event /// </summary> /// <param name="eventType">the event type to send</param> /// <param name="sender">reference to the sender (this)</param> public void SendEvent(EventType eventType, ILinkableComponent sender) { if (((Oatc.OpenMI.Sdk.Backbone.LinkableComponent) this.link.TargetComponent).HasListeners()) { Oatc.OpenMI.Sdk.Backbone.Event eventD = new Oatc.OpenMI.Sdk.Backbone.Event(eventType); eventD.Description = eventType.ToString(); eventD.Sender = sender; ITime t = this._engine.GetCurrentTime(); if (t is ITimeStamp) { eventD.SimulationTime = t as ITimeStamp; } else { eventD.SimulationTime = ((ITimeSpan)this._engine.GetCurrentTime()).End; } this.link.TargetComponent.SendEvent(eventD); } }
/// <summary> /// Send event /// </summary> /// <param name="eventType">the event type to send</param> /// <param name="sender">reference to the sender (this)</param> public void SendEvent( EventType eventType, ILinkableComponent sender) { if (((Oatc.OpenMI.Sdk.Backbone.LinkableComponent)this.link.TargetComponent).HasListeners()) { Oatc.OpenMI.Sdk.Backbone.Event eventD = new Oatc.OpenMI.Sdk.Backbone.Event(eventType); eventD.Description = eventType.ToString(); eventD.Sender = sender; ITime t = this._engine.GetCurrentTime(); if (t is ITimeStamp) { eventD.SimulationTime = t as ITimeStamp; } else { eventD.SimulationTime = ((ITimeSpan)this._engine.GetCurrentTime()).End; } this.link.TargetComponent.SendEvent(eventD); } }
public void RunSequence(ITime triggerTime) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Parallel execution feature disabled."; _runListener.OnEvent(theEvent); /* try find edge model and trigger them first, is possible */ int edgeModel = 0; foreach (Model uimodel in _models) { if (!isModelEngineComponent(uimodel.LinkableComponent)) continue; int outDegree = 0; LinkableRunEngine runEngine = (LinkableRunEngine)uimodel.LinkableComponent; foreach (ILink link in runEngine.GetProvidingLinks()) { if (isModelEngineComponent(link.TargetComponent)) outDegree++; } theEvent = new Event(EventType.Informative); theEvent.Description = "Out degree of " + uimodel.ModelID + " is " + outDegree + "."; _runListener.OnEvent(theEvent); if (outDegree == 0) { runEngine.RunToTime(triggerTime, -1); edgeModel++; } } /* no edge model was found, perhaps the composition is combined with circles */ if (edgeModel == 0) { theEvent = new Event(EventType.Informative); theEvent.Description = "No edge model found, try invoke all of them."; _runListener.OnEvent(theEvent); foreach (Model uimodel in _models) { if (!isModelEngineComponent(uimodel.LinkableComponent)) continue; LinkableRunEngine runEngine = (LinkableRunEngine)uimodel.LinkableComponent; runEngine.RunToTime(triggerTime, -1); } } }
/// <summary> /// This method is called in <see cref="Run">Run</see> method. /// </summary> private void RunThreadFunction() { Trigger trigger = GetTrigger(); Debug.Assert(trigger != null); Thread.Sleep(0); try { // run it !!! trigger.Run(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(TriggerInvokeTime))); // close models down if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Closing models down"; _runListener.OnEvent(theEvent); } foreach (UIModel uimodel in _models) { if (_runListener != null) { string ModelID = uimodel.ModelID; Event theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Finish() on model " + ModelID; _runListener.OnEvent(theEvent); } uimodel.LinkableComponent.Finish(); } // thread finishes - send well known event if (_runListener != null) { _simulationFinishedEvent.Description = "Simulation Complete (" + DateTime.Now.ToString() + ")"; _runListener.OnEvent(SimulationFinishedEvent); } } catch (Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured during simulation: " + e.ToString(); _runListener.OnEvent(theEvent); _simulationFailedEvent.Description = "Simulation Failed (" + DateTime.Now.ToString() + ")"; _runListener.OnEvent(SimulationFailedEvent); } } finally { _running = false; _runListener = null; // release listener } }
/// <summary> /// This method is called in <see cref="Run">Run</see> method. /// </summary> private void RunThreadFunction() { bool succeed = false; foreach (Model uimodel in _models) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Prepare() method of model " + uimodel.ModelID; _runListener.OnEvent(theEvent); } uimodel.LinkableComponent.Prepare(); } //Trigger trigger = GetTrigger(); //Debug.Assert(trigger != null); //WHAT THE HELL IS THIS FOR? Thread.Sleep(0); try { // run it !!! // trigger.Run(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(TriggerInvokeTime))); ITime triggerTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(TriggerInvokeTime)); CompositionRunner runner = new CompositionRunner(_models, _runListener); if (Parallelized) runner.RunParallel(triggerTime, ParallelizeMode); else runner.RunSequence(triggerTime); // close models down if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Closing models down..."; _runListener.OnEvent(theEvent); } foreach (Model uimodel in _models) { if (_runListener != null) { string ModelID = uimodel.ModelID; Event theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Finish() on model " + ModelID; _runListener.OnEvent(theEvent); } uimodel.LinkableComponent.Finish(); } // thread finishes - send well known event if (_runListener != null) { _simulationFinishedEvent.Description = "Simulation finished successfuly at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFinishedEvent); } succeed = true; } catch (Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured during simulation: " + e.ToString(); _runListener.OnEvent(theEvent); _simulationFailedEvent.Description = "Simulation FAILED at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFailedEvent); } } finally { endTime = DateTime.Now; var ev = new Event { Description = "Elapsed time: " + (endTime - startTime), Type = EventType.GlobalProgress }; _runListener.OnEvent(ev); _running = false; _runListener = null; // release listener } if (AfterSimulationHandler != null) { AfterSimulationHandler(this, succeed); } }
private void scheduleAggressive(ITime triggerTime) { List<List<Model>> orders; lock (this) { orders = getTopologicalOrder(_queueModels); } if (orders.Count > 0) { List<Model> tier = orders[0]; foreach (Model model in tier) { if (!isModelEngineComponent(model.LinkableComponent)) continue; lock (this) { if (_runningModels.Contains(model)) continue; _runningModels.Add(model); } Event theEvent = new Event(EventType.Informative); theEvent.Description = "Triggering model: " + model.ModelID; _runListener.OnEvent(theEvent); LinkableRunEngine runEngine = (LinkableRunEngine)model.LinkableComponent; new Thread(new ThreadStart(delegate() { runEngine.RunToTime(triggerTime, -1); lock (this) { _runningModels.Remove(model); _queueModels.Remove(model); } _s.Release(); })).Start(); } } }
/// <summary> /// Runs simulation. /// </summary> /// <param name="runListener">Simulation listener.</param> /// <param name="runInSameThread">If <c>true</c>, simulation is run in same thread like caller, /// ie. method blocks until simulation don't finish. If <c>false</c>, simulation is /// run in separate thread and method returns immediately.</param> /// <remarks> /// Simulation is run the way that trigger invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see> /// method of the model it's connected to /// at the time specified by <see cref="TriggerInvokeTime">TriggerInvokeTime</see> property. /// If you need to use more than one listener you can use <see cref="ProxyListener">ProxyListener</see> /// class or <see cref="ProxyMultiThreadListener">ProxyMultiThreadListener</see> if <c>runInSameThread</c> is <c>false</c>. /// </remarks> public void Run(IListener runListener, bool runInSameThread) { if (!HasTrigger()) throw (new Exception("Composition has no trigger.")); if (_running) throw (new Exception("Simulation is already running.")); _running = true; _runListener = runListener; try { TimeStamp runToTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(_triggerInvokeTime)); // Create informative message if (_runListener != null) { StringBuilder description = new StringBuilder(); description.Append("Starting simulation at "); description.Append(DateTime.Now.ToString()); description.Append(","); description.Append(" composition consists from following models:\n"); foreach (UIModel model in _models) { description.Append(model.ModelID); description.Append(", "); } // todo: add more info? Event theEvent = new Event(EventType.Informative); theEvent.Description = description.ToString(); _runListener.OnEvent(theEvent); } _runPrepareForComputationStarted = true; // prepare for computation if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Preparing for computation"; _runListener.OnEvent(theEvent); } foreach (UIModel uimodel in _models) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Prepare() method of model " + uimodel.ModelID; _runListener.OnEvent(theEvent); } uimodel.LinkableComponent.Prepare(); } // subscribing event listener to all models if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Subscribing proxy event listener"; _runListener.OnEvent(theEvent); for (int i = 0; i < _runListener.GetAcceptedEventTypeCount(); i++) foreach (UIModel uimodel in _models) { theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Subscribe() method with EventType." + ((EventType)i).ToString() + " of model " + uimodel.ModelID; _runListener.OnEvent(theEvent); for (int j = 0; j < uimodel.LinkableComponent.GetPublishedEventTypeCount(); j++) if (uimodel.LinkableComponent.GetPublishedEventType(j) == _runListener.GetAcceptedEventType(i)) { uimodel.LinkableComponent.Subscribe(_runListener, _runListener.GetAcceptedEventType(i)); break; } } } if (!runInSameThread) { // creating run thread if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Creating run thread"; _runListener.OnEvent(theEvent); } _runThread = new Thread(new ThreadStart(RunThreadFunction)); // starting thread if (_runListener != null) { Event theEvent = new Event(EventType.GlobalProgress); theEvent.Description = "Starting run thread"; _runListener.OnEvent(theEvent); } _runThread.Start(); } else { // run simulation in same thread (for example when running from console) if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Running simulation in same thread"; _runListener.OnEvent(theEvent); } RunThreadFunction(); } } catch (System.Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured while initiating simulation run: " + e.ToString(); _runListener.OnEvent(theEvent); _runListener.OnEvent(SimulationFailedEvent); // todo: add info about time to this event } } }
private void runStandardParallel(ITime triggerTime) { List<Thread> threads = new List<Thread>(); List<Model> uimodels = new List<Model>(_models); List<List<Model>> orders; while ((orders = getTopologicalOrder(uimodels)).Count > 0) { List<Model> tier = orders[0]; string modelIds = ""; foreach (Model uimodel in tier) modelIds += uimodel.ModelID + ", "; Event theEvent = new Event(EventType.Informative); theEvent.Description = "Tier models: " + modelIds; _runListener.OnEvent(theEvent); foreach (Model uimodel in tier) { if (!isModelEngineComponent(uimodel.LinkableComponent)) continue; LinkableRunEngine runEngine = (LinkableRunEngine)uimodel.LinkableComponent; Thread th = new Thread(new ThreadStart(delegate() { runEngine.RunToTime(triggerTime, -1); })); th.Start(); threads.Add(th); uimodels.Remove(uimodel); } foreach (Thread th in threads) { th.Join(); } threads.Clear(); } }
/// <summary> /// Adds a data operation /// </summary> /// <param name="dataOperation">The data operation</param> public void AddDataOperation (IDataOperation dataOperation) { if ( ! (dataOperation is ICloneable ) ) { // Data Operation can not be cloned, issue warning Event warning = new Event(EventType.Warning); warning.Description = "DataOperation " + dataOperation.ID + " can not be cloned yet!"; warning.Sender = _sourceComponent; _sourceComponent.SendEvent(warning); _dataOperations.Add(dataOperation); } else { _dataOperations.Add(((ICloneable)dataOperation).Clone()); } }
/// <summary> /// Runs simulation. /// </summary> /// <param name="runListener">Simulation listener.</param> /// <param name="runInSameThread">If <c>true</c>, simulation is run in same thread like caller, /// ie. method blocks until simulation don't finish. If <c>false</c>, simulation is /// run in separate thread and method returns immediately.</param> /// <remarks> /// Simulation is run the way that trigger invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see> /// method of the model it's connected to /// at the time specified by <see cref="TriggerInvokeTime">TriggerInvokeTime</see> property. /// If you need to use more than one listener you can use <see cref="ProxyListener">ProxyListener</see> /// class or <see cref="ProxyMultiThreadListener">ProxyMultiThreadListener</see> if <c>runInSameThread</c> is <c>false</c>. /// </remarks> public void Run(Logger logger, bool runInSameThread) { LoggerListener loggerListener = new LoggerListener(logger); ProgressListener progressListener = new ProgressListener(); progressListener.ModelProgressChangedHandler += new ModelProgressChangedDelegate(delegate(ILinkableComponent linkableComponent, ITimeStamp simTime) { string guid = cmGuidMapping[linkableComponent]; string progress = CalendarConverter.ModifiedJulian2Gregorian(simTime.ModifiedJulianDay).ToString(); CompositionModelProgressChangedHandler(this, guid, progress); }); ArrayList listeners = new ArrayList(); listeners.Add(loggerListener); listeners.Add(progressListener); ProxyListener proxyListener = new ProxyListener(); proxyListener.Initialize(listeners); startTime = DateTime.Now; if (_running) throw (new Exception("Simulation is already running.")); _running = true; _runListener = proxyListener; try { TimeStamp runToTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(_triggerInvokeTime)); // Create informative message if (_runListener != null) { StringBuilder description = new StringBuilder(); description.Append("Starting simulation at "); description.Append(DateTime.Now.ToString()); description.Append(","); description.Append(" composition consists from following models:\n"); foreach (Model model in _models) { description.Append(model.ModelID); description.Append(", "); } // todo: add more info? Event theEvent = new Event(EventType.Informative); theEvent.Description = description.ToString(); _runListener.OnEvent(theEvent); } _runPrepareForComputationStarted = true; // prepare for computation if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Preparing for computation...."; _runListener.OnEvent(theEvent); } // subscribing event listener to all models if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Subscribing proxy event listener...."; _runListener.OnEvent(theEvent); for (int i = 0; i < _runListener.GetAcceptedEventTypeCount(); i++) foreach (Model uimodel in _models) { theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Subscribe() method with EventType." + ((EventType)i).ToString() + " of model " + uimodel.ModelID; _runListener.OnEvent(theEvent); for (int j = 0; j < uimodel.LinkableComponent.GetPublishedEventTypeCount(); j++) if (uimodel.LinkableComponent.GetPublishedEventType(j) == _runListener.GetAcceptedEventType(i)) { uimodel.LinkableComponent.Subscribe(_runListener, _runListener.GetAcceptedEventType(i)); break; } } } if (!runInSameThread) { // creating run thread if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Creating run thread...."; _runListener.OnEvent(theEvent); } _runThread = new Thread(RunThreadFunction) { Priority = ThreadPriority.Normal }; // starting thread... if (_runListener != null) { Event theEvent = new Event(EventType.GlobalProgress); theEvent.Description = "Starting run thread...."; _runListener.OnEvent(theEvent); } _runThread.Start(); } else { // run simulation in same thread (for example when running from console) if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Running simulation in same thread...."; _runListener.OnEvent(theEvent); } RunThreadFunction(); } } catch (System.Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured while initiating simulation run: " + e.ToString(); _runListener.OnEvent(theEvent); _runListener.OnEvent(SimulationFailedEvent); // todo: add info about time to this event endTime = DateTime.Now; var ev = new Event { Description = "Elapsed time: " + (endTime - startTime), Type = EventType.GlobalProgress }; _runListener.OnEvent(ev); } } finally { } }