Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic typer service integration #300

Open
wants to merge 16 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ public UpdateContainer doIt(Workspace workspace) throws CommandException {
// *****************************************************************************************
// *****************************************************************************************


// Check if the flag is set to enable semantic labeling.
// If yes then upload semantic types and columns to the server
Boolean onlineSemanticTypingEnabled = modelingConfiguration.getOnlineSemanticTypingEnabled();
if(onlineSemanticTypingEnabled) {
try {
semanticModel.uploadUserSemanticTypes();
} catch (Exception e) {
logger.error("Unable to upload semantic types");
}
}

try {
R2RMLAlignmentFileSaver fileSaver = new R2RMLAlignmentFileSaver(workspace);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package edu.isi.karma.controller.command;

import edu.isi.karma.config.ModelingConfigurationRegistry;
import edu.isi.karma.controller.update.AbstractUpdate;
import edu.isi.karma.controller.update.ErrorUpdate;
import edu.isi.karma.controller.update.UpdateContainer;
import edu.isi.karma.rep.Workspace;
import edu.isi.karma.view.VWorkspace;
import edu.isi.karma.webserver.ContextParametersRegistry;
import edu.isi.karma.webserver.ServletContextParameterMap;
import org.json.JSONStringer;
import org.json.JSONWriter;
import org.slf4j.LoggerFactory;

import org.slf4j.Logger;

import java.io.*;
import java.util.Properties;

/**
* Created by alse on 10/4/16.
* Every karma client is going to be assigned a random string as an id.
* This id - karma client name - can be changed to a given value by calling this command
*/
public class SetKarmaClientNameCommand extends Command {
private String karmaClientName;
private String property = "karma.client.name";

private static Logger logger = LoggerFactory.getLogger(SetKarmaClientNameCommand.class);

protected SetKarmaClientNameCommand(String id, String model, String name) {
super(id, model);
karmaClientName = name;
}

@Override
public String getCommandName() {
return this.getClass().getSimpleName();
}

@Override
public String getTitle() {
return "Set Karma name Configuration";
}

@Override
public String getDescription() {
return null;
}

@Override
public CommandType getCommandType() {
return CommandType.notInHistory;
}

@Override
public UpdateContainer doIt(Workspace workspace) throws CommandException {
UpdateContainer uc = new UpdateContainer();
try{

uc.add(new AbstractUpdate() {
@Override
public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) {
try {
// read modeling.properties file
String fileName = ContextParametersRegistry.getInstance()
.getContextParameters(ContextParametersRegistry.getInstance().getDefault().getId())
.getParameterValue(ServletContextParameterMap.ContextParameter.USER_CONFIG_DIRECTORY)
+ "/modeling.properties";

BufferedReader file = new BufferedReader(new FileReader(fileName));
String line;
String modelingPropertiesContent = "";
while ((line = file.readLine()) != null) {
if (line.startsWith(property)) {
modelingPropertiesContent += property + "=" + karmaClientName + '\n';
} else {
modelingPropertiesContent += line + '\n';
}
}
file.close();
FileOutputStream fileOut = new FileOutputStream(fileName);
fileOut.write(modelingPropertiesContent.getBytes());
fileOut.close();

// reload modeling configuration
ModelingConfigurationRegistry.getInstance().getModelingConfiguration(ContextParametersRegistry.getInstance().getDefault().getId()).load();
JSONStringer jsonStr = new JSONStringer();

JSONWriter writer = jsonStr.object();
writer.key("updateType").value(this.getClass().getName());
writer.endObject();
pw.print(writer.toString());
} catch (Exception e) {
logger.error("Error updating Modeling Configuraion", e);
}
}
});
} catch (Exception e) {
logger.error("Error updating Modeling Configuraion:" , e);
uc.add(new ErrorUpdate("Error updating Modeling Configuraion"));
}
return uc;
}

@Override
public UpdateContainer undoIt(Workspace workspace) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.isi.karma.controller.command;
import edu.isi.karma.rep.Workspace;

import javax.servlet.http.HttpServletRequest;

/**
* Created by alse on 10/4/16.
*/
public class SetKarmaClientNameCommandFactory extends CommandFactory{
enum Arguments {
value
}
@Override
public Command createCommand(HttpServletRequest request, Workspace workspace) {
return new SetKarmaClientNameCommand(getNewId(workspace), Command.NEW_MODEL, request.getParameter(Arguments.value.name()));
}

@Override
public Class<? extends Command> getCorrespondingCommand() {
return SetKarmaClientNameCommand.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package edu.isi.karma.controller.command;

import com.sun.org.apache.xpath.internal.operations.Bool;
import edu.isi.karma.config.ModelingConfigurationRegistry;
import edu.isi.karma.controller.update.AbstractUpdate;
import edu.isi.karma.controller.update.ErrorUpdate;
import edu.isi.karma.controller.update.UpdateContainer;
import edu.isi.karma.rep.Workspace;
import edu.isi.karma.semantictypes.typinghandler.HybridSTModelHandler;
import edu.isi.karma.semantictypes.typinghandler.RemoteSTModelHandler;
import edu.isi.karma.view.VWorkspace;
import edu.isi.karma.webserver.ContextParametersRegistry;
import edu.isi.karma.webserver.ServletContextParameterMap;
import org.json.JSONStringer;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

/**
* Created by alse on 9/29/16.
* This function toggles the setting in modeling.properties file responsible for setting the semantictyper to local or remote
* Based on the current value of the property, this just toggles the value
*/
public class ToggleOnlineSemanticTypingCommand extends Command {

private String property = "online.semantic.typing";

private static Logger logger = LoggerFactory.getLogger(UpdateUIConfigurationCommand.class);

protected ToggleOnlineSemanticTypingCommand(String id, String model) {
super(id, model);
}

@Override
public String getCommandName() {
return this.getClass().getSimpleName();
}

@Override
public String getTitle() {
return "Set Modeling Configuration";
}

@Override
public String getDescription() {
return null;
}

@Override
public CommandType getCommandType() {
return CommandType.notInHistory;
}

@Override
public UpdateContainer doIt(Workspace workspace) throws CommandException {
UpdateContainer uc = new UpdateContainer();
try{

uc.add(new AbstractUpdate() {
@Override
public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) {
try {
// read modeling.properties file
String fileName = ContextParametersRegistry.getInstance()
.getContextParameters(ContextParametersRegistry.getInstance().getDefault().getId())
.getParameterValue(ServletContextParameterMap.ContextParameter.USER_CONFIG_DIRECTORY)
+ "/modeling.properties";

Properties prop = new Properties();
prop.load(new FileInputStream(fileName));

BufferedReader file = new BufferedReader(new FileReader(fileName));
String line;
String modelingPropertiesContent = "";
while ((line = file.readLine()) != null) {
if (line.startsWith(property)) {
String value = Boolean.toString(!Boolean.valueOf(prop.getProperty(property)));
modelingPropertiesContent += property + "=" + value + '\n';
} else {
modelingPropertiesContent += line + '\n';
}
}
file.close();
FileOutputStream fileOut = new FileOutputStream(fileName);
fileOut.write(modelingPropertiesContent.getBytes());
fileOut.close();

String contextId = vWorkspace.getWorkspace().getContextId();
Boolean isModelEnabled = vWorkspace.getWorkspace().getSemanticTypeModelHandler().getModelHandlerEnabled();

// If online is enabled then use RemoteSTModelHandler for semantic typing else use HybridSTModelHandler
if (!Boolean.valueOf(prop.getProperty(property))){
vWorkspace.getWorkspace().setSemanticTypeModelHandler(new RemoteSTModelHandler(contextId));
} else {
vWorkspace.getWorkspace().setSemanticTypeModelHandler(new HybridSTModelHandler(contextId));
}
vWorkspace.getWorkspace().getSemanticTypeModelHandler().setModelHandlerEnabled(isModelEnabled);

ModelingConfigurationRegistry.getInstance().getModelingConfiguration(ContextParametersRegistry.getInstance().getDefault().getId()).load();
JSONStringer jsonStr = new JSONStringer();

JSONWriter writer = jsonStr.object();
writer.key("updateType").value(this.getClass().getName());
writer.endObject();
pw.print(writer.toString());
} catch (Exception e) {
logger.error("Error updating Modeling Configuraion", e);
}
}
});
} catch (Exception e) {
logger.error("Error updating Modeling Configuraion:" , e);
uc.add(new ErrorUpdate("Error updating Modeling Configuraion"));
}
return uc;
}

@Override
public UpdateContainer undoIt(Workspace workspace) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.isi.karma.controller.command;

import edu.isi.karma.rep.Workspace;

import javax.servlet.http.HttpServletRequest;

/**
* Created by alse on 9/29/16.
* Request @param property: is the name of property in modeling.properties eg. online.semantic.typing, train.on.apply.history
* Request @param value: is the value of the property to be set.
*/
public class ToggleOnlineSemanticTypingCommandFactory extends CommandFactory{
@Override
public Command createCommand(HttpServletRequest request, Workspace workspace) {
return new ToggleOnlineSemanticTypingCommand(getNewId(workspace), Command.NEW_MODEL);
}

@Override
public Class<? extends Command> getCorrespondingCommand() {
return ToggleOnlineSemanticTypingCommand.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -81,7 +82,9 @@ public class ModelingConfiguration {

private Boolean showModelsWithoutMatching;
private String defaultProperty = null;

private Boolean onlineSemanticTypingEnabled; // decides whether we should use remote semantic typing or local
private String karmaClientName; // name of the instance of karma set to a random uuid string by default

private final String newLine = System.getProperty("line.separator");

private String defaultModelingProperties =
Expand All @@ -92,7 +95,8 @@ public class ModelingConfiguration {
"##########################################################################################" + newLine +
"" + newLine +
"train.on.apply.history=false" + newLine +
"predict.on.apply.history=false" + newLine +
"predict.on.apply.history=false" + newLine +
"online.semantic.typing=false" + newLine +
"" + newLine +
"##########################################################################################" + newLine +
"#" + newLine +
Expand Down Expand Up @@ -163,10 +167,13 @@ public class ModelingConfiguration {
"##########################################################################################" + newLine +
"" + newLine +
"models.display.nomatching=false" + newLine +
"history.store.old=false"
"history.store.old=false" +
"#" + newLine +
"##########################################################################################" + newLine +
"" + newLine +
"karma.client.name=" + UUID.randomUUID().toString()
;


public void load() {
try {
Properties modelingProperties = loadParams();
Expand Down Expand Up @@ -290,6 +297,11 @@ public void load() {
out.println("default.property=http://schema.org/name");
out.close();
}

// set it to false by default
onlineSemanticTypingEnabled = Boolean.parseBoolean(modelingProperties.getProperty("online.semantic.typing", "false"));
// random uuid is set by default
karmaClientName = modelingProperties.getProperty("karma.client.name", UUID.randomUUID().toString());
} catch (IOException e) {
logger.error("Error occured while reading config file ...", e);
System.exit(1);
Expand Down Expand Up @@ -543,7 +555,19 @@ public String getDefaultProperty() {
load();
return defaultProperty;
}


public Boolean getOnlineSemanticTypingEnabled(){
if(onlineSemanticTypingEnabled == null)
load();
return onlineSemanticTypingEnabled;
}

public String getKarmaClientName(){
if(karmaClientName == null)
load();
return karmaClientName;
}

public void setManualAlignment()
{
ontologyAlignment = false;
Expand Down
Loading