using jython to prototype and extend java‐based...
TRANSCRIPT
UsingJythontoPrototypeandExtendJava‐basedSystems
DaleParson,DylanSchwesinger
andTheaSteele,KutztownUniversityPACISE2011
PythonandJython
• JythonisanopensourceimplementaEonofaninterpreterforPython.Python:
– hasclasses,inheritanceandpolymorphism,makingitagoodcandidateforspecifyingandprototypingobject‐orientedJavasystemsandcomponents.
– hassource‐levelgenericcontainertypes(sequences,setsandmaps),funcEonalprogrammingconstructs(first‐classfuncEons,closures,generators,higherorderfuncEons),andrun‐EmeinterpretaEon,makingitagoodcandidateforrapidprototyping.
JythonandJava
• JythoniswriOeninJavatocompilePythonsourcetobytecodefortheJavaVirtualMachine(JVM).
• JythoncanimportanycompiledJavaclassanduseitsobjectsasPythonobjects.
• Jythoncanusethesubstan<alJavalibrarycodebasewithoutanyneedtowritewrappercode.
• JythonalsocomeswithasubstanEalnaEvePythonlibrarysimilartoC‐basedPython.
• AJavaapplicaEoncanuseaJythoninterpreterasaJavaobject,requiringveryliOlewrappercode.
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.
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.
Jython‐Javaextensionmechanisms
• JythoncanimportanduseJavaclasses,includingreflecEonandmulEthreading.
• JythoncanimplementJavainterfaces,includingeventlistenerinterfaces.
• Interfaceinheritanceandpolymorphism.
• JythoncanextendJavaclasses.• ImplementaEoninheritanceanddelegaEon.
• JythoninterpretercanworkasaJavaobject.
AJavagraphicalgame,allinJython
Buildingthegame
• Jythoninterpreterallowedustobuildandplayincrementally–itisworkingpseudocode.
• Physicalprototypewasnotnecessary.• WeavoidedmanydetailedissuesofJavaimplementaEonwhileexploringthegamedesignspace.
• Codebaseiscompactandreadable.
• WecouldusethefullJavaSwinglibrary.• OtherslibrariessuchasSWTareequallyaccessible.
• GUIcodeismoreconcisethanJavacode.
Game‐buildingpi\alls.
• DynamictypingdeferstypeerrordetecEonunElrunEme.
• ExhausEvetesEngisessenEaltocatchtypeerrors.• TranslaEonofJavatypestoPythontypesoccurswhenthereisaPythoncounterpart.
• Forexample,aJavaStringobjectbecomesaPythonstring,requiringPythonstringfuncEons,notjava.lang.Stringmethods.ThistranslaEondoesnothappenforJavaclasseswithnoPythoncounterparts.
• Interpretedperformancenoprobleminthisproject.
Jython‐JavaMusicalKeyboard
JustIntonaEonKeyboard
• OnerowperMIDI(MusicalInstrumentDigitalInterface)channel(voice),upto16.
• javax.sound.midisoundlibraryusedbyJython.• Hookstoexternalso`ware&hardwaresynthsaswell.
• BuOonsforkeys,spinnersforoctaveandotherparameters,check&comboboxesfromSwing.
• ConstrucEonoftranslaEontablesfornon‐standardscalesusesPythonfuncEonalprogramming.
LiveCoding!
• LivecodingistheacceptedpracEceofcoding‐as‐performancebylaptopmusicians.
• JythonsupportsuseofastylizedformofPythonprogrammingtoimprovisesynthesizedmusic.
• Pythonsupportforextensibleclasses,reflecEon,andsourcecodeinterpretaEon(eval,execandcompile)allowusingPythonasadomain‐specificlanguage.
• EachgraphicalcontrolbecomesasymbolavailableformanipulaEonasanrvalueoranlvalueinaPythonexpression.SwingEmersusedfortempo&meter.
JythonConstructsforLiveCoding
• AJythonsubclassforeachJavaGUIcontrolclassaddsacommongeOer/seOermethodpairtoeach.Gedng/sedngbecomesorthogonaltothetypeofcontrol.
• APythonpropertyallowseachorthogonalgeOertobemanipulatedasanrvalue,andeachseOerasanlvalue,inalivecodePythonexpression.
• PythonsupportsaddiEonoffieldsandmethodstoclassesandindividualobjectsa`erconstrucEon.IncrementalconstrucEonoflivecodingfields,methodsandproperEesoccursintandemwithassociated,incrementalGUIconstrucEon.
• Python’sexecfuncEonallowslivecodetobecompiled.
Workavoidedinlivecoding
• AlanguagesuchasJavawouldrequiredesigningaspecialpurposelanguage,
• andusingtoolssuchasscannerandparsergeneratorstocompilelivecodetoanintermediateform(VMcode),
• andprovidingrunEmesupportforthelivecodeVM.• Jythoneliminatestheneedfordesigningacustomlanguageanditssupporttools.Thedomain‐specificlanguageisanincrementalextensionofPython.
Jython,JavaandC++performance
• JythonisconsiderableslowthanJava,andperformancecanactuallydegradeinmulEthreadedJythononamassivemulEprocessor,but
• JavacanrunasfastorfasterthanopEmizedC++onmodernprocessorsandmulEprocessors.
• Seebenchmarksreportedinthepaper.
• ConclusionistheJavaisnowfastenoughtohandlemanyEme‐constrainedtasks(e.g.,audiodataflow),makingJythonaOracEveinatwo‐Eeredso`warearchitecture.
Two‐EeredArchitecture
JythonuserinterfaceandconfiguraEonwithsynchronizedinteracEonwithJavasignalprocessingthreads
Java signal processing thread(s) audio inputs
audio outputs
query audio meta-data
configure audio signal thread(s)
Conclusions
• Python’sobject‐orientedfeaturesmakeitagoodfitforobject‐orientedmodeling,anditsfuncEonallanguagefeaturesandrun‐EmecompilaEonmakeitgoodforcompactprototyping.
• Jython’sseamlessintegraEonintoJavamakeitidealforincrementalprototypingandextensionofJavasystemswhileleveragingJava&Pythonlibraries.
• PythonextensionmechanismsmakeitidealforincrementalconstrucEonofdomainlanguages.