Decision Optimization

 View Only
  • 1.  Tuple Schema

    Posted Mon November 30, 2020 09:52 AM
    In the OPL Java API, how do I get the schema of a tupleset, specifically the names and data types of the columns?

    #DecisionOptimization


  • 2.  RE: Tuple Schema

    Posted Mon November 30, 2020 09:59 AM
    Edited by System Fri January 20, 2023 04:21 PM
    Hi

    tuple t
    {
      int a;
      string b;
      
    }
    
    {t} tupleSet={<1,"2">};
    
    execute
    {
      
    
    
    var nbFields=tupleSet.getNFields();
        for(var j=0;j<nbFields;j++) write(tupleSet.getFieldName(j),";");
        writeln();
        
      }  ​


    gives

    a;b;

    and

    tuple t
    {
      int a;
      string b;
      
    }
    
    {t} tupleSet={<1,"2">};
    
    execute
    {
      
      var nbFields=tupleSet.getNFields();
      for(var j=0;j<nbFields;j++) 
      {
        write(tupleSet.getFieldName(j),":");
        var value=Opl.first(tupleSet)[tupleSet.getFieldName(j)];
        writeln(typeof(value));
      }    
      writeln();
     
    } ​

    gives

    a:number
    b:string​


    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: Tuple Schema

    Posted Tue December 01, 2020 01:17 PM
    Thanks Alex. Your answer isn't quite what I need. First of all, I'm working in Java, not OPLScript, and some of the classes differ from OPLScript (e.g. IloTupleset has no getNFields() or getFieldName() methods). I have to work with IloTupleSchema instead, where I can use getSize() and getColumnName(). Also, I can't directly find the type of the column, but instead I have to try each type using isSymbol(), etc. (That's ok, because I would have to match the JSON type with the OPL type anyway.)

    More fundamentally, in my application, I'm writing a custom OPL data source to read from JSON. I need the tuple schema to tell the customRead method how to parse the JSON. At the point where this occurs, I have created an IloOplModel from a .mod file but I have read no data. I cannot call IloOplModel,generate() because there is no data. So when I try to call
    oplModel.getElement(tableName).asTupleSet().getSchema()
    to get the schema, I get an error
    ilog.concert.IloException: Impossible to load model.

    So my question is, how can I get the tuple schema defined in the model before I read any data and generate the model?


    ------------------------------
    Jeremy Bloom
    ------------------------------



  • 4.  RE: Tuple Schema

    Posted Wed December 02, 2020 04:49 AM
    Hi,

    "So my question is, how can I get the tuple schema defined in the model before I read any data and generate the model?"

    ==> Can you try to use a dummy .dat so that you can call generate ?

    For reading json, have you tried a converter from json to dat like
    Turn json into dat files
    in https://www.linkedin.com/pulse/tips-tricks-opl-cplex-alex-fleischer/  ?

    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 5.  RE: Tuple Schema

    Posted Wed December 02, 2020 11:43 PM
    Edited by System Fri January 20, 2023 04:12 PM
    I found a solution. The relevant code is :
    IloOplTupleSchemaDefinition tupleSchema= oplModel
      .getModelDefinition()
      .getElementDefinition(tableName)   //set of
      .getLeaf()                                          //tuple
      .asTuple()
      .getTupleSchema();

    Notice that it relies on two undocumented classes, IloOplElementDefinition and IloOplTupleSchemaDefinition. It seems to work because getting the model definition does not require generating the model. I hope this helps someone else down the road.

    ------------------------------
    Jeremy Bloom
    ------------------------------