Пример #1
0
        /// <summary>
        /// Determines whether the integer is equal to the provided integer.
        /// </summary>
        /// <param name="other">The other integer.</param>
        /// <returns><c>true</c> if the integers are equal, <c>false</c> if not, and
        /// <c>null</c> if the conclusion of the comparison is not certain.</returns>
        public virtual Trilean IsEqualTo(IntegerValue other)
        {
            if (!IsKnown || !other.IsKnown)
            {
                // We are dealing with at least one unknown bit in the bit fields.
                // Conclusion is therefore either false or unknown.

                if (Size != other.Size)
                {
                    return(Trilean.False);
                }

                // Check if we definitely know this is not equal to the other.
                // TODO: this could probably use performance improvements.
                for (int i = 0; i < Size * 8; i++)
                {
                    Trilean a = GetBit(i);
                    Trilean b = other.GetBit(i);

                    if (a.IsKnown && b.IsKnown && a.Value != b.Value)
                    {
                        return(Trilean.False);
                    }
                }

                return(Trilean.Unknown);
            }

            return(Equals(other));
        }
Пример #2
0
        /// <summary>
        /// Adds a second (partially) known integer to the current integer.
        /// </summary>
        /// <param name="other">The integer to add.</param>
        /// <exception cref="ArgumentException">Occurs when the sizes of the integers do not match.</exception>
        public virtual void Add(IntegerValue other)
        {
            // The following implements a ripple full-adder.

            AssertSameBitSize(other);

            Span <byte> sumBuffer  = stackalloc byte[Size];
            Span <byte> maskBuffer = stackalloc byte[Size];

            maskBuffer.Fill(0xFF);

            var sum  = new BitField(sumBuffer);
            var mask = new BitField(maskBuffer);

            Trilean carry = false;

            for (int i = 0; i < sumBuffer.Length * 8; i++)
            {
                var a = GetBit(i);
                var b = other.GetBit(i);

                // Implement full-adder logic.
                var s = a ^ b ^ carry;
                var c = (carry & (a ^ b)) | (a & b);

                sum[i]  = s.ToBooleanOrFalse();
                mask[i] = s.IsKnown;
                carry   = c;
            }

            SetBits(sumBuffer, maskBuffer);
        }
Пример #3
0
        public static Vector3D Op3dCalculateDestinationXYOnly(Trilean bExtruderInRelativeMode, GCode ogcGCodeRequest, Vector3D op3dInitial)
        {
            var vector3D = new Vector3D(op3dInitial);

            if (bExtruderInRelativeMode == Trilean.True)
            {
                if (ogcGCodeRequest.HasX)
                {
                    vector3D.x += ogcGCodeRequest.X;
                }

                if (ogcGCodeRequest.HasY)
                {
                    vector3D.y += ogcGCodeRequest.Y;
                }
            }
            else
            {
                if (ogcGCodeRequest.HasX)
                {
                    vector3D.x = ogcGCodeRequest.X;
                }

                if (ogcGCodeRequest.HasY)
                {
                    vector3D.y = ogcGCodeRequest.Y;
                }
            }
            return(vector3D);
        }
Пример #4
0
        public async Task HospitalizationRecommendedGivenThreeOrMoreSymptoms(Trilean concussionHistory,
                                                                             Trilean alteredMentalStatus, Trilean basilarSkullFracture, Trilean palpableSkullFracture,
                                                                             Trilean signsOfOtherSkullFracture,
                                                                             Trilean lossOfConciousness, Trilean vomiting,
                                                                             Trilean severeHeadache, Trilean severeMechanismOfInjury, Trilean optScalpHematoma, Trilean otherScalpHematoma,
                                                                             Trilean abnormalBehaviorPerParents)
        {
            _patient.Data.ConcussionHistory            = concussionHistory;
            _patient.Data.SignsOfAlteredMentalStatus   = alteredMentalStatus;
            _patient.Data.SignsOfBasilarSkullFracture  = basilarSkullFracture;
            _patient.Data.SignsOfPalpableSkullFracture = palpableSkullFracture;
            _patient.Data.SignsOfOtherSkullFracture    = signsOfOtherSkullFracture;
            _patient.Data.LossOfConsciousness          = lossOfConciousness;
            _patient.Data.Vomiting                = vomiting;
            _patient.Data.SevereHeadache          = severeHeadache;
            _patient.Data.SevereMechanismOfInjury = severeMechanismOfInjury;
            _patient.Data.OptScalpHematoma        = optScalpHematoma;
            _patient.Data.OtherScalpHematoma      = otherScalpHematoma;
            _patient.Data.AbnormalBehaviorPerParentalAssessment = abnormalBehaviorPerParents;

            var results = await _rule.EvaluateAsync(_patient);

            Assert.Single(results,
                          x => x.Type == DecisionSupportResultType.ActionRecommendation &&
                          x.Description == HospitalizationRecommended);
        }
