예제 #1
0
        internal static IArgumentArity Default(Type type, Argument argument, ISymbolSet parents)
        {
            if (type == typeof(bool))
            {
                return(ZeroOrOne);
            }

            var parent = parents.Count > 0 ? parents[0] : default;

            if (typeof(IEnumerable).IsAssignableFrom(type) &&
                type != typeof(string))
            {
                return(parent is ICommand
                           ? ZeroOrMore
                           : OneOrMore);
            }

            if (parent is ICommand &&
                (argument.HasDefaultValue ||
                 type.IsNullable()))
            {
                return(ZeroOrOne);
            }

            return(ExactlyOne);
        }
예제 #2
0
        private static IList <IOption> GatherOptions(ISymbolSet symbols)
        {
            var optionList = new List <IOption>();

            foreach (var symbol in symbols)
            {
                if (symbol is IOption option)
                {
                    var arity = option.Argument.Arity;

                    if (arity.MaximumNumberOfArguments == 1 &&
                        arity.MinimumNumberOfArguments == 1) // Exactly One
                    {
                        optionList.Add(option);
                    }
                }

                if (symbol is ICommand command)
                {
                    optionList.AddRange(GatherOptions(command.Children));
                }
            }

            return(optionList);
        }
예제 #3
0
        // The purpose of this sample is to output a multidimensional array x[i][j] to illustrate how arrays and subarrays are managed.
        // To access the elements of an array, you must first access the subarrays up to  the last dimension, then you can get the values.
        //  Here, as there are two dimensions, you have to get one subarray from which you can directly get the values.
        //
        // The array of integers is indexed by two sets of strings..
        //
        // The simplified model is:
        //
        // {string} s1 = ...;
        // {string} s2 = ...;
        // {int} x[s1][s2] = ...;
        //
        static int sample2()
        {
            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 + "/iterators.mod");
                OplModel            opl        = rc.GetOplModel();
                opl.Generate();

                // Get the x, s1 and s2 elements from the OplModel.
                IIntMap    x  = opl.GetElement("x").AsIntMap();
                ISymbolSet s1 = opl.GetElement("s1").AsSymbolSet();
                ISymbolSet s2 = opl.GetElement("s2").AsSymbolSet();

                // Iterate on the first indexer.
                IEnumerator it1 = s1.GetEnumerator();
                while (it1.MoveNext())
                {
                    // Get the second dimension array from the first dimension.
                    IIntMap sub = x.GetSub((string)it1.Current);
                    // Iterate on the second indexer of x (that is the indexer of the subarray).
                    IEnumerator it2 = s2.GetEnumerator();
                    while (it2.MoveNext())
                    {
                        // This is the last dimension of the array, so you can directly use the get method.
                        Console.Out.WriteLine(it1.Current + " " + it2.Current + " " + sub.Get((string)it2.Current));
                    }
                }
                Console.Out.WriteLine("---------------------");
                status = 0;
            }
            catch (ILOG.OPL.OplException ex)
            {
                Console.WriteLine(ex.Message);
                status = 1;
            }
            catch (IloException ex)
            {
                Console.Out.WriteLine("### exception: " + ex.Message);
                status = 2;
            }
            catch (System.Exception ex)
            {
                Console.Out.WriteLine("### UNEXPECTED ERROR ..." + ex.Message);
                status = 3;
            }

            oplF.End();
            return(status);
        }
예제 #4
0
        public static bool HasAnyOfType <T>(this ISymbolSet source)
        {
            for (var i = 0; i < source.Count; i++)
            {
                if (source[i] is T)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #5
0
        static int sample3()
        {
            int status = 0;

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

            try
            {
                OplErrorHandler    errHandler = oplF.CreateOplErrorHandler(Console.Out);
                OplSettings        settings   = oplF.CreateOplSettings(errHandler);
                OplModelSource     src        = oplF.CreateOplModelSourceFromString(getModelTextSample3(), "tuple array iterator");
                OplModelDefinition def        = oplF.CreateOplModelDefinition(src, settings);
                Cplex    cplex = oplF.CreateCplex();
                OplModel opl   = oplF.CreateOplModel(def, cplex);
                opl.Generate();

                // get the string set used to index the array of tuples
                ITupleMap  arrayT = opl.GetElement("arrayT").AsTupleMap();
                ISymbolSet ids    = opl.GetElement("ids").AsSymbolSet();
                // iterate on the index set to retrieve the tuples stored in the array
                IEnumerator it = ids.GetEnumerator();
                while (it.MoveNext())
                {
                    Console.Out.Write("arrayT[" + it.Current + "] = ");
                    IMapIndexArray id = oplF.MapIndexArray(0);
                    id.Add(it.Current.ToString());
                    ITuple t = arrayT.MakeTuple();
                    arrayT.GetAt(id, t);
                    Console.Out.WriteLine(t);
                }
            }
            catch (ILOG.OPL.OplException ex)
            {
                Console.WriteLine(ex.Message);
                status = 1;
            }
            catch (IloException e)
            {
                status = 2;
                Console.Out.WriteLine("### exception: " + e.Message);
            }
            catch (System.Exception ex)
            {
                status = 3;
                Console.Out.WriteLine("### UNEXPECTED ERROR ..." + ex.Message);
            }
            oplF.End();
            return(status);
        }
예제 #6
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");
                    }
                }
            }
        }
    }