Skip to content

Commit

Permalink
#7654 support for autotranslation in Kotlin (#7681)
Browse files Browse the repository at this point in the history
* #7654 support for autotranslation in Kotlin

* #7654 autotranslation comm open handler for kotlin
  • Loading branch information
lmitusinski authored and LeeTZ committed Jul 17, 2018
1 parent 440d1f2 commit b89f3fe
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 5 deletions.
10 changes: 10 additions & 0 deletions doc/groovy/GeneralAutotranslation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
"return NamespaceClient.getBeakerX().get(\"bar\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%kotlin\n",
"beakerx[\"bar\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class DefaultBeakerXJsonSerializer extends BaseBeakerXJsonSerializer {
@Override
protected BeakerObjectConverter createSerializer() {
BasicObjectSerializer serializer = new BasicObjectSerializer();
serializer.addfTypeDeserializer(new TableDisplayDeSerializer(serializer));
serializer.addfTypeDeserializer(new AutotranslationDefaultDeserializer());
serializer.addTypeDeserializer(new TableDisplayDeSerializer(serializer));
serializer.addTypeDeserializer(new AutotranslationDefaultDeserializer());
return serializer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx.kotlin.autotranslation;

import com.twosigma.beakerx.BeakerXClient;
import com.twosigma.beakerx.BeakerXClientManager;

public class NSClientProxy {

public static NSClientProxy API = new NSClientProxy();
private BeakerXClient client;

private NSClientProxy() {
client = BeakerXClientManager.get();
}

public Object get(String key) {
return client.get(key);
}

public Object set(String key, Object value) {
return client.set(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.handler.Handler;
import com.twosigma.beakerx.kernel.comm.AutotranslationHandler;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.kernel.comm.KernelControlCommandListHandler;
import com.twosigma.beakerx.kernel.comm.KernelControlInterrupt;
Expand All @@ -29,13 +30,18 @@ public class KotlinCommOpenHandler extends CommOpenHandler{
new KernelControlInterrupt(kernel),
new KernelControlCommandListHandler(kernel)};

private Handler<?>[] AUTOTRANSLATION_HANDLER = {
new AutotranslationHandler(kernel)};

public KotlinCommOpenHandler(KernelFunctionality kernel) {
super(kernel);
}

public Handler<Message>[] getKernelControlChanelHandlers(String targetName){
if(TargetNamesEnum.KERNEL_CONTROL_CHANNEL.getTargetName().equalsIgnoreCase(targetName)){
return (Handler<Message>[]) KERNEL_CONTROL_CHANNEL_HANDLERS;
if(TargetNamesEnum.KERNEL_CONTROL_CHANNEL.getTargetName().equalsIgnoreCase(targetName)) {
return (Handler<Message>[]) KERNEL_CONTROL_CHANNEL_HANDLERS;
} else if (TargetNamesEnum.BEAKER_AUTOTRANSLATION.getTargetName().equalsIgnoreCase(targetName)) {
return (Handler<Message>[]) AUTOTRANSLATION_HANDLER;
}else{
return (Handler<Message>[]) new Handler<?>[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public KotlinDefaultVariables() {
"com.twosigma.beakerx.mimetype.FileLinkContainer.FileLink",
"com.twosigma.beakerx.mimetype.FileLinkContainer.FileLinks",
"java.lang.Math.*",
"com.twosigma.beakerx.mimetype.MIMEContainer"
"com.twosigma.beakerx.mimetype.MIMEContainer",
"com.twosigma.beakerx.kotlin.autotranslation.NSClientProxy.API as beakerx"
//"com.twosigma.beaker.table.*",
//"com.twosigma.beaker.table.format.*",
//"com.twosigma.beaker.table.renderer.*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx.kotlin.kernel;

import com.twosigma.beakerx.BeakerXCommRepositoryMock;
import com.twosigma.beakerx.DefaultBeakerXJsonSerializer;
import com.twosigma.beakerx.KernelSetUpFixtureTest;
import com.twosigma.beakerx.NamespaceClient;
import com.twosigma.beakerx.NamespaceClientTest;
import com.twosigma.beakerx.kernel.CloseKernelAction;
import com.twosigma.beakerx.kernel.EvaluatorParameters;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.kernel.KernelSocketsFactory;
import com.twosigma.beakerx.kernel.comm.Comm;
import com.twosigma.beakerx.kotlin.evaluator.KotlinEvaluator;
import com.twosigma.beakerx.message.Message;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static com.twosigma.beakerx.MessageFactoryTest.getExecuteRequestMessage;
import static com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForIdleMessage;
import static com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForResult;
import static com.twosigma.beakerx.evaluator.EvaluatorTest.getCacheFolderFactory;
import static com.twosigma.beakerx.evaluator.EvaluatorTest.getTestTempFolderFactory;
import static com.twosigma.beakerx.evaluator.TestBeakerCellExecutor.cellExecutor;
import static org.assertj.core.api.Assertions.assertThat;

public class KotlinAutotranslationTest extends KernelSetUpFixtureTest {

private NamespaceClientTest.AutotranslationServiceTestImpl autotranslationService;

@Override
protected KernelFunctionality createKernel(String sessionId, KernelSocketsFactory kernelSocketsFactory, CloseKernelAction closeKernelAction) {
autotranslationService = new NamespaceClientTest.AutotranslationServiceTestImpl();
NamespaceClient nc = new NamespaceClient(autotranslationService, new DefaultBeakerXJsonSerializer(), new BeakerXCommRepositoryMock());
KotlinEvaluator evaluator = new KotlinEvaluator(sessionId, sessionId, cellExecutor(), getTestTempFolderFactory(), getEvaluatorParameters(), nc);
return new Kotlin(sessionId, evaluator, kernelSocketsFactory, closeKernelAction, getCacheFolderFactory(), new BeakerXCommRepositoryMock());
}

private EvaluatorParameters getEvaluatorParameters() {
KotlinDefaultVariables defaultVariables = new KotlinDefaultVariables();
HashMap kernelParameters = new HashMap();
kernelParameters.put(defaultVariables.IMPORTS, defaultVariables.getImports());
return new EvaluatorParameters(kernelParameters);
}

@Test
public void getAndSetAutotranslation() throws InterruptedException {
//given
String code = "beakerx[\"bar\"] = mapOf(\"name\" to \"John\", \"lastName\" to \"Smith\", \"age\" to \"32\")\n" +
"(beakerx[\"bar\"] as Map<String, String>)[\"name\"]";
String value = "John";
//when
Message message = getExecuteRequestMessage(code);
kernelSocketsService.handleMsg(message);
//then
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets());
assertThat(idleMessage).isPresent();
Message result = waitForResult(kernelSocketsService.getKernelSockets()).get();
verifyResultValue(result, value);
}

@Test
public void kotlinAutotranslationSetValue() throws InterruptedException {
//given
String code = "beakerx[\"bar\"] = mapOf(\"key\" to \"value\")";
String serializedMap = "{\"type\":\"TableDisplay\",\"columnNames\":[\"Key\",\"Value\"],\"values\":[[\"key\",\"value\"]],\"subtype\":\"Dictionary\"}";
//when
Message message = getExecuteRequestMessage(code);
kernelSocketsService.handleMsg(message);
//then
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets());
assertThat(idleMessage).isPresent();
String result = autotranslationService.get("bar");
assertThat(result).isEqualTo(serializedMap);
}

@Test
public void kotlinAutotranslationGetValue() throws InterruptedException {
//given
String code = "(beakerx[\"bar\"] as Map<String, String>)[\"key\"]";
String serializedMap = "{\"type\":\"TableDisplay\",\"columnNames\":[\"Key\",\"Value\"],\"values\":[[\"key\",\"value\"]],\"subtype\":\"Dictionary\"}";
String value = "value";
//when
autotranslationService.update("bar", serializedMap);
Message message = getExecuteRequestMessage(code);
kernelSocketsService.handleMsg(message);
//then
Optional<Message> idleMessage = waitForIdleMessage(kernelSocketsService.getKernelSockets());
assertThat(idleMessage).isPresent();
Message result = waitForResult(kernelSocketsService.getKernelSockets()).get();
verifyResultValue(result, value);

}

private void verifyResultValue(Message message, String value) {
Map actual = ((Map) message.getContent().get(Comm.DATA));
String result = (String) actual.get("text/plain");
assertThat(result).isEqualTo(value);
}
}

0 comments on commit b89f3fe

Please sign in to comment.