Пример #5
0
        /// <summary>
        /// Subtracts a second (partially) known integer from the current integer.
        /// </summary>
        /// <param name="other">The integer to subtract.</param>
        /// <exception cref="ArgumentException">Occurs when the sizes of the integers do not match.</exception>
        public virtual void Subtract(IntegerValue other)
        {
            // The following implements a ripple full-subtractor.

            AssertSameBitSize(other);

            Span <byte> differenceBuffer = stackalloc byte[Size];
            Span <byte> maskBuffer       = stackalloc byte[Size];

            maskBuffer.Fill(0xFF);

            var difference = new BitField(differenceBuffer);
            var mask       = new BitField(maskBuffer);

            Trilean borrow = false;

            for (int i = 0; i < differenceBuffer.Length * 8; i++)
            {
                var a = GetBit(i);
                var b = other.GetBit(i);

                // Implement full-subtractor logic.
                var d    = a ^ b ^ borrow;
                var bOut = (!a & borrow) | (!a & b) | (b & borrow);

                difference[i] = d.ToBooleanOrFalse();
                mask[i]       = d.IsKnown;
                borrow        = bOut;
            }

            SetBits(differenceBuffer, maskBuffer);
        }
Пример #6
0
        public void NotTest(TrileanValue value, TrileanValue expected)
        {
            Trilean x = value;
            Trilean y = expected;

            Assert.Equal(y, x.Not());
            Assert.Equal(y, !x);
        }
Пример #7
0
 /// <summary>
 /// Computes the overflow operator of the trilean.
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 public static Trilean3 Overflow(Trilean3 left, Trilean3 right)
 {
     return(new Trilean3(
                Trilean.Overflow(left.X, right.X),
                Trilean.Overflow(left.Y, right.Y),
                Trilean.Overflow(left.Z, right.Z)
                ));
 }
Пример #8
0
 /// <summary>
 /// Determines if the trileans are valued (not ambiguous).
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static Boolean3 IsValued(Trilean3 value)
 {
     return(new Boolean3(
                Trilean.IsValued(value.X),
                Trilean.IsValued(value.Y),
                Trilean.IsValued(value.Z)
                ));
 }
Пример #9
0
        public void AndTest(TrileanValue a, TrileanValue b, TrileanValue expected)
        {
            Trilean x = a;
            Trilean y = b;
            Trilean z = expected;

            Assert.Equal(z, x.And(y));
            Assert.Equal(z, x & y);
        }
Пример #10
0
        public void XorTest(TrileanValue a, TrileanValue b, TrileanValue expected)
        {
            Trilean x = a;
            Trilean y = b;
            Trilean z = expected;

            Assert.Equal(z, x.Xor(y));
            Assert.Equal(z, x ^ y);
        }
Пример #11
0
 public Extruder()
 {
     ishomed            = Trilean.Unknown;
     inRelativeMode     = Trilean.Unknown;
     position           = new Vector3DE(0.0f, 0.0f, 0.0f, 0.0f);
     Z_Valid            = true;
     Temperature        = -273f;
     Fan                = 0;
     iNozzleSizeMicrons = 0;
 }
Пример #12
0
 public Extruder(Extruder rhs)
 {
     ishomed            = rhs.ishomed;
     inRelativeMode     = rhs.inRelativeMode;
     position           = rhs.position;
     Z_Valid            = rhs.Z_Valid;
     Temperature        = rhs.Temperature;
     iNozzleSizeMicrons = rhs.iNozzleSizeMicrons;
     Fan = rhs.Fan;
 }
