private void InsertIntoTable(DbConnection con, ITupleSchema tupleSchema, ITuple tuple, string insertCommand)
        {
            OplElementDefinition     elementDefinition = this.Model.ModelDefinition.getElementDefinition(tupleSchema.Name);
            OplTupleSchemaDefinition tupleDef          = elementDefinition.asTupleSchema();

            using (DbCommand insert = con.CreateCommand())
            {
                insert.CommandText = insertCommand;
                for (int i = 0; i < tupleSchema.Size; i++)
                {
                    OplElementDefinitionType.Type oplType = tupleDef.getComponent(i).getElementDefinitionType();
                    object oplValue = null;
                    if (oplType == OplElementDefinitionType.Type.INTEGER)
                    {
                        oplValue = tuple.GetIntValue(i);
                    }
                    else if (oplType == OplElementDefinitionType.Type.FLOAT)
                    {
                        oplValue = tuple.GetNumValue(i);
                    }
                    else if (oplType == OplElementDefinitionType.Type.STRING)
                    {
                        oplValue = tuple.GetStringValue(i);
                    }
                    DbUtils.AddParameterWithValue(insert, "@value" + i, oplValue);
                }
                insert.ExecuteNonQuery();
            }
        }
    /// <summary>
    ///  Look for a string field in a tuple and return its value.
    /// </summary>
    /// <param name="tuple">The tuple to search.</param>
    /// <param name="field">The name of the field to find.</param>
    /// <param name="def">The value to return if the field is not found or is not of string type.</param>
    /// <returns></returns>
    private static string tryLoadField(ITuple tuple, String field, string def = null)
    {
        ITupleSchema schema = tuple.Schema;
        int          size   = schema.Size;

        for (int i = 0; i < size; ++i)
        {
            if (schema.IsSymbol(i) && field.Equals(schema.GetColumnName(i)))
            {
                return(tuple.GetStringValue(i));
            }
        }
        return(def);
    }
