public void EndBuildSystem() { _writtenSystems.Enqueue(_system); _system = null; _signalMappings.Clear(); _busMappings.Clear(); }
public void SubscribeSystem(IMathSystem system) { if (_system != null && _system.InstanceId == system.InstanceId) { return; // already subscribed } if (_system != null) { throw new InvalidOperationException("There's already a system subscribed."); } _system = system; foreach (ISystemObserver observer in _observers) { observer.AttachedToSystem(system); if (observer.AutoInitialize) { InitializeObserverWithCurrentSystem(observer); } } Mediator.Instance.AttachObserver(this); if (SystemChanged != null) { SystemChanged(this, EventArgs.Empty); } }
private void WhiteboardForm_Load(object sender, EventArgs e) { //netron.View.SetBackgroundType(CanvasBackgroundTypes.Gradient); //Ambience amb = netron.View.Model.Pages[0].Ambience; //amb.BackgroundType = CanvasBackgroundTypes.Gradient; //amb.BackgroundColor = Color.Gold; //amb.GradientColor1 = Color.Gold; //Color.WhiteSmoke; //amb.GradientColor2 = Color.Goldenrod; //Color.SteelBlue; entitySelector.Entities = Service <ILibrary> .Instance.GetAllEntities(); entitySelector.UpdateEntities(); IMathSystem system = Binder.CreateSystem(); ISystemMediator mediator = Binder.GetInstance <ISystemMediator, IMathSystem>(system); // attach a system logger (with console output), something // we get for free thanks to the channels subsystem (mediator/observer). LogSystemObserver lo = new LogSystemObserver(new TextLogWriter(Console.Out)); mediator.AttachObserver(lo); _ctrl.Load(system); }
public void Interpret(FileInfo file, IMathSystem system) { if(file == null) throw new ArgumentNullException("file"); Interpret(file.OpenText(), system); }
protected override void UnloadSystem(IMathSystem system) { _bridge.Model.OnConnectorAttached -= Model_OnConnectorAttached; _bridge.Model.OnConnectorDetached -= Model_OnConnectorDetached; base.UnloadSystem(system); _bridge = null; }
public void ReadSystem(IMathSystem system) { if(system == null) throw new ArgumentNullException("system"); system.AcceptSystemBuilder(_builder); }
public void Interpret(TextReader reader, IMathSystem system) { if(scanner == null) scanner = new ParserScanner(reader, system); else scanner.Reset(reader, system); scanner.AllStatements(); }
public Bridge(IMathSystem system, Document document) { _system = system; _document = document; _signals = new Dictionary <Guid, SignalShape>(); _buses = new Dictionary <Guid, BusShape>(); _ports = new Dictionary <Guid, PortShape>(); }
protected virtual void UnloadSystem(IMathSystem system) { if (_sysMediator != null) { _sysMediator.DetachObserver(this); _sysMediator = null; } }
public void Interpret(FileInfo file, IMathSystem system) { if (file == null) { throw new ArgumentNullException("file"); } Interpret(file.OpenText(), system); }
private Bridge CreateBridge(Document document) { IMathSystem sys = Binder.GetInstance <IMathSystem>(); Bridge bridge = new Bridge(sys, document); _mathBridges.Add(sys.InstanceId, bridge); _netronBridges.Add(document, bridge); return(bridge); }
private Bridge CreateBridge(IMathSystem system) { Document doc = new Document(); Bridge bridge = new Bridge(system, doc); _mathBridges.Add(system.InstanceId, bridge); _netronBridges.Add(doc, bridge); return(bridge); }
public void ReadSystem(IMathSystem system) { if (system == null) { throw new ArgumentNullException("system"); } system.AcceptSystemBuilder(_builder); }
public IArchitecture InstantiateToPort(Port port) { if (port == null) { throw new ArgumentNullException("port"); } IMathSystem system = MathSystem.ReadXml(_xml); return(new CompoundArchitecture(_architectureId, _entityId, port, system)); }
public void Interpret(TextReader reader, IMathSystem system) { if (scanner == null) { scanner = new ParserScanner(reader, system); } else { scanner.Reset(reader, system); } scanner.AllStatements(); }
public ParserScanner(TextReader reader, IMathSystem system) { if(system == null) throw new ArgumentNullException("system"); if(reader == null) throw new ArgumentNullException("reader"); tokenizer = new ParserMarker(reader); this.system = system; this.library = Service<ILibrary>.Instance; this.builder = Service<IBuilder>.Instance; }
public void AutoSimplify_MathOp_AlgebraDerive() { IMathSystem s = _p.CurrentSystem; _p.Interpret(@"Signal x;"); _p.Interpret(@"Signal fn;"); _p.Interpret(@"Signal fdiv;"); _p.Interpret(@"Signal fdiff;"); _p.Interpret(@"fn <- 3*exp(-3*x)-x;"); _p.Interpret(@"fdiff <- diff(3*exp(-3*x)-x, x);"); _p.Interpret(@"fdiv <- fn / fdiff;"); Signal x = s.LookupNamedSignal("x"); Std.ConstrainAlwaysReal(x); Signal fdiv = s.LookupNamedSignal("fdiv"); Assert.AreEqual(new MathIdentifier("Divide", "Std"), fdiv.DrivenByPort.Entity.EntityId, "A"); Signal fdiv_n = fdiv.DrivenByPort.InputSignals[0]; Signal fdiv_d = fdiv.DrivenByPort.InputSignals[1]; Assert.AreEqual(new MathIdentifier("Subtract", "Std"), fdiv_n.DrivenByPort.Entity.EntityId, "B"); Assert.AreEqual(new MathIdentifier("Derive", "Std"), fdiv_d.DrivenByPort.Entity.EntityId, "C"); // Execute MathOp Std.Derive Signal simplified = Std.AutoSimplify(fdiv); Assert.AreEqual("(3*exp(-3*x)+-1*x)*(-1+-9*exp(-3*x))^(-1)", _f.Format(simplified, FormattingOptions.Compact), "D"); Assert.AreEqual(new MathIdentifier("Multiply", "Std"), simplified.DrivenByPort.Entity.EntityId, "E"); Signal simplified_n = simplified.DrivenByPort.InputSignals[0]; Signal simplified_d = simplified.DrivenByPort.InputSignals[1]; Assert.AreEqual(new MathIdentifier("Add", "Std"), simplified_n.DrivenByPort.Entity.EntityId, "F"); Assert.AreEqual(new MathIdentifier("Power", "Std"), simplified_d.DrivenByPort.Entity.EntityId, "G"); s.PromoteAsInput(x); s.AddSignalTree(simplified, true, false); //s.PromoteAsOutput(simplified); s.RemoveUnusedObjects(); // The Derive Mapping should be removed from the system Assert.IsFalse(s.GetAllPorts().Exists(delegate(Port port) { return(port.Entity.EqualsById(new MathIdentifier("Derive", "Std"))); }), "H"); Assert.AreEqual(-0.3, s.Evaluate(0.0)[0], 1e-8, "x=0.0"); Assert.AreEqual(.5874238104, s.Evaluate(1.0)[0], 1e-8, "x=1.0"); Assert.AreEqual(3.139070661, s.Evaluate(Constants.Pi)[0], 1e-8, "x=Pi"); Assert.AreEqual(-.3334664750, s.Evaluate(-2.5)[0], 1e-8, "x=-2.5"); }
public static IMathSystem ReadXml(FileInfo file) { if (file == null) { throw new ArgumentNullException("file"); } XmlReader reader = XmlReader.Create(file.OpenText()); IMathSystem sys = ReadXml(reader); reader.Close(); return(sys); }
public CompoundArchitecture(MathIdentifier id, MathIdentifier entityId, Port port, IMathSystem system) : base(id, entityId, false) { this.inputSignals = port.InputSignals; this.outputSignals = port.OutputSignals; this.system = system; this.system.OutputValueChanged += system_OutputValueChanged; SetPort(port); for(int i = 0; i < inputSignals.Count; i++) inputSignals[i].ValueChanged += CompoundArchitecture_SignalValueChanged; system.PushInputValueRange(inputSignals); }
/// <summary>Clear the buffer. Replace the current stream with a new one.</summary> public void Reset(TextReader reader, IMathSystem system) { if (system == null) { throw new ArgumentNullException("system"); } if (reader == null) { throw new ArgumentNullException("reader"); } this.system = system; tokenizer.Reset(reader); }
public void SystemToSystemCloneTest() { Project p = new Project(); MathSystem s1 = p.CurrentSystem; // BUILD SYSTEM 1: sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo; s1.AddSignalTree(sinx2t2, true, true); // EVALUATE SYSTEM 1 FOR x=1.5 x.PostNewValue(new RealValue(1.5)); p.SimulateInstant(); Assert.AreEqual(0, s1.BusCount, "A0"); Assert.AreEqual(5, s1.SignalCount, "A1"); Assert.AreEqual(3, s1.PortCount, "A2"); Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "A3"); // CLONE SYSTEM 1 TO SYSTEM 2 /* * HINT: would be simpler to just call: * MathSystem s2 = s1.Clone(); */ SystemWriter writer = new SystemWriter(); SystemReader reader = new SystemReader(writer); reader.ReadSystem(s1); IMathSystem s2 = writer.WrittenSystems.Dequeue(); Assert.AreEqual(0, s2.BusCount, "B0"); Assert.AreEqual(5, s2.SignalCount, "B1"); Assert.AreEqual(3, s2.PortCount, "B2"); Assert.AreEqual("Std.Real(1.55614639377584)", s2.GetOutput(0).Value.ToString(), "B3"); // EVALUATE SYSTEM 2 FOR x=2.5 s2.GetInput(0).PostNewValue(new RealValue(2.5)); p.SimulateInstant(); Assert.AreEqual("Std.Real(-0.0663584330951136)", s2.GetOutput(0).Value.ToString(), "C0"); // CHECK SYSTEM 1 STILL ON x=1.5 Assert.AreEqual("Std.Real(1.5)", x.Value.ToString(), "D0"); Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "D1"); }
public CompoundArchitecture(MathIdentifier id, MathIdentifier entityId, Port port, IMathSystem system) : base(id, entityId, false) { this.inputSignals = port.InputSignals; this.outputSignals = port.OutputSignals; this.system = system; this.system.OutputValueChanged += system_OutputValueChanged; SetPort(port); for (int i = 0; i < inputSignals.Count; i++) { inputSignals[i].ValueChanged += CompoundArchitecture_SignalValueChanged; } system.PushInputValueRange(inputSignals); }
public ParserScanner(TextReader reader, IMathSystem system) { if (system == null) { throw new ArgumentNullException("system"); } if (reader == null) { throw new ArgumentNullException("reader"); } tokenizer = new ParserMarker(reader); this.system = system; this.library = Service <ILibrary> .Instance; this.builder = Service <IBuilder> .Instance; }
private void ScanDefineArchitecture() { tokenizer.Match(TokenTypes.TextIdentifier, "architecture"); MathIdentifier architectureId = ScanEntityMathIdentifierOrLabel(true); MathIdentifier entityId = ScanEntityMathIdentifierOrLabel(true); IEntity entity = library.LookupEntity(entityId); tokenizer.Match("{"); IMathSystem originalSystem = system; IMathSystem tempSystem = Binder.CreateSystem(); system = tempSystem; while (tokenizer.LookaheadFistToken.Text != "}") { NextStatement(); } foreach (string input in entity.InputSignals) { tempSystem.PromoteAsInput(tempSystem.LookupNamedSignal(input)); } foreach (string output in entity.OutputSignals) { tempSystem.AddSignalTree(tempSystem.LookupNamedSignal(output), tempSystem.GetAllInputs(), true, false); } ReadOnlySignalSet leafs = tempSystem.GetAllLeafSignals(); foreach (Signal s in leafs) { Signal so; if (originalSystem.TryLookupNamedSignal(s.Label, out so) && so.Value != null) { s.PostNewValue(so.Value); } } Service <ISimulationMediator> .Instance.SimulateInstant(); system = originalSystem; tokenizer.Match("}"); tempSystem.RemoveUnusedObjects(); tempSystem.PublishToLibrary(architectureId, entity.EntityId); }
public void UnsubscribeSystem(IMathSystem system) { if (_system == null) { return; // wasn't registered, no matter (might be programming error, but doesn't help anyone to throw; more robust this way. } //throw new InvalidOperationException("There's no system subscribed."); Mediator.Instance.DetachObserver(this); for (int i = _observers.Count - 1; i >= 0; i--) { if (_observers[i].AutoDetachOnSystemChanged) { DetachObserver(_observers[i]); } else { _observers[i].DetachedFromSystem(system); } } _system = null; }
public ISystemMediator GetInstance(IMathSystem p1) { ISystemMediatorSource source = p1 as ISystemMediatorSource; if (source == null) { SystemMediator sm = new SystemMediator(); sm.SubscribeSystem(p1); return(sm); } if (source.HasSystemMediator) { return(source.SystemMediator); } else { SystemMediator sm = new SystemMediator(); source.SystemMediator = sm; sm.SubscribeSystem(p1); // probably redundant, but doesn't matter (subscribe is robust). return(sm); } }
protected override void LoadSystem(IMathSystem system) { base.LoadSystem(system); Bridge bridge; if (!_mathBridges.ContainsKey(system.InstanceId)) { bridge = CreateBridge(system); } else { bridge = _mathBridges[system.InstanceId]; } _presentation.AttachToDocument(bridge.Document); _bridge = bridge; _bridge.Model.OnConnectorAttached += Model_OnConnectorAttached; _bridge.Model.OnConnectorDetached += Model_OnConnectorDetached; if (ModelChanged != null) { ModelChanged(this, EventArgs.Empty); } }
void ISystemObserver.DetachedFromSystem(IMathSystem system) { UnloadSystem(system); }
public void BeginBuildSystem(int inputSignalCount, int outputSignalCount, int busCount) { _system = Binder.CreateSystem(); _signalMappings.Clear(); _busMappings.Clear(); }
void ISystemObserver.AttachedToSystem(IMathSystem system) { LoadSystem(system); }
private Bridge CreateBridge(IMathSystem system) { Document doc = new Document(); Bridge bridge = new Bridge(system, doc); _mathBridges.Add(system.InstanceId, bridge); _netronBridges.Add(doc, bridge); return bridge; }
protected virtual void LoadSystem(IMathSystem system) { // reuses mediator if system already has one _sysMediator = Binder.GetInstance<ISystemMediator, IMathSystem>(system); _sysMediator.AttachObserver(this); }
public void Interpret(string expression, IMathSystem system) { Interpret(new StringReader(expression), system); }
//public CommandChannel CurrentCommands //{ // get { return _sysMediator.CurrentSystem.Mediator.Commands; } //} #region Load/Unload System protected virtual void LoadSystem(IMathSystem system) { // reuses mediator if system already has one _sysMediator = Binder.GetInstance <ISystemMediator, IMathSystem>(system); _sysMediator.AttachObserver(this); }
public void DetachedFromSystem(IMathSystem system) { }
public void SubscribeSystem(IMathSystem system) { if(_system != null && _system.InstanceId == system.InstanceId) return; // already subscribed if(_system != null) throw new InvalidOperationException("There's already a system subscribed."); _system = system; foreach(ISystemObserver observer in _observers) { observer.AttachedToSystem(system); if(observer.AutoInitialize) InitializeObserverWithCurrentSystem(observer); } Mediator.Instance.AttachObserver(this); if(SystemChanged != null) SystemChanged(this, EventArgs.Empty); }
public void UnsubscribeSystem(IMathSystem system) { if(_system == null) return; // wasn't registered, no matter (might be programming error, but doesn't help anyone to throw; more robust this way. //throw new InvalidOperationException("There's no system subscribed."); Mediator.Instance.DetachObserver(this); for(int i = _observers.Count - 1; i >= 0; i--) { if(_observers[i].AutoDetachOnSystemChanged) DetachObserver(_observers[i]); else _observers[i].DetachedFromSystem(system); } _system = null; }
/// <summary>Clear the buffer. Replace the current stream with a new one.</summary> public void Reset(TextReader reader, IMathSystem system) { if(system == null) throw new ArgumentNullException("system"); if(reader == null) throw new ArgumentNullException("reader"); this.system = system; tokenizer.Reset(reader); }
protected override void LoadSystem(IMathSystem system) { base.LoadSystem(system); Bridge bridge; if(!_mathBridges.ContainsKey(system.InstanceId)) bridge = CreateBridge(system); else bridge = _mathBridges[system.InstanceId]; _presentation.AttachToDocument(bridge.Document); _bridge = bridge; _bridge.Model.OnConnectorAttached += Model_OnConnectorAttached; _bridge.Model.OnConnectorDetached += Model_OnConnectorDetached; if(ModelChanged != null) ModelChanged(this, EventArgs.Empty); }
public Bridge(IMathSystem system, Document document) { _system = system; _document = document; _signals = new Dictionary<Guid, SignalShape>(); _buses = new Dictionary<Guid, BusShape>(); _ports = new Dictionary<Guid, PortShape>(); }
public void AttachedToSystem(IMathSystem system) { _currentSysId = system.InstanceId; _writer.WriteEntry(DateTime.Now, _currentSysId, LogAction.SystemChanged, _emptyGuid, _emptyGuid, _emptyGuid, _emptyId, -1); }
private void ScanDefineArchitecture() { tokenizer.Match(TokenTypes.TextIdentifier, "architecture"); MathIdentifier architectureId = ScanEntityMathIdentifierOrLabel(true); MathIdentifier entityId = ScanEntityMathIdentifierOrLabel(true); IEntity entity = library.LookupEntity(entityId); tokenizer.Match("{"); IMathSystem originalSystem = system; IMathSystem tempSystem = Binder.CreateSystem(); system = tempSystem; while(tokenizer.LookaheadFistToken.Text != "}") NextStatement(); foreach(string input in entity.InputSignals) tempSystem.PromoteAsInput(tempSystem.LookupNamedSignal(input)); foreach(string output in entity.OutputSignals) tempSystem.AddSignalTree(tempSystem.LookupNamedSignal(output), tempSystem.GetAllInputs(), true, false); ReadOnlySignalSet leafs = tempSystem.GetAllLeafSignals(); foreach(Signal s in leafs) { Signal so; if(originalSystem.TryLookupNamedSignal(s.Label, out so) && so.Value != null) s.PostNewValue(so.Value); } Service<ISimulationMediator>.Instance.SimulateInstant(); system = originalSystem; tokenizer.Match("}"); tempSystem.RemoveUnusedObjects(); tempSystem.PublishToLibrary(architectureId, entity.EntityId); }
public void AttachedToSystem(IMathSystem system) { _currentSysId = system.InstanceId; _writer.WriteEntry(DateTime.Now, _currentSysId, LogAction.SystemChanged, _emptyGuid, _emptyGuid, _emptyGuid, _emptyId, null); }
public void Load(IMathSystem system) { LoadSystem(system); }
protected virtual void UnloadSystem(IMathSystem system) { if(_sysMediator != null) { _sysMediator.DetachObserver(this); _sysMediator = null; } }