Пример #13
0
        public static void VGetEffectiveMovementGCode(Trilean bExtruderIsHomed, bool bExtruderZValid, Trilean bExtruderInRelativeMode, Vector3D op3dDestination, Vector3D op3dInitial, ref GCode ogcGCodeEffectiveMove)
        {
            if (bExtruderInRelativeMode == Trilean.True)
            {
                if (bExtruderIsHomed == Trilean.True)
                {
                    if (ogcGCodeEffectiveMove.HasX)
                    {
                        ogcGCodeEffectiveMove.X = op3dDestination.x - op3dInitial.x;
                    }

                    if (ogcGCodeEffectiveMove.HasY)
                    {
                        ogcGCodeEffectiveMove.Y = op3dDestination.y - op3dInitial.y;
                    }
                }
                if (!bExtruderZValid || !ogcGCodeEffectiveMove.HasZ)
                {
                    return;
                }

                ogcGCodeEffectiveMove.Z = op3dDestination.z - op3dInitial.z;
            }
            else
            {
                if (Trilean.False != bExtruderInRelativeMode)
                {
                    return;
                }

                if (bExtruderIsHomed == Trilean.True)
                {
                    if (ogcGCodeEffectiveMove.HasX)
                    {
                        ogcGCodeEffectiveMove.X = op3dDestination.x;
                    }

                    if (ogcGCodeEffectiveMove.HasY)
                    {
                        ogcGCodeEffectiveMove.Y = op3dDestination.y;
                    }
                }
                if (!bExtruderZValid || !ogcGCodeEffectiveMove.HasZ)
                {
                    return;
                }

                ogcGCodeEffectiveMove.Z = op3dDestination.z;
            }
        }
Пример #14
0
        public static bool?ToBool(this Trilean value)
        {
            switch (value)
            {
            case Trilean.False:
                return(false);

            case Trilean.True:
                return(true);

            case Trilean.Unknown:
                return(null);
            }
            return(null);
        }
Пример #15
0
        public static Vector3D Op3dCalculateDestination(Trilean bExtruderIsHomed, bool bExtruderZValid, Trilean bExtruderInRelativeMode, GCode ogcGCodeRequest, Vector3D op3dInitial)
        {
            var op3dInitial1 = new Vector3D(op3dInitial);

            if (bExtruderIsHomed == Trilean.True)
            {
                op3dInitial1 = MovementUtility.Op3dCalculateDestinationXYOnly(bExtruderInRelativeMode, ogcGCodeRequest, op3dInitial1);
            }

            if (bExtruderZValid)
            {
                op3dInitial1 = MovementUtility.Op3dCalculateDestinationZOnly(bExtruderInRelativeMode, ogcGCodeRequest, op3dInitial1);
            }

            return(op3dInitial1);
        }
Пример #16
0
        public static Vector3D Op3dCalculateDestinationZOnly(Trilean bExtruderInRelativeMode, GCode ogcGCodeRequest, Vector3D op3dInitial)
        {
            var vector3D = new Vector3D(op3dInitial);

            if (ogcGCodeRequest.HasZ)
            {
                if (bExtruderInRelativeMode == Trilean.True)
                {
                    vector3D.z += ogcGCodeRequest.Z;
                }
                else
                {
                    vector3D.z = ogcGCodeRequest.Z;
                }
            }
            return(vector3D);
        }
Пример #17
0
        public async Task MoreInformationNeededGivenPartialInformation(Trilean concussionHistory,
                                                                       Trilean alteredMentalStatus, Trilean basilarSkullFracture, Trilean palpableSkullFracture,
                                                                       Trilean signsOfOtherSkullFracture,
                                                                       Trilean lossOfConciousness, Trilean vomiting,
                                                                       Trilean severeHeadache, Trilean severeMechanismOfInjury, Trilean optScalpHematoma,
                                                                       Trilean otherScalpHematoma,
                                                                       Trilean abnormalBehaviorPerParents)
        {
            _patient.Data.ConcussionHistory            = concussionHistory;
            _patient.Data.SignsOfAlteredMentalStatus   = alteredMentalStatus;
            _patient.Data.SignsOfBasilarSkullFracture  = basilarSkullFracture;
            _patient.Data.SignsOfPalpableSkullFracture = palpableSkullFracture;
            _patient.Data.SignsOfOtherSkullFracture    = signsOfOtherSkullFracture;
            _patient.Data.LossOfConsciousness          = lossOfConciousness;
            _patient.Data.Vomiting                = vomiting;
            _patient.Data.SevereHeadache          = severeHeadache;
            _patient.Data.SevereMechanismOfInjury = severeMechanismOfInjury;
            _patient.Data.OptScalpHematoma        = optScalpHematoma;
            _patient.Data.AbnormalBehaviorPerParentalAssessment = abnormalBehaviorPerParents;
            var results = await _rule.EvaluateAsync(_patient);

            var expected = new[]
            {
                nameof(Observations.ConcussionHistory),
                nameof(Observations.SignsOfAlteredMentalStatus),
                nameof(Observations.SignsOfBasilarSkullFracture),
                nameof(Observations.SignsOfPalpableSkullFracture),
                nameof(Observations.SignsOfOtherSkullFracture),
                nameof(Observations.LossOfConsciousness),
                nameof(Observations.Vomiting),
                nameof(Observations.SevereHeadache),
                nameof(Observations.SevereMechanismOfInjury),
                nameof(Observations.OptScalpHematoma),
                nameof(Observations.OtherScalpHematoma),
                nameof(Observations.AbnormalBehaviorPerParentalAssessment),
                nameof(Observations.SevereSymptoms),
                nameof(Observations.ChronicDiseases)
            };
            var observationType = typeof(Observations);

            expected = expected.Where(e =>
                                      Trilean.Unknown == (Trilean)observationType.GetProperty(e).GetValue(_patient.Data)).ToArray();
            Assert.Contains(results,
                            x => x.Type == DecisionSupportResultType.MoreInformationRequired &&
                            x.Description.Split(',').Count(d => expected.Contains(d.Trim())) == expected.Length);
        }
