00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef PPL_ppl_java_common_defs_hh
00025 #define PPL_ppl_java_common_defs_hh 1
00026
00027 #define PPL_NO_AUTOMATIC_INITIALIZATION
00028 #include "ppl.hh"
00029
00030 #ifdef PPL_WATCHDOG_LIBRARY_ENABLED
00031 #include "pwl.hh"
00032 #endif
00033
00034 #include <jni.h>
00035 #include "interfaced_boxes.hh"
00036 #include "marked_pointers.hh"
00037
00038 #define CATCH_ALL \
00039 catch(const Java_ExceptionOccurred& e) { \
00040 } \
00041 catch(const std::overflow_error& e) { \
00042 handle_exception(env, e); \
00043 } \
00044 catch(const std::length_error& e) { \
00045 handle_exception(env, e); \
00046 } \
00047 catch (const std::bad_alloc& e) { \
00048 handle_exception(env, e); \
00049 } \
00050 catch (const std::domain_error& e) { \
00051 handle_exception(env, e); \
00052 } \
00053 catch (const std::invalid_argument& e) { \
00054 handle_exception(env, e); \
00055 } \
00056 catch(const std::logic_error& e) { \
00057 handle_exception(env, e); \
00058 } \
00059 catch (const std::exception& e) { \
00060 handle_exception(env, e); \
00061 } \
00062 catch (const timeout_exception& e) { \
00063 handle_exception(env, e); \
00064 } \
00065 catch (const deterministic_timeout_exception& e) { \
00066 handle_exception(env, e); \
00067 } \
00068 catch (...) { \
00069 handle_exception(env); \
00070 }
00071
00072 #define CHECK_EXCEPTION_ASSERT(env) \
00073 assert(!env->ExceptionOccurred())
00074 #define CHECK_EXCEPTION_THROW(env) \
00075 do { \
00076 if (env->ExceptionOccurred()) \
00077 throw Java_ExceptionOccurred(); \
00078 } while (0)
00079 #define CHECK_EXCEPTION_RETURN(env, val) \
00080 do { \
00081 if (env->ExceptionOccurred()) \
00082 return val; \
00083 } while (0)
00084 #define CHECK_EXCEPTION_RETURN_VOID(env) \
00085 do { \
00086 if (env->ExceptionOccurred()) \
00087 return; \
00088 } while (0)
00089 #define CHECK_RESULT_ABORT(env, cond) \
00090 do { \
00091 if (!(cond)) \
00092 abort(); \
00093 } while (0)
00094 #define CHECK_RESULT_ASSERT(env, cond) \
00095 assert(cond)
00096 #define CHECK_RESULT_THROW(env, cond) \
00097 do { \
00098 if (!(cond)) \
00099 throw Java_ExceptionOccurred(); \
00100 } while (0)
00101 #define CHECK_RESULT_RETURN(env, cond, val) \
00102 do { \
00103 if (!(cond)) \
00104 return val; \
00105 } while (0)
00106 #define CHECK_RESULT_RETURN_VOID(env, cond) \
00107 do { \
00108 if (!(cond)) \
00109 return; \
00110 } while (0)
00111
00112
00113 namespace Parma_Polyhedra_Library {
00114
00115 namespace Interfaces {
00116
00117 namespace Java {
00118
00119 struct Java_ExceptionOccurred : public std::exception {
00120 };
00121
00122 class timeout_exception
00123 : public Parma_Polyhedra_Library::Throwable {
00124 public:
00125 void throw_me() const {
00126 throw *this;
00127 }
00128 int priority() const {
00129 return 0;
00130 }
00131 };
00132
00133 class deterministic_timeout_exception
00134 : public Parma_Polyhedra_Library::Throwable {
00135 public:
00136 void throw_me() const {
00137 throw *this;
00138 }
00139 int priority() const {
00140 return 0;
00141 }
00142 };
00143
00144 #ifdef PPL_WATCHDOG_LIBRARY_ENABLED
00145 extern Parma_Watchdog_Library::Watchdog* p_timeout_object;
00146
00147 typedef
00148 Parma_Watchdog_Library::Threshold_Watcher
00149 <Parma_Polyhedra_Library::Weightwatch_Traits> Weightwatch;
00150
00151 extern Weightwatch* p_deterministic_timeout_object;
00152 #endif // PPL_WATCHDOG_LIBRARY_ENABLED
00153
00154 void reset_timeout();
00155
00156 void reset_deterministic_timeout();
00157
00158 void
00159 handle_exception(JNIEnv* env, const std::logic_error& e);
00160
00161 void
00162 handle_exception(JNIEnv* env, const std::invalid_argument& e);
00163
00164 void
00165 handle_exception(JNIEnv* env, const std::domain_error& e);
00166
00167 void
00168 handle_exception(JNIEnv* env, const std::overflow_error& e);
00169
00170 void
00171 handle_exception(JNIEnv* env, const std::length_error& e);
00172
00173 void
00174 handle_exception(JNIEnv* env, const std::bad_alloc&);
00175
00176 void
00177 handle_exception(JNIEnv* env, const std::exception& e);
00178
00179 void
00180 handle_exception(JNIEnv* env, const timeout_exception& e);
00181
00182 void
00183 handle_exception(JNIEnv* env, const deterministic_timeout_exception& e);
00184
00185 void
00186 handle_exception(JNIEnv* env);
00187
00189
00193 class Java_Class_Cache {
00194 public:
00195
00196 jclass Boolean;
00197 jclass Integer;
00198 jclass Long;
00199 jclass Iterator;
00200
00201 jclass Artificial_Parameter;
00202 jclass Artificial_Parameter_Sequence;
00203 jclass Bounded_Integer_Type_Overflow;
00204 jclass Bounded_Integer_Type_Representation;
00205 jclass Bounded_Integer_Type_Width;
00206 jclass By_Reference;
00207 jclass Coefficient;
00208 jclass Congruence;
00209 jclass Constraint;
00210 jclass Generator;
00211 jclass Grid_Generator;
00212 jclass Generator_Type;
00213 jclass Grid_Generator_Type;
00214 jclass Constraint_System;
00215 jclass Congruence_System;
00216 jclass Generator_System;
00217 jclass Grid_Generator_System;
00218 jclass Linear_Expression;
00219 jclass Linear_Expression_Coefficient;
00220 jclass Linear_Expression_Difference;
00221 jclass Linear_Expression_Sum;
00222 jclass Linear_Expression_Times;
00223 jclass Linear_Expression_Unary_Minus;
00224 jclass Linear_Expression_Variable;
00225 jclass MIP_Problem_Status;
00226 jclass Optimization_Mode;
00227 jclass Pair;
00228 jclass PIP_Problem_Control_Parameter_Name;
00229 jclass PIP_Problem_Control_Parameter_Value;
00230 jclass PIP_Problem_Status;
00231 jclass Poly_Con_Relation;
00232 jclass Poly_Gen_Relation;
00233 jclass PPL_Object;
00234 jclass Relation_Symbol;
00235 jclass Variable;
00236 jclass Variables_Set;
00237
00239 Java_Class_Cache();
00240
00242 void init_cache(JNIEnv* env);
00243
00245 void clear_cache(JNIEnv* env);
00246
00247 private:
00249 void init_cache(JNIEnv* env, jclass& field, const char* name);
00250
00252 void clear_cache(JNIEnv* env, jclass& field);
00253
00254
00255 Java_Class_Cache(const Java_Class_Cache&);
00256
00257 Java_Class_Cache& operator=(const Java_Class_Cache&);
00258 };
00259
00261
00267 struct Java_FMID_Cache {
00268
00269 jmethodID Boolean_valueOf_ID;
00270 jmethodID Boolean_boolValue_ID;
00271 jmethodID Integer_valueOf_ID;
00272 jmethodID Integer_intValue_ID;
00273 jmethodID Long_valueOf_ID;
00274 jmethodID Long_longValue_ID;
00275
00276
00277
00278 jfieldID Artificial_Parameter_le_ID;
00279 jfieldID Artificial_Parameter_den_ID;
00280 jmethodID Artificial_Parameter_init_ID;
00281 jmethodID Artificial_Parameter_Sequence_init_ID;
00282 jmethodID Artificial_Parameter_Sequence_add_ID;
00283
00284 jfieldID Bounded_Integer_Type_Overflow_OVERFLOW_WRAPS_ID;
00285 jfieldID Bounded_Integer_Type_Overflow_OVERFLOW_UNDEFINED_ID;
00286 jfieldID Bounded_Integer_Type_Overflow_OVERFLOW_IMPOSSIBLE_ID;
00287 jmethodID Bounded_Integer_Type_Overflow_ordinal_ID;
00288
00289 jfieldID Bounded_Integer_Type_Representation_UNSIGNED_ID;
00290 jfieldID Bounded_Integer_Type_Representation_SIGNED_2_COMPLEMENT_ID;
00291 jmethodID Bounded_Integer_Type_Representation_ordinal_ID;
00292
00293 jfieldID Bounded_Integer_Type_Width_BITS_8_ID;
00294 jfieldID Bounded_Integer_Type_Width_BITS_16_ID;
00295 jfieldID Bounded_Integer_Type_Width_BITS_32_ID;
00296 jfieldID Bounded_Integer_Type_Width_BITS_64_ID;
00297 jfieldID Bounded_Integer_Type_Width_BITS_128_ID;
00298 jmethodID Bounded_Integer_Type_Width_ordinal_ID;
00299
00300 jfieldID By_Reference_obj_ID;
00301 jmethodID By_Reference_init_ID;
00302
00303 jfieldID Coefficient_value_ID;
00304 jmethodID Coefficient_init_from_String_ID;
00305 jmethodID Coefficient_toString_ID;
00306
00307 jmethodID Complexity_Class_ordinal_ID;
00308
00309 jfieldID Congruence_mod_ID;
00310 jfieldID Congruence_lhs_ID;
00311 jfieldID Congruence_rhs_ID;
00312 jmethodID Congruence_init_ID;
00313
00314 jfieldID Constraint_lhs_ID;
00315 jfieldID Constraint_rhs_ID;
00316 jfieldID Constraint_kind_ID;
00317 jmethodID Constraint_init_ID;
00318
00319 jmethodID Degenerate_Element_ordinal_ID;
00320
00321 jfieldID Generator_gt_ID;
00322 jfieldID Generator_le_ID;
00323 jfieldID Generator_div_ID;
00324 jmethodID Generator_line_ID;
00325 jmethodID Generator_ray_ID;
00326 jmethodID Generator_point_ID;
00327 jmethodID Generator_closure_point_ID;
00328
00329 jfieldID Grid_Generator_gt_ID;
00330 jfieldID Grid_Generator_le_ID;
00331 jfieldID Grid_Generator_div_ID;
00332 jmethodID Grid_Generator_grid_line_ID;
00333 jmethodID Grid_Generator_parameter_ID;
00334 jmethodID Grid_Generator_grid_point_ID;
00335
00336 jmethodID Generator_Type_ordinal_ID;
00337 jmethodID Grid_Generator_Type_ordinal_ID;
00338
00339 jmethodID Constraint_System_init_ID;
00340 jmethodID Constraint_System_add_ID;
00341 jmethodID Congruence_System_init_ID;
00342 jmethodID Congruence_System_add_ID;
00343 jmethodID Generator_System_init_ID;
00344 jmethodID Generator_System_add_ID;
00345 jmethodID Grid_Generator_System_init_ID;
00346 jmethodID Grid_Generator_System_add_ID;
00347
00348 jmethodID System_iterator_ID;
00349 jmethodID System_Iterator_has_next_ID;
00350 jmethodID System_Iterator_next_ID;
00351
00352 jmethodID Linear_Expression_sum_ID;
00353 jmethodID Linear_Expression_times_ID;
00354
00355 jfieldID Linear_Expression_Coefficient_coeff_ID;
00356 jmethodID Linear_Expression_Coefficient_init_ID;
00357 jfieldID Linear_Expression_Difference_lhs_ID;
00358 jfieldID Linear_Expression_Difference_rhs_ID;
00359 jfieldID Linear_Expression_Sum_lhs_ID;
00360 jfieldID Linear_Expression_Sum_rhs_ID;
00361 jfieldID Linear_Expression_Times_coeff_ID;
00362 jfieldID Linear_Expression_Times_lin_expr_ID;
00363 jmethodID Linear_Expression_Times_init_from_coeff_var_ID;
00364 jfieldID Linear_Expression_Unary_Minus_arg_ID;
00365 jmethodID Linear_Expression_Variable_init_ID;
00366 jmethodID Linear_Expression_Variable_var_id_ID;
00367
00368 jfieldID MIP_Problem_Status_UNFEASIBLE_MIP_PROBLEM_ID;
00369 jfieldID MIP_Problem_Status_UNBOUNDED_MIP_PROBLEM_ID;
00370 jfieldID MIP_Problem_Status_OPTIMIZED_MIP_PROBLEM_ID;
00371 jmethodID MIP_Problem_Status_ordinal_ID;
00372
00373 jfieldID Optimization_Mode_MAXIMIZATION_ID;
00374 jfieldID Optimization_Mode_MINIMIZATION_ID;
00375 jmethodID Optimization_Mode_ordinal_ID;
00376
00377 jfieldID PIP_Problem_Control_Parameter_Name_CUTTING_STRATEGY_ID;
00378 jfieldID PIP_Problem_Control_Parameter_Name_PIVOT_ROW_STRATEGY;
00379 jmethodID PIP_Problem_Control_Parameter_Name_ordinal_ID;
00380
00381 jfieldID PIP_Problem_Control_Parameter_Value_CUTTING_STRATEGY_FIRST_ID;
00382 jfieldID PIP_Problem_Control_Parameter_Value_CUTTING_STRATEGY_DEEPEST_ID;
00383 jfieldID PIP_Problem_Control_Parameter_Value_CUTTING_STRATEGY_ALL_ID;
00384 jfieldID PIP_Problem_Control_Parameter_Value_PIVOT_ROW_STRATEGY_FIRST_ID;
00385 jfieldID PIP_Problem_Control_Parameter_Value_PIVOT_ROW_STRATEGY_MAX_COLUMN_ID;
00386 jmethodID PIP_Problem_Control_Parameter_Value_ordinal_ID;
00387
00388 jfieldID PIP_Problem_Status_UNFEASIBLE_PIP_PROBLEM_ID;
00389 jfieldID PIP_Problem_Status_OPTIMIZED_PIP_PROBLEM_ID;
00390 jmethodID PIP_Problem_Status_ordinal_ID;
00391
00392 jfieldID Pair_first_ID;
00393 jfieldID Pair_second_ID;
00394
00395 jmethodID Poly_Con_Relation_init_ID;
00396 jmethodID Poly_Gen_Relation_init_ID;
00397
00398 jfieldID PPL_Object_ptr_ID;
00399
00400 jfieldID Relation_Symbol_EQUAL_ID;
00401 jfieldID Relation_Symbol_GREATER_OR_EQUAL_ID;
00402 jfieldID Relation_Symbol_GREATER_THAN_ID;
00403 jmethodID Relation_Symbol_ordinal_ID;
00404
00405 jfieldID Variable_varid_ID;
00406 jmethodID Variable_init_ID;
00407
00408 jmethodID Variables_Set_init_ID;
00409 jmethodID Variables_Set_add_ID;
00410 jmethodID Variables_Set_iterator_ID;
00411
00412 jmethodID Variables_Set_Iterator_has_next_ID;
00413 jmethodID Variables_Set_Iterator_next_ID;
00414 };
00415
00417 extern Java_Class_Cache cached_classes;
00418
00420 extern Java_FMID_Cache cached_FMIDs;
00421
00422
00432 template <typename U, typename V>
00433 U
00434 jtype_to_unsigned(const V& value);
00435
00437 jobject
00438 bool_to_j_boolean_class(JNIEnv* env, const bool value);
00439
00441 jint
00442 j_integer_to_j_int(JNIEnv* env, jobject j_integer);
00443
00445 jobject
00446 j_int_to_j_integer(JNIEnv* env, jint value);
00447
00449 jlong
00450 j_long_class_to_j_long(JNIEnv* env, jobject j_long);
00451
00453 jobject
00454 j_long_to_j_long_class(JNIEnv* env, jlong value);
00455
00460 bool
00461 is_java_marked(JNIEnv* env, jobject ppl_object);
00462
00463
00468 jobject
00469 build_java_poly_gen_relation(JNIEnv* env, Poly_Gen_Relation& r);
00470
00475 jobject
00476 build_java_poly_con_relation(JNIEnv* env, Poly_Con_Relation& r);
00477
00482 Variables_Set
00483 build_cxx_variables_set(JNIEnv* env, jobject v_set);
00484
00489 jobject
00490 build_java_variables_set(JNIEnv* env, const Variables_Set& v_set);
00491
00496 Relation_Symbol
00497 build_cxx_relsym(JNIEnv* env, jobject j_relsym);
00498
00503 Bounded_Integer_Type_Overflow
00504 build_cxx_bounded_overflow(JNIEnv* env, jobject j_bounded_overflow);
00505
00510 Bounded_Integer_Type_Width
00511 build_cxx_bounded_width(JNIEnv* env, jobject j_bounded_width);
00512
00517 Bounded_Integer_Type_Representation
00518 build_cxx_bounded_rep(JNIEnv* env, jobject j_bounded_rep);
00519
00524 Optimization_Mode
00525 build_cxx_optimization_mode(JNIEnv* env, jobject j_opt_mode);
00526
00531 jobject
00532 build_java_optimization_mode(JNIEnv* env, const Optimization_Mode& opt_mode);
00533
00538 MIP_Problem::Control_Parameter_Name
00539 build_cxx_control_parameter_name(JNIEnv* env, jobject j_cp_name);
00540
00545 jobject
00546 build_java_control_parameter_name
00547 (JNIEnv* env, const MIP_Problem::Control_Parameter_Name& cp_name);
00548
00553 MIP_Problem::Control_Parameter_Value
00554 build_cxx_control_parameter_value(JNIEnv* env, jobject j_cp_value);
00555
00560 jobject
00561 build_java_control_parameter_value
00562 (JNIEnv* env, const MIP_Problem::Control_Parameter_Value& cp_value);
00563
00568 PIP_Problem::Control_Parameter_Name
00569 build_cxx_pip_problem_control_parameter_name(JNIEnv* env, jobject j_cp_name);
00570
00575 jobject
00576 build_java_pip_problem_control_parameter_name
00577 (JNIEnv* env, const PIP_Problem::Control_Parameter_Name& cp_name);
00578
00583 PIP_Problem::Control_Parameter_Value
00584 build_cxx_pip_problem_control_parameter_value(JNIEnv* env, jobject j_cp_value);
00585
00590 jobject
00591 build_java_pip_problem_control_parameter_value
00592 (JNIEnv* env, const PIP_Problem::Control_Parameter_Value& cp_value);
00593
00598 jobject
00599 build_java_mip_status(JNIEnv* env, const MIP_Problem_Status& mip_status);
00600
00605 jobject
00606 build_java_pip_status(JNIEnv* env, const PIP_Problem_Status& pip_status);
00607
00612 Variable
00613 build_cxx_variable(JNIEnv* env, jobject j_var);
00614
00619 jobject
00620 build_java_variable(JNIEnv* env, const Variable& var);
00621
00626 Coefficient
00627 build_cxx_coeff(JNIEnv* env, jobject j_coeff);
00628
00633 jobject
00634 build_java_coeff(JNIEnv* env, const Coefficient& ppl_coeff);
00635
00640 Constraint
00641 build_cxx_constraint(JNIEnv* env, jobject j_constraint);
00642
00648 PIP_Tree_Node::Artificial_Parameter
00649 build_cxx_artificial_parameter(JNIEnv* env, jobject j_ap);
00650
00655 Linear_Expression
00656 build_cxx_linear_expression(JNIEnv* env, jobject j_le);
00657
00662 Congruence
00663 build_cxx_congruence(JNIEnv* env, jobject j_cg);
00664
00669 Generator
00670 build_cxx_generator(JNIEnv* env, jobject j_g);
00671
00676 Grid_Generator
00677 build_cxx_grid_generator(JNIEnv* env, jobject j_g);
00678
00683 Grid_Generator_System
00684 build_cxx_grid_generator_system(JNIEnv* env, jobject j_gs);
00685
00690 Constraint_System
00691 build_cxx_constraint_system(JNIEnv* env, jobject j_cs);
00692
00697 PIP_Tree_Node::Artificial_Parameter
00698 build_cxx_artificial_parameter(JNIEnv* env, jobject j_art_param);
00699
00704 PIP_Tree_Node::Artificial_Parameter_Sequence
00705 build_cxx_artificial_parameter_sequence(JNIEnv* env, jobject j_aps);
00706
00711 Generator_System
00712 build_cxx_generator_system(JNIEnv* env, jobject j_gs);
00713
00718 Congruence_System
00719 build_cxx_congruence_system(JNIEnv* env, jobject j_cgs);
00720
00725 jobject
00726 build_java_constraint(JNIEnv* env, const Constraint& c);
00727
00732 jobject
00733 build_java_artificial_parameter(JNIEnv* env,
00734 const PIP_Tree_Node::Artificial_Parameter& ap);
00735
00740 jobject
00741 build_java_congruence(JNIEnv* env, const Congruence& cg);
00742
00747 jobject
00748 build_java_generator(JNIEnv* env, const Generator& g);
00749
00754 jobject
00755 build_java_grid_generator(JNIEnv* env, const Grid_Generator& g);
00756
00761 jobject
00762 build_java_constraint_system(JNIEnv* env, const Constraint_System& cs);
00763
00768 jobject
00769 build_java_artificial_parameter_sequence(JNIEnv* env,
00770 const PIP_Tree_Node::Artificial_Parameter_Sequence& aps);
00771
00776 jobject
00777 build_java_grid_generator_system(JNIEnv* env, const Grid_Generator_System& gs);
00778
00783 jobject
00784 build_java_generator_system(JNIEnv* env, const Generator_System& gs);
00785
00790 jobject
00791 build_java_congruence_system(JNIEnv* env, const Congruence_System& cgs);
00792
00797 jobject
00798 build_java_linear_expression_coefficient(JNIEnv* env, const Coefficient& coeff);
00799
00804 void
00805 set_generator(JNIEnv* env, jobject dst, jobject src);
00806
00810 void
00811 set_coefficient(JNIEnv* env, jobject dst, jobject src);
00812
00817 void
00818 set_by_reference(JNIEnv* env, jobject by_ref_dst, jobject src);
00819
00824 jobject
00825 get_by_reference(JNIEnv* env, jobject by_reference);
00826
00827
00838 void
00839 set_pair_element(JNIEnv* env, jobject dst_pair, int arg, jobject src);
00840
00851 jobject
00852 get_pair_element(JNIEnv* env, int arg, jobject pair);
00853
00855 void*
00856 get_ptr(JNIEnv* env, jobject ppl_object);
00857
00859 template <typename T>
00860 void
00861 set_ptr(JNIEnv* env, jobject ppl_object,
00862 const T* address, bool to_be_marked = false);
00863
00868 template <typename R>
00869 jobject
00870 build_linear_expression(JNIEnv* env, const R& r);
00871
00872 }
00873
00874 }
00875
00876 }
00877
00878
00879 #include "ppl_java_common.inlines.hh"
00880
00881 #endif // !defined(PPL_ppl_prolog_common_defs_hh)