History-Research-Environment / HRE--History-Research-Environment

Main repo for HRE code
https://historyresearchenvironment.org/
GNU Affero General Public License v3.0
32 stars 6 forks source link

Jython Support #42

Closed MichaelErichsen closed 2 years ago

MichaelErichsen commented 6 years ago

From @MichaelErichsen on October 23, 2017 10:46

I would also be very interested within the Eclipse environment to be able to use Jython as a scripting language for report composition.

I would like to see this as a way where report scripts can be modified by more expert users to achieve bespoke reports.

Hence the concept would be that Jython would access the HRE Java components to sequence a report.

I understand that Jython scripts can also contain their own options selection GUI via Swing components initiated from the Jython script.

I don't want HRE to be limited to the rigid canned reports that we have in TMG.

(Robin)

Copied from original issue: MichaelErichsen/HRE--History-Research-Environment#31

MichaelErichsen commented 6 years ago

Jython is an implementation of the high-level, dynamic, object-oriented language Python seamlessly integrated with the Java platform. The predecessor to Jython, JPython, is certified as 100% Pure Java. Jython is freely available for both commercial and non-commercial use and is distributed with source code. Jython is complementary to Java and is especially suited for the following tasks:

Embedded scripting - Java programmers can add the Jython libraries to their system to allow end users to write simple or complicated scripts that add functionality to the application.
Interactive experimentation - Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications. This allows programmers to experiment and debug any Java system using Jython.
Rapid application development - Python programs are typically 2-10X shorter than the equivalent Java program. This translates directly to increased programmer productivity. The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products. 
MichaelErichsen commented 6 years ago

How to Connect Python to H2: https://stackoverflow.com/questions/41493117/connect-python-to-h2

MichaelErichsen commented 6 years ago

It is possible that this can be used: http://www.pydev.org/index.html

MichaelErichsen commented 6 years ago

Also https://stackoverflow.com/questions/33868279/use-jython-in-eclipse-plugin

MichaelErichsen commented 6 years ago

And https://stackoverflow.com/questions/30099299/eclipse-rcp-and-jython

MichaelErichsen commented 6 years ago

Installed Pydev plugin from Eclipse Marketplace

MichaelErichsen commented 6 years ago

Jython installed from http://www.jython.org/archive/22/installation.html

MichaelErichsen commented 6 years ago

Main documentation: http://www.jython.org/jythonbook/en/1.0/index.html

MichaelErichsen commented 6 years ago

Newer Jython installation: http://www.jython.org/downloads.html

MichaelErichsen commented 6 years ago

Added jython.jar from the Jython installation directory to the "Other jars" directory, which is part of the Plug-in Target Platform

MichaelErichsen commented 6 years ago

Added an access rule for org/python/** in Java Build Path, Libraries, Plug-in Dependencies

MichaelErichsen commented 6 years ago

Created a Java Plug-in project for testing. PyDev, Set as PyDev project. Configured Jython in the project properties. Added both Java code and Jythin code in the src folder of the project

MichaelErichsen commented 6 years ago

A python module that implements a Java interface to

create a building object

from org.jython.book.interfaces import BuildingType

class Building(BuildingType): def init(self, name, address, id): self.name = name self.address = address self.id = id

def getBuildingName(self):
    return self.name

def getBuildingAddress(self):
    return self.address

def getBuldingId(self):
    return self.id
MichaelErichsen commented 6 years ago

/*

import org.jython.book.interfaces.BuildingType; import org.python.core.PyObject; import org.python.core.PyString; import org.python.util.PythonInterpreter;

public class BuildingFactory {

private PyObject buildingClass;

/**
 * Create a new PythonInterpreter object, then use it to
 * execute some python code. In this case, we want to
 * import the python module that we will coerce.
 *
 * Once the module is imported than we obtain a reference to
 * it and assign the reference to a Java variable
 */

public BuildingFactory() {
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("from Building import Building");
    buildingClass = interpreter.get("Building");
}

/**
 * The create method is responsible for performing the actual
 * coercion of the referenced python module into Java bytecode
 */

public BuildingType create (String name, String location, String id) {

    PyObject buildingObject = buildingClass.__call__(new PyString(name),
                                                     new PyString(location),
                                                     new PyString(id));
    return (BuildingType)buildingObject.__tojava__(BuildingType.class);
}

}

MichaelErichsen commented 6 years ago

// Java interface for a building object package org.jython.book.interfaces;

public interface BuildingType {

public String getBuildingName();
public String getBuildingAddress();
public String getBuildingId();

}

MichaelErichsen commented 6 years ago

package org.jython.book;

import org.jython.book.util.BuildingFactory; import org.jython.book.interfaces.BuildingType;

public class Main {

private static void print(BuildingType building) {
    System.out.println("Building Info: " +
            building.getBuildingId() + " " +
            building.getBuildingName() + " " +
            building.getBuildingAddress());
}

/**
 * Create three building objects by calling the create() method of
 * the factory.
 */

public static void main(String[] args) {
    BuildingFactory factory = new BuildingFactory();
    print(factory.create("BUILDING-A", "100 WEST MAIN", "1"));
    print(factory.create("BUILDING-B", "110 WEST MAIN", "2"));
    print(factory.create("BUILDING-C", "120 WEST MAIN", "3"));

}

}

MichaelErichsen commented 6 years ago

Run main as Java Application:

Building Info: null BUILDING-A 100 WEST MAIN Building Info: null BUILDING-B 110 WEST MAIN Building Info: null BUILDING-C 120 WEST MAIN

MichaelErichsen commented 6 years ago

The method and code is taken from http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#using-jython-within-java-applications

MichaelErichsen commented 6 years ago

Now needing to find a way to embed this in HRE v0.1

MichaelErichsen commented 6 years ago

SWT from Jython: https://stackoverflow.com/questions/516571/is-it-possible-to-use-swt-from-jython

LynnAnnMcL commented 6 years ago

What does that mean? thanks, Lynn

On Fri, Jul 20, 2018 at 1:15 PM Michael Erichsen notifications@github.com wrote:

Assigned #42 https://github.com/History-Research-Environment/HRE--History-Research-Environment/issues/42 to @LynnAnnMcL https://github.com/LynnAnnMcL.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/History-Research-Environment/HRE--History-Research-Environment/issues/42#event-1745287244, or mute the thread https://github.com/notifications/unsubscribe-auth/Anhp-C-e7frpDZBvSxStpkrh36bxVwWKks5uIhAzgaJpZM4VIMUh .