public void UseCase_SimpleSystemAsFunction() { // TODO: Replace with new (easier to use) MathFunction class instead of MathSystem _p.KeepTrack = true; MathSystem s = _p.CurrentSystem; s.RemoveUnusedObjects(); Signal x = Binder.CreateSignal(); Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); Signal sinx2 = StdBuilder.Sine(x2); Assert.AreEqual(0, s.InputCount, "Input Signal Count A"); Assert.AreEqual(0, s.OutputCount, "Output Signal Count A"); Assert.AreEqual(0, s.BusCount, "Bus Count A"); Assert.AreEqual(3, s.SignalCount, "Signal Count A"); Assert.AreEqual(2, s.PortCount, "Port Count A"); s.PromoteAsInput(x); s.PromoteAsOutput(sinx2); s.RemoveUnusedObjects(); Assert.AreEqual(1, s.InputCount, "Input Signal Count B"); Assert.AreEqual(1, s.OutputCount, "Output Signal Count B"); Assert.AreEqual(0, s.BusCount, "Bus Count B"); Assert.AreEqual(3, s.SignalCount, "Signal Count B"); Assert.AreEqual(2, s.PortCount, "Port Count B"); double ret = Math.Round(s.Evaluate(Math.PI)[0], 4); Assert.AreEqual(-0.4303, ret, "Result"); }
/// <param name="inputSignalIndexes">note: negative indexes are interpreted as internal signal indexes, incremented by one (thus -1 means the internal signal with index zero)!</param> /// <param name="outputSignalIndexes">note: negative indexes are interpreted as internal signal indexes, incremented by one (thus -1 means the internal signal with index zero)!</param> public CompoundProcess(MathSystem system, int[] inputSignalIndexes, int[] outputSignalIndexes, int[] busIndexes) { this.system = system; this.inputSignalIndexes = inputSignalIndexes; this.outputSignalIndexes = outputSignalIndexes; this.busIndexes = busIndexes; if (inputSignalIndexes.Length != system.InputCount) { throw new ArgumentException("The count of input signal mappings doesn't match the compound process structure.", "inputSignalIndexes"); } if (outputSignalIndexes.Length != system.OutputCount) { throw new ArgumentException("The count of output signal mappings doesn't match the compound process structure.", "outputSignalIndexes"); } if (busIndexes.Length != system.BusCount) { throw new ArgumentException("The count of bus mappings doesn't match the compound process structure.", "busIndexes"); } this.inputSignals = new Signal[inputSignalIndexes.Length]; this.outputSignals = new Signal[outputSignalIndexes.Length]; this.buses = new Bus[busIndexes.Length]; this.system.OutputValueChanged += system_OutputValueChanged; }
public void UseCase_SystemAsCompoundArchitecture() { _p.KeepTrack = false; MathSystem s = _p.CurrentSystem; s.RemoveUnusedObjects(); Signal x = Binder.CreateSignal(); Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); Signal sinx2 = StdBuilder.Sine(x2); s.AddSignalTree(sinx2, true, true); s.RemoveUnusedObjects(); s.PublishToLibrary("SineOfSquaredX", "sinx2"); Signal y = Binder.CreateSignal(); y.PostNewValue(RealValue.E); Signal z = Service <IBuilder> .Instance.Function("sinx2", y); _p.SimulateInstant(); //.SimulateFor(new TimeSpan(1)); IValueStructure res = z.Value; RealValue resReal = RealValue.ConvertFrom(res); Assert.AreEqual(0.8939, Math.Round(resReal.Value, 4)); }
public void Parser_SimpleExpressions() { Project p = new Project(); // TODO: UNCOMMENT //LogObserver lo = new LogObserver(new TextLogWriter(Console.Out)); //p.AttachLocalObserver(lo); MathSystem s = p.CurrentSystem; Assert.AreEqual(0, s.SignalCount, "0"); p.Interpret("signal x;"); Assert.AreEqual(1, s.SignalCount, "A1"); Assert.IsTrue(s.ContainsNamedSignal("x"), "A2"); p.Interpret("x"); Assert.AreEqual(1, s.SignalCount, "B1"); p.Interpret("y <- x^2"); Assert.AreEqual(3, s.SignalCount, "C1"); Assert.AreEqual("Std.Power", s.LookupNamedSignal("y").DrivenByPort.Entity.EntityId.ToString(), "C2"); Assert.AreEqual("x", s.LookupNamedSignal("y").DrivenByPort.InputSignals[0].Label, "C3"); Assert.AreEqual("Std.Integer(2)", s.LookupNamedSignal("y").DrivenByPort.InputSignals[1].Value.ToString(), "C4"); p.Interpret("instantiate + in a,b out c;"); Assert.AreEqual(6, s.SignalCount, "D1"); Assert.AreEqual("Std.Add", s.LookupNamedSignal("c").DrivenByPort.Entity.EntityId.ToString(), "D2"); Assert.AreEqual("a", s.LookupNamedSignal("c").DrivenByPort.InputSignals[0].Label, "D3"); Assert.AreEqual("b", s.LookupNamedSignal("c").DrivenByPort.InputSignals[1].Label, "D4"); p.Interpret("d <- diff(a*b,a);"); Assert.AreEqual(8, s.SignalCount, "E1"); Assert.AreEqual("Std.Derive", s.LookupNamedSignal("d").DrivenByPort.Entity.EntityId.ToString(), "E2"); Assert.AreEqual("Std.Multiply", s.LookupNamedSignal("d").DrivenByPort.InputSignals[0].DrivenByPort.Entity.EntityId.ToString(), "E3"); Assert.AreEqual("a", s.LookupNamedSignal("d").DrivenByPort.InputSignals[1].Label, "E4"); }
public void Pattern_TreePattern() { Project p = new Project(); MathSystem s = p.CurrentSystem; // 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"; Pattern psimp = new Pattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Assert.AreEqual(true, psimp.Match(sinx2, sinx2.DrivenByPort), "B01"); Assert.AreEqual(false, psimp.Match(x2, x2.DrivenByPort), "B02"); TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinsqr.Add(new Pattern(new EntityCondition(new MathIdentifier("Square", "Std")))); Assert.AreEqual(true, psinsqr.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinsqr.Match(x2, x2.DrivenByPort), "B04"); TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinadd.Add(new Pattern(new EntityCondition(new MathIdentifier("Add", "Std")))); Assert.AreEqual(false, psinadd.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinadd.Match(x2, x2.DrivenByPort), "B04"); }
public void Parser_StructuralExpressions() { Project p = new Project(); ILibrary l = Service <ILibrary> .Instance; MathSystem s = p.CurrentSystem; s.AddNamedSignal("w", new RealValue(0.1)); p.SimulateInstant(); p.Interpret("define entity Test \"test\" function in x,c out y;"); Assert.IsTrue(l.ContainsEntity(new MathIdentifier("Test", "Work")), "01"); Assert.AreEqual("Work.Test", l.LookupEntity("test", 2).EntityId.ToString()); p.Interpret("instantiate Work.Test in x->a,c->a*b out res1;\nres2 <- test(a*b,b);"); Assert.AreEqual("Work.Test", s.LookupNamedSignal("res1").DrivenByPort.Entity.EntityId.ToString(), "02"); Assert.AreEqual("Work.Test", s.LookupNamedSignal("res2").DrivenByPort.Entity.EntityId.ToString(), "03"); Assert.AreEqual("Std.Multiply", s.LookupNamedSignal("res1").DrivenByPort.InputSignals[1].DrivenByPort.Entity.EntityId.ToString(), "04"); Assert.AreEqual("Std.Multiply", s.LookupNamedSignal("res2").DrivenByPort.InputSignals[0].DrivenByPort.Entity.EntityId.ToString(), "05"); p.Interpret("define architecture TestArch Test { y <- x * sin(c) +w; };"); Assert.IsTrue(l.ContainsEntity(new MathIdentifier("Test", "Work")), "06"); s.LookupNamedSignal("a").PostNewValue(new RealValue(0.25)); s.LookupNamedSignal("b").PostNewValue(new RealValue(0.75)); p.SimulateInstant(); Signal res1 = s.LookupNamedSignal("res1"); Signal res2 = s.LookupNamedSignal("res2"); Assert.AreEqual("Work.TestArch", res1.DrivenByPort.CurrentArchitecture.ArchitectureId.ToString()); Assert.AreEqual("Work.TestArch", res2.DrivenByPort.CurrentArchitecture.ArchitectureId.ToString()); Assert.AreEqual(0.1466, Math.Round(RealValue.ConvertFrom(res1.Value).Value, 4)); Assert.AreEqual(0.2278, Math.Round(RealValue.ConvertFrom(res2.Value).Value, 4)); }
public void SystemToExpressionTest() { Project p = new Project(); MathSystem s1 = p.CurrentSystem; // BUILD SYSTEM 1: sin(x^2)*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"); // SERIALIZE SYSTEM 1 TO EXPRESSION ExpressionWriter writer = new ExpressionWriter(); SystemReader reader = new SystemReader(writer); reader.ReadSystem(s1); string expr = writer.WrittenExpressions.Dequeue(); Console.WriteLine(expr); // .... }
public void EndBuildSystem() { _writtenSystems.Enqueue(_system); _system = null; _signalMappings.Clear(); _busMappings.Clear(); }
public Bridge(MathSystem system, Document document) { _system = system; _document = document; _signals = new Dictionary <Guid, SignalShape>(); _buses = new Dictionary <Guid, BusShape>(); _ports = new Dictionary <Guid, PortShape>(); }
private Bridge CreateBridge(MathSystem system) { Document doc = new Document(); Bridge bridge = new Bridge(system, doc); _mathBridges.Add(system.InstanceId, bridge); _netronBridges.Add(doc, bridge); return(bridge); }
private Bridge CreateBridge(Document document) { MathSystem sys = Project.AddSystem(); Bridge bridge = new Bridge(sys, document); _mathBridges.Add(sys.InstanceId, bridge); _netronBridges.Add(document, bridge); return(bridge); }
public void Interpret(FileInfo file, MathSystem system) { if (file == null) { throw new ArgumentNullException("file"); } Interpret(file.OpenText(), system); }
public Architecture InstantiateToPort(Port port) { if (port == null) { throw new ArgumentNullException("port"); } MathSystem system = MathSystem.ReadXml(_xml, port.Context); return(new CompoundArchitecture(_architectureId, _entityId, port, system)); }
public CompoundArchitectureFactory(MathIdentifier architectureId, MathIdentifier entityId, string xml) { _architectureId = architectureId; _entityId = entityId; _xml = xml; Entity dummy = MathSystem.ReadXmlEntity(xml, new MathIdentifier("Dummy", "Temp"), string.Empty); _inputCnt = dummy.InputSignals.Length; _outputCnt = dummy.OutputSignals.Length; _busCnt = dummy.Buses.Length; }
public void Interpret(TextReader reader, MathSystem system) { if (scanner == null) { scanner = new ParserScanner(reader, system); } else { scanner.Reset(reader, system); } scanner.AllStatements(); }
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"); }
protected override void LoadSystem(MathSystem system) { Bridge bridge; if (!_mathBridges.ContainsKey(system.InstanceId)) { bridge = CreateBridge(system); } else { bridge = _mathBridges[system.InstanceId]; } _presentation.AttachToDocument(bridge.Document); _bridge = bridge; }
private void btnBuildSample_Click(object sender, EventArgs e) { s = _project.CurrentSystem; Context c = s.Context; Builder b = c.Builder; Signal x = new Signal(c); x.Label = "x"; x.AddConstraint(RealSetProperty.Instance); Signal x2 = b.Square(x); x2.Label = "x2"; Signal sinx2 = Std.Sine(c, x2); sinx2.Label = "sinx2"; Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo(c); Signal sinx2t2cosx = sinx2t2 + Std.Cosine(c, x); sinx2t2cosx.Label = "sinx2t2cosx"; s.AddSignalTree(sinx2t2, true, true); }
public ParserScanner(TextReader reader, MathSystem system) { if (system == null) { throw new ArgumentNullException("system"); } if (reader == null) { throw new ArgumentNullException("reader"); } tokenizer = new ParserMarker(reader, system.Context); this.system = system; this.context = system.Context; }
/// <summary>Clear the buffer. Replace the current stream with a new one.</summary> public void Reset(TextReader reader, MathSystem system) { if (system == null) { throw new ArgumentNullException("system"); } if (reader == null) { throw new ArgumentNullException("reader"); } this.system = system; this.context = system.Context; tokenizer.Reset(reader, context); }
public CompoundArchitecture(MathIdentifier id, MathIdentifier entityId, Port port, MathSystem 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].SignalValueChanged += CompoundArchitecture_SignalValueChanged; } system.PushInputValueRange(inputSignals); }
internal void UnsubscribeSystem(MathSystem system) { DisableTracking(system.Context); for (int i = _observers.Count - 1; i >= 0; i--) { if (_observers[i].AutoDetachOnSystemChanged) { DetachObserver(_observers[i]); } else { _observers[i].DetachedFromSystem(system); } } _system = null; }
public void FormatPrecedenceTest() { MathSystem s = project.CurrentSystem; s.RemoveUnusedObjects(); project.Interpret("x <- a+3+b*(c+d)/(1/2+3/4)^(5/6);"); Signal x = s.LookupNamedSignal("x"); Assert.AreEqual("(a+3)+(b*(c+d))/(1/2+3/4)^(5/6)", Service <IFormatter> .Instance.Format(x, FormattingOptions.Compact), "A01"); Signal x2 = Std.AutoSimplify(x); Assert.AreEqual("3+a+b*(c+d)*((5/4)^(5/6))^(-1)", Service <IFormatter> .Instance.Format(x2, FormattingOptions.Compact), "B01"); }
internal void SubscribeSystem(MathSystem system) { _system = system; EnableTracking(system.Context); foreach (ISystemObserver observer in _observers) { observer.AttachedToSystem(system); if (observer.AutoInitialize) { InitializeObserverWithCurrentSystem(observer); } } if (SystemChanged != null) { SystemChanged(this, EventArgs.Empty); } }
public void UseCase_ComplexDerivedSystemAsFunction() { // TODO: Replace with new (easier to use) MathFunction class instead of MathSystem _p.KeepTrack = false; MathSystem s = _p.CurrentSystem; Signal x = Binder.CreateSignal(); x.Label = "x"; Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal secx2 = StdBuilder.Secant(x2); secx2.Label = "secx2"; Signal diff = Std.Derive(secx2, x); diff.Label = "diff1"; s.AddSignalTree(diff, true, true); s.RemoveUnusedObjects(); Assert.AreEqual("2*sec(sqr(x))*tan(sqr(x))*x", _f.Format(diff, FormattingOptions.Compact), "Formatted Diff A"); Assert.AreEqual(1, s.InputCount, "Input Signal Count A"); Assert.AreEqual(1, s.OutputCount, "Output Signal Count A"); Assert.AreEqual(0, s.BusCount, "Bus Count A"); Assert.AreEqual(6, s.SignalCount, "Signal Count A"); Assert.AreEqual(4, s.PortCount, "Port Count A"); Signal diff2 = Std.AutoSimplify(diff); s.UnpromoteAsOutput(diff); s.AddSignalTree(diff2, true, true); s.RemoveUnusedObjects(); Assert.AreEqual("2*sec(sqr(x))*tan(sqr(x))*x", _f.Format(diff2, FormattingOptions.Compact), "Formatted Diff B"); Assert.AreEqual(1, s.InputCount, "Input Signal Count B"); Assert.AreEqual(1, s.OutputCount, "Output Signal Count B"); Assert.AreEqual(0, s.BusCount, "Bus Count B"); Assert.AreEqual(6, s.SignalCount, "Signal Count B"); Assert.AreEqual(4, s.PortCount, "Port Count B"); IValueStructure vs = s.Evaluate(ComplexValue.I)[0]; Assert.IsInstanceOfType(typeof(ComplexValue), vs, "Result is complex."); ComplexValue cv = (ComplexValue)vs; Assert.AreEqual(0, Math.Round(cv.RealValue, 4), "Real Result"); Assert.AreEqual(-5.7649, Math.Round(cv.ImaginaryValue, 4), "Imag Result"); }
private void ScanDefineArchitecture() { tokenizer.Match(TokenTypes.TextIdentifier, "architecture"); MathIdentifier architectureId = ScanEntityMathIdentifierOrLabel(true); MathIdentifier entityId = ScanEntityMathIdentifierOrLabel(true); Entity entity = context.Library.LookupEntity(entityId); tokenizer.Match("{"); MathSystem originalSystem = system; MathSystem tempSystem = new MathSystem(system.Context); 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); } ReadOnlyCollection <Signal> leafs = tempSystem.GetAllLeafSignals(); foreach (Signal s in leafs) { Signal so; if (originalSystem.TryLookupNamedSignal(s.Label, out so) && so.Value != null) { s.PostNewValue(so.Value); } } context.Scheduler.SimulateInstant(); system = originalSystem; tokenizer.Match("}"); tempSystem.RemoveUnusedObjects(); tempSystem.PublishToLibrary(architectureId, entity.EntityId); }
public void Pattern_Conditions() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Power(x, IntegerValue.ConstantTwo); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; AlwaysTrueCondition ctrue = AlwaysTrueCondition.Instance; Assert.AreEqual(true, ctrue.FulfillsCondition(x, x.DrivenByPort), "A01"); EntityCondition centity = new EntityCondition(new MathIdentifier("Sine", "Std")); Assert.AreEqual(true, centity.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A02"); Assert.AreEqual(false, centity.FulfillsCondition(x2, x2.DrivenByPort), "A03"); //InputSignalsPropertyCondition cinputconst = new InputSignalsPropertyCondition(new MathIdentifier("Constant", "Std"), CombinationMode.AtLeastOne); InputSignalsFlagCondition cinputconst = new InputSignalsFlagCondition(StdAspect.ConstantFlag, FlagState.Enabled, CombinationMode.AtLeastOne); Assert.AreEqual(true, cinputconst.FulfillsCondition(x2, x2.DrivenByPort), "A04"); Assert.AreEqual(false, cinputconst.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A05"); OrCondition cor = new OrCondition(centity, cinputconst); Assert.AreEqual(true, cor.FulfillsCondition(x2, x2.DrivenByPort), "A06"); Assert.AreEqual(true, cor.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A07"); Assert.AreEqual(false, cor.FulfillsCondition(x, x.DrivenByPort), "A08"); AndCondition cand = new AndCondition(ctrue, centity); Assert.AreEqual(true, cand.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A09"); Assert.AreEqual(false, cand.FulfillsCondition(x2, x2.DrivenByPort), "A10"); }
public void Pattern_CoalescedTreeMatching() { Project p = new Project(); MathSystem s = p.CurrentSystem; // 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"; CoalescedTreeNode root = new CoalescedTreeNode(AlwaysTrueCondition.Instance); root.Subscribe(new MathIdentifier("A", "Test")); CoalescedTreeNode sin = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Sine", "Std"))); sin.AddGroup(new MathIdentifier("B", "Test"), "sin"); root.ConditionAxis.Add(sin); CoalescedTreeNode sqr = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Square", "Std"))); sqr.AddGroup(new MathIdentifier("B", "Test"), "sqr"); sqr.Subscribe(new MathIdentifier("B", "Test")); CoalescedChildPattern sqrPattern = new CoalescedChildPattern(); sqrPattern.AddChild(sqr); sin.PatternAxis.Add(sqrPattern); CoalescedTreeNode tan = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Tangent", "Std"))); tan.AddGroup(new MathIdentifier("B", "Test"), "tan"); tan.Subscribe(new MathIdentifier("C", "Test")); root.ConditionAxis.Add(tan); MatchCollection res = root.MatchAll(sinx2, sinx2.DrivenByPort, 1); Assert.AreEqual(true, res.Contains(new MathIdentifier("A", "Test")), "C01"); Assert.AreEqual(true, res.Contains(new MathIdentifier("B", "Test")), "C02"); Assert.AreEqual(false, res.Contains(new MathIdentifier("C", "Test")), "C03"); Match mA = res[new MathIdentifier("A", "Test")]; Assert.AreEqual(new MathIdentifier("A", "Test"), mA.PatternId, "C04"); Assert.AreEqual(0, mA.GroupCount, "C05"); Match mB = res[new MathIdentifier("B", "Test")]; Assert.AreEqual(new MathIdentifier("B", "Test"), mB.PatternId, "C06"); Assert.AreEqual(2, mB.GroupCount, "C07"); Group mBsqr = mB["sqr"]; Assert.AreEqual(1, mBsqr.Count, "C08"); Assert.AreEqual(x2.InstanceId, mBsqr[0].First.InstanceId, "C09"); Assert.AreEqual(x2.DrivenByPort.InstanceId, mBsqr[0].Second.InstanceId, "C10"); Group mBsin = mB["sin"]; Assert.AreEqual(1, mBsin.Count, "C11"); Assert.AreEqual(sinx2.InstanceId, mBsin[0].First.InstanceId, "C12"); Assert.AreEqual(sinx2.DrivenByPort.InstanceId, mBsin[0].Second.InstanceId, "C13"); }
public void Pattern_CoalescedTreeDeduction() { Project p = new Project(); MathSystem s = p.CurrentSystem; // 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"; TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinadd_add = new Pattern(new EntityCondition(new MathIdentifier("Add", "Std"))); psinadd.Add(psinadd_add); psinadd_add.Group = "add"; TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinsqr_sqr = new Pattern(new EntityCondition(new MathIdentifier("Square", "Std"))); psinsqr.Add(psinsqr_sqr); psinsqr.Group = "sin"; psinsqr_sqr.Group = "sqr"; // generate coalesced tree CoalescedTreeNode root; List <CoalescedTreeNode> list = CoalescedTreeNode.CreateRootTree(out root); psinadd.MergeToCoalescedTree(new MathIdentifier("SinAdd", "Test"), list); psinsqr.MergeToCoalescedTree(new MathIdentifier("SinSqr", "Test"), list); // test whether the tree was generated correctly Assert.AreEqual(1, root.ConditionAxis.Count, "D01"); Assert.AreEqual(0, root.PatternAxis.Count, "D02"); CoalescedTreeNode csin = root.ConditionAxis[0]; Assert.AreEqual(true, csin.Condition is EntityCondition, "D03"); Assert.AreEqual(new MathIdentifier("Sine", "Std"), ((EntityCondition)csin.Condition).EntityId, "D04"); Assert.AreEqual(1, csin.GroupAxis.Count, "D05"); Assert.AreEqual(0, csin.SubscriptionAxis.Count, "D06"); Assert.AreEqual("sin", csin.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D07"); Assert.AreEqual(1, csin.PatternAxis.Count, "D08"); Assert.AreEqual(1, csin.PatternAxis[0].ChildrenAxis.Count, "D09"); Assert.AreEqual(true, csin.PatternAxis[0].ChildrenAxis[0].Condition is AlwaysTrueCondition, "D10"); CoalescedTreeNode cadd = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[0]; Assert.AreEqual(true, cadd.Condition is EntityCondition, "D11"); Assert.AreEqual(new MathIdentifier("Add", "Std"), ((EntityCondition)cadd.Condition).EntityId, "D12"); Assert.AreEqual(1, cadd.GroupAxis.Count, "D13"); Assert.AreEqual(1, cadd.SubscriptionAxis.Count, "D14"); Assert.AreEqual("add", cadd.GroupAxis[new MathIdentifier("SinAdd", "Test")], "D15"); Assert.AreEqual(new MathIdentifier("SinAdd", "Test"), cadd.SubscriptionAxis[0], "D16"); Assert.AreEqual(0, cadd.PatternAxis.Count, "D18"); CoalescedTreeNode csqr = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[1]; Assert.AreEqual(true, csqr.Condition is EntityCondition, "D19"); Assert.AreEqual(new MathIdentifier("Square", "Std"), ((EntityCondition)csqr.Condition).EntityId, "D20"); Assert.AreEqual(1, csqr.GroupAxis.Count, "D21"); Assert.AreEqual(1, csqr.SubscriptionAxis.Count, "D22"); Assert.AreEqual("sqr", csqr.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D23"); Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), csqr.SubscriptionAxis[0], "D24"); Assert.AreEqual(0, csqr.PatternAxis.Count, "D26"); // test whether the tree works as expected MatchCollection res = Match.MatchAll(sinx2, sinx2.DrivenByPort, root); Assert.AreEqual(true, res.Contains(new MathIdentifier("SinSqr", "Test")), "D27"); Assert.AreEqual(false, res.Contains(new MathIdentifier("SinAdd", "Test")), "D28"); Match match = res[new MathIdentifier("SinSqr", "Test")]; Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), match.PatternId, "D29"); Assert.AreEqual(2, match.GroupCount, "D30"); Assert.AreEqual(1, match["sin"].Count, "D31"); Assert.AreEqual(sinx2.InstanceId, match["sin"][0].First.InstanceId, "D32"); Assert.AreEqual(1, match["sqr"].Count, "D33"); Assert.AreEqual(x2.InstanceId, match["sqr"][0].First.InstanceId, "D34"); }
public void SystemToXmlSerializeTest() { Project p = new Project(); MathSystem s1 = p.CurrentSystem; // BUILD SYSTEM 1: sin(x^2)*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"); // SERIALIZE SYSTEM 1 TO XML /* * HINT: would be simpler to just call: * string s2xml = s1.WriteXml(false); */ StringBuilder sb = new StringBuilder(); { XmlWriterSettings settings = new XmlWriterSettings(); settings.ConformanceLevel = ConformanceLevel.Document; settings.OmitXmlDeclaration = false; settings.Indent = true; settings.NewLineHandling = NewLineHandling.Entitize; settings.Encoding = Config.InternalEncoding; XmlWriter xwriter = XmlWriter.Create(sb, settings); xwriter.WriteStartElement("Systems"); XmlSystemWriter writer = new XmlSystemWriter(xwriter); SystemReader reader = new SystemReader(writer); reader.ReadSystem(s1); xwriter.WriteEndElement(); xwriter.Flush(); xwriter.Close(); } string s2xml = sb.ToString(); Console.WriteLine(s2xml); // READER XML BACK TO SYSTEM 2 /* * HINT: would be simpler to just call: * MathSystem s2 = MathSystem.ReadXml(s2xml, c); */ IMathSystem s2; { StringReader sr = new StringReader(s2xml); XmlReader xreader = XmlReader.Create(sr); xreader.ReadToFollowing("Systems"); xreader.Read(); SystemWriter writer = new SystemWriter(); XmlSystemReader reader = new XmlSystemReader(writer); reader.ReadSystems(xreader, false); xreader.ReadEndElement(); 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"); //-0.0331792165475568 }