示例#1
0
def init_jvm(java_home=None,
             jvm_dll=None,
             jvm_maxmem=None,
             jvm_classpath=None,
             jvm_properties=None,
             jvm_options=None,
             config_file=None,
             config=None):
    """
    Creates a configured Java virtual machine which will be used by jpy.

    :param java_home: The Java JRE or JDK home directory used to search JVM shared library, if 'jvm_dll' is omitted.
    :param jvm_dll: The JVM shared library file. My be inferred from 'java_home'.
    :param jvm_maxmem: The JVM maximum heap space, e.g. '400M', '8G'. Refer to the java executable '-Xmx' option.
    :param jvm_classpath: The JVM search paths for Java class files. Separated by colons (Unix) or semicolons
                          (Windows). Refer to the java executable '-cp' option.
    :param jvm_properties: A dictionary of key -> value pairs passed to the JVM as Java system properties.
                        Refer to the java executable '-D' option.
    :param jvm_options: A list of extra options for the JVM. Refer to the java executable options.
    :param config_file: Extra configuration file (e.g. 'jpyconfig.py') to be loaded if 'config' parameter is omitted.
    :param config: An optional default configuration object providing default attributes
                   for the 'jvm_maxmem', 'jvm_classpath', 'jvm_properties', 'jvm_options' parameters.
    :return: a tuple (cdll, actual_jvm_options) on success, None otherwise.
    """
    if not config:
        config = _get_python_api_config(config_file=config_file)

    cdll = preload_jvm_dll(jvm_dll_file=jvm_dll,
                           java_home_dir=java_home,
                           config_file=config_file,
                           config=config,
                           fail=False)

    import jpy

    if not jpy.has_jvm():
        jvm_options = get_jvm_options(jvm_maxmem=jvm_maxmem,
                                      jvm_classpath=jvm_classpath,
                                      jvm_properties=jvm_properties,
                                      jvm_options=jvm_options,
                                      config=config)
        logger.debug('Creating JVM with options %s' % repr(jvm_options))
        jpy.create_jvm(options=jvm_options)

        py_lib_initializer = jpy.get_type('org.jpy.PyLibInitializer')
        py_lib_initializer.initPyLib(
            _find_python_dll_file(fail=True),
            _get_module_path('jpy', fail=True),
            _get_module_path('jdl', fail=True)
        )
    else:
        jvm_options = None

    # print('jvm_dll =', jvm_dll)
    # print('jvm_options =', jvm_options)
    return cdll, jvm_options
 def test_multiple_jpy_create_destroy_jvm(self):
   self.assertFalse(jpy.has_jvm())
   jpy.create_jvm([])
   self.assertTrue(jpy.has_jvm())
   jpy.destroy_jvm()
   self.assertFalse(jpy.has_jvm())
   jpy.create_jvm([])
   self.assertTrue(jpy.has_jvm())
   jpy.destroy_jvm()
   self.assertFalse(jpy.has_jvm())
示例#3
0
def init_jvm(java_home=None,
             jvm_dll=None,
             jvm_maxmem=None,
             jvm_classpath=None,
             jvm_properties=None,
             jvm_options=None,
             config_file=None,
             config=None):
    """
    Creates a configured Java virtual machine which will be used by jpy.

    :param java_home: The Java JRE or JDK home directory used to search JVM shared library, if 'jvm_dll' is omitted.
    :param jvm_dll: The JVM shared library file. My be inferred from 'java_home'.
    :param jvm_maxmem: The JVM maximum heap space, e.g. '400M', '8G'. Refer to the java executable '-Xmx' option.
    :param jvm_classpath: The JVM search paths for Java class files. Separated by colons (Unix) or semicolons
                          (Windows). Refer to the java executable '-cp' option.
    :param jvm_properties: A dictionary of key -> value pairs passed to the JVM as Java system properties.
                        Refer to the java executable '-D' option.
    :param jvm_options: A list of extra options for the JVM. Refer to the java executable options.
    :param config_file: Extra configuration file (e.g. 'jpyconfig.py') to be loaded if 'config' parameter is omitted.
    :param config: An optional default configuration object providing default attributes
                   for the 'jvm_maxmem', 'jvm_classpath', 'jvm_properties', 'jvm_options' parameters.
    :return: a tuple (cdll, actual_jvm_options) on success, None otherwise.
    """
    if not config:
        config = _get_python_api_config(config_file=config_file)

    cdll = preload_jvm_dll(jvm_dll_file=jvm_dll,
                           java_home_dir=java_home,
                           config_file=config_file,
                           config=config,
                           fail=False)

    import jpy

    if not jpy.has_jvm():
        jvm_options = get_jvm_options(jvm_maxmem=jvm_maxmem,
                                      jvm_classpath=jvm_classpath,
                                      jvm_properties=jvm_properties,
                                      jvm_options=jvm_options,
                                      config=config)
        logging.debug('Creating JVM with options %s' % repr(jvm_options))
        jpy.create_jvm(options=jvm_options)
    else:
        jvm_options = None

    # print('jvm_dll =', jvm_dll)
    # print('jvm_options =', jvm_options)
    return cdll, jvm_options