Пример #18
0
        public static Vector3D Op3dCalculateDestinationWithClipping(Trilean bExtruderIsHomed, bool bExtruderZValid, ref bool bDestinationHasBeenClipped, Vector3D op3dDestination, Vector3D op3dInitial, PrinterProfile printerProfile)
        {
            var intercept = new Vector3D();

            bDestinationHasBeenClipped = false;
            if (bExtruderIsHomed == Trilean.True)
            {
                bDestinationHasBeenClipped = printerProfile.PrinterSizeConstants.WarningRegion.LineIntercepts(out intercept, op3dInitial, op3dDestination);
            }
            else if (bExtruderZValid)
            {
                intercept.x = 0.0f;
                intercept.y = 0.0f;
                bDestinationHasBeenClipped = printerProfile.PrinterSizeConstants.UnhomedSafeZRange.Intercepts(out intercept.z, op3dInitial.z, op3dDestination.z);
            }
            return(intercept);
        }
Пример #19
0
        /// <summary>
        /// Determines whether the integer is greater than the provided integer.
        /// </summary>
        /// <param name="other">The other integer.</param>
        /// <param name="signed">Indicates the integers should be interpreted as signed or unsigned integers.</param>
        /// <returns><c>true</c> if the current integer is greater than the provided integer, <c>false</c> if not, and
        /// <c>null</c> if the conclusion of the comparison is not certain.</returns>
        public virtual Trilean IsGreaterThan(IntegerValue other, bool signed)
        {
            if (signed)
            {
                var thisSigned  = GetLastBit();
                var otherSigned = other.GetLastBit();
                if (thisSigned.IsUnknown || otherSigned.IsUnknown)
                {
                    return(Trilean.Unknown);
                }

                // If the signs do not match, we know the result
                if (thisSigned ^ otherSigned)
                {
                    return(otherSigned);
                }

                if (thisSigned)
                {
                    return(IsLessThan(other, false));
                }
            }

            // The following implements the "truth" table:
            // "-" indicates we do not know the answer yet.
            //
            //    | 0 | 1 | ?
            // ---+---+---+---
            //  0 | - | 0 | 0
            //  --+---+---+---
            //  1 | 1 | - | ?
            //  --+---+---+---
            //  ? | ? | 0 | ?

            AssertSameBitSize(other);

            for (int i = Size * 8 - 1; i >= 0; i--)
            {
                Trilean a = GetBit(i);
                Trilean b = other.GetBit(i);

                switch (a.Value, b.Value)
                {
Пример #20
0
        /// <summary>
        /// Parses a (partially) known bit string and sets the contents of integer to the parsed result.
        /// </summary>
        /// <param name="bitString">The bit string to parse.</param>
        /// <exception cref="OverflowException"></exception>
        public void SetBits(string bitString)
        {
            Span <byte> backingBits = stackalloc byte[Size];
            Span <byte> backingMask = stackalloc byte[Size];

            backingMask.Fill(0xFF);

            var bits = new BitField(backingBits);
            var mask = new BitField(backingMask);

            for (int i = 0; i < bitString.Length; i++)
            {
                Trilean bit = bitString[bitString.Length - i - 1] switch
                {
                    '0' => Trilean.False,
                    '1' => Trilean.True,
                    '?' => Trilean.Unknown,
                    _ => throw new FormatException()
                };

                if (i >= 8 * backingBits.Length)
                {
                    if (bit.IsUnknown || bit)
                    {
                        throw new OverflowException();
                    }
                }
                else if (bit.IsKnown)
                {
                    bits[i] = bit.ToBooleanOrFalse();
                }
                else
                {
                    mask[i] = false;
                }
            }

            SetBits(backingBits, backingMask);
        }
Пример #21
0
            public void setValue(string key, string value)
            {
                switch (key)
                {
                case "Stream Mode":
                    StreamMode = value;
                    break;

                case "DVB Subtitles":
                    HasDVB = GetBoolFromString(value);
                    break;

                case "Teletext":
                    HasTeletext = GetBoolFromString(value);
                    break;

                case "EIA-608":
                    Has608 = GetBoolFromString(value);
                    break;

                case "CEA-708":
                    Has708 = GetBoolFromString(value);
                    break;

                case "MPEG-4 Timed Text":
                    HasMPEG4TimedText = GetBoolFromString(value);
                    break;

                case "XDS":
                    HasXDS = GetBoolFromString(value);
                    break;

                default:
                    break;
                }
            }
Пример #22
0
 /// <summary>
 /// Constructs a three dimensional trilen with the given components.
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="z"></param>
 public Trilean3(Trilean x, Trilean y, Trilean z)
 {
     X = x;
     Y = y;
     Z = z;
 }
Пример #23
0
        /// <summary>
        /// Converts the provided trilean to an I4 stack value, pushes it onto the stack and returns the success
        /// dispatcher result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <param name="result">The trilean value.</param>
        /// <returns>The dispatch result.</returns>
        protected static DispatchResult ConvertToI4AndReturnSuccess(CilExecutionContext context, Trilean result)
        {
            var i4Result = new I4Value(result.ToBooleanOrFalse() ? 1 : 0, 0xFFFFFFFEu | (result.IsKnown ? 1u : 0u));

            context.ProgramState.Stack.Push(i4Result);
            return(DispatchResult.Success());
        }
Пример #24
0
 /// <inheritdoc />
 public override void SetBit(int index, Trilean value) => _value.SetBit(index, value);
Пример #25
0
 /// <summary>
 /// Writes a single bit value at the provided index.
 /// </summary>
 /// <param name="index">The index of the bit to write.</param>
 /// <param name="value">The new value of the bit to write. <c>null</c> indicates an unknown bit value.</param>
 /// <exception cref="ArgumentOutOfRangeException">Occurs when an invalid index was provided.</exception>
 public abstract void SetBit(int index, Trilean value);
Пример #26
0
 /// <summary>
 /// Constructs a three dimensional trilen with the given components.
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="z"></param>
 public Trilean3(int x, int y, int z)
 {
     X = (Trilean)x;
     Y = (Trilean)y;
     Z = (Trilean)z;
 }
Пример #27
0
        /// <summary>
        /// Multiplies the current integer with a second (partially) known integer.
        /// </summary>
        /// <param name="other">The integer to multiply with.</param>
        /// <exception cref="ArgumentException">Occurs when the sizes of the integers do not match.</exception>
        public virtual void Multiply(IntegerValue other)
        {
            AssertSameBitSize(other);

            // We implement the standard multiplication by adding and shifting, but instead of storing all intermediate
            // results, we can precompute the two possible intermediate results and shift those and add them to an end
            // result to preserve time and memory.

            // First possible intermediate result is the current value multiplied by 0. It is redundant to compute this.

            // Second possibility is the current value multiplied by 1.
            var multipliedByOne = (IntegerValue)Copy();

            // Third possibility is thee current value multiplied by ?. This is effectively marking all set bits unknown.
            // TODO: We could probably optimise the following operations for the native integer types.
            var multipliedByUnknown = (IntegerValue)Copy();

            Span <byte> maskBuffer = stackalloc byte[Size];

            multipliedByOne.GetMask(maskBuffer);

            Span <byte> bitsBuffer = stackalloc byte[Size];

            multipliedByOne.GetBits(bitsBuffer);

            var mask = new BitField(maskBuffer);
            var bits = new BitField(bitsBuffer);

            mask.Not();
            mask.Or(bits);
            mask.Not();

            Span <byte> unknownBits = stackalloc byte[Size];

            multipliedByUnknown.GetBits(unknownBits);

            multipliedByUnknown.SetBits(unknownBits, maskBuffer);

            // Final result.
            var result = new IntegerNValue(Size);

            int lastShiftByOne     = 0;
            int lastShiftByUnknown = 0;

            for (int i = 0; i < Size * 8; i++)
            {
                Trilean bit = other.GetBit(i);

                if (!bit.IsKnown)
                {
                    multipliedByUnknown.LeftShift(i - lastShiftByUnknown);
                    result.Add(multipliedByUnknown);
                    lastShiftByUnknown = i;
                }
                else if (bit.ToBoolean())
                {
                    multipliedByOne.LeftShift(i - lastShiftByOne);
                    result.Add(multipliedByOne);
                    lastShiftByOne = i;
                }
            }

            Span <byte> resultBits = stackalloc byte[Size];
            Span <byte> resultMask = stackalloc byte[Size];

            result.GetBits(resultBits);
            result.GetMask(resultMask);

            SetBits(resultBits, resultMask);
        }