Exemple #1
0
        private EquationDetails CreateEquationDetailsFromCsv(ContentManager contentManager, CsvEquationImport csvEI)
        {
            IList <VarInfo> variableInfos = new List <VarInfo>();

            foreach (var strVarInfo in csvEI.Variables)
            {
                variableInfos.Add(CreateVariableFromCsv(contentManager, strVarInfo));
            }

            IList <double> testValues = new List <double>();

            foreach (var strTestVal in csvEI.TestVariables)
            {
                double dVal;
                if (double.TryParse(strTestVal, out dVal))
                {
                    testValues.Add(dVal);
                }
            }

            string          eqName = (string.IsNullOrEmpty(csvEI.EquationName) ? csvEI.EquationRef : csvEI.EquationName);
            EquationDetails ed     = new EquationDetails(eqName, csvEI.EquationAsText, variableInfos);

            ed.TestValues = testValues;

            return(ed);
        }
Exemple #2
0
        public void CreateFromCsv(ContentManager contentManager, string libraryName,
                                  string libraryDescription, IList <CsvEquationImport> csvEquationImports)
        {
            Name        = libraryName;
            Description = libraryDescription;

            IDictionary <string, SectionNode> allSectionNodes = new Dictionary <string, SectionNode>();

            foreach (var csvEI in csvEquationImports)
            {
                try
                {
                    SectionNode sectionNode = CreateSectionNodeFromCsv(csvEI.SectionNode, allSectionNodes);

                    if ((csvEI.Status == 0) || (csvEI.Status == 1))
                    {
                        EquationDetails ed = CreateEquationDetailsFromCsv(contentManager, csvEI);
                        AddEquationToSectionNode(contentManager, sectionNode, ed);
                    }
                    else
                    {
                        int iDbg = 0;
                    }
                }
                catch (Exception ex)
                {
                    // Log but then continue with the rest of them
                    Logging.LogMessage($"Error processing equation '{csvEI.EquationAsText}': {ex.Message}");
                }
            }
        }