示例#4
0
        '-Djava.class.path=' + os.pathsep.join(class_path),
        '-Djava.library.path=' + os.pathsep.join(library_path),
        '-Xmx' + max_mem
    ]

    if config.has_option('DEFAULT', 'java_options'):
        extra_options = config.get('DEFAULT', 'java_options')
        options += extra_options.split('|')

    return options


# Figure out if this module is called from a Java VM. If not, derive a list of Java VM options and create the Java VM.
called_from_java = jpy.has_jvm()
if not called_from_java:
    jpy.create_jvm(options=_get_snap_jvm_options())

# Don't need these functions anymore
del _get_snap_jvm_options
del _get_snap_jvm_env
del _collect_snap_jvm_env


# noinspection PyUnusedLocal
def annotate_RasterDataNode_methods(type_name, method):
    index = -1

    if sys.version_info >= (
            3,
            0,
            0,
示例#5
0
    def __init_subjective_logic_library(self, classpath: str):
        jpy.create_jvm(['-Xmx512M', classpath])

        self.SubjectiveOpinion = jpy.get_type(
            'de.tum.i4.subjectivelogic.SubjectiveOpinion')
        self.ArrayList = jpy.get_type('java.util.ArrayList')
示例#6
0
import unittest
import jpy

jpy.create_jvm(options=['-Djava.class.path=target/test-classes', '-Xmx512M'])

class TestMethodReturnValues(unittest.TestCase):


    def setUp(self):
        self.Fixture = jpy.get_type('org.jpy.fixtures.MethodReturnValueTestFixture')
        self.assertIsNotNone(self.Fixture)
        self.Thing = jpy.get_type('org.jpy.fixtures.Thing')
        self.assertIsNotNone(self.Thing)


    def test_void(self):
        fixture = self.Fixture()
        self.assertEqual(fixture.getVoid(), None)


    def test_primitive_values(self):
        fixture = self.Fixture()
        self.assertEqual(fixture.getValue_boolean(True), True)
        self.assertEqual(fixture.getValue_byte(11), 11)
        self.assertEqual(fixture.getValue_short(12), 12)
        self.assertEqual(fixture.getValue_int(13), 13)
        self.assertEqual(fixture.getValue_long(14), 14)
        self.assertAlmostEqual(fixture.getValue_float(15.1), 15.1, places=5)
        self.assertEqual(fixture.getValue_double(16.2), 16.2)

示例#7
0
import unittest
import jpy
import time
import random

jpy.create_jvm(options=['-Xmx512M'])


class TestPerformance(unittest.TestCase):
    def test_general_rt_perf(self):

        Integer = jpy.get_type('java.lang.Integer')
        String = jpy.get_type('java.lang.String')
        File = jpy.get_type('java.io.File')
        HashMap = jpy.get_type('java.util.HashMap')

        # 1 million
        N = 1000000

        indexes = list(range(N))
        random.shuffle(indexes)

        t0 = time.time()
        pairs = [(Integer(index), File('path')) for index in indexes]
        t1 = time.time()
        print('Integer + File object instantiation took', t1 - t0, 's for', N,
              'calls, this is', 1000 * (t1 - t0) / N, 'ms per call')

        map = HashMap()

        t0 = time.time()
示例#8
0
    options = ['-Djava.awt.headless=true',
               '-Djava.class.path=' + os.pathsep.join(class_path),
               '-Djava.library.path=' + os.pathsep.join(library_path),
               '-Xmx' + max_mem]

    if config.has_option('DEFAULT', 'java_options'):
        extra_options = config.get('DEFAULT', 'java_options')
        options += extra_options.split('|')

    return options


# Figure out if this module is called from a Java VM. If not, derive a list of Java VM options and create the Java VM.
called_from_java = jpy.has_jvm()
if not called_from_java:
    jpy.create_jvm(options=_get_snap_jvm_options())


# Don't need these functions anymore
del _get_snap_jvm_options
del _get_snap_jvm_env
del _collect_snap_jvm_env


# noinspection PyUnusedLocal
def annotate_RasterDataNode_methods(type_name, method):
    index = -1

    if sys.version_info >= (3, 0, 0,):
        arr_z_type_str = "<class '[Z'>"
        arr_i_type_str = "<class '[I'>"
#!/usr/bin/env python3

# Demonstration script for jpy

from common import jar_path
import jpy

jpy.create_jvm(["-Djava.class.path=" + jar_path])
StringBuilder = jpy.get_type("java.lang.StringBuilder")
sb = StringBuilder()
sb.append("Demonstration of ")
sb.append("StringBuilder")
print(sb.toString())

Main = jpy.get_type("net.talvi.pythonjavabridgedemos.Main")
main = Main("Bob")
print(main.greet("Wotcha"))
示例#10
0
import jpy

# Create a new JVM with the SL library on its classpath
jpy.create_jvm([
    '-Xmx512M',
    '-Djava.class.path=/home/ubuntu/Desktop/Aggregatio/subjective_logic/build/libs/subjective-logic-java-library-0.1.0.jar'
])

# Get a reference of the SubjectiveOpinion Java class through jpy bridge
SubjectiveOpinion = jpy.get_type('de.tum.i4.subjectivelogic.SubjectiveOpinion')

# Create two subjective opinions
so1 = SubjectiveOpinion(0.7, 0.00, 0.3, 0.50)
so2 = SubjectiveOpinion(0.55 - 1e-2, 0.45, 1e-2, 0.50)

# Get a reference to Java ArrayList through jpy bridge
ArrayList = jpy.get_type('java.util.ArrayList')
olist = ArrayList()

# Add the subjective opinions to the array list
olist.add(so1)
olist.add(so2)

print("SO 1: " + so1.toString())
print("SO 2: " + so2.toString())

# Perform Consensus&Compromise Fusion of the opinions on the list
ccf = SubjectiveOpinion.ccCollectionFuse(olist)
print("CCF: " + ccf.toString())

# Perform Cumulative Fusion of the opinions in the list
示例#11
0
#/usr/bin/env python3.3 or higher
import jpy
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import rcParams
rcParams['mathtext.fontset'] = 'stix'  #latex
rcParams['font.family'] = 'STIXGeneral'

# Create a new JVM with the SL library on its classpath -- change claspath!
jpy.create_jvm([
    '-Xmx512M',
    '-Djava.class.path=/home/ge73xus/catkin_ws/src/knowledge_aggregation/subjective_logic/build/libs/subjective-logic-java-library-0.1.0.jar'
])

# Get a reference of the SubjectiveOpinion Java class through jpy bridge
SubjectiveOpinion = jpy.get_type('de.tum.i4.subjectivelogic.SubjectiveOpinion')
ArrayList = jpy.get_type('java.util.ArrayList')


######## Compare operators / combination for wave ############
def compare_operators(operator_type):
    vac_opinion = SubjectiveOpinion(0.0, 0.0, 1, 0.50)
    occ_opinion = SubjectiveOpinion(0.7, 0.0, 0.3, 0.50)
    empty_opinion = SubjectiveOpinion(0.0, 0.7, 0.3, 0.50)

    so_list = ArrayList()
    expectation_list = []
    uncertainty_list = []

    aggregated_op = vac_opinion
    uncertainty_list.append(aggregated_op.getUncertainty())