def testJIntArraySliceCast(self): JA = JArray(JInt) ja = JA(self.VALUES) ja2 = ja[::2] jo = jpype.JObject(ja2, object) ja3 = jpype.JObject(jo, JA) self.assertEqual(type(jo), jpype.JClass("java.lang.Object")) self.assertEqual(type(ja2), JA) self.assertEqual(type(ja3), JA) self.assertEqual(list(ja2), list(ja3))
def get_csv_datasource(self, config: Config): ds = self.JRCsvDataSource(self.get_data_file_input_stream(config), config.csvCharset) ds.setUseFirstRowAsHeader( jpype.JObject(jpype.JBoolean(config.csvFirstRow))) if config.csvFirstRow: ds.setColumnNames(config.csvColumns) ds.setRecordDelimiter( self.StringEscapeUtils.unescapeJava(config.csvRecordDel)) ds.setFieldDelimiter(config.csvFieldDel) return jpype.JObject(ds, self.JRCsvDataSource)
def get_address(rai): scyjava.start_jvm() class_name = util.Helpers.classNameSimple(rai) class_name_full = util.Helpers.className(rai) if class_name in ('ArrayImg', 'UnsafeImg'): access = jpype.JObject(class_name_full, rai).update(None) access_type = util.Helpers.className(access) if 'basictypelongaccess.unsafe' in access_type: return jpype.JObject(access_type, access).getAddress() else: raise ValueError("Excpected unsafe access but got {}".format(access_type)) else: raise ValueError("Excpected ArrayImg or UnsafeImg but got {}".format(class_name))
def testFile(self): import pathlib if not hasattr(pathlib.Path, '__fspath__'): raise common.unittest.SkipTest("not tested on cygwin") JFile = jpype.JClass("java.io.File") self.assertIsInstance( jpype.JObject(pathlib.Path(__file__).absolute(), JFile), JFile)
def testComparableNull(self): Instant = jpype.JClass("java.time.Instant") i1 = Instant.parse("1970-01-01T00:00:00Z") i3 = jpype.JObject(None, Instant) self.assertTrue(i1 == i1) self.assertFalse(i1 == i3) self.assertFalse(i3 == i1) self.assertTrue(i1 != i3) self.assertTrue(i3 != i1) with self.assertRaises(ValueError): print(i1 < i3) with self.assertRaises(ValueError): print(i1 <= i3) with self.assertRaises(ValueError): print(i1 > i3) with self.assertRaises(ValueError): print(i1 >= i3) with self.assertRaises(ValueError): print(i3 < i1) with self.assertRaises(ValueError): print(i3 <= i1) with self.assertRaises(ValueError): print(i3 > i1) with self.assertRaises(ValueError): print(i3 >= i1) with self.assertRaises(ValueError): print(i3 < i3) with self.assertRaises(ValueError): print(i3 <= i3) with self.assertRaises(ValueError): print(i3 > i3) with self.assertRaises(ValueError): print(i3 >= i3)
def testDifferentiateClassAndObject(self): h = self.__jp.Test1() self.assertEqual(h.callWithSomething(self.__jp.Test1), u"Class") result = h.callWithSomething( jpype.JObject(self.__jp.Test1, jpype.java.lang.Object)) self.assertEqual(result, u"Object")
def testJPJavaFrameCharArray(self): ja = JArray(JChar)(5) _jpype.fault("JPJavaFrame::NewCharArray") with self.assertRaisesRegex(SystemError, "fault"): JArray(JChar)(1) _jpype.fault("JPJavaFrame::SetCharArrayRegion") with self.assertRaisesRegex(SystemError, "fault"): ja[0] = 0 _jpype.fault("JPJavaFrame::GetCharArrayRegion") with self.assertRaisesRegex(SystemError, "fault"): print(ja[0]) _jpype.fault("JPJavaFrame::GetCharArrayElements") # Special case, only BufferError is allowed from getBuffer with self.assertRaises(BufferError): memoryview(ja[0:3]) _jpype.fault("JPJavaFrame::ReleaseCharArrayElements") with self.assertRaisesRegex(SystemError, "fault"): ja[0:3] = bytes([1, 2, 3]) # Not sure why this one changed. _jpype.fault("JPJavaFrame::ReleaseCharArrayElements") with self.assertRaisesRegex(SystemError, "fault"): jpype.JObject(ja[::2], jpype.JObject) _jpype.fault("JPJavaFrame::ReleaseCharArrayElements") def f(): # Special case no fault is allowed memoryview(ja[0:3]) f() ja = JArray(JChar)(5) # lgtm [py/similar-function] _jpype.fault("JPCharType::setArrayRange") with self.assertRaisesRegex(SystemError, "fault"): ja[1:3] = [0, 0]
def testArrayFault(self): ja = JArray(JLong)(5) _jpype.fault("JPJavaFrame::NewLongArray") with self.assertRaisesRegex(SystemError, "fault"): JArray(JLong)(1) _jpype.fault("JPJavaFrame::SetLongArrayRegion") with self.assertRaisesRegex(SystemError, "fault"): ja[0] = 0 _jpype.fault("JPJavaFrame::GetLongArrayRegion") with self.assertRaisesRegex(SystemError, "fault"): print(ja[0]) _jpype.fault("JPJavaFrame::GetLongArrayElements") # Special case, only BufferError is allowed from getBuffer with self.assertRaises(BufferError): memoryview(ja[0:3]) _jpype.fault("JPJavaFrame::ReleaseLongArrayElements") with self.assertRaisesRegex(SystemError, "fault"): ja[0:3] = bytes([1, 2, 3]) _jpype.fault("JPJavaFrame::ReleaseLongArrayElements") with self.assertRaisesRegex(SystemError, "fault"): jpype.JObject(ja[::2], jpype.JObject) _jpype.fault("JPJavaFrame::ReleaseLongArrayElements") def f(): # Special case no fault is allowed memoryview(ja[0:3]) f() _jpype.fault("JPLongType::setArrayRange") with self.assertRaisesRegex(SystemError, "fault"): ja[1:3] = [0, 0]
def testDifferentiateClassAndObject(self): h = self.__jp.Test1() assert h.callWithSomething(self.__jp.Test1) == u"Class" assert h.callWithSomething( jpype.JObject(self.__jp.Test1, jpype.java.lang.Object)) == u"Object"
def gmpe(self): """Load the ground motion prediction equation specified in the configuration file. The key used in the configuration file is GMPE_MODEL_NAME. :returns: jpype wrapper around an instance of the ground motion prediction equation. """ deserializer = java.jclass("GMPEDeserializer")() package_name = "org.opensha.sha.imr.attenRelImpl" class_name = self.params["GMPE_MODEL_NAME"] fqn = package_name + "." + class_name gmpe = deserializer.deserialize( java.jclass("JsonPrimitive")(fqn), None, None) tree_data = java.jclass("GmpeLogicTreeData")() tree_data.setGmpeParams( self.params["COMPONENT"], self.params["INTENSITY_MEASURE_TYPE"], jpype.JDouble(float(self.params["PERIOD"])), jpype.JDouble(float(self.params["DAMPING"])), self.params["GMPE_TRUNCATION_TYPE"], jpype.JDouble(float(self.params["TRUNCATION_LEVEL"])), "Total", jpype.JDouble(float(self.params["REFERENCE_VS30_VALUE"])), jpype.JObject(gmpe, java.jclass("AttenuationRelationship"))) return gmpe
def testJPJavaFrameBooleanArray(self): _jpype.fault("JPJavaFrame::NewBooleanArray") with self.assertRaisesRegex(SystemError, "fault"): JArray(JBoolean)(1) ja = JArray(JBoolean)(5) _jpype.fault("JPJavaFrame::SetBooleanArrayRegion") with self.assertRaisesRegex(SystemError, "fault"): ja[0] = 0 _jpype.fault("JPJavaFrame::GetBooleanArrayRegion") with self.assertRaisesRegex(SystemError, "fault"): print(ja[0]) _jpype.fault("JPJavaFrame::GetBooleanArrayElements") with self.assertRaises(BufferError): memoryview(ja[0:3]) _jpype.fault("JPJavaFrame::ReleaseBooleanArrayElements") with self.assertRaisesRegex(SystemError, "fault"): ja[0:3] = bytes([1, 2, 3]) _jpype.fault("JPJavaFrame::ReleaseBooleanArrayElements") with self.assertRaisesRegex(SystemError, "fault"): jpype.JObject(ja[::2], jpype.JObject) _jpype.fault("JPJavaFrame::ReleaseBooleanArrayElements") def f(): # Special case no fault is allowed memoryview(ja[0:3]) f()
def testDifferentiateClassAndObject(self): h = JClass('jpype.attr.Test1')() self.assertEqual(h.callWithSomething( JClass('jpype.attr.Test1')), u"Class") result = h.callWithSomething(jpype.JObject(JClass('jpype.attr.Test1'), jpype.java.lang.Object)) self.assertEqual(result, u"Object")
def storeOutputAtom(self, otuple): # all the otuple elements are ISymbol s #logging.info("jsci.storeOutputAtom %s", otuple) s = dlvhex.storeOutputAtom([x.hid for x in otuple]) #logging.info(" got symbol %s", s) r = jpype.JObject(JavaSymbolImpl(s), ISymbol) #logging.info("jsci.storeOutputAtom %s returns %s with type %s", otuple, repr(r), type(r)) return r
def storeAtom(self, tuple_): # all the tuple_ elements are ISymbol s #logging.info("jsci.storeAtom %s", tuple_) s = dlvhex.storeAtom([x.hid for x in tuple_]) #logging.info(" got symbol %s", s) r = jpype.JObject(JavaSymbolImpl(s), ISymbol) #logging.info("jsci.storeAtom %s returns %s with type %s", tuple_, repr(r), type(r)) return r
def testCollectionAddAll(self): l = [1, 2, 3, 4] l2 = ['a', 'b'] jlist = jpype.JClass("java.util.ArrayList")() jlist.addAll(l) jcollection = jpype.JObject(jlist, jpype.java.util.Collection) jcollection.addAll(l2) l.extend(l2) self.assertEqual(l, list(jcollection))
def testAutoConvert(self): Instant = jpype.JClass("java.time.Instant") now = "2019-11-12T03:20:54.710948400Z" then = Instant.parse(now) self.assertEqual(str(then), now) then = Instant.parse(jpype.JString(now)) self.assertEqual(str(then), now) then = Instant.parse(jpype.JObject(now, "java.lang.CharSequence")) self.assertEqual(str(then), now)
def main(): # Do everything in a temporary directory that will be deleted at the end. Not always the best choice, # but good idea when doing interactive analysis with tempfile.TemporaryDirectory() as tmp: # in PyCharm, such chains are possible with autocomplete all the way, with type hints for the parameters # This unfortunately does not work in Jupyter. config = jconfig.ConfigUtils.loadConfig( jnet.URL( 'https://raw.githubusercontent.com/matsim-org/matsim/master/examples/scenarios/equil/config_plans1.xml' )) config.controler().setDumpDataAtEnd(False) config.controler().setLastIteration(1) config.controler().setOutputDirectory(tmp) scenario = jscenario.ScenarioUtils.loadScenario(config) # You can manipulate the scenario in any way you like # Type hints from Java generics are not yet supported, so if you want support, you need explicit hints link: jnetapi.Link for link in scenario.getNetwork().getLinks().values(): link.setCapacity(link.getCapacity() * 2) person: jpopapi.Person for person in scenario.getPopulation().getPersons().values(): plan: jpopapi.Plan for plan in person.getPlans(): # JPype is usually rather smart at deducting the Java type from python primitive types, but there # are some caveats. Here, for instance, the setScore method expects a Double object, but np.nan # is a float that can only be converted to primitive types, such as double. # jpype.JObject converts it to the "boxed" type. plan.setScore(jpype.JObject(np.nan)) controler = jcontroler.Controler(scenario) class ShoutListener(EventListener): def reset(self, iteration): print( "########################################################################################" ) print(iteration) @listen_to(event_type.ActivityStartEvent, event_type.ActivityEndEvent) def handleAct(self, event: Union[event_type.ActivityStartEvent, event_type.ActivityEndEvent]): # type hints for protobufs are unfortunately a bit noisy # for the moment, the structure of the events is similar to the MATSim ones, # but in a future version, it is planned to make them richer print(event.persId) controler.addEventHandler(ShoutListener()) controler.run()
def storeConstant(self, s): # convert to python string, otherwise various string operations done within hexlite will fail on the java strings pythonstr = str(s) if len(pythonstr) == 0 or (pythonstr[0] != '"' and pythonstr[-1] != '"' and not rValidConstant.match(pythonstr)): raise ValueError( "cannot storeConstant for term '{}' with is probably a string (use storeString)" .format(pythonstr)) r = jpype.JObject(JavaSymbolImpl(dlvhex.storeConstant(pythonstr)), ISymbol) #logging.info("storeConstant %s returns %s with type %s", s, repr(r), type(r)) return r
def test_cast_lda_sampler_to_gibbs_sampler(): filename = os.path.join(os.getcwd(), "tests", "corpus.txt") stoplist_filename = os.path.join(os.getcwd(), "tests", "stoplist.txt") config = pypclda.create_simple_lda_config( dataset_filename=filename, stoplist_filename=stoplist_filename) sampler = pypclda.create_lda_sampler_of_type( config, "cc.mallet.topics.PolyaUrnSpaliasLDA") gibbs_sampler = jpype.JObject(sampler, cc.mallet.topics.LDAGibbsSampler) assert gibbs_sampler is not None assert gibbs_sampler.sample is not None assert 'LDAGibbsSampler' in gibbs_sampler.__repr__()
def sample_pclda(config, dataset, iterations=2000, sampler_type="cc.mallet.topics.PolyaUrnSpaliasLDA", testset=None, save_sampler=True): """Run the PCLDA (default Polya Urn) sampler Parameters ---------- config : [type] LDA config object dataset : [type] LDA dataset iterations : int, optional number of iterations to run, by default 2000 samplerType : str, optional Java class of the sampler. Must implement the LDASampler interface, by default "cc.mallet.topics.PolyaUrnSpaliasLDA" testset : [type], optional If give, the left-to-right held out log likelihood will be calculated on this dataset, by default None save_sampler : bool, optional indicates that the sampler should be saved to file after finishing, by default True Returns ------- LDASampler LDA sampler object """ sampler = create_lda_sampler_of_type(config, sampler_type) casted_sampler = jpype.JObject( sampler, cc.mallet.topics.LDAGibbsSampler) # enable access to `sample` function sampler.addInstances(dataset) if testset is not None: sampler.addTestInstances(testset) casted_sampler.sample(java.lang.Integer(iterations)) if save_sampler: save_lda_sampler(sampler, config) return sampler
def testJObjectUnknownObject(self): class Fred(object): pass with self.assertRaises(TypeError): jpype.JObject(Fred())
def testJObjectBadType(self): class Fred(object): pass with self.assertRaises(TypeError): jpype.JObject(1, Fred)
def testMonitorOnNull(self): value = jpype.JObject(None) with self.assertRaises(TypeError): _jpype._JMonitor(value)
def testProtectedMethod(self): cls = jpype.JClass('jpype.types.MethodsTest') with self.assertRaises(AttributeError): cls.callProtectedObject(jpype.JObject())
def testInstant(self): import datetime now = datetime.datetime.utcnow() Instant = jpype.JClass("java.time.Instant") self.assertIsInstance(jpype.JObject(now, Instant), Instant)
def testStaticMethod(self): cls = jpype.JClass('jpype.types.MethodsTest') cls.callStaticObject(jpype.JObject())
def storeOutputAtom(self, atom): logging.warning("TBD") return jpype.JObject(None, IAtom)
def __init__(self, config: Config, input_file): self.SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) self.LIB_PATH = os.path.join(self.SCRIPT_DIR, 'libs') self.JDBC_PATH = os.path.join(self.LIB_PATH, 'jdbc') if not os.path.exists(self.LIB_PATH): raise NameError('Library directory not found at {0}'.format(self.LIB_PATH)) self.config = config if not jpype.isJVMStarted(): classpath = [ os.path.join(self.LIB_PATH, "*"), os.path.join(self.JDBC_PATH, "*"), ] if self.config.resource and os.path.isdir(self.config.resource): classpath.append(os.path.join(self.config.resource, "*")) if self.config.jvm_classpath: classpath.append(self.config.jvm_classpath) if self.config.jvm_classpath is None: jpype.startJVM("-Djava.system.class.loader=org.update4j.DynamicClassLoader", "-Xmx{}".format(self.config.jvm_maxmem), classpath=classpath) self.Locale = jpype.JPackage('java').util.Locale self.jvJRLoader = jpype.JPackage('net').sf.jasperreports.engine.util.JRLoader self.JasperReport = jpype.JPackage('net').sf.jasperreports.engine.JasperReport self.JasperPrint = jpype.JPackage('net').sf.jasperreports.engine.JasperPrint self.JRXmlLoader = jpype.JPackage('net').sf.jasperreports.engine.xml.JRXmlLoader self.jvJasperCompileManager = jpype.JPackage('net').sf.jasperreports.engine.JasperCompileManager self.LocaleUtils = jpype.JPackage('org').apache.commons.lang.LocaleUtils self.Locale = jpype.JPackage('java').util.Locale self.jvJasperFillManager = jpype.JPackage('net').sf.jasperreports.engine.JasperFillManager self.JREmptyDataSource = jpype.JPackage('net').sf.jasperreports.engine.JREmptyDataSource self.JasperExportManager = jpype.JPackage('net').sf.jasperreports.engine.JasperExportManager self.FileOutputStream = jpype.JPackage('java').io.FileOutputStream self.JRRtfExporter = jpype.JPackage('net').sf.jasperreports.engine.export.JRRtfExporter self.SimpleExporterInput = jpype.JPackage('net').sf.jasperreports.export.SimpleExporterInput self.SimpleWriterExporterOutput = jpype.JPackage('net').sf.jasperreports.export.SimpleWriterExporterOutput self.JRDocxExporter = jpype.JPackage('net').sf.jasperreports.engine.export.ooxml.JRDocxExporter self.JRPptxExporter = jpype.JPackage('net').sf.jasperreports.engine.export.ooxml.JRPptxExporter self.JRXlsxExporter = jpype.JPackage('net').sf.jasperreports.engine.export.ooxml.JRXlsxExporter self.SimpleOutputStreamExporterOutput = jpype.JPackage('net').sf.jasperreports.export.SimpleOutputStreamExporterOutput self.JROdsExporter = jpype.JPackage('net').sf.jasperreports.engine.export.oasis.JROdsExporter self.JROdtExporter = jpype.JPackage('net').sf.jasperreports.engine.export.oasis.JROdtExporter self.SimpleHtmlExporterOutput = jpype.JPackage('net').sf.jasperreports.export.SimpleHtmlExporterOutput self.JRXmlExporter = jpype.JPackage('net').sf.jasperreports.engine.export.JRXmlExporter self.SimpleXmlExporterOutput = jpype.JPackage('net').sf.jasperreports.export.SimpleXmlExporterOutput self.HashMap = jpype.JPackage('java').util.HashMap self.JRXlsExporter = jpype.JPackage('net').sf.jasperreports.engine.export.JRXlsExporter self.SimpleXlsReportConfiguration = jpype.JPackage('net').sf.jasperreports.export.SimpleXlsReportConfiguration self.JRXlsMetadataExporter = jpype.JPackage('net').sf.jasperreports.engine.export.JRXlsMetadataExporter self.SimpleXlsMetadataReportConfiguration = jpype.JPackage('net').sf.jasperreports.export.SimpleXlsMetadataReportConfiguration self.JRXlsxExporter = jpype.JPackage('net').sf.jasperreports.engine.export.ooxml.JRXlsxExporter self.SimpleXlsxReportConfiguration = jpype.JPackage('net').sf.jasperreports.export.SimpleXlsxReportConfiguration self.JRCsvExporter = jpype.JPackage('net').sf.jasperreports.engine.export.JRCsvExporter self.SimpleCsvExporterConfiguration = jpype.JPackage('net').sf.jasperreports.export.SimpleCsvExporterConfiguration self.JRCsvMetadataExporter = jpype.JPackage('net').sf.jasperreports.engine.export.JRCsvMetadataExporter self.SimpleCsvMetadataExporterConfiguration = jpype.JPackage('net').sf.jasperreports.export.SimpleCsvMetadataExporterConfiguration self.JRSaver = jpype.JPackage('net').sf.jasperreports.engine.util.JRSaver self.File = jpype.JPackage('java').io.File self.ApplicationClasspath = jpype.JPackage('br').com.acesseonline.classpath.ApplicationClasspath if self.config.useJaxen: self.DefaultJasperReportsContext = jpype.JPackage('net').sf.jasperreports.engine.DefaultJasperReportsContext self.context = self.DefaultJasperReportsContext.getInstance(); self.JRPropertiesUtil = jpype.JPackage('net').sf.jasperreports.engine.JRPropertiesUtil self.JRPropertiesUtil.getInstance(self.context).setProperty("net.sf.jasperreports.xpath.executer.factory", "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory"); self.input_file = input_file self.defaultLocale = self.Locale.getDefault() if self.config.has_resource(): self.add_jar_class_path(self.config.resource) if self.config.has_resource(): if os.path.isdir(self.config.resource): try: res = self.File(self.config.resource) self.ApplicationClasspath.add(res) except Exception as ex: raise NameError( 'It was not possible to add the path {0} to the Class Path: ERROR: {1}'\ .format(self.config.resource, str(ex))) if self.config.has_jdbc_dir(): self.add_jar_class_path(self.config.jdbcDir) try: # This fails in case of an jrxml file j_object = self.jvJRLoader.loadObject(self.File(input_file)) cast_error = True try: self.jasper_report = jpype.JObject(j_object, self.JasperReport) cast_error = False self.initial_input_type = 'JASPER_REPORT' except: # nothing to do here pass try: self.jasper_print = jpype.JObject(j_object, self.JasperPrint) cast_error = False self.initial_input_type = 'JASPER_PRINT' except: # nothing to do here pass if cast_error: raise NameError('input file: {0} is not of a valid type'.format(self.input_file)) except Exception: try: self.jasper_design = self.JRXmlLoader.load(input_file) self.initial_input_type = 'JASPER_DESIGN' self.compile() except Exception as ex: raise NameError('input file: {0} is not a valid jrxml file:'.format(str(ex)))
def testMethod(self): cls = jpype.JClass('jpype.types.MethodsTest') with self.assertRaises(TypeError): cls.callObject(jpype.JObject()) cls.callObject(cls(), jpype.JObject())
def testExpandStacktrace(self): Th = jpype.JClass('java.lang.Throwable') null = jpype.JObject(None, Th) # The next line should not fail Th._expandStacktrace(null)