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
------------------------------
Original Message:
Sent: Wed December 02, 2020 04:49 AM
From: ALEX FLEISCHER
Subject: Tuple Schema
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]
Original Message:
Sent: Tue December 01, 2020 01:17 PM
From: Jeremy Bloom
Subject: Tuple Schema
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
Original Message:
Sent: Mon November 30, 2020 09:59 AM
From: ALEX FLEISCHER
Subject: Tuple Schema
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:numberb:string
------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
Original Message:
Sent: Fri November 27, 2020 12:29 AM
From: Jeremy Bloom
Subject: Tuple Schema
In the OPL Java API, how do I get the schema of a tupleset, specifically the names and data types of the columns?
#DecisionOptimization