Beispiel #1
0
        private static bool Equals(PrefixedUnit o1, PrefixedUnit o2)
        {
            if (o1 == null || o2 == null)
            {
                return(false);
            }

            return(Equals(o1.BaseUnit, o2.BaseUnit) && Equals(o1.Prefix, o2.Prefix));
        }
Beispiel #2
0
        public void SetUp()
        {
            this.prefixedUnitRuleChecker = new PrefixedUnitRuleChecker();

            this.siteReferenceDataLibrary = new SiteReferenceDataLibrary();

            this.prefixedUnit = new PrefixedUnit();

            this.siteReferenceDataLibrary.Unit.Add(this.prefixedUnit);
        }
Beispiel #3
0
        public static PrefixedUnit?ConvertTextToPrefixedUnit(string?txt)
        {
            PrefixedUnit?result = null;

            switch (txt)
            {
            // volts
            case "V":
                result = new PrefixedUnit(Unit.V);
                break;

            case "mV":
                result = new PrefixedUnit(Multiple.m, Unit.V);
                break;

            case MicroVolt:
                result = new PrefixedUnit(Multiple.mi, Unit.V);
                break;

            case "nV":
                result = new PrefixedUnit(Multiple.n, Unit.V);
                break;

            // seconds
            case "S":
                result = new PrefixedUnit(Unit.S);
                break;

            case "mS":
                result = new PrefixedUnit(Multiple.m, Unit.S);
                break;

            case MicroSecond:
                result = new PrefixedUnit(Multiple.mi, Unit.S);
                break;

            case "nS":
                result = new PrefixedUnit(Multiple.n, Unit.S);
                break;

            // other
            case "C":
                result = new PrefixedUnit(Unit.Celsius);
                break;
                //case "mg": acceleration: m/s^2 in g=9.81
                //    return PrefixedUnit.None;
                //case "ARU": // Arbitrary respiration unit
                //    return PrefixedUnit.None;
            }

            return(result);
        }
        public void Setup()
        {
            this.prefixedUnit = new PrefixedUnit();

            this.unitPrefix = new UnitPrefix();
            this.unitPrefix.ConversionFactor = "conv";
            this.unitPrefix.Name             = "unit";
            this.unitPrefix.ShortName        = "u";

            this.measurementUnit           = new SimpleUnit();
            this.measurementUnit.Name      = "measurement";
            this.measurementUnit.ShortName = "m";
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PrefixedUnitDialogViewModel"/> class.
        /// </summary>
        /// <param name="prefixedUnit">
        /// The <see cref="PrefixedUnit"/> that is the subject of the current view-model. This is the object
        /// that will be either created, or edited.
        /// </param>
        /// <param name="transaction">
        /// The <see cref="ThingTransaction"/> that contains the log of recorded changes.
        /// </param>
        /// <param name="session">
        /// The <see cref="ISession"/> in which the current <see cref="Thing"/> is to be added or updated
        /// </param>
        /// <param name="isRoot">
        /// Assert if this <see cref="PrefixedUnitDialogViewModel"/> is the root of all <see cref="IThingDialogViewModel"/>
        /// </param>
        /// <param name="dialogKind">
        /// The kind of operation this <see cref="PrefixedUnitDialogViewModel"/> performs
        /// </param>
        /// <param name="thingDialogNavigationService">
        /// The <see cref="IThingDialogNavigationService"/> that is used to navigate to a dialog of a specific <see cref="Thing"/>.
        /// </param>
        /// <param name="container">
        /// The <see cref="Thing"/> that contains the created <see cref="Thing"/> in this Dialog
        /// </param>
        /// <param name="chainOfContainers">
        /// The optional chain of containers that contains the <paramref name="container"/> argument
        /// </param>
        public PrefixedUnitDialogViewModel(PrefixedUnit prefixedUnit, IThingTransaction transaction, ISession session, bool isRoot, ThingDialogKind dialogKind, IThingDialogNavigationService thingDialogNavigationService, Thing container = null, IEnumerable <Thing> chainOfContainers = null)
            : base(prefixedUnit, transaction, session, isRoot, dialogKind, thingDialogNavigationService, container, chainOfContainers)
        {
            this.WhenAnyValue(vm => vm.Container).Subscribe(_ => this.PopulatePossiblePrefix());
            this.WhenAnyValue(vm => vm.Container).Subscribe(_ => this.PopulatePossibleReferenceUnit());

            this.WhenAnyValue(vm => vm.Container).Subscribe(_ => this.UpdateOkCanExecute());
            this.WhenAnyValue(vm => vm.SelectedPrefix).Subscribe(_ => this.SetShortNameAndName());
            this.WhenAnyValue(vm => vm.SelectedPrefix).Subscribe(_ => this.UpdateOkCanExecute());

            this.WhenAnyValue(vm => vm.SelectedReferenceUnit).Subscribe(_ => this.SetShortNameAndName());
            this.WhenAnyValue(vm => vm.SelectedReferenceUnit).Subscribe(_ => this.UpdateOkCanExecute());
        }
Beispiel #6
0
        public void VerifySetNameAndShortName()
        {
            var prefixedUnit = new PrefixedUnit(Guid.NewGuid(), null, null);
            var vm           = new PrefixedUnitDialogViewModel(prefixedUnit, this.transaction, this.session.Object, true, ThingDialogKind.Create, this.dialogService.Object, null);

            vm.SelectedPrefix = null;

            Assert.AreEqual(string.Empty, vm.Name);
            Assert.AreEqual(string.Empty, vm.ShortName);

            vm.SelectedPrefix = vm.PossiblePrefix.First();

            Assert.AreEqual("centigram", vm.Name);
            Assert.AreEqual("cg", vm.ShortName);
        }
Beispiel #7
0
        public void VerifyThatPropertiesAreSet()
        {
            var prefixedUnit = new PrefixedUnit(Guid.NewGuid(), null, null);
            var vm           = new PrefixedUnitDialogViewModel(prefixedUnit, this.transaction, this.session.Object, true, ThingDialogKind.Create, this.dialogService.Object, null);

            Assert.AreEqual(this.genericSiteReferenceDataLibrary.Iid, vm.Container.Iid);
            Assert.IsNotNull(vm.SelectedPrefix);
            Assert.IsNotNull(vm.SelectedReferenceUnit);
            Assert.That(vm.ShortName, Is.Not.Null.Or.Not.Empty);
            Assert.That(vm.Name, Is.Not.Null.Or.Not.Empty);

            Assert.That(vm["ShortName"], Is.Empty.Or.Null);
            Assert.That(vm["Name"], Is.Empty.Or.Null);

            Assert.IsTrue(vm.OkCommand.CanExecute(null));
        }
Beispiel #8
0
        /// <summary>
        /// Tries to get a prefixed unit from a shortcut, considering all units in this environment.
        /// </summary>
        /// <param name="shortCut">The shortcut. Can be a compound of prefix and unit, e.g. 'mA'. An empty string is converted to <see cref="Altaxo.Units.Dimensionless.Unity.Instance"/></param>
        /// <param name="result">If successfully, the resulting prefixed unit.</param>
        /// <returns>True if the conversion was successful; false otherwise.</returns>
        /// <exception cref="ArgumentNullException">s</exception>
        public bool TryGetPrefixedUnitFromShortcut(string shortCut, out IPrefixedUnit result)
        {
            if (null == shortCut)
            {
                throw new ArgumentNullException(nameof(shortCut));
            }

            shortCut = shortCut.Trim();

            if ("" == shortCut) // If string is empty, we consider this as a dimensionless unit "Unity"
            {
                result = new PrefixedUnit(SIPrefix.None, Altaxo.Units.Dimensionless.Unity.Instance);
                return(true);
            }

            SIPrefix prefix = null;

            foreach (IUnit u in UnitsSortedByShortcutLengthDescending) // for each unit
            {
                if (string.IsNullOrEmpty(u.ShortCut) || (!shortCut.EndsWith(u.ShortCut)))
                {
                    continue;
                }

                var prefixString = shortCut.Substring(0, shortCut.Length - u.ShortCut.Length);

                if (prefixString.Length == 0) // if prefixString is empty, then it is the unit without prefix
                {
                    result = new PrefixedUnit(SIPrefix.None, u);
                    return(true);
                }

                prefix = SIPrefix.TryGetPrefixFromShortcut(prefixString);

                if (null != prefix) // we found a prefix, thus we can return prefix + unit
                {
                    result = new PrefixedUnit(prefix, u);
                    return(true);
                }
            }

            result = null;
            return(false);
        }
        /// <summary>
        /// Serialize the <see cref="PrefixedUnit"/>
        /// </summary>
        /// <param name="prefixedUnit">The <see cref="PrefixedUnit"/> to serialize</param>
        /// <returns>The <see cref="JObject"/></returns>
        private JObject Serialize(PrefixedUnit prefixedUnit)
        {
            var jsonObject = new JObject();

            jsonObject.Add("alias", this.PropertySerializerMap["alias"](prefixedUnit.Alias.OrderBy(x => x, this.guidComparer)));
            jsonObject.Add("classKind", this.PropertySerializerMap["classKind"](Enum.GetName(typeof(CDP4Common.CommonData.ClassKind), prefixedUnit.ClassKind)));
            jsonObject.Add("definition", this.PropertySerializerMap["definition"](prefixedUnit.Definition.OrderBy(x => x, this.guidComparer)));
            jsonObject.Add("excludedDomain", this.PropertySerializerMap["excludedDomain"](prefixedUnit.ExcludedDomain.OrderBy(x => x, this.guidComparer)));
            jsonObject.Add("excludedPerson", this.PropertySerializerMap["excludedPerson"](prefixedUnit.ExcludedPerson.OrderBy(x => x, this.guidComparer)));
            jsonObject.Add("hyperLink", this.PropertySerializerMap["hyperLink"](prefixedUnit.HyperLink.OrderBy(x => x, this.guidComparer)));
            jsonObject.Add("iid", this.PropertySerializerMap["iid"](prefixedUnit.Iid));
            jsonObject.Add("isDeprecated", this.PropertySerializerMap["isDeprecated"](prefixedUnit.IsDeprecated));
            jsonObject.Add("modifiedOn", this.PropertySerializerMap["modifiedOn"](prefixedUnit.ModifiedOn));
            jsonObject.Add("prefix", this.PropertySerializerMap["prefix"](prefixedUnit.Prefix));
            jsonObject.Add("referenceUnit", this.PropertySerializerMap["referenceUnit"](prefixedUnit.ReferenceUnit));
            jsonObject.Add("revisionNumber", this.PropertySerializerMap["revisionNumber"](prefixedUnit.RevisionNumber));
            jsonObject.Add("thingPreference", this.PropertySerializerMap["thingPreference"](prefixedUnit.ThingPreference));
            return(jsonObject);
        }
Beispiel #10
0
        public void TestUnitCompositions()
        {
            var quantA = BaseQuantity.DefineNewBaseQuantity("quantA",
                                                            "a");

            var a      = (BaseUnit)quantA.FundamentalUnit;
            var b      = new BaseUnit("b", a, 2);
            var prefix = new Prefix("prefix", 4);
            var c      = PrefixedUnit.GetPrefixedUnit(a, prefix);

            var composition =
                a.UnitComposition *
                b.UnitComposition /
                c.UnitComposition;

            var d = Unit.DefineFromComposition("d", composition);

            Assert.AreSame(quantA, d.Quantity);
            Assert.IsInstanceOfType(d, typeof(BaseUnit));
            Assert.AreEqual(0.5m, d.FundamentalMultiplier);
        }
Beispiel #11
0
        public QuantityWithUnitGuiEnvironment(IEnumerable <IUnit> fixedUnits, IEnumerable <IUnit> additionalUnits)
        {
            if (null != fixedUnits)
            {
                _fixedUnits = fixedUnits.ToArray();
            }
            else
            {
                _fixedUnits = _emptyUnitList;
            }

            _additionalUnits = new ObservableCollection <IUnit>(additionalUnits);
            CreateUnitListSortedByShortcutLengthDescending();
            _additionalUnits.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(EhAdditionalUnits_CollectionChanged);

            if (null != fixedUnits?.FirstOrDefault())
            {
                DefaultUnit = new PrefixedUnit(SIPrefix.None, fixedUnits.FirstOrDefault());
            }
            else if (0 < _additionalUnits.Count)
            {
                DefaultUnit = new PrefixedUnit(SIPrefix.None, _additionalUnits[0]);
            }
        }
Beispiel #12
0
        public void VerififyThatInvalidContainerThrowsException()
        {
            var prefixedUnit = new PrefixedUnit(Guid.NewGuid(), null, null);

            Assert.Throws <ArgumentException>(() => new PrefixedUnitDialogViewModel(prefixedUnit, this.transaction, this.session.Object, true, ThingDialogKind.Inspect, this.dialogService.Object, this.siteDir));
        }
Beispiel #13
0
 public EegChannel(string name, ChannelType type, PrefixedUnit units)
 {
     Name  = name;
     Type  = type;
     Units = units;
 }
        public void Setup()
        {
            this.npgsqlTransaction = null;
            this.securityContext   = new Mock <ISecurityContext>();

            // There is a chain a -> b -> c
            this.prefixedUnitC = new PrefixedUnit {
                Iid = Guid.NewGuid()
            };
            this.prefixedUnitB = new PrefixedUnit {
                Iid = Guid.NewGuid(), ReferenceUnit = this.prefixedUnitC.Iid
            };
            this.prefixedUnitA = new PrefixedUnit {
                Iid = Guid.NewGuid(), ReferenceUnit = this.prefixedUnitB.Iid
            };
            this.prefixedUnitD = new PrefixedUnit {
                Iid = Guid.NewGuid()
            };

            // Outside the rdl chain
            this.prefixedUnitE = new PrefixedUnit {
                Iid = Guid.NewGuid()
            };

            // There is a chain librayA -> LibraryB
            this.referenceDataLibraryB =
                new SiteReferenceDataLibrary {
                Iid = Guid.NewGuid(), Unit = { this.prefixedUnitD.Iid }
            };
            this.referenceDataLibraryA = new ModelReferenceDataLibrary
            {
                Iid  = Guid.NewGuid(),
                Unit =
                {
                    this.prefixedUnitA.Iid,
                    this.prefixedUnitB.Iid,
                    this.prefixedUnitC.Iid
                },
                RequiredRdl = this.referenceDataLibraryB.Iid
            };

            this.siteReferenceDataLibraryService = new Mock <ISiteReferenceDataLibraryService>();
            this.siteReferenceDataLibraryService
            .Setup(
                x => x.Get(
                    this.npgsqlTransaction,
                    It.IsAny <string>(),
                    null,
                    It.IsAny <ISecurityContext>()))
            .Returns(new List <ReferenceDataLibrary> {
                this.referenceDataLibraryB
            });

            this.conversionBasedUnitService = new Mock <IConversionBasedUnitService>();
            this.conversionBasedUnitService
            .Setup(
                x => x.Get(
                    this.npgsqlTransaction,
                    It.IsAny <string>(),
                    new List <Guid>
            {
                this.prefixedUnitD.Iid,
                this.prefixedUnitA.Iid,
                this.prefixedUnitB.Iid,
                this.prefixedUnitC.Iid
            },
                    It.IsAny <ISecurityContext>())).Returns(
                new List <ConversionBasedUnit>
            {
                this.prefixedUnitD,
                this.prefixedUnitA,
                this.prefixedUnitB,
                this.prefixedUnitC
            });
        }
Beispiel #15
0
        public void TestConversionsAndDefinitions()
        {
            /*  A = fundamental
             *      A = 2*B + 40
             *      A = 8*C + 360
             *
             *  B = (A/2) - 20 = 4*C + 160
             *      B.multiplier = 2
             *      B.offset = 20
             *
             *  C = (B/4) - 40 = (A/8) - 45
             *      C.multiplier = 8
             *      C.offset = 45
             */

            var quant = BaseQuantity.DefineNewBaseQuantity(
                "quantity", "a");

            var      a = (BaseUnit)quant.FundamentalUnit;
            BaseUnit b = new ("b", a,
                              multiplier : 2m,
                              offset : 20m);

            BaseUnit c = new ("c", b,
                              multiplier : 4m,
                              offset : 40m);


            Assert.AreEqual(1m, a.FundamentalMultiplier);
            Assert.AreEqual(0m, a.FundamentalOffset);

            Assert.AreEqual(2m, b.FundamentalMultiplier);
            Assert.AreEqual(20m, b.FundamentalOffset);

            Assert.AreEqual(8m, c.FundamentalMultiplier);
            Assert.AreEqual(45m, c.FundamentalOffset);

            {
                // Convert to fundamental
                var aAsFund = a.ConvertToFundamental(3m);
                Assert.AreEqual(3m, aAsFund.Magnitude);
                Assert.AreSame(a, aAsFund.Unit);

                var bAsFund = b.ConvertToFundamental(3m);
                Assert.AreEqual(46m, bAsFund.Magnitude);
                Assert.AreSame(a, bAsFund.Unit);

                var cAsFund = c.ConvertToFundamental(3m);
                Assert.AreEqual(384m, cAsFund.Magnitude);
                Assert.AreSame(a, cAsFund.Unit);
            }

            {
                // Convert to A
                var aAsA = a.ConvertTo(3m, a);
                Assert.AreEqual(3m, aAsA.Magnitude);
                Assert.AreSame(a, aAsA.Unit);

                var bAsA = b.ConvertToFundamental(3m);
                Assert.AreEqual(46m, bAsA.Magnitude);
                Assert.AreSame(a, bAsA.Unit);

                var cAsA = c.ConvertToFundamental(3m);
                Assert.AreEqual(384m, cAsA.Magnitude);
                Assert.AreSame(a, cAsA.Unit);
            }

            {
                // Convert to B
                var aAsB = a.ConvertTo(4m, b);
                Assert.AreEqual(-18m, aAsB.Magnitude);
                Assert.AreSame(b, aAsB.Unit);

                var bAsB = b.ConvertTo(3m, b);
                Assert.AreEqual(3m, bAsB.Magnitude);
                Assert.AreSame(b, bAsB.Unit);

                var cAsB = c.ConvertTo(2m, b);
                Assert.AreEqual(168m, cAsB.Magnitude);
                Assert.AreSame(b, cAsB.Unit);
            }

            {
                // Convert to C
                var aAsC = a.ConvertTo(16m, c);
                Assert.AreEqual(-43m, aAsC.Magnitude);
                Assert.AreSame(c, aAsC.Unit);

                var bAsC = b.ConvertTo(8m, c);
                Assert.AreEqual(-38m, bAsC.Magnitude);
                Assert.AreSame(c, bAsC.Unit);

                var cAsC = c.ConvertTo(3m, c);
                Assert.AreEqual(3m, cAsC.Magnitude);
                Assert.AreSame(c, cAsC.Unit);
            }

            Prefix testPrefix = new("TestPrefix", 10m);
            var    pA         = PrefixedUnit.GetPrefixedUnit(a, testPrefix);
            var    pB         = PrefixedUnit.GetPrefixedUnit(b, testPrefix);
            var    pC         = PrefixedUnit.GetPrefixedUnit(c, testPrefix);

            Assert.AreEqual(10m, pA.FundamentalMultiplier);
            Assert.AreEqual(0m, pA.FundamentalOffset);

            Assert.AreEqual(20m, pB.FundamentalMultiplier);
            Assert.AreEqual(2m, pB.FundamentalOffset);

            Assert.AreEqual(80m, pC.FundamentalMultiplier);
            Assert.AreEqual(4.5m, pC.FundamentalOffset);

            {
                var aAsPa = a.ConvertTo(20m, pA);
                Assert.AreEqual(2m, aAsPa.Magnitude);
                Assert.AreSame(pA, aAsPa.Unit);

                var cAsPc = c.ConvertTo(20m, pC);
                Assert.AreEqual(2m, cAsPc.Magnitude);
                Assert.AreSame(pC, cAsPc.Unit);

                var paAsA = pA.ConvertTo(2m, a);
                Assert.AreEqual(20m, paAsA.Magnitude);
                Assert.AreSame(a, paAsA.Unit);

                var pcAsC = pC.ConvertTo(2m, c);
                Assert.AreEqual(20m, pcAsC.Magnitude);
                Assert.AreSame(c, pcAsC.Unit);
            }

            {
                var aAsPc = a.ConvertTo(3200m, pC);
                Assert.AreEqual(35.5m, aAsPc.Magnitude);
                Assert.AreSame(pC, aAsPc.Unit);

                var cAsPa = c.ConvertTo(30m, pA);
                Assert.AreEqual(60m, cAsPa.Magnitude);
                Assert.AreSame(pA, cAsPa.Unit);

                var paAsC = pA.ConvertTo(60m, c);
                Assert.AreEqual(30m, paAsC.Magnitude);
                Assert.AreSame(c, paAsC.Unit);

                var pCAsA = pC.ConvertTo(35.5m, a);
                Assert.AreEqual(3200m, pCAsA.Magnitude);
                Assert.AreSame(a, pCAsA.Unit);
            }
        }
Beispiel #16
0
 public abstract T Visit(PrefixedUnit v);
        /// <summary>
        /// Persist the DTO composition to the ORM layer.
        /// </summary>
        /// <param name="transaction">
        /// The transaction object.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource will be stored.
        /// </param>
        /// <param name="prefixedUnit">
        /// The prefixedUnit instance to persist.
        /// </param>
        /// <returns>
        /// True if the persistence was successful.
        /// </returns>
        private bool CreateContainment(NpgsqlTransaction transaction, string partition, PrefixedUnit prefixedUnit)
        {
            var results = new List <bool>();

            foreach (var alias in this.ResolveFromRequestCache(prefixedUnit.Alias))
            {
                results.Add(this.AliasService.CreateConcept(transaction, partition, alias, prefixedUnit));
            }

            foreach (var definition in this.ResolveFromRequestCache(prefixedUnit.Definition))
            {
                results.Add(this.DefinitionService.CreateConcept(transaction, partition, definition, prefixedUnit));
            }

            foreach (var hyperLink in this.ResolveFromRequestCache(prefixedUnit.HyperLink))
            {
                results.Add(this.HyperLinkService.CreateConcept(transaction, partition, hyperLink, prefixedUnit));
            }

            return(results.All(x => x));
        }
Beispiel #18
0
 public EegCoordinateSystem(CoordinateSystem eegCoordinateSystem, PrefixedUnit eegCoordinateUnits)
 {
     EEGCoordinateSystem = eegCoordinateSystem;
     EEGCoordinateUnits  = eegCoordinateUnits;
 }