Exemple #3
0
        // The purpose of this sample is to check the result of filtering by iterating on the generated data element.
        //
        // The data element is an array of strings that is indexed by a set of strings.
        // It is filled as the result of an iteration on a set of tuples that filters out the duplicates.
        // It is based on the model used in "Sparsity" run configuration of the "transp" example.
        //
        //
        // The simplified model is:
        //
        // {string} Products = ...;
        // tuple Route { string p; string o; string d; }
        // {Route} Routes = ...;
        // {string} orig[p in Products] = { o | <p,o,d> in Routes };
        //
        static int sample1()
        {
            int          status  = 127;
            const string DATADIR = "../..";

            OplFactory.DebugMode = true;
            OplFactory oplF = new OplFactory();

            try
            {
                OplErrorHandler     errHandler = oplF.CreateOplErrorHandler(Console.Out);
                OplRunConfiguration rc         = oplF.CreateOplRunConfiguration(DATADIR + "/transp2.mod", DATADIR + "/transp2.dat");
                OplModel            opl        = rc.GetOplModel();
                opl.Generate();
                Console.Out.WriteLine("Verification of the computation of orig: ");

                // Get the orig, Routes, Product elements from the OplModel.
                ISymbolSetMap orig     = opl.GetElement("Orig").AsSymbolSetMap();
                ITupleSet     Routes   = opl.GetElement("Routes").AsTupleSet();
                ISymbolSet    Products = opl.GetElement("Products").AsSymbolSet();

                Console.Out.Write("Products = ");
                for (int j = 0; j <= Products.Size - 1; j++)
                {
                    Console.Out.Write(Products.GetValue(j) + " ");
                }
                Console.Out.WriteLine();

                // Iterate through the orig to see the result of the data element filtering.
                IEnumerator it1 = Products.GetEnumerator();
                while (it1.MoveNext())
                {
                    string p = ((string)it1.Current);
                    // This is the last dimension of the array (as it is a one-dimensional array), so you can use the get method directly.
                    Console.Out.WriteLine("for p = " + p + " we have " + orig.Get(p));
                }
                Console.Out.WriteLine("---------------------");

                // Iterate through the TupleSet.
                IEnumerator it2 = Routes.GetEnumerator();
                while (it2.MoveNext())
                {
                    ITuple t = ((ITuple)it2.Current);
                    // Get the string "p" from the tuple.
                    string p = t.GetStringValue("p");
                    // if "p" is in the indexer, we will try to add the "o" string to the array.
                    if (Products.Contains(p))
                    {
                        Console.Out.WriteLine("for p = " + p + " we will have " + t.GetStringValue("o") + " from " + t);
                    }
                }
                Console.Out.WriteLine("---------------------");
                status = 0;
            }
            catch (ILOG.OPL.OplException ex)
            {
                Console.WriteLine(ex.Message);
                status = 2;
            }
            catch (ILOG.Concert.Exception ex)
            {
                Console.WriteLine(ex.Message);
                status = 3;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                status = 4;
            }

            oplF.End();
            return(status);
        }
    /// <summary>
    ///  Process one load specification.
    /// </summary>
    /// <param name="handler">The data loader that constructs OPL elements.</param>
    /// <param name="tuple">The load specification.</param>
    private void loadSpec(OplDataHandler handler, ITuple tuple)
    {
        // Get the connection string
        // In the SimpleData implementation we don't use that string.
        // If you create a data source that is backed up by a file or
        // by a database, then this string can be used to specify
        // locations and/or credentials.
        string connection = tuple.GetStringValue(CONN_FIELD);

        connection = Interpolate(connection, handler);

        string connectionType = tryLoadField(tuple, TYPE_FIELD, "SimpleData");

        connectionType = Interpolate(connectionType, handler);
        IDataProvider provider = null;

        if (connectionType.Equals("SimpleData"))
        {
            provider = new SimpleData(connection);
        }
        else if (connectionType.Equals("SQLite"))
        {
            provider = new SQLiteData(connection);
        }
        else
        {
            provider = new GenericData(connectionType, connection);
        }

        // Process each of load specifications and load the respective
        // element.
        using (provider)
        {
            ISymbolSet  data = tuple.GetSymbolSetValue(DATA_FIELD);
            IEnumerator e    = data.GetEnumerator();
            while (e.MoveNext())
            {
                // Split specification into element name and
                // initialiation string (statement).
                string s    = Interpolate(e.Current.ToString().Trim(), handler);
                int    eq   = s.IndexOf('=');
                string name = s.Substring(0, eq).Trim();
                string stmt = s.Substring(eq + 1).Trim();

                // Inspect the type of the element and populate it.
                OplElement          elem = handler.getElement(name);
                OplElementType.Type type = elem.ElementType;
                using (IEnumerator <IRow> rows = provider.getRows(stmt))
                {
                    // (collections of) integers
                    if (type == OplElementType.Type.INT)
                    {
                        loadPrimitive(handler, name, rows, SET_INT);
                    }
                    else if (type == OplElementType.Type.SET_INT)
                    {
                        loadPrimitiveCollection(handler, name, rows, START_SET, END_SET, SET_INT);
                    }
                    else if (type == OplElementType.Type.MAP_INT)
                    {
                        loadPrimitiveCollection(handler, name, rows, START_ARRAY, END_ARRAY, SET_INT);
                    }

                    // (collections of) floating point values
                    else if (type == OplElementType.Type.NUM)
                    {
                        loadPrimitive(handler, name, rows, SET_FLOAT);
                    }
                    else if (type == OplElementType.Type.SET_NUM)
                    {
                        loadPrimitiveCollection(handler, name, rows, START_SET, END_SET, SET_FLOAT);
                    }
                    else if (type == OplElementType.Type.MAP_NUM)
                    {
                        loadPrimitiveCollection(handler, name, rows, START_ARRAY, END_ARRAY, SET_FLOAT);
                    }

                    // (collections of) tuples
                    else if (type == OplElementType.Type.STRING)
                    {
                        loadPrimitive(handler, name, rows, SET_STRING);
                    }
                    else if (type == OplElementType.Type.SET_SYMBOL)
                    {
                        loadPrimitiveCollection(handler, name, rows, START_SET, END_SET, SET_STRING);
                    }
                    else if (type == OplElementType.Type.MAP_SYMBOL)
                    {
                        loadPrimitiveCollection(handler, name, rows, START_ARRAY, END_ARRAY, SET_STRING);
                    }

                    else if (type == OplElementType.Type.TUPLE)
                    {
                        loadTuple(handler, name, rows);
                    }
                    else if (type == OplElementType.Type.SET_TUPLE)
                    {
                        loadTupleCollection(handler, name, rows, elem.AsTupleSet().Schema, START_SET, END_SET);
                    }
                    else if (type == OplElementType.Type.MAP_TUPLE)
                    {
                        loadTupleCollection(handler, name, rows, elem.AsTupleMap().Schema, START_ARRAY, END_ARRAY);
                    }
                    else
                    {
                        throw new NotImplementedException("element type " + type + " not implemented");
                    }
                }
            }
        }
    }