File: glpk.i

package info (click to toggle)
glpk-java 1.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,580 kB
  • sloc: sh: 3,609; java: 1,794; xml: 259; makefile: 154; ansic: 35
file content (398 lines) | stat: -rw-r--r-- 10,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
%module GLPK

%pragma(java) jniclassclassmodifiers="
  /**
   * The intermediary JNI class.
   * Loads the native library.
   */
   public class"

%pragma(java) jniclasscode=%{
  static {
    try {
      if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        // try to load Windows libraries
%}
#ifdef GLPKPRELOAD
%pragma(java) jniclasscode=%{
        try {
          System.loadLibrary("glpk_4_65");
        } catch (UnsatisfiedLinkError en) {
          // The dependent library might be in the OS library search path.
        }
%}
#endif
%pragma(java) jniclasscode=%{
        System.loadLibrary("glpk_4_65_java");
      } else {
        // try to load Linux library
%}
#ifdef GLPKPRELOAD
%pragma(java) jniclasscode=%{
        try {
          System.loadLibrary("glpk");
        } catch (UnsatisfiedLinkError e) {
          // The dependent library might be in the OS library search path.
        }
%}
#endif
%pragma(java) jniclasscode=%{
        System.loadLibrary("glpk_java");
      }
    } catch (UnsatisfiedLinkError e) {
      /**
       * Information string.
       */
      String info = "\n"
        + "The dynamic link library for GLPK for Java could not be "
        + "loaded.\nConsider using\njava -Djava.library.path=\n"
        + "The current value of system property java.library.path is:\n"
        + System.getProperty("java.library.path") + "\n\n";

      try {
          /**
           * Number of bits.
           */
          String bits = null;

          bits = System.getProperty("com.ibm.vm.bitmode");
          if (bits == null) {
              bits = System.getProperty("sun.arch.data.model");
          }

          info +=
            "java.vendor: " + System.getProperty("java.vendor")
            + "\njava.version: "
            + System.getProperty("java.version")
            + "\njava.vm.name: "
            + System.getProperty("java.vm.name")
            + "\njava.vm.version: "
            + System.getProperty("java.vm.version")
            + "\njava.runtime.version: "
            + System.getProperty("java.runtime.version");
          if (bits != null) {
              info += "\ndata model: " + bits + " bit";
          }
      } catch (SecurityException ex) {
          info += "\n\n";
          info += ex.getMessage();
      }
      info += "\n";
      System.err.println(info);
      throw e;  
    }
  }
%}

/* As there is no good transformation for va_list
 * we will just do nothing.
 * cf. http://swig.org/Doc1.3/SWIGDocumentation.html#Varargs_nn8
 * This typemap is necessary to compile on amd64
 * Linux. 
 */
%typemap(in) (va_list arg) {
}

/* The function glp_term_hook is modified to preset
 * the callback function.
 */
%exception glp_term_hook  { 
  arg1 = glp_java_term_hook;
  arg2 = (void *) jenv;
  $action
}

/* The function glp_init_iocp is modified to preset
 * the callback function.
 */
%typemap(out) void glp_init_iocp {
  arg1->cb_func = glp_java_cb;
  arg1->cb_info = (void *) jenv;
}