Exemple #3
0
        public void CreateFromDatabase(ContentManager contentManager,
                                       SqLiteContentDatabase database, IList <TblEqLibSectionNode_Row> sectionNodeRows,
                                       IList <TblEqLibEquation_Row> equationRows, IList <TblEqLibVariable_Row> variableRows)
        {
            Name        = database.GetInfoVal("EquationLibraryName");
            Description = database.GetInfoVal("EquationLibraryDescription");

            // ----------- Read all the section nodes
            IDictionary <int, Tuple <TblEqLibSectionNode_Row, SectionNode> > allSectionNodes
                = new Dictionary <int, Tuple <TblEqLibSectionNode_Row, SectionNode> >();                       // Key is TblEqLibSectionNode_Row.SectionNodeID

            foreach (var nodeRow in sectionNodeRows)
            {
                SectionNode sectionNode = new SectionNode(nodeRow.Name)
                {
                    Description = nodeRow.Description
                };
                allSectionNodes.Add(nodeRow.SectionNodeID, new Tuple <TblEqLibSectionNode_Row, SectionNode>(nodeRow, sectionNode));
            }

            // ----------- Connect the parents
            foreach (var nodeTpl in allSectionNodes)
            {
                TblEqLibSectionNode_Row nodeRow     = nodeTpl.Value.Item1;
                SectionNode             sectionNode = nodeTpl.Value.Item2;

                if (nodeRow.ParentNodeID > 0)
                {
                    int parentNodeID = nodeRow.ParentNodeID;
                    if (allSectionNodes.ContainsKey(parentNodeID))
                    {
                        sectionNode.Parent = allSectionNodes[parentNodeID].Item2;
                    }
                    else
                    {
                        int iDbg = 0;   // TODO
                    }
                }
                else
                {
                    TopSectionNodes.Add(sectionNode);
                }
            }

            // ----------- Check for duplications or loops... TODO

            // ----------- Store the EquationDetails
            IDictionary <int, Tuple <TblEqLibEquation_Row, EquationDetails> > allEquationDetails
                = new Dictionary <int, Tuple <TblEqLibEquation_Row, EquationDetails> >();                       // Key is TblEqLibEquation_Row.EquationID

            foreach (var eqnRow in equationRows)
            {
                EquationDetails ed = new EquationDetails(eqnRow.Name, eqnRow.EquationAsText);
                allEquationDetails.Add(eqnRow.EquationID, new Tuple <TblEqLibEquation_Row, EquationDetails>(eqnRow, ed));
            }

            // ----------- Add the variables to the EquationDetails
            foreach (var varRow in variableRows)
            {
                string    paramTypName = varRow.ParamType?.ToLower();
                ParamType paramType    = null;
                if (contentManager.ParamTypes.ContainsKey(paramTypName))
                {
                    paramType = contentManager.ParamTypes[paramTypName];
                }
                else
                {
                    int iDbg = 0;   // TODO
                }

                VarInfo vi = new VarInfo(varRow.Name, varRow.Description, paramType);

                int equationID = varRow.EquationID;
                if (allEquationDetails.ContainsKey(equationID))
                {
                    allEquationDetails[equationID].Item2.VariableInfos.Add(vi); // TODO: Check for duplicate names
                }
                else
                {
                    int iDbg = 0;   // TODO
                }
            }

            // ----------- Create the EquationCalcs in the section nodes
            foreach (var eqTpl in allEquationDetails)
            {
                TblEqLibEquation_Row eqnRow = eqTpl.Value.Item1;
                EquationDetails      ed     = eqTpl.Value.Item2;

                int sectionNodeID = eqnRow.SectionNodeID;
                if (allSectionNodes.ContainsKey(sectionNodeID))
                {
                    AddEquationToSectionNode(contentManager, allSectionNodes[sectionNodeID].Item2, ed);
                }
                else
                {
                    int iDbg = 0;   // TODO
                }
            }
        }
Exemple #4
0
        public static void AddEquationToSectionNode(ContentManager contentManager, SectionNode sectionNode, EquationDetails ed)
        {
            string description = "";

            if (!string.IsNullOrEmpty(ed.Name))
            {
                description += ((string.IsNullOrEmpty(description) ? "" : " ") + ed.Name);
            }
            if (!string.IsNullOrEmpty(ed.Reference))
            {
                description += ((string.IsNullOrEmpty(description) ? "" : " ") + ed.Reference);
            }
            if (!string.IsNullOrEmpty(ed.Description))
            {
                description += ((string.IsNullOrEmpty(description) ? "" : " ") + ed.Description);
            }

            // ----------------------
            JongErrWarn  errW    = null;
            FunctionCalc funCalc = contentManager.CreateFunctionCalcFromExpression(ed.EquationAsText, description, ed.VariableInfos, out errW);

            // ----------------------
            if (funCalc != null)
            {
                // ------------- Record the test values if there are any
                if ((ed.TestValues != null) && (ed.TestValues.Count > 0))
                {
                    funCalc.TestValues = new Dictionary </*variable name*/ string, double>();

                    for (int i = 0; i < ed.VariableInfos.Count; i++)
                    {
                        VarInfo vi = ed.VariableInfos[i];
                        funCalc.TestValues.Add(vi.Name, ed.TestValues[i]);
                    }
                }
                funCalc.ExpectedDimensions = new Dictionary </*variable name*/ string, Dimensions>();
                if (ed.VariableInfos != null)
                {
                    foreach (VarInfo vi in ed.VariableInfos)
                    {
                        funCalc.ExpectedDimensions.Add(vi.Name, vi.ParamType?.Dimensions);
                    }
                }

                // -------------
                sectionNode.EqnCalcs.Add((EqnCalc)funCalc);
            }
        }