Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        public double[] Evaluate(params double[] inputs)
        {
            if (inputs == null)
            {
                throw new ArgumentNullException("inputs");
            }

            if (inputs.Length != _inputs.Count)
            {
                throw new System.ArgumentException("The count of inputs doesn't match the systems count of input signals.", "inputs");
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                _inputs[i].PostNewValue(new RealValue(inputs[i]));
            }

            Service <ISimulationMediator> .Instance.SimulateInstant();

            double[] outputs = new double[_outputs.Count];
            for (int i = 0; i < outputs.Length; i++)
            {
                outputs[i] = RealValue.ConvertFrom(_outputs[i].Value).Value;
            }
            return(outputs);
        }
Ejemplo n.º 4
0
 /// <returns>true if an event shall be posted.</returns>
 private bool UpdatePeriods(int index, Signal inputSignal)
 {
     if (RealValue.CanConvertLosslessFrom(inputSignal.Value))
     {
         periods[index] = TimeSpan.FromSeconds(1.0 / RealValue.ConvertFrom(inputSignal.Value).Value);
         return(true);
     }
     else
     {
         periods[index] = TimeSpan.Zero;
         return(false);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Whether the signal is restricted to be <= 0
        /// </summary>
        public static bool IsAlwaysNonpositive(Signal signal)
        {
            if (signal == null)
            {
                throw new ArgumentNullException("signal");
            }

            return(signal.IsFlagEnabled(StdAspect.NegativeOrZeroConstraintFlag) ||
                   IsConstant(signal) && RealValue.CanConvertLosslessFrom(signal.Value) && RealValue.ConvertFrom(signal.Value).Value <= 0d);
        }