%{
#include "glpk.h"
#include "glpk_java.h"
#include <locale.h>
#include <setjmp.h>
#include <jni.h>
#include <config.h>

/*
 * Function declarations
 */
int glp_java_term_hook(void *info, const char *s);
void glp_java_error_hook(void *in);

/*
 * Static variables to handle errors inside callbacks
 */
#define GLP_JAVA_MAX_CALLBACK_LEVEL 4
TLS int glp_java_callback_level = 0;
TLS int glp_java_error_occured = 0;
TLS jmp_buf *glp_java_callback_env[GLP_JAVA_MAX_CALLBACK_LEVEL];

/*
 * Message level.
 */
TLS int glp_java_msg_level = GLP_JAVA_MSG_LVL_OFF;

/**
 * Aborts with error message.
 */
void glp_java_error(char *message) {
    glp_error("%s\n", message);
}

/**
 * Sets message level.
 */
void glp_java_set_msg_lvl(int msg_lvl) {
    glp_java_msg_level = msg_lvl;
}

/**
 * Sets locale for number formatting.
 */
void glp_java_set_numeric_locale(const char *locale) {
    setlocale(LC_NUMERIC, locale);
    }

/**
 * Terminal hook function.
 */
int glp_java_term_hook(void *info, const char *s) {
    jclass cls;
    jmethodID mid = NULL;
    JNIEnv *env = (JNIEnv *) info;
    jstring str = NULL;
    jint ret = 0;

    glp_java_callback_level++;
    if (glp_java_callback_level >= GLP_JAVA_MAX_CALLBACK_LEVEL) {
        glp_java_error_occured = 1;
    } else {
        glp_java_error_occured = 0;
        cls = (*env)->FindClass(env, "org/gnu/glpk/GlpkTerminal");
        if (cls != NULL) {
            mid = (*env)->GetStaticMethodID(
                env, cls, "callback", "(Ljava/lang/String;)I");
            if (mid != NULL) {
                str = (*env)->NewStringUTF(env, s);
                ret = (*env)->CallStaticIntMethod(env, cls, mid, str);
                if (str != NULL) {
                    (*env)->DeleteLocalRef( env, str );
                }
            }
            (*env)->DeleteLocalRef( env, cls );
        }
    }
    glp_java_callback_level--;
    if (glp_java_error_occured) {
       longjmp(*glp_java_callback_env[glp_java_callback_level], 1);
    }
    return ret;
}

/**
 * Call back function for MIP solver.
 */
void glp_java_cb(glp_tree *tree, void *info) {
    jclass cls;
    jmethodID mid = NULL;
    JNIEnv *env = (JNIEnv *) info;
    jlong ltree;

    glp_java_callback_level++;
    if (glp_java_callback_level >= GLP_JAVA_MAX_CALLBACK_LEVEL) {
        glp_java_error_occured = 1;
    } else {
        glp_java_error_occured = 0;
        cls = (*env)->FindClass(env, "org/gnu/glpk/GlpkCallback");
        if (cls != NULL) {
            mid = (*env)->GetStaticMethodID(
                env, cls, "callback", "(J)V");
        }
        if (mid != NULL) {
            *(glp_tree **)&ltree = tree;
            (*env)->CallStaticVoidMethod(env, cls, mid, ltree);
        }
        if (cls != NULL) {
            (*env)->DeleteLocalRef( env, cls );
        }
    }
    glp_java_callback_level--;
    if (glp_java_error_occured) {
       longjmp(*glp_java_callback_env[glp_java_callback_level], 1);
    }
}

/**
 * This hook function will be processed if an error occured
 * calling the glpk library.
 *
 * @param in pointer to long jump environment
 */
void glp_java_error_hook(void *in) {
    glp_java_error_occured = 1;
    /* free GLPK memory */
    glp_free_env();
    /* safely return */
    longjmp(*((jmp_buf*)in), 1);
}

/**
 * This function is used to throw a Java exception.
 *
 * @param env Java environment
 * @param message detail message
 */
void glp_java_throw(JNIEnv *env, char *message) {
    jclass newExcCls;
    newExcCls = (*env)->FindClass(env, "org/gnu/glpk/GlpkException");
    if (newExcCls == NULL) {
        newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
    }
    if (newExcCls != NULL) {
        (*env)->ThrowNew(env, newExcCls, message);
    }
}

/**
 * This function is used to throw a java.lang.OutOfMemoryError.
 *
 * @param env Java environment
 * @param message detail message
 */
void glp_java_throw_outofmemory(JNIEnv *env, char *message) {
    jclass newExcCls;
    newExcCls = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
    if (newExcCls != NULL) {
        (*env)->ThrowNew(env, newExcCls, message);
    }
}

/**
 * Gets arc data.
 *
 * @param arc arc
 * @return data
 */
glp_java_arc_data *glp_java_arc_get_data(const glp_arc *arc) {
   return (glp_java_arc_data *) arc->data;
}

/**
 * Gets vertex.
 *
 * @param G graph
 * @param i index
 * @return vertex
 */
glp_vertex *glp_java_vertex_get(
      const glp_graph *G, const int i) {
   if (i < 1 || i > G->nv) {
      glp_error( "Index %d is out of range.\n", i);
   }
   return G->v[i];
}

/**
 * Gets vertex data.
 *
 * @param G graph
 * @param i index to vertex
 * @return data
 */
glp_java_vertex_data *glp_java_vertex_data_get(
      const glp_graph *G, const int i) {
   if (i < 1 || i > G->nv) {
      glp_error( "Index %d is out of range.\n", i);
   }
   return (glp_java_vertex_data *) G->v[i]->data;
}

/**
 * Gets vertex data.
 *
 * @param v vertex
 * @return data
 */
glp_java_vertex_data *glp_java_vertex_get_data(
      const glp_vertex *v) {
   return v->data;
}

%}

// Add handling for structures
%include "glpk_java_structures.i"
%glp_structure(glp_arc)
%glp_structure(glp_graph)
%glp_structure(glp_vertex)

// Add handling for arrays
%include "glpk_java_arrays.i"
%glp_array_functions(int, intArray)
%glp_array_functions(double, doubleArray)

// Add handling for String arrays in glp_main
%include "various.i"
%apply char **STRING_ARRAY { const char *argv[] };

// Exception handling
%exception {
    jmp_buf glp_java_env;

    if (glp_java_msg_level != GLP_JAVA_MSG_LVL_OFF) {
        glp_printf("entering function $name.\n");
    }
    glp_java_callback_env[glp_java_callback_level] = &glp_java_env;
    if (setjmp(glp_java_env)) {
        glp_java_throw(jenv, "function $name failed");
    } else {
        glp_error_hook(glp_java_error_hook, &glp_java_env);
        $action;
    }
    glp_java_callback_env[glp_java_callback_level] = NULL;
    glp_error_hook(NULL, NULL);
    if (glp_java_msg_level != GLP_JAVA_MSG_LVL_OFF) {
        glp_printf("leaving function $name.\n");
    }
}

%typemap(javaclassmodifiers) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [],
  SWIGTYPE (CLASS::*) "
/**
 * Wrapper class for pointer generated by SWIG.
 * <p>Please, refer to doc/glpk-java.pdf of the GLPK for Java distribution
 * and to doc/glpk.pdf of the GLPK source distribution
 * for details. You can download the GLPK source distribution from
 * <a href=\"ftp://ftp.gnu.org/gnu/glpk\">ftp://ftp.gnu.org/gnu/glpk</a>.
 */
public class";

%pragma(java) moduleclassmodifiers = "
/**
 * Wrapper class generated by SWIG.
 * <p>Please, refer to doc/glpk-java.pdf of the GLPK for Java distribution
 * and to doc/glpk.pdf of the GLPK source distribution
 * for details. You can download the source distribution from
 * <a href=\"ftp://ftp.gnu.org/gnu/glpk\">ftp://ftp.gnu.org/gnu/glpk</a>.
 *
 * <p>For handling arrays of int and double the following methods are
 * provided:
 * @see #new_doubleArray(int)
 * @see #delete_doubleArray(SWIGTYPE_p_double)
 * @see #doubleArray_getitem(SWIGTYPE_p_double, int)
 * @see #doubleArray_setitem(SWIGTYPE_p_double, int, double)
 * @see #new_intArray(int)
 * @see #delete_intArray(SWIGTYPE_p_int)
 * @see #intArray_getitem(SWIGTYPE_p_int, int)
 * @see #intArray_setitem(SWIGTYPE_p_int, int, int)
 */
public class";
// Add the library to be wrapped
%include "glpk_java.i"
%include "glpk_javadoc.i"
%include "glpk.h"