PPL Java Language Interface 2.0
PPL Java Language Interface

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.

Linear expressions

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.

Build requirements

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.

Installation and example

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:

PPL_JAVA_DIR="/usr/local/lib/ppl"
javac -classpath "$PPL_JAVA_DIR/ppl_java.jar" My_Test.java
java -Djava.library.path="$PPL_JAVA_DIR" \
-classpath ".:$PPL_JAVA_DIR/ppl_java.jar" My_Test

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.

Library and worker lifetimes

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.

Numerical domains and representations

  • The numerical abstract domains available to the Java user as Java classes consist of the simple domains, powersets of a simple domain and products of simple domains. Note that the default configuration will only enable a subset of these domains (if you need a different set of domains, see configuration option --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.
    • The simple domains are:
      • convex polyhedra, which consist of C_Polyhedron and NNC_Polyhedron;
      • weakly relational, which consist of BD_Shape_N and Octagonal_Shape_N where N is one of the numeric types int8_t, int16_t, int32_t, int64_t, float, double, long_double, mpz_class, mpq_class;
      • boxes which consist of Int8_Box, Int16_Box, Int32_Box, Int64_Box, Uint8_Box, Uint16_Box, Uint32_Box, Uint64_Box, Float_Box, Double_Box, Long_Double_Box, Z_Box, Rational_Box; and
      • the Grid domain.
    • The powerset domains are Pointset_Powerset_S where S is a simple domain.
    • The product domains consist of Direct_Product_S_T, Smash_Product_S_T and Constraints_Product_S_T where S and T are simple domains.
  • In the following, any of the above numerical abstract domains is called a PPL domain and any element of a PPL domain is called a PPL object.
  • A Java program can create a new object for a PPL domain by using the constructors for the class corresponding to the domain.
  • For a PPL object with space dimension k, the identifiers used for the PPL variables must lie between 0 and $k-1$ and correspond to the indices of the associated Cartesian axes. For example, when using methods that combine PPL polyhedra or add constraints or generators to a representation of a PPL polyhedron, the polyhedra referenced and any constraints or generators in the call should follow all the (space) dimension-compatibility rules stated in Section Representations of Convex Polyhedra of the main PPL user manual.
  • As explained above, a polyhedron has a fixed topology C or NNC, that is determined at the time of its initialization. All subsequent operations on the polyhedron must respect all the topological compatibility rules stated in Section Representations of Convex Polyhedra of the main PPL user manual.
  • A system of constraints (i.e., an instance of class 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.

Native objects and borrowed views

For PIP ownership and traversal operations, see PIP_Problem and PIP_Tree_Node.

PIP solution-tree handles

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

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.

Explicit release

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.