Skip to content
Snippets Groups Projects
Commit f46b268d authored by Reiner Jung's avatar Reiner Jung
Browse files

Creating a basic Java jupyter kernel.

parent 37dbc0cb
No related branches found
No related tags found
No related merge requests found
Showing
with 140 additions and 118 deletions
......@@ -6,12 +6,6 @@
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/main" path="src/main/resources">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
......@@ -19,15 +13,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/resources">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="src" path="/org.xtext.example.mydsl2"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
File deleted
File deleted
File deleted
File deleted
File deleted
......@@ -7,10 +7,11 @@
*/
apply plugin: 'application'
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName='Dummykernel.DummyMain'
sourceCompatibility = 1.11
targetCompatibility = 1.11
mainClassName='jupyter.kernel.OConfKernelMain'
repositories {
flatDir{
......@@ -26,9 +27,12 @@ repositories {
}
dependencies {
compile files('libs/org.xtext.example.mydsl2-1.0.0-SNAPSHOT.jar')
compile files('libs/org.oceandsl.configuration-1.0.0-SNAPSHOT.jar')
compile files('libs/org.oceandsl.configuration.model.support.mitgcm-1.0.0-SNAPSHOT.jar')
compile group: 'io.github.spencerpark', name: 'jupyter-jvm-basekernel', version: '2.3.0'
compile group: 'com.google.inject', name:'guice', version:'4.0'
// https://mvnrepository.com/artifact/org.reflections/reflections
compile group: 'org.reflections', name: 'reflections', version: '0.9.12'
// This dependency is exported to consumers, that is to say found on their compile classpath.
// api 'org.apache.commons:commons-math3:3.6.1'
compile group: 'org.eclipse.emf', name:'org.eclipse.emf.ecore', version: '2.10.1'
......@@ -37,8 +41,6 @@ dependencies {
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
File mode changed from 100644 to 100755
File deleted
package Xtextgenerator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import com.google.inject.Injector;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess2;
import org.eclipse.xtext.generator.InMemoryFileSystemAccess;
import org.eclipse.xtext.resource.XtextResource;
//import org.eclipse.emf.mwe.internal.core.ast.util.Injector;
//import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.xtext.example.mydsl2.MyDslStandaloneSetup;
import org.xtext.example.mydsl2.generator.MyCustomGenerator;
import org.xtext.example.mydsl2.myDsl.Model;
public class MyXtextGenerator {
// private static final XtextResourceSet resourceSet;
private final IFileSystemAccess2 fsa;
public MyXtextGenerator() {
this.fsa = new InMemoryFileSystemAccess();
}
public static Model parseToModel(final String dsl) {
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("temp.mydsl2"));
try {
resource.load(new ByteArrayInputStream(dsl.getBytes("UTF-8")), new HashMap<>());
} catch (IOException e) {
throw new RuntimeException(e);
}
return (Model) resource.getContents().get(0);
}
public static String generate(final String dslText) {
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("temp.mydsl2"));
try {
resource.load(new ByteArrayInputStream(dslText.getBytes("UTF-8")), new HashMap<>());
} catch (IOException e) {
throw new RuntimeException(e);
}
String result = MyCustomGenerator.generateFromString(resource);
return result;
}
}
package Dummykernel;
package jupyter.kernel;
import java.util.List;
import Xtextgenerator.MyXtextGenerator;
import io.github.spencerpark.jupyter.kernel.magic.CellMagicParseContext;
import io.github.spencerpark.jupyter.kernel.magic.MagicParser;
import io.github.spencerpark.jupyter.kernel.magic.registry.Magics;
import jupyter.kernel.generator.ConfigurationDSLRunnerUtils;
public class Magicsprocessor {
public class MagicsProcessor {
private final MagicParser parser;
public Magicsprocessor() {
public MagicsProcessor() {
this.parser = new MagicParser("(?<=(?:^|=))\\s*/", "//");
}
......@@ -22,17 +22,17 @@ public class Magicsprocessor {
return this.eval(ctx);
}
return MyXtextGenerator.generate(source);
return ConfigurationDSLRunnerUtils.generate(source);
}
private String eval(final CellMagicParseContext ctx) {
Magics magics = DummyKernel.getInstance().getMagics();
Magics magics = OConfKernel.getInstance().getMagics();
String name = ctx.getMagicCall().getName();
List<String> args = ctx.getMagicCall().getArgs();
String body = ctx.getMagicCall().getBody();
String result = MyXtextGenerator.generate(body);
String result = ConfigurationDSLRunnerUtils.generate(body);
try {
magics.applyCellMagic(name, args, result);
magics.applyCellMagic(name, args, result.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
......
package Dummykernel;
package jupyter.kernel;
import io.github.spencerpark.jupyter.kernel.BaseKernel;
import io.github.spencerpark.jupyter.kernel.LanguageInfo;
......@@ -6,28 +6,26 @@ import io.github.spencerpark.jupyter.kernel.display.DisplayData;
import io.github.spencerpark.jupyter.kernel.magic.common.WriteFile;
import io.github.spencerpark.jupyter.kernel.magic.registry.Magics;
public class DummyKernel extends BaseKernel {
private static final DummyKernel kernel = new DummyKernel();
public class OConfKernel extends BaseKernel {
private static final OConfKernel KERNEL = new OConfKernel();
// private final ScriptEngine engine;
private final LanguageInfo languageInfo;
private final Magics magics;
private final Magicsprocessor mgcp;
private final MagicsProcessor mgcp;
private DummyKernel() {
private OConfKernel() {
super();
this.magics = new Magics();
this.mgcp = new Magicsprocessor();
this.mgcp = new MagicsProcessor();
this.magics.registerMagics(new WriteFile());
// this.engine = new MyScriptEngine();
this.languageInfo = new LanguageInfo.Builder("XText").version("1.0").mimetype("text/mydsl2")
.fileExtension(".mydsl2").pygments("hsail").codemirror("java").build();
this.languageInfo = new LanguageInfo.Builder("OceanDSL-Config").version("1.1").mimetype("text/oconf")
.fileExtension(".oconf").pygments("oconf").codemirror("oconf").build();
}
public static DummyKernel getInstance() {
if (DummyKernel.kernel == null) {
return new DummyKernel();
}
return DummyKernel.kernel;
public static OConfKernel getInstance() {
return OConfKernel.KERNEL;
}
public Magics getMagics() {
......@@ -40,8 +38,7 @@ public class DummyKernel extends BaseKernel {
}
public Object evalRaw(String expr) throws Exception {
expr = this.mgcp.transformMagics(expr);
return expr;
return this.mgcp.transformMagics(expr);
}
@Override
......@@ -50,9 +47,9 @@ public class DummyKernel extends BaseKernel {
if (result != null) {
return result instanceof DisplayData ? (DisplayData) result : this.getRenderer().render(result);
}
} else {
return null;
}
}
}
package Dummykernel;
package jupyter.kernel;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -9,7 +9,7 @@ import io.github.spencerpark.jupyter.channels.JupyterConnection;
import io.github.spencerpark.jupyter.channels.JupyterSocket;
import io.github.spencerpark.jupyter.kernel.KernelConnectionProperties;
public class DummyMain {
public class OConfKernelMain {
public static void main(final String[] args) throws Exception {
if (args.length < 1) {
throw new IllegalArgumentException("Missing connection file argument");
......@@ -33,7 +33,7 @@ public class DummyMain {
envEngineArgs = "-scripting";
}
DummyKernel kernel = DummyKernel.getInstance();
OConfKernel kernel = OConfKernel.getInstance();
kernel.becomeHandlerForConnection(connection);
connection.connect();
......
package jupyter.kernel.generator;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import com.google.inject.Injector;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.oceandsl.configuration.ConfigurationDSLStandaloneSetup;
import org.oceandsl.configuration.IClimateModelSupportProvider;
import org.oceandsl.configuration.ModelSupportRegistration;
import org.oceandsl.configuration.dsl.ConfigurationModel;
import org.oceandsl.configuration.generator.IGenerator;
public class ConfigurationDSLRunnerUtils {
public ConfigurationDSLRunnerUtils() {
}
public static ConfigurationModel parseToModel(final String input) {
Injector injector = new ConfigurationDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("temp.oconf"));
try {
resource.load(new ByteArrayInputStream(input.getBytes("UTF-8")), new HashMap<>());
} catch (IOException e) {
throw new RuntimeException(e);
}
return (ConfigurationModel) resource.getContents().get(0);
}
public static String generate(final String input) {
Injector injector = new ConfigurationDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createURI("temp.oconf"));
try {
resource.load(new ByteArrayInputStream(input.getBytes("UTF-8")), new HashMap<>());
} catch (IOException e) {
throw new RuntimeException(e);
}
ConfigurationModel configurationModel = (ConfigurationModel) resource.getContents().get(0);
if (configurationModel.getModelSetup() != null) {
if (configurationModel.getModelSetup().getDeclarationModel() != null) {
String declarationModelName = configurationModel.getModelSetup().getDeclarationModel().getName();
IClimateModelSupportProvider climateModelSupportProvider = ModelSupportRegistration
.getModelSupportProvider(declarationModelName);
if (climateModelSupportProvider != null) {
for (IGenerator generator : climateModelSupportProvider.getGenerators()) {
if (generator.useGenerator(configurationModel)) {
System.out.println("Generating " + generator.getFilename());
CharSequence content = generator.generate(configurationModel);
if (content != null) {
try {
// TODO make the root path a configuration setting
BufferedWriter writer = Files.newBufferedWriter(Paths.get("/home/jovyan/work/" + generator.getFilename()),
StandardOpenOption.CREATE);
writer.write(content.toString());
writer.close();
} catch (IOException e) {
System.err.println("IOError " + e.getMessage());
e.printStackTrace();
}
}
}
}
}
}
}
return "";
}
}
......@@ -3,9 +3,18 @@
*/
package Dummykernel;
public class DummyMainTest {
import org.junit.Test;
import jupyter.kernel.generator.ConfigurationDSLRunnerUtils;
public class TestConfigurationDSLRunnerUtils {
/*
* @Test public void testSomeLibraryMethod() { DummyMain classUnderTest = new DummyMain();
* assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod()); }
*/
@Test
public void testGenerate() {
ConfigurationDSLRunnerUtils.generate("example : mitgcm");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment