Example #1
0
        /// <summary>
        /// Evaluates a math expression of 2 units.
        /// </summary>
        /// <param name="node">The AST node the evaluation is a part of.</param>
        /// <param name="left">The unit on the left</param>
        /// <param name="right">The unit on the right</param>
        /// <param name="op">The math operator</param>
        /// <param name="units"></param>
        /// <returns></returns>
        public static LObject CalcUnits(AstNode node, LUnit left, LUnit right, Operator op, Units units)
        {
            double baseUnitsValue = 0;
            if (op == Operator.Multiply)
            {
                baseUnitsValue = left.BaseValue * right.BaseValue;
            }
            else if (op == Operator.Divide)
            {
                baseUnitsValue = left.BaseValue / right.BaseValue;
            }
            else if (op == Operator.Add)
            {
                baseUnitsValue = left.BaseValue + right.BaseValue;
            }
            else if (op == Operator.Subtract)
            {
                baseUnitsValue = left.BaseValue - right.BaseValue;
            }
            else if (op == Operator.Modulus)
            {
                baseUnitsValue = left.BaseValue % right.BaseValue;
            }

            var relativeValue = units.ConvertToRelativeValue(baseUnitsValue, left.SubGroup, null);
            var result = new LUnit(relativeValue);
            result.BaseValue = baseUnitsValue;
            result.Group = left.Group;
            result.SubGroup = left.SubGroup;
            //result.Value = relativeValue;
            var lclass = LangTypeHelper.ConvertToLangUnit(result);
            return lclass;
        }
Example #2
0
        /// <summary>
        /// Converts the inputs into a LUnit object e.g. 3, "yards"
        /// </summary>
        /// <param name="value"></param>
        /// <param name="unitsName"></param>
        /// <returns></returns>
        public LUnit ConvertToUnits(double value, string unitsName)
        {
            // Get length for "feet".
            var group       = _subGroupsToGroupMap[unitsName];
            var sourceUnits = GetUnitsFor(group, unitsName);

            // e.g. Convert( 5, "feet")
            // Convert 5 feet to inches.

            // 1. Convert from source to base
            // e.g. 5 feet to 60 inches.
            double sourceBaseUnits = value * sourceUnits.ConversionValue;
            var    unit            = new LUnit(value);

            unit.Group     = group;
            unit.SubGroup  = unitsName;
            unit.BaseValue = sourceBaseUnits;
            return(unit);
        }
Example #3
0
        private static void ValidateUnits(LUnit u1, LUnit u2)
        {
            // Validate.
            if (u1 == null || u2 == null)
                throw new ArgumentException("Can not add units when 1 unit is empty");

            // Check for matching groups e.g. length + length or weight + weight.
            if (u1.Group != u2.Group)
                throw new ArgumentException("Can not add units " + u1.Group + " to " + u2.Group);
        }
Example #4
0
        /// <summary>
        /// Add 2 unites together.
        /// </summary>
        /// <param name="u1"></param>
        /// <param name="u2"></param>
        /// <returns></returns>
        public static LUnit AddUnits(LUnit u1, LUnit u2)
        {
            // Validate
            LangTypeHelper.ValidateUnits(u1, u2);

            // Now convert the values to their base value.
            double totalBase = u1.BaseValue + u2.BaseValue;
            var unit = new LUnit(totalBase);
            unit.BaseValue = totalBase;
            unit.Group = u1.Group;
            unit.SubGroup = u1.SubGroup;

            // Set the value to the converted relative value 
            // e.g. if u1.subgroup = feet and u2.subgroup == inches.
            // then multiply result.basevalue by the conversion value.
            // Now convert the units
            return unit;
        }
Example #5
0
        /// <summary>
        /// Converts the inputs into a LUnit object e.g. 3, "yards"
        /// </summary>
        /// <param name="value"></param>
        /// <param name="unitsName"></param>
        /// <returns></returns>
        public LUnit ConvertToUnits(double value, string unitsName)
        {
            // Get length for "feet".
            var group = _subGroupsToGroupMap[unitsName];
            var sourceUnits = GetUnitsFor(group, unitsName);

            // e.g. Convert( 5, "feet")
            // Convert 5 feet to inches.

            // 1. Convert from source to base
            // e.g. 5 feet to 60 inches.
            double sourceBaseUnits = value * sourceUnits.ConversionValue;
            var unit = new LUnit(value);
            unit.Group = group;
            unit.SubGroup = unitsName;
            unit.BaseValue = sourceBaseUnits;
            return unit;
        }
Example #6
0
 /// <summary>
 /// Evaluates a math expression of 2 time spans.
 /// </summary>
 /// <param name="node">The AST node the evaluation is a part of.</param>
 /// <param name="lhs">The time on the left hand side</param>
 /// <param name="rhs">The time on the right hand side</param>
 /// <param name="op">The math operator.</param>
 /// <returns></returns>
 public static LBool CompareUnits(AstNode node, LUnit lhs, LUnit rhs, Operator op)
 {
     var result = false;
     if (op == Operator.LessThan)             result = lhs.BaseValue < rhs.BaseValue;
     else if (op == Operator.LessThanEqual)   result = lhs.BaseValue <= rhs.BaseValue;
     else if (op == Operator.MoreThan)        result = lhs.BaseValue > rhs.BaseValue;
     else if (op == Operator.MoreThanEqual)   result = lhs.BaseValue >= rhs.BaseValue;
     else if (op == Operator.EqualEqual)      result = lhs.BaseValue == rhs.BaseValue;
     else if (op == Operator.NotEqual)        result = lhs.BaseValue != rhs.BaseValue;
     return new LBool(result);
 }