Skip to content

Commit

Permalink
#5039: (scala) add support for autotranslation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete committed Jun 13, 2018
1 parent 3645f31 commit edc77e6
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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;


public class BeakerxClientManager {

private static BeakerxClient beakerxClientInst;

public static BeakerxClient register(BeakerxClient beakerxClient) {
beakerxClientInst = beakerxClient;
return beakerxClientInst;
}

public static BeakerxClient get() {
return beakerxClientInst;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.twosigma.beakerx.chart;

import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.widget.CommActions;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -90,9 +90,9 @@ protected void onActionDetails(HashMap content, Message message) {
info.setGraphics(g);
updateDetails(info);
if (CommActions.ONCLICK.equals(info.getActionType())) {
KernelManager.get().getBeakerx().runByTag(info.getTag());
BeakerxClientManager.get().runByTag(info.getTag());
} else if (CommActions.ONKEY.equals(info.getActionType())) {
KernelManager.get().getBeakerx().runByTag(info.getTag());
BeakerxClientManager.get().runByTag(info.getTag());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.twosigma.beakerx.evaluator;

import com.twosigma.beakerx.BeakerxClient;
import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.DefaultJVMVariables;
import com.twosigma.beakerx.inspect.Inspect;
import com.twosigma.beakerx.inspect.InspectResult;
Expand Down Expand Up @@ -70,7 +71,7 @@ public BaseEvaluator(String id,
sessionId = sId;
executor = cellExecutor;
tempFolder = tempFolderFactory.createTempFolder();
this.beakerxClient = beakerxClient;
this.beakerxClient = BeakerxClientManager.register(beakerxClient);
outDir = getOrCreateFile(tempFolder.toString() + File.separator + "outDir").getPath();
classPath = new Classpath();
classPath.add(new PathToJar(outDir));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ protected Kernel(final String sessionId,
initJvmRepr();
}

@Override
public BeakerxClient getBeakerx() {
return this.evaluatorManager.getBeakerx();
}

public abstract CommOpenHandler getCommOpenHandler(Kernel kernel);

public abstract KernelHandler<Message> getKernelInfoHandler(Kernel kernel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,4 @@ public interface KernelFunctionality {
MagicKernelManager getManagerByCommId(String commId);

void addCommIdManagerMapping(String commId, String kernel);

BeakerxClient getBeakerx();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package com.twosigma.beakerx.kernel.comm;

import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.message.Message;

import java.util.Map;

import static com.twosigma.beakerx.handler.KernelHandlerWrapper.wrapBusyIdle;

public class AutotranslationHandler extends BaseHandler<Boolean> {
Expand All @@ -34,9 +37,8 @@ public void handle(Message message) {
}

private void handleMsg(Message message, KernelFunctionality kernel) {
System.out.println(message);
Map<String, Object> data = (Map) message.getContent().get("data");
kernel.getBeakerx().update((String) data.get("name"), data.get("value"));
BeakerxClientManager.get().update((String) data.get("name"), data.get("value"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.CodeCell;
import com.twosigma.beakerx.jvm.serialization.BasicObjectSerializer;
import com.twosigma.beakerx.jvm.serialization.BeakerObjectConverter;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void handle(Message message) {
private void handleMsg(Message message) {
try {
List<CodeCell> cells = getBeakerCodeCells(getValueFromData(message, getHandlerCommand()));
kernel.getBeakerx().getMessageQueue("CodeCells").put(cells);
BeakerxClientManager.get().getMessageQueue("CodeCells").put(cells);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.twosigma.beakerx.table;

import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.table.action.TableActionDetails;
import com.twosigma.beakerx.widget.CommActions;
Expand Down Expand Up @@ -87,11 +87,11 @@ private void onActionDetails(HashMap content, Message message) {
tableDisplay.setDetails(details);
if (CommActions.CONTEXT_MENU_CLICK.equals(details.getActionType())) {
if (tableDisplay.getContextMenuTags() != null && !tableDisplay.getContextMenuTags().isEmpty() && details.getContextMenuItem() != null && !details.getContextMenuItem().isEmpty()) {
KernelManager.get().getBeakerx().runByTag(tableDisplay.getContextMenuTags().get(details.getContextMenuItem()));
BeakerxClientManager.get().runByTag(tableDisplay.getContextMenuTags().get(details.getContextMenuItem()));
}
} else if (CommActions.DOUBLE_CLICK.equals(details.getActionType())) {
if (tableDisplay.getDoubleClickTag() != null && !tableDisplay.getDoubleClickTag().isEmpty()) {
KernelManager.get().getBeakerx().runByTag(tableDisplay.getDoubleClickTag());
BeakerxClientManager.get().runByTag(tableDisplay.getDoubleClickTag());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.twosigma.beakerx.widget;

import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.handler.Handler;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.message.Message;

import java.io.Serializable;
Expand All @@ -34,7 +34,7 @@ public class Button extends ValueWidget<Boolean> {
public static final String ICON = "icon";
public static final String BUTTON_STYLE = "button_style";

private String tooltip= EMPTY_STRING;
private String tooltip = EMPTY_STRING;
private String tag;
private String icon = EMPTY_STRING;
private String button_style = EMPTY_STRING;
Expand All @@ -59,7 +59,7 @@ protected HashMap<String, Serializable> content(HashMap<String, Serializable> co
content.put(BUTTON_STYLE, button_style);
return content;
}


public String getTooltip() {
return tooltip;
Expand Down Expand Up @@ -88,12 +88,12 @@ private void handleOnClick(Message message) {
handleCommEventSync(message, CommActions.CLICK, this::onClick);
}

public void onClick(HashMap content, Message message){
if(actionPerformed != null){
public void onClick(HashMap content, Message message) {
if (actionPerformed != null) {
actionPerformed.executeAction(content, message);
}
if(getTag() != null && !getTag().isEmpty()){
KernelManager.get().getBeakerx().runByTag(getTag());
if (getTag() != null && !getTag().isEmpty()) {
BeakerxClientManager.get().runByTag(getTag());
}
}

Expand Down Expand Up @@ -121,5 +121,5 @@ public void setButton_style(String button_style) {
this.button_style = button_style;
sendUpdate(BUTTON_STYLE, button_style);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,4 @@ public MagicKernelManager getManagerByCommId(String commId) {
@Override
public void addCommIdManagerMapping(String commId, String kernel) {}

@Override
public BeakerxClient getBeakerx() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@

public interface BeakerxObjectFactory {

String create(String sessionId);
String create();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
public class BeakerxObjectFactoryImpl implements BeakerxObjectFactory {

@Override
public String create(String sessionId) {
return "import com.twosigma.beakerx.NamespaceClient\n" +
public String create() {
return "import com.twosigma.beakerx.BeakerxClientManager\n" +
"import language.dynamics\n" +
"var _beaker = NamespaceClient.getBeaker(\"" + sessionId + "\")\n" +
"object beaker extends Dynamic {\n" +
" def selectDynamic( field : String ) = _beaker.get(field)\n" +
"var _beakerx = BeakerxClientManager.get()\n" +
"object beakerx extends Dynamic {\n" +
" def selectDynamic( field : String ) = _beakerx.get(field)\n" +
" def updateDynamic (field : String)(value : Any) : Any = {\n" +
" _beaker.set(field,value)\n" +
" _beakerx.set(field,value)\n" +
" return value\n" +
" }\n" +
" def applyDynamic(methodName: String)(args: AnyRef*) = {\n" +
" def argtypes = args.map(_.getClass)\n" +
" def method = _beaker.getClass.getMethod(methodName, argtypes: _*)\n" +
" method.invoke(_beaker,args: _*)\n" +
" def method = _beakerx.getClass.getMethod(methodName, argtypes: _*)\n" +
" method.invoke(_beakerx,args: _*)\n" +
" }\n" +
"}\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ public class ScalaEvaluator extends BaseEvaluator {
private ScalaEvaluatorGlue shell;

public ScalaEvaluator(String id, String sId, Provider<BeakerObjectConverter> osp, EvaluatorParameters evaluatorParameters) {
this(id, sId, osp, new BeakerCellExecutor("scala"), new BeakerxObjectFactoryImpl(), new TempFolderFactoryImpl(), evaluatorParameters, new NamespaceClient(sId, new AutotranslationServiceImpl()));
this(id,
sId,
osp,
new BeakerCellExecutor("scala"),
new BeakerxObjectFactoryImpl(),
new TempFolderFactoryImpl(),
evaluatorParameters,
new NamespaceClient(sId, new AutotranslationServiceImpl()));
}

public ScalaEvaluator(String id, String sId, Provider<BeakerObjectConverter> osp, CellExecutor cellExecutor, BeakerxObjectFactory beakerxObjectFactory, TempFolderFactory tempFolderFactory, EvaluatorParameters evaluatorParameters, BeakerxClient beakerxClient) {
Expand Down Expand Up @@ -155,7 +162,7 @@ private ScalaEvaluatorGlue createNewEvaluator() {
addImportsToShell(shell, getImports().getImportPaths());
}
logger.debug("creating beaker object");
String r = shell.evaluate2(this.beakerxObjectFactory.create(getSessionId()));
String r = shell.evaluate2(this.beakerxObjectFactory.create());
if (r != null && !r.isEmpty()) {
logger.warn("ERROR creating beaker object: {}", r);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class NoBeakerxObjectTestFactory implements BeakerxObjectFactory {

@Override
public String create(String sessionId) {
public String create() {
return "()";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.twosigma.beakerx.sql.autocomplete.db;

import com.twosigma.beakerx.BeakerxClient;
import com.twosigma.beakerx.BeakerxClientManager;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.sql.BeakerParser;
import com.twosigma.beakerx.sql.ConnectionStringHolder;
Expand All @@ -40,7 +41,7 @@ public class DbExplorerFactory {
public static DbInfo getDbInfo(String txt, JDBCClient jdbcClient, String sessionId,
ConnectionStringHolder defaultConnectionString, Map<String, ConnectionStringHolder> namedConnectionString) {

final BeakerxClient namespaceClient = KernelManager.get().getBeakerx();
final BeakerxClient namespaceClient = BeakerxClientManager.get();
final BeakerParser beakerParser;
try {
beakerParser = new BeakerParser(txt, namespaceClient,
Expand Down

0 comments on commit edc77e6

Please sign in to comment.