1313// This class is in package ilog.cplex so that we can call some undocumented functions
1414package ilog .cplex ;
1515
16- import java .io .File ;
17- import java .io .FileInputStream ;
18- import java .io .IOException ;
19- import java .io .InputStream ;
16+ import java .io .*;
17+ import java .nio .charset .StandardCharsets ;
2018import java .util .HashMap ;
2119import java .util .Iterator ;
2220import java .util .Map ;
4038import org .w3c .dom .Node ;
4139import org .w3c .dom .NodeList ;
4240import org .xml .sax .SAXException ;
41+ import sun .misc .IOUtils ;
4342
4443/** Base class for external solves.
4544 * @author daniel.junglas@de.ibm.com
@@ -143,6 +142,9 @@ private void addExpr(HashMap<String,IloNumVar> name2var, HashMap<IloNumVar,Strin
143142 *
144143 */
145144 protected static class Solution {
145+
146+ private String solution = null ;
147+
146148 boolean feasible = false ;
147149 /** Map variable names to values. */
148150 public HashMap <String ,Double > name2val = new HashMap <String ,Double >();
@@ -160,7 +162,7 @@ protected static class Solution {
160162 public boolean pfeas = false ;
161163 /** Dual feasible? */
162164 public boolean dfeas = false ;
163-
165+
164166 public Solution () {}
165167 public Solution (File solutionXml , Set <String > knownVariables , Set <String > knownConstraints ) throws IOException {
166168 this ();
@@ -170,8 +172,17 @@ public Solution(InputStream solutionXml, Set<String> knownVariables, Set<String>
170172 this ();
171173 parse (solutionXml , knownVariables , knownConstraints );
172174 }
173-
175+
176+ public boolean hasSolution () {
177+ return solution != null ;
178+ }
179+
180+ public String getSolution () {
181+ return solution ;
182+ }
183+
174184 public void reset () {
185+ solution = null ;
175186 feasible = false ;
176187 name2val = new HashMap <String , Double >();
177188 var2val = new HashMap <IloNumVar , Double >();
@@ -189,6 +200,16 @@ public void parse(File solutionXml, Set<String> knownVariables, Set<String> know
189200 try (FileInputStream fis = new FileInputStream (solutionXml )) {
190201 parse (fis , knownVariables , knownConstraints );
191202 }
203+ try (FileInputStream fis = new FileInputStream (solutionXml )) {
204+ ByteArrayOutputStream result = new ByteArrayOutputStream ();
205+ byte [] buffer = new byte [1024 ];
206+ int length ;
207+ while ((length = fis .read (buffer )) != -1 ) {
208+ result .write (buffer , 0 , length );
209+ }
210+ // StandardCharsets.UTF_8.name() > JDK 7
211+ solution = result .toString ("UTF-8" );
212+ }
192213 }
193214
194215 private enum ParserState {
@@ -220,8 +241,9 @@ private static String[] getAttributes(XMLStreamReader reader, String... attrs) {
220241 * @param knownVariables The names of the variables for which values should be extracted from <code>solutionXml</code>.
221242 * @throws IOException If an input/output error occurs or mandatory solution information is missing.
222243 */
223- public void parse (InputStream solutionXml , Set <String > knownVariables , Set <String > knownConstraints ) throws IOException {
244+ private void parse (InputStream solutionXml , Set <String > knownVariables , Set <String > knownConstraints ) throws IOException {
224245 reset ();
246+
225247 boolean ok = false ;
226248 try {
227249 final String MALFORMED_XML = "Malformed XML" ;
@@ -438,11 +460,10 @@ public void parse2(InputStream solutionXml, Set<String> knownVariables) throws I
438460 }
439461 }
440462
441- private Solution result = null ;
463+ protected Solution result = null ;
442464
443465 public boolean solve () throws IloException {
444- result = null ;
445-
466+
446467 HashMap <IloNumVar ,String > oldVarNames = null ;
447468 if (namingStrategy == NamingStrategy .MAKE_NAMES )
448469 oldVarNames = new HashMap <IloNumVar , String >();
0 commit comments