Using Jython to Prototype and Extend Java‐based...

17
Using Jython to Prototype and Extend Java‐based Systems Dale Parson, Dylan Schwesinger and Thea Steele, Kutztown University PACISE 2011

Transcript of Using Jython to Prototype and Extend Java‐based...

Page 1: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

UsingJythontoPrototypeandExtendJava‐basedSystems

DaleParson,DylanSchwesinger

andTheaSteele,KutztownUniversityPACISE2011

Page 2: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

PythonandJython

•  JythonisanopensourceimplementaEonofaninterpreterforPython.Python:

– hasclasses,inheritanceandpolymorphism,makingitagoodcandidateforspecifyingandprototypingobject‐orientedJavasystemsandcomponents.

– hassource‐levelgenericcontainertypes(sequences,setsandmaps),funcEonalprogrammingconstructs(first‐classfuncEons,closures,generators,higherorderfuncEons),andrun‐EmeinterpretaEon,makingitagoodcandidateforrapidprototyping.

Page 3: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

JythonandJava

•  JythoniswriOeninJavatocompilePythonsourcetobytecodefortheJavaVirtualMachine(JVM).

•  JythoncanimportanycompiledJavaclassanduseitsobjectsasPythonobjects.

•  Jythoncanusethesubstan<alJavalibrarycodebasewithoutanyneedtowritewrappercode.

•  JythonalsocomeswithasubstanEalnaEvePythonlibrarysimilartoC‐basedPython.

•  AJavaapplicaEoncanuseaJythoninterpreterasaJavaobject,requiringveryliOlewrappercode.

Page 4: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

ThreeExtensionLanguageExtensionMechanisms

Extension Language Script (1)

Extension Language Interpreter

Dynamically loaded application primitives (2)

Built-in language primitives

Primitive-to-extension callbacks (3)

Arrows show direction of subroutine invocations.

Page 5: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

ThreeJythonExtensionMechanisms

Jython script (1)

Jython interpreter, byte code compiler

Dynamically imported Java classes (2)

Built-in language primitives

Java-to-Jython callbacks (3), e.g., Jython listeners for Java-generated events.

Page 6: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Jython‐Javaextensionmechanisms

•  JythoncanimportanduseJavaclasses,includingreflecEonandmulEthreading.

•  JythoncanimplementJavainterfaces,includingeventlistenerinterfaces.

•  Interfaceinheritanceandpolymorphism.

•  JythoncanextendJavaclasses.•  ImplementaEoninheritanceanddelegaEon.

•  JythoninterpretercanworkasaJavaobject.

Page 7: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

AJavagraphicalgame,allinJython

Page 8: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Buildingthegame

•  Jythoninterpreterallowedustobuildandplayincrementally–itisworkingpseudocode.

•  Physicalprototypewasnotnecessary.• WeavoidedmanydetailedissuesofJavaimplementaEonwhileexploringthegamedesignspace.

•  Codebaseiscompactandreadable.

•  WecouldusethefullJavaSwinglibrary.•  OtherslibrariessuchasSWTareequallyaccessible.

•  GUIcodeismoreconcisethanJavacode.

Page 9: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Game‐buildingpi\alls.

•  DynamictypingdeferstypeerrordetecEonunElrunEme.

•  ExhausEvetesEngisessenEaltocatchtypeerrors.•  TranslaEonofJavatypestoPythontypesoccurswhenthereisaPythoncounterpart.

•  Forexample,aJavaStringobjectbecomesaPythonstring,requiringPythonstringfuncEons,notjava.lang.Stringmethods.ThistranslaEondoesnothappenforJavaclasseswithnoPythoncounterparts.

•  Interpretedperformancenoprobleminthisproject.

Page 10: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Jython‐JavaMusicalKeyboard

Page 11: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

JustIntonaEonKeyboard

•  OnerowperMIDI(MusicalInstrumentDigitalInterface)channel(voice),upto16.

•  javax.sound.midisoundlibraryusedbyJython.•  Hookstoexternalso`ware&hardwaresynthsaswell.

•  BuOonsforkeys,spinnersforoctaveandotherparameters,check&comboboxesfromSwing.

•  ConstrucEonoftranslaEontablesfornon‐standardscalesusesPythonfuncEonalprogramming.

Page 12: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

LiveCoding!

•  LivecodingistheacceptedpracEceofcoding‐as‐performancebylaptopmusicians.

•  JythonsupportsuseofastylizedformofPythonprogrammingtoimprovisesynthesizedmusic.

•  Pythonsupportforextensibleclasses,reflecEon,andsourcecodeinterpretaEon(eval,execandcompile)allowusingPythonasadomain‐specificlanguage.

•  EachgraphicalcontrolbecomesasymbolavailableformanipulaEonasanrvalueoranlvalueinaPythonexpression.SwingEmersusedfortempo&meter.

Page 13: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

JythonConstructsforLiveCoding

•  AJythonsubclassforeachJavaGUIcontrolclassaddsacommongeOer/seOermethodpairtoeach.Gedng/sedngbecomesorthogonaltothetypeofcontrol.

•  APythonpropertyallowseachorthogonalgeOertobemanipulatedasanrvalue,andeachseOerasanlvalue,inalivecodePythonexpression.

•  PythonsupportsaddiEonoffieldsandmethodstoclassesandindividualobjectsa`erconstrucEon.IncrementalconstrucEonoflivecodingfields,methodsandproperEesoccursintandemwithassociated,incrementalGUIconstrucEon.

•  Python’sexecfuncEonallowslivecodetobecompiled.

Page 14: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Workavoidedinlivecoding

•  AlanguagesuchasJavawouldrequiredesigningaspecialpurposelanguage,

•  andusingtoolssuchasscannerandparsergeneratorstocompilelivecodetoanintermediateform(VMcode),

•  andprovidingrunEmesupportforthelivecodeVM.•  Jythoneliminatestheneedfordesigningacustomlanguageanditssupporttools.Thedomain‐specificlanguageisanincrementalextensionofPython.

Page 15: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Jython,JavaandC++performance

•  JythonisconsiderableslowthanJava,andperformancecanactuallydegradeinmulEthreadedJythononamassivemulEprocessor,but

•  JavacanrunasfastorfasterthanopEmizedC++onmodernprocessorsandmulEprocessors.

•  Seebenchmarksreportedinthepaper.

•  ConclusionistheJavaisnowfastenoughtohandlemanyEme‐constrainedtasks(e.g.,audiodataflow),makingJythonaOracEveinatwo‐Eeredso`warearchitecture.

Page 16: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Two‐EeredArchitecture

JythonuserinterfaceandconfiguraEonwithsynchronizedinteracEonwithJavasignalprocessingthreads

Java signal processing thread(s) audio inputs

audio outputs

query audio meta-data

configure audio signal thread(s)

Page 17: Using Jython to Prototype and Extend Java‐based Systemsfaculty.kutztown.edu/parson/pubs/JythonPACISE2011Talk.pdf · Jython script (1) Jython interpreter, byte code compiler Dynamically

Conclusions

•  Python’sobject‐orientedfeaturesmakeitagoodfitforobject‐orientedmodeling,anditsfuncEonallanguagefeaturesandrun‐EmecompilaEonmakeitgoodforcompactprototyping.

•  Jython’sseamlessintegraEonintoJavamakeitidealforincrementalprototypingandextensionofJavasystemswhileleveragingJava&Pythonlibraries.

•  PythonextensionmechanismsmakeitidealforincrementalconstrucEonofdomainlanguages.