|
PPL Java Language Interface 2.0
|
The Parma Polyhedra Library comes equipped with an interface for the Java language. The Java interface provides access to the numerical abstractions (convex polyhedra, BD shapes, octagonal shapes, etc.) implemented by the PPL library. A general introduction to the numerical abstractions, their representation in the PPL and the operations provided by the PPL is given in the main PPL user manual. Here we just describe those aspects that are specific to the Java interface.
Built-in linear expression classes copy operands on construction and make independent copies on clone(). Copying built-in nodes and converting expressions to native PPL values use explicit work lists, with stack usage independent of tree depth. Supply finite, acyclic expression trees. Native conversion preserves expression grouping and uses the configured coefficient rules at each operation. Large trees require heap storage and processing time in proportion to the work performed.
The Java interface requires a JDK with javac -h support (JDK 8 or later) and its JNI development headers. Java classes and native headers are generated together by javac; the obsolete javah tool is not needed. Select a JDK with configure's --with-java=DIR option or JAVA_HOME. The --enable-interfaces option selects the interfaces to build.
The Java interface provides the parma_polyhedra_library package. The JAR ppl_java.jar and the JNI shared library are installed in the configured pkglibdir, normally ${libdir}/ppl. Here libdir is the configured library directory, which already includes the installation prefix; for example, libdir=/usr/local/lib gives /usr/local/lib/ppl. A nonstandard libdir changes both Java installation paths accordingly.
Save the following program as My_Test.java. It loads the JNI library, initializes the interface, uses an owned native object, releases that object and finalizes the interface. It prints the native PPL version and the problem's space dimension. Check the reported version against the versions your application supports.
import parma_polyhedra_library.MIP_Problem;
import parma_polyhedra_library.Parma_Polyhedra_Library;
public final class My_Test {
public static void main(String[] args) {
System.loadLibrary("ppl_java");
Parma_Polyhedra_Library.initialize_library();
try {
System.out.println("PPL " + Parma_Polyhedra_Library.version());
MIP_Problem problem = new MIP_Problem(1);
try {
System.out.println("Space dimension: "
+ problem.space_dimension());
} finally {
problem.free();
}
} finally {
Parma_Polyhedra_Library.finalize_library();
}
}
}
Set PPL_JAVA_DIR to the actual configured pkglibdir before compiling and running. The following POSIX-shell example uses the default directory for an installation under /usr/local:
The classpath locates the JAR, while -Djava.library.path lets System.loadLibrary("ppl_java") locate the JNI library without embedding a platform-specific shared-library filename in the Java source. The platform loader must also find the PPL and GMP shared libraries; a nonstandard installation may require its runtime search paths to be configured. These commands use : as the classpath separator on Unix-like systems; Windows uses ;.
System.load with the full absolute path of the JNI shared library is another supported loading method; its filename depends on the platform.
Call initialize_library() after loading the JNI library and before constructing or using objects from other PPL package classes. Release owned native objects using their class-specific cleanup methods while the library and their thread state are still initialized. Stop using borrowed views before their owners are released or invalidated. Garbage collection is not a substitute for arranging this cleanup before finalize_library().
Concurrent native computations require a PPL build configured with --enable-thread-safe and compliance with the binding's synchronization rules. Each additional participating worker calls initialize_thread() before its PPL work, cleans up its native objects, and calls finalize_thread() before exiting. The thread that calls initialize_library() already has its thread state and uses the library lifecycle for its own entry and exit.
Serialize library initialization and finalization on the same thread. Join all workers after their PPL work and thread cleanup before calling finalize_library(). Use independently owned objects in different workers, or synchronize every access to shared objects, including queries that can update internal caches. See README.thread-safe.md for the native lifetime and synchronization rules. Reinitialization starts a fresh lifetime; it does not revive objects or views from a completed lifetime.
--enable-instantiations). The Termination methods for computing all affine ranking functions or quasi-ranking functions require the Polyhedron instantiation, since their results are polyhedra. When it is not selected, those methods are omitted; termination tests and methods computing one ranking function remain available for the selected simple domains. With --enable-instantiations=none, the domain-independent classes remain available and Termination has no methods.k, the identifiers used for the PPL variables must lie between 0 and 
parma_polyhedra_library.Constraint_System) is implemented by extending class java.util.ArrayList (note: java.util.Vector was used up to version 1.0.) As a consequence, it is possible to iterate over the constraints in the system by using corresponding inherited methods. Similarly, it is possible to modify a system of constraints by using methods such as add; be warned, however, that the constraint system obtained from a PPL object is a copy of the (C++) data structure used in the object itself: any modification will not directly affect the original PPL object; clearly, the modified constraint system can be used with appropriate methods to, e.g., build a new PPL object or modify an existing one. The same observations apply to systems of congruences and systems of (grid) generators.For PIP ownership and traversal operations, see PIP_Problem and PIP_Tree_Node.
PIP_Problem.solution() and optimizing_solution() return borrowed tree handles that retain the owning problem. Child handles and the typed views returned by as_solution() and as_decision() independently retain that same owner, so a child remains usable after its parent wrapper is collected.
Updating the problem or calling its free() invalidates all existing tree handles, including descendants and typed views. Once argument conversion and admission checks succeed, entering a native update invalidates handles even if the update does nothing or subsequently throws. Later tree operations raise Invalid_Argument_Exception. Obtain a fresh root and traverse it again after an update. Cached solution queries preserve existing handles.
Powerset iterators and disjunct views retain their owning powerset. A view is valid only while its owner has not been released or used by an operation that can change or normalize its disjunct sequence. Such operations may invalidate existing views even when they make no mathematical change or fail. Comparisons, property queries, conversions and string output can normalize the sequence. Space dimension, size, invariant checks, memory-size queries and ASCII dumps preserve views, as do iterator creation, copying, comparison and movement. Iterators from different owners cannot be compared or used as one range; movement past either boundary and dereferencing the end iterator raise an invalid-argument exception.
Disjunct views are read-only. Construct an owned copy to modify a disjunct. The copy has an independent lifetime. Dropping a single disjunct advances and refreshes the supplied iterator itself; other existing views are invalidated. Dropping a range invalidates all existing views, including its boundary iterators. Invalidated views raise an invalid-argument exception before native storage is accessed. Java explicit release remains safe for invalidated views.
PIP_Problem.free(), MIP_Problem.free() and Partial_Function.free() clear their native handles; repeated calls are harmless. Their object finalizers use the same cleanup behavior. Operations requiring a released native object raise Invalid_Argument_Exception.
Use explicit release when prompt native-resource cleanup is needed. Continue to observe the separate initialize_library() and finalize_library() lifecycle described above; per-object cleanup does not replace it.