/// <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); } }
private void SendEvent(EventType eventType, string message) { HydroNumerics.OpenMI.Sdk.Backbone.Event eventD = new HydroNumerics.OpenMI.Sdk.Backbone.Event(eventType); eventD.Description = eventType.ToString() + " (" + message + ")"; eventD.Sender = this; eventD.SimulationTime = TimeToTimeStamp(_engineApiAccess.GetCurrentTime()); SendEvent(eventD); }
static CompositionManager() { _simulationFinishedEvent = new Event(EventType.GlobalProgress); _simulationFinishedEvent.Description = "Simulation finished successfuly..."; _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) { HydroNumerics.OpenMI.Sdk.Backbone.Event eventA = new HydroNumerics.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 Equals() { Event e1 = new Event(EventType.Informative); e1.Description = "Description"; e1.SimulationTime = new TimeStamp(44060.0); e1.SetAttribute("key1","value1"); e1.SetAttribute("key2","value2"); e1.SetAttribute("key3","value3"); Assert.IsTrue(e1.Equals(e)); Assert.IsFalse(e.Equals(null)); Assert.IsFalse(e.Equals("string")); }
public void SendEvent(EventType eventType, ILinkableComponent sender, string msg) { if (((HydroNumerics.OpenMI.Sdk.Backbone.LinkableComponent) this.link.TargetComponent).HasListeners()) { HydroNumerics.OpenMI.Sdk.Backbone.Event eventD = new HydroNumerics.OpenMI.Sdk.Backbone.Event(eventType); eventD.Description = eventType.ToString() + msg; 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); } }
private void SendSourceBeforeGetValuesReturn(double returnValue, ILink link) { Event aEvent = new Event(); aEvent.Type = EventType.SourceBeforeGetValuesReturn; aEvent.Sender = this; aEvent.Description = ModelID + "returned the value: " + returnValue.ToString() + " for \"" + link.SourceQuantity.ID + "\""; SendEvent(aEvent); }
private void SendSourceAfterGetValuesCallEvent(ITime time, ILink link) { Event aEvent = new Event(); aEvent.Type = EventType.SourceAfterGetValuesCall; aEvent.Sender = this; aEvent.Description = ModelID + " (" + ComponentID + ") was requested for values for \"" + link.SourceQuantity.ID + "\" at time: " + ITimeToString(time); SendEvent(aEvent); }
/// <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()); } }
private void SendSourceAfterGetValuesCallEvent(ITime time, string LinkID) { HydroNumerics.OpenMI.Sdk.Backbone.Event eventA = new HydroNumerics.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); }
/// <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 } } }
/// <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 finished successfuly at " + 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 at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFailedEvent); } } finally { _running = false; _runListener = null; // release listener } }
public void EqualsType() { Event e1 = new Event(EventType.DataChanged); e1.Description = "Description"; e1.SimulationTime = new TimeStamp(44060.0); e1.SetAttribute("key1","value1"); e1.SetAttribute("key2","value2"); e1.SetAttribute("key3","value3"); Assert.IsFalse(e1.Equals(e)); }
private void SendTargetBeforeGetValuesCall(ITime time, ILink link) { Event aEvent = new Event(); aEvent.Type = EventType.TargetBeforeGetValuesCall; aEvent.Sender = this; aEvent.Description = ModelID + " requested value for \"" + link.TargetQuantity.ID + "\" at time: " + ITimeToString(time); SendEvent(aEvent); }
private void SendTargetAfterGetValuesReturn(double returnedValue, ILink link) { Event aEvent = new Event(); aEvent.Type = EventType.TargetAfterGetValuesReturn; aEvent.Sender = this; aEvent.Description = ModelID + " recieved value " + returnedValue.ToString() + " for \"" + link.TargetQuantity.ID +"\""; SendEvent(aEvent); }
public void SendEvent(EventType eventType, ILinkableComponent sender, string msg) { if (((HydroNumerics.OpenMI.Sdk.Backbone.LinkableComponent)this.link.TargetComponent).HasListeners()) { HydroNumerics.OpenMI.Sdk.Backbone.Event eventD = new HydroNumerics.OpenMI.Sdk.Backbone.Event(eventType); eventD.Description = eventType.ToString() + msg; 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); } }