Ejemplo n.º 1
0
        private void processDefault(string[] fields)
        {
            int    i    = 0;
            string line = null;

            string[]         n        = null;
            int              j        = 0;
            sinter_IVariable thisIVar = (sinter_IVariable)getIOByName(fields[1]);

            if (thisIVar == null)
            {
                throw new System.IO.IOException("Cannot apply labels, Object " + fields[1] + " not found");
            }
            if (thisIVar.isScalar)
            {
                sinter_Variable thisVar = (sinter_Variable)thisIVar;
                if (thisVar.type == sinter_Variable.sinter_IOType.si_DOUBLE)
                {
                    thisVar.dfault = Convert.ToDouble(fields[2]);
                }
                else if (thisVar.type == sinter_Variable.sinter_IOType.si_INTEGER)
                {
                    thisVar.dfault = Convert.ToInt32(fields[2]);
                }
                else if (thisVar.type == sinter_Variable.sinter_IOType.si_STRING)
                {
                    thisVar.dfault = Convert.ToString(fields[2]);
                }
            }
            else if (thisIVar.isVec)
            {
                sinter_Vector thisVar = (sinter_Vector)thisIVar;
                if (thisVar.type == sinter_Variable.sinter_IOType.si_DOUBLE_VEC)
                {
                    thisVar.setElementDefault(0, Convert.ToDouble(fields[2]));
                }
                else if (thisVar.type == sinter_Variable.sinter_IOType.si_INTEGER_VEC)
                {
                    thisVar.setElementDefault(0, Convert.ToInt32(fields[2]));
                }
                for (i = 1; i <= thisVar.size - 1; i++)
                {
                    line = inFileStream.ReadLine();
                    n    = line.Split('|');
                    if (n.Length == 0)
                    {
                        return;
                    }
                    for (j = 0; j <= n.Length - 1; j++)
                    {
                        n[j] = n[j].Trim();
                    }
                    if (thisVar.type == sinter_Variable.sinter_IOType.si_DOUBLE_VEC)
                    {
                        thisVar.setElementDefault(i, Convert.ToDouble(n[0]));
                    }
                    else if (thisVar.type == sinter_Variable.sinter_IOType.si_INTEGER_VEC)
                    {
                        thisVar.setElementDefault(i, Convert.ToInt32(n[0]));
                    }
                }
            }
            else if (thisIVar.isTable)
            {
                sinter_Table thisTable = (sinter_Table)thisIVar;
                for (j = 2; j <= fields.Length - 1; j++)
                {
                    sinter_Variable thisVar = (sinter_Variable)thisTable.getElement(0, j - 2);
                    if (thisVar.type == sinter_Variable.sinter_IOType.si_DOUBLE)
                    {
                        thisVar.dfault = Convert.ToDouble(fields[j]);
                    }
                    else if (thisVar.type == sinter_Variable.sinter_IOType.si_INTEGER)
                    {
                        thisVar.dfault = Convert.ToInt32(fields[j]);
                    }
                    else
                    {
                        throw new System.IO.IOException("Cannot apply minimum on " + thisVar.name + ".  Bad type");
                    }
                }
                for (i = 1; i <= thisTable.MNRows; i++)
                {
                    line = inFileStream.ReadLine();
                    n    = line.Split('|');
                    if (n.Length == 0)
                    {
                        return;
                    }
                    for (j = 0; j <= n.Length - 1; j++)
                    {
                        n[j] = n[j].Trim();
                    }
                    for (j = 0; j <= n.Length - 1; j++)
                    {
                        sinter_Variable thisVar = (sinter_Variable)thisTable.getElement(0, j);
                        if (thisVar.type == sinter_Variable.sinter_IOType.si_DOUBLE)
                        {
                            thisVar.dfault = Convert.ToDouble(fields[j]);
                        }
                        else if (thisVar.type == sinter_Variable.sinter_IOType.si_INTEGER)
                        {
                            thisVar.dfault = Convert.ToInt32(fields[j]);
                        }
                        else
                        {
                            throw new System.IO.IOException("Cannot apply maximum on " + thisVar.name + ".  Bad type");
                        }
                    }
                }
            }
            ////////////////////////////////////////////////////////////////
            //Now that we've set all the defaults, make them the basic value
            ////////////////////////////////////////////////////////////////
            thisIVar.resetToDefault();
        }
Ejemplo n.º 2
0
        public void sendDefaults(JObject inputDict)
        {
            //If we didn't get any defaults, just return
            if (inputDict == null || inputDict.Count <= 0)
            {
                return;
            }

            checkAllInputs(inputDict);

            foreach (KeyValuePair <String, JToken> entry in inputDict)
            {
                int    row     = -1;
                int    column  = -1;
                string varName = sinter_SetupFile.parseVariable(entry.Key, ref row, ref column);

                sinter_IVariable inputVal = this.getIOByName(varName);
                if (inputVal == null)
                {
                    Console.WriteLine("Ignoring non-var: " + varName);
                }
                else if (inputVal.isOutput)
                {
                    throw new System.IO.IOException(string.Format("Variable {0} is an output varaible.  It should not be in the input list.", varName));
                }
                else
                {
                    if (inputVal.isScalar)
                    {
                        ((sinter_Variable)inputVal).dfault = sinter_HelperFunctions.convertJTokenToNative(entry.Value);
                    }
                    else if (inputVal.isVec)
                    {
                        //Check that the indexes are valid
                        sinter_Vector thisVar = (sinter_Vector)inputVal;
                        if (row >= 0)   //Editing a single entry of a vector
                        {
                            thisVar.setElementDefault(row, sinter_HelperFunctions.convertJTokenToNative(entry.Value));
                        }
                        else
                        { //copying in an entire vector at once
                            Newtonsoft.Json.Linq.JArray jsonArray = (Newtonsoft.Json.Linq.JArray)entry.Value;
                            if (thisVar.size != jsonArray.Count)
                            {
                                throw new System.IO.IOException(string.Format("Variable {0} has an index out of range.  Upper bound: {1}, bad index: {2}", varName, (int)thisVar.size - 1, row));
                            }
                            //Annoyingly, a jsonArray is not a C# Array, so I have to copy element by element, rather than just copying the array
                            for (int ii = 0; ii < thisVar.size; ++ii)
                            {
                                thisVar.setElementDefault(ii, sinter_HelperFunctions.convertJTokenToNative(jsonArray[ii]));
                            }
                        }
                    }

                    else if (inputVal.isTable)
                    {
                        sinter_Table thisTable = (sinter_Table)inputVal;
                        //Check that the indexes are valid
                        if (row >= 0 && row <= (int)thisTable.MNRows &&
                            column >= 0 && column <= (int)thisTable.MNCols)
                        {
                            thisTable.setElementDefault(row, column, sinter_HelperFunctions.convertJTokenToNative(entry.Value));
                        }
                        else
                        {
                            throw new System.IO.IOException(string.Format("Variable {0} has an index out of range. Range: {1},{2} Index: {3},{4}", varName, (int)thisTable.MNRows, (int)thisTable.MNCols, row, column));
                        }
                    }
                }

                //Now, make the default the current real value
                inputVal.resetToDefault();
            }
        }