Initializing help system before first use

Working with models and accessing dynamic libraries in Mosel


Type: Programming
Rating: 4 (medium-difficult)
Description: mmexlib.cs: Working with models and accessing dynamic libraries in Mosel (requires burglari.bim, chess2.bim, trans.bim)
  • load and unload BIM models
  • run a model in Mosel
  • display information about loaded models
  • display information about additional libraries required by the loaded models
mmdispmod.cs: Display the contents of a model; the information is read from a bim file
  • display run-time parameters, requirements, symbols, package/module dependencies, annotations
mmdispdso.cs: Display the contents of a module
  • display constants, types, control paramters, subroutines, I/O drivers
Note that these examples require the provided mos files to be pre-compiled.
File(s): mmexlib.cs, mmdispmod.cs, mmdispdso.cs
Data file(s): burglari.mos, chess2.mos, trans.mos


mmexlib.cs
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexlib.cs                                     */
/*  ```````````````                                     */
/*  Example for the use of the Mosel libraries          */
/*  (working with models and accessing the dynamic      */
/*  libraries loaded by Mosel)                          */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: J.Farmer / S. Heipcke                   */
/********************************************************/

using System;
using Mosel;

namespace mmexlib {
  public class mmexlibClass {
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
      XPRM mosel;
      XPRMModel[] mods = new XPRMModel[3];

      // Initialise Mosel
      mosel = XPRM.Init();
      // Load the BIM files
      mods[0] = mosel.LoadModel("Models/burglari.bim");
      mods[1] = mosel.LoadModel("Models/chess2.bim");
      mods[2] = mosel.LoadModel("Models/trans.bim");
      Console.WriteLine("Models loaded");

      // Display basic information about the models
      foreach (XPRMModel m in mods) {
        Console.WriteLine(
          "  {0}: {1} ({2}, `{3}' size:{4})",
          m.Number,
          m.Name,
          m.SysComment,
          m.UserComment,
          m.Size
        );
      }
      Console.WriteLine();

      // Enumerate all loaded modules and display information
      Console.WriteLine("Additional libraries loaded:");
      foreach (XPRMModule mo in mosel.Modules) {
        Console.WriteLine(
          "  {0} (version {1}) used by {2} model(s)",
          mo.Name,
          mo.Version,
          mo.NumberOfReferences
        );
      }
    }

  }
}


mmdispmod.cs
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmdispmod.cs                                   */
/*  `````````````````                                   */
/*  Example for the use of the Mosel libraries          */
/*  (display the contents of a model)                   */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: J.Farmer & Y. Colombani                 */
/********************************************************/

using System;
using Mosel;

namespace mmdispmod {
  public class mmdispmodClass {
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    [STAThread]
    static void Main(string[] args) {
      XPRM mosel;
      XPRMModel mod;
      XPRMProcedure proc;

      if(args.Length!=1)
      {
        Console.WriteLine("Usage: mmdispmod modname.bim");
        return;
      }

      // Initialize Mosel
      mosel = XPRM.Init();
      // Load a BIM file
      mod = mosel.LoadModel(args[0]);

      // List of model parameters
      foreach(XPRMParameter p in mod.Parameters) {
        Console.WriteLine(" {0}", p.Name);
      }
      Console.WriteLine();

      // List of symbols
      foreach(XPRMIdentifier symb in mod.Identifiers) {
        switch (symb.StructCode) {
          case XPRMVarStruct.CONST: // Constant: display value
            Console.WriteLine(
              " {0}= {1}",
              symb.Name,
              ((XPRMConstant) symb).Value
            );

            break;
          case XPRMVarStruct.REF:   // Reference: display type
            Console.WriteLine(
              " {0}: {1}",
              symb.Name,
              symb.TypeName
            );
            break;
          case XPRMVarStruct.ARRAY: // Array: display type
            Console.WriteLine(
              " {0}: array of {1}",
              symb.Name,
              symb.TypeName
            );
            break;
          case XPRMVarStruct.SET:   // Set: display type
            Console.WriteLine(
              " {0}: set of {1}",
              symb.Name,
              symb.TypeName
            );
            break;
          case XPRMVarStruct.PROC:  // Subroutine
            proc = (XPRMProcedure) symb;
            do { // Look for all overloading proc/func
              dispProcFct(proc);  // Display the prototype
            } while ((proc=proc.Next())!=null);
            break;
          default:
            Console.WriteLine(" {0}: ?", symb.Name);
            break;
        }
      }
    }


    /// <summary>
    /// Display a prototype from a signature
    /// </summary>
    static void dispProcFct(XPRMProcedure proc) {
      char[] parms;
      int i;
      if (proc.TypeCode!=XPRMVarType.NOT)
        Console.Write(" function {0}", proc.Name);
      else
        Console.Write(" procedure {0}", proc.Name);

      if (proc.NbParameters>0) {
        Console.Write("(");
        parms = proc.ParameterTypes.ToCharArray();
        i = 0;
        while (i<parms.Length) {
          if (i>0) Console.Write(",");
          i = dispType(i, parms)+1;
        }
        Console.Write(")");
      }

      if (proc.TypeCode!=XPRMVarType.NOT)
        Console.Write(":{0}", proc.TypeName);
      Console.WriteLine();
    }

    /// <summary>
    /// Display a type name from a signature
    /// </summary>
    static int dispType(int i, char[] parms) {
      int j;
      switch (parms[i]) {
        case 'i': Console.Write("integer"); break;
        case 'r': Console.Write("real"); break;
        case 's': Console.Write("string"); break;
        case 'b': Console.Write("boolean"); break;
        case 'v': Console.Write("mpvar"); break;
        case 'c': Console.Write("linctr"); break;
        case 'I': Console.Write("range"); break;
        case 'a': Console.Write("array"); break;
        case 'e': Console.Write("set"); break;
        case '|':
          i++;
          do {
            Console.Write(parms[i++]);
          } while (parms[i]!='|');
          break;
        case '!':
          i++;
          do {
            Console.Write(parms[i++]);
          } while (parms[i]!='!');
          break;
        case 'A':
          Console.Write("array (");
          j=++i;
          while (parms[i]!='.') {
            if (j!=i) Console.Write(",");
            i=dispType(i, parms)+1;
          }
          Console.Write(") of ");
          i=dispType(i, parms)+1;
          break;
        case 'E':
          Console.Write("set of ");
          i = dispType(++i, parms);
          break;
        default:
          Console.Write("?");
          break;
      }
      return i;
    }

  }
}

mmdispdso.cs
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmdispdso.cs                                   */
/*  `````````````````                                   */
/*  Example for the use of the Mosel libraries          */
/*  (display the contents of a module)                  */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: J.Farmer & Y. Colombani                 */
/********************************************************/

using System;
using Mosel;

namespace mmdispdso {
  public class mmdispdsoClass {
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    [STAThread]
    static void Main(string[] args) {
      XPRM mosel;
      XPRMModule module;

      if (args.Length!=1) {
        Console.WriteLine("Usage: mmdispdso modulename");
        return;
      }

      // Initialize Mosel
      mosel = XPRM.Init();
      // Load the module
      module = mosel.LoadModule(args[0]);
      // And output basic information
      Console.WriteLine(
        "Module `{0}' version {1}",
        module.Name,
        module.Version
      );
      if (module.Certificate!=null && module.Certificate.Length>0)
        Console.WriteLine(" ({0})", module.Certificate);
      else
        Console.WriteLine();

      Console.WriteLine();  // spacing

      // Output a list of defined constants
      Console.WriteLine("Constants:");
      foreach (XPRMConstant p in module.Constants) {
        Console.WriteLine(" {0}={1}", p.Name, p.Value);
      }
      Console.WriteLine();  // spacing

      // Output a list of types defined within the module
      Console.WriteLine("Types:");
      foreach (XPRMNativeType nt in module.Types) {
        Console.Write(" {0} (", nt.Name);
        if (nt.HasCreate) Console.Write("create");
        if (nt.HasDelete) Console.Write(",delete");
        if (nt.HasToString) Console.Write(",tostring");
        if (nt.HasPRTBL) Console.Write("+");
        if (nt.HasFromString) Console.Write(",fromstring");
        Console.WriteLine(")");
      }
      Console.WriteLine();  // spacing

      // List of control parameters
      Console.WriteLine("Control Parameters:");
      foreach (XPRMParameter p in module.Parameters) {
        Console.Write(" {0}: {1} (", p.Name, p.TypeName);
        if (p.Description!=null && p.Description.Length>0)
          Console.Write("{0},", p.Description);
        Console.WriteLine(rwstatus(p) + ")");
      }
      Console.WriteLine();  // spacing

      // List of subroutines
      Console.WriteLine("Procedures and Functions:");
      foreach (XPRMProcedure p in module.Procedures) {
        dispProcFct(p);
      }
      Console.WriteLine();  // spacing

      // List of IO drivers
      Console.WriteLine("I/O drivers:");
      foreach(XPRMIODriver iod in module.IODrivers) {
        Console.WriteLine(
          " {0}:{1}",
          iod.Name,
          (iod.Info!=null)?iod.Info:""
        );
      }
    }


    /// <summary>
    /// Return the r/w status of a control parameter
    /// </summary>
    static string rwstatus(XPRMParameter p) {
      if (p.IsReadable)
        if (p.IsWriteable)
          return "r/w";
        else
          return "r";
      else
        if (p.IsWriteable)
          return "w";
        else
          return "?";
    }


    /// <summary>
    /// Display a prototype from a signature
    /// </summary>
    static void dispProcFct(XPRMProcedure proc) {
      char[] parms;
      int i;
      if (proc.TypeCode!=XPRMVarType.NOT)
        Console.Write(" function {0}", proc.Name);
      else
        Console.Write(" procedure {0}", proc.Name);

      if (proc.NbParameters>0) {
        Console.Write("(");
        parms = proc.ParameterTypes.ToCharArray();
        i = 0;
        while (i<parms.Length) {
          if (i>0) Console.Write(",");
          i = dispType(i, parms)+1;
        }
        Console.Write(")");
      }

      if (proc.TypeCode!=XPRMVarType.NOT)
        Console.Write(":{0}", proc.TypeName);
      Console.WriteLine();
    }

    /// <summary>
    /// Display a type name from a signature
    /// </summary>
    static int dispType(int i, char[] parms) {
      int j;
      switch (parms[i]) {
        case 'i': Console.Write("integer"); break;
        case 'r': Console.Write("real"); break;
        case 's': Console.Write("string"); break;
        case 'b': Console.Write("boolean"); break;
        case 'v': Console.Write("mpvar"); break;
        case 'c': Console.Write("linctr"); break;
        case 'I': Console.Write("range"); break;
        case 'a': Console.Write("array"); break;
        case 'e': Console.Write("set"); break;
        case '|':
          i++;
          do {
            Console.Write(parms[i++]);
          } while (parms[i]!='|');
          break;
        case '!':
          i++;
          do {
            Console.Write(parms[i++]);
          } while (parms[i]!='!');
          break;
        case 'A':
          Console.Write("array (");
          j=++i;
          while (parms[i]!='.') {
            if (j!=i) Console.Write(",");
            i=dispType(i, parms)+1;
          }
          Console.Write(") of ");
          i=dispType(i+1, parms);
          break;
        case 'E':
          Console.Write("set of ");
          i = dispType(++i, parms);
          break;
        default:
          Console.Write("?");
          break;
      }
      return i;
    }

  }
}