示例#1
0
        public void SetFormula(int srcIndex, int trgIndex, IBxFormula fml)
        {
            if (srcIndex == trgIndex)
            {
                return;
            }
            _SrcUnitFormulas formulas = m_formulas[srcIndex];

            formulas.SetFormula(trgIndex, fml);
        }
示例#2
0
        public void SetFormula(string srcUnit, string trgUnit, IBxFormula fml)
        {
            if (srcUnit == trgUnit)
            {
                return;
            }
            IBxUnit src = Parse(srcUnit);
            IBxUnit trg = Parse(trgUnit);

            if ((src == null) || (trg == null))
            {
                return;
            }
            _SrcUnitFormulas formulas = m_formulas[src.Index];

            formulas.SetFormula(trg.Index, fml);
        }
示例#3
0
        public IBxFormula GetFormula(int srcIndex, int trgIndex)
        {
            if (srcIndex == trgIndex)
            {
                return(ParsedFormula.s_sameUnitFormula);
            }

            _SrcUnitFormulas formulas = m_formulas[srcIndex];
            IBxFormula       fml      = formulas.GetFormula(trgIndex);

            if (fml == null)
            {
                if (trgIndex == m_nDefaultUnitIndex)
                {
                    throw new Exception(string.Format("not find the convertion between {0} to default unit {1} \n", this[srcIndex].ID, this[m_nDefaultUnitIndex].ID));
                }

                if (srcIndex == m_nDefaultUnitIndex)
                {
                    IBxFormula temp = GetFormula(trgIndex, srcIndex);
                    if (!(temp is ParsedFormula))
                    {
                        throw new Exception(string.Format("It must be a ParsedFormula between {0] to defaultUnit {1}\n", this[trgIndex].ID, this[srcIndex].ID));
                    }
                    fml = ParsedFormula.Inverse((ParsedFormula)temp);
                    formulas.SetFormula(trgIndex, fml);
                }
                else
                {
                    IBxFormula fml1 = GetFormula(srcIndex, m_nDefaultUnitIndex);
                    IBxFormula fml2 = GetFormula(m_nDefaultUnitIndex, trgIndex);
                    fml = new CombinedFormula(fml1, fml2);
                    formulas.SetFormula(trgIndex, fml);
                }
            }
            return(fml);
        }
示例#4
0
        public void LoadUnitConfigNode(XmlElement cateNode)
        {
            FormulaParser parser = new FormulaParser();

            try
            {
                m_id  = cateNode.GetAttribute("id");
                _name = cateNode.GetAttribute("name");
                _code = cateNode.GetAttribute("code");

                XmlElement unitsNode   = (XmlElement)cateNode.SelectSingleNode("Units");
                string     defaultUnit = unitsNode.GetAttribute("primaryUnit");

                //获取所有的单位
                XmlNodeList    unitsNodeList = cateNode.SelectSingleNode("Units").ChildNodes;
                List <IBxUnit> units         = new List <IBxUnit>();
                List <string>  fmls          = new List <string>();
                int            nIndex        = 0;
                string         id            = null;
                string         name          = null;
                string         code          = null;
                int            dd            = 0;
                foreach (XmlElement one in unitsNodeList)
                {
                    id = one.GetAttribute("id");
                    if (id == defaultUnit)
                    {
                        m_nDefaultUnitIndex = nIndex;
                    }
                    name = one.GetAttribute("name");
                    if (string.IsNullOrEmpty(name))
                    {
                        name = id;
                    }
                    name = UnitNameConvert.Convert(name);
                    code = one.GetAttribute("code");
                    if (!Int32.TryParse(one.GetAttribute("decimalDigits"), out dd))
                    {
                        throw new Exception("no decimal digis in unit " + id + " !");
                    }

                    units.Add(new BUUnit(id, name, code, dd, this, nIndex));
                    fmls.Add(one.GetAttribute("defaultUnit"));
                    nIndex++;
                }
                m_units = units.ToArray();
                //end

                //生成默认转换公式
                int nCounts = m_units.Length;
                m_formulas = new _SrcUnitFormulas[nCounts];
                nIndex     = 0;
                _SrcUnitFormulas temp = null;
                foreach (IBxUnit one in m_units)
                {
                    temp = new _SrcUnitFormulas(nIndex, nCounts);
                    m_formulas[nIndex] = temp;
                    if (nIndex != m_nDefaultUnitIndex)
                    {
                        parser.Formula = fmls[nIndex];
                        parser.Variant = one.ID;
                        temp.SetFormula(m_nDefaultUnitIndex, new ParsedFormula(parser.Parse()));
                    }
                    nIndex++;
                }
                //end
            }
            catch (System.Exception) { }

            try
            {
                XmlElement  formulasNode    = (XmlElement)cateNode.SelectSingleNode("Formulas");
                XmlNodeList formulaNodeList = formulasNode.ChildNodes;
                string      srcUnit;
                string      trgUnit;
                foreach (XmlElement one in formulaNodeList)
                {
                    srcUnit        = one.GetAttribute("src");
                    trgUnit        = one.GetAttribute("trg");
                    parser.Formula = one.GetAttribute("convertion");
                    parser.Variant = srcUnit;
                    SetFormula(srcUnit, trgUnit, new ParsedFormula(parser.Parse()));
                }
            }
            catch (System.Exception) { }
        }