Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
szeke committed Dec 26, 2013
2 parents d9e1267 + bed97eb commit 3f34220
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 347 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ log/*
src/main/webapp/publish/KML/*.*
src/main/webapp/publish/Source Description/*.*
src/main/webapp/publish/History/*.*
src/main/webapp/publish/GRAPHVIZ/*.*
src/main/webapp/publish/CSV/*.*
src/main/webapp/publish/RDF/*.*
src/main/webapp/publish/MDB/*.*
Expand Down
75 changes: 64 additions & 11 deletions src/main/java/edu/isi/karma/controller/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;

import edu.isi.karma.controller.update.UpdateContainer;
import edu.isi.karma.rep.Entity;
import edu.isi.karma.rep.Workspace;
Expand Down Expand Up @@ -84,24 +88,26 @@ public abstract UpdateContainer doIt(Workspace workspace)
* Has this command been executed already?
*/
private boolean isExecuted = false;

/**
* Flag that should be unset if you don't want this command instance to be written into the history
* Flag that should be unset if you don't want this command instance to be
* written into the history
*/
private boolean saveInHistory = true;

/**
* Flag to tell if the command history should be written after this command has been executed
* Flag to tell if the command history should be written after this command
* has been executed
*/
private boolean writeWorksheetHistoryAfterCommandExecutes = true;

private boolean appendToHistory = false;

/**
* List of tags for the command
*/
private List<CommandTag> tags = new ArrayList<CommandTag>();

private String inputParameterJson;

public enum CommandTag {
Expand All @@ -119,19 +125,19 @@ public boolean isExecuted() {
public void setExecuted(boolean isExecuted) {
this.isExecuted = isExecuted;
}

public boolean isSavedInHistory() {
return saveInHistory;
}

public void saveInHistory(boolean flag) {
this.saveInHistory = flag;
}

public void writeWorksheetHistoryAfterCommandExecutes(boolean flag) {
this.writeWorksheetHistoryAfterCommandExecutes = flag;
}

public boolean writeWorksheetHistoryAfterCommandExecutes() {
return this.writeWorksheetHistoryAfterCommandExecutes;
}
Expand Down Expand Up @@ -186,4 +192,51 @@ public boolean appendToHistory() {
public void setAppendToHistory(boolean appendToHistory) {
this.appendToHistory = appendToHistory;
}

// /////////////////////////////////////////////////////////////////////////////
//
// Methods to help with logging and error reporting.
//
// /////////////////////////////////////////////////////////////////////////////

protected void logCommand(Logger logger, Workspace workspace) {
try {
logger.info("Executing command:\n"
+ getArgsJSON(workspace).toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}

/*
* Pedro
*
* We should have an abstraction for Commands that operate on worksheets,
* and this method should go there.
*/
protected String formatWorsheetId(Workspace workspace, String worksheetId) {
return worksheetId + " ("
+ workspace.getWorksheet(worksheetId).getTitle() + ")";
}

/*
* Pedro
*
* Return an HNodeId in a nice format for printing on command logs.
*/
protected String formatHNodeId(Workspace workspace, String hNodeId) {
return hNodeId + " (" + workspace.getFactory().getColumnName(hNodeId)
+ ")";
}

/*
* Pedro
*
* Meant to be overriden, but would need to define for all commands. We are
* going to need a nicer way to record the inputs of commands to reason
* about them, so perhaps this will also be easier and nicer to do.
*/
protected JSONObject getArgsJSON(Workspace workspace) {
return new JSONObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import edu.isi.karma.controller.command.Command;
import edu.isi.karma.controller.command.CommandException;
import edu.isi.karma.controller.command.alignment.ChangeInternalNodeLinksCommandFactory.Arguments;
import edu.isi.karma.controller.update.AlignmentSVGVisualizationUpdate;
import edu.isi.karma.controller.update.SemanticTypesUpdate;
import edu.isi.karma.controller.update.UpdateContainer;
Expand All @@ -49,26 +50,27 @@ public class ChangeInternalNodeLinksCommand extends Command {
private final String alignmentId;
private JSONArray initialEdges;
private JSONArray newEdges;

// Required for undo
private Alignment oldAlignment;
private Alignment oldAlignment;
private DirectedWeightedMultigraph<Node, Link> oldGraph;

private StringBuilder descStr = new StringBuilder();
private static Logger logger = LoggerFactory.getLogger(ChangeInternalNodeLinksCommand.class);

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

private enum JsonKeys {
edgeSourceId, edgeId, edgeTargetId
}

public ChangeInternalNodeLinksCommand(String id, String worksheetId,
String alignmentId, JSONArray initialEdges, JSONArray newEdges) {
super(id);
this.worksheetId = worksheetId;
this.alignmentId = alignmentId;
this.initialEdges = initialEdges;
this.newEdges = newEdges;

addTag(CommandTag.Modeling);
}

Expand All @@ -95,56 +97,72 @@ public CommandType getCommandType() {
@SuppressWarnings("unchecked")
@Override
public UpdateContainer doIt(Workspace workspace) throws CommandException {
// String alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
logCommand(logger, workspace);
// String alignmentId =
// AlignmentManager.Instance().constructAlignmentId(workspace.getId(),
// worksheetId);
Alignment alignment = AlignmentManager.Instance().getAlignment(
alignmentId);
Worksheet worksheet = workspace.getWorksheet(worksheetId);
OntologyManager ontMgr = workspace.getOntologyManager();

// Save the original alignment for undo
oldAlignment = alignment.getAlignmentClone();
oldGraph = (DirectedWeightedMultigraph<Node, Link>)alignment.getGraph().clone();

// First delete the links that are not present in newEdges and present in intialEdges
oldGraph = (DirectedWeightedMultigraph<Node, Link>) alignment
.getGraph().clone();

// First delete the links that are not present in newEdges and present
// in intialEdges
try {
deleteLinks(alignment);
addNewLinks(alignment, ontMgr);
alignment.align();

} catch (JSONException e) {
e.printStackTrace();
}

return getAlignmentUpdateContainer(alignment, worksheet, workspace);
}

private void addNewLinks(Alignment alignment, OntologyManager ontMgr) throws JSONException {
for (int j=0; j<newEdges.length(); j++) {
private void addNewLinks(Alignment alignment, OntologyManager ontMgr)
throws JSONException {
for (int j = 0; j < newEdges.length(); j++) {
JSONObject newEdge = newEdges.getJSONObject(j);

String sourceId = newEdge.getString(JsonKeys.edgeSourceId.name());
String targetId = newEdge.getString(JsonKeys.edgeTargetId.name());
String edgeUri = newEdge.getString(JsonKeys.edgeId.name());

String linkId = LinkIdFactory.getLinkId(edgeUri, sourceId, targetId);
Link newLink = alignment.getLinkById(linkId);

String linkId = LinkIdFactory
.getLinkId(edgeUri, sourceId, targetId);
Link newLink = alignment.getLinkById(linkId);
if (newLink == null) {
Node sourceNode = alignment.getNodeById(sourceId);
if (sourceNode == null) {
logger.error("NULL source node! Please notify this error.");
String errorMessage = "Error while adding new links: the new link goes FROM node '"
+ sourceId
+ "', but this node is NOT in the alignment.";
logger.error(errorMessage);
}
Node targetNode = alignment.getNodeById(targetId);
if (targetNode == null) {
logger.error("NULL target node! Please notify this error.");
String errorMessage = "Error while adding new links: the new link goes TO node '"
+ targetId
+ "', but this node is NOT in the alignment.";
logger.error(errorMessage);
}
Label linkLabel = ontMgr.getUriLabel(edgeUri);

newLink = alignment.addObjectPropertyLink(sourceNode, targetNode, linkLabel);
alignment.changeLinkStatus(newLink.getId(), LinkStatus.ForcedByUser);

newLink = alignment.addObjectPropertyLink(sourceNode,
targetNode, linkLabel);
alignment.changeLinkStatus(newLink.getId(),
LinkStatus.ForcedByUser);
} else {
alignment.changeLinkStatus(linkId, LinkStatus.ForcedByUser);
}
// Add info to description string
if (j == newEdges.length()-1) {
if (j == newEdges.length() - 1) {
descStr.append(newLink.getLabel().getDisplayName());
} else {
descStr.append(newLink.getLabel().getDisplayName() + ",");
Expand All @@ -156,26 +174,29 @@ private void deleteLinks(Alignment alignment) throws JSONException {
for (int i = 0; i < initialEdges.length(); i++) {
JSONObject initialEdge = initialEdges.getJSONObject(i);
boolean exists = false;
for (int j=0; j<newEdges.length(); j++) {

for (int j = 0; j < newEdges.length(); j++) {
JSONObject newEdge = newEdges.getJSONObject(j);
if ((initialEdge.getString(JsonKeys.edgeSourceId.name())
.equals(newEdge.getString(JsonKeys.edgeSourceId.name())))

&& (initialEdge.getString(JsonKeys.edgeTargetId.name())
.equals(newEdge.getString(JsonKeys.edgeTargetId.name())))

&& initialEdge.getString(JsonKeys.edgeId.name())
.equals(newEdge.getString(JsonKeys.edgeId.name()))) {

&& (initialEdge.getString(JsonKeys.edgeTargetId.name())
.equals(newEdge.getString(JsonKeys.edgeTargetId
.name())))

&& initialEdge.getString(JsonKeys.edgeId.name())
.equals(newEdge.getString(JsonKeys.edgeId
.name()))) {
exists = true;
}
}

if (!exists) {
String linkId = LinkIdFactory.getLinkId(initialEdge.getString(JsonKeys.edgeId.name()),
initialEdge.getString(JsonKeys.edgeSourceId.name()),
String linkId = LinkIdFactory.getLinkId(
initialEdge.getString(JsonKeys.edgeId.name()),
initialEdge.getString(JsonKeys.edgeSourceId.name()),
initialEdge.getString(JsonKeys.edgeTargetId.name()));

// alignment.changeLinkStatus(linkId, LinkStatus.Normal);
alignment.removeLink(linkId);
}
Expand All @@ -184,26 +205,40 @@ private void deleteLinks(Alignment alignment) throws JSONException {

@Override
public UpdateContainer undoIt(Workspace workspace) {
Worksheet worksheet = workspace
.getWorksheet(worksheetId);
Worksheet worksheet = workspace.getWorksheet(worksheetId);

// Revert to the old alignment
AlignmentManager.Instance().addAlignmentToMap(alignmentId, oldAlignment);
AlignmentManager.Instance()
.addAlignmentToMap(alignmentId, oldAlignment);
oldAlignment.setGraph(oldGraph);

// Get the alignment update
return getAlignmentUpdateContainer(oldAlignment, worksheet, workspace);
}
//TODO this is in worksheetcommand

// TODO this is in worksheetcommand
private UpdateContainer getAlignmentUpdateContainer(Alignment alignment,
Worksheet worksheet, Workspace workspace) {
// Add the visualization update
UpdateContainer c = new UpdateContainer();
c.add(new SemanticTypesUpdate(worksheet, worksheetId, alignment));
c.add(new AlignmentSVGVisualizationUpdate(
worksheetId, alignment));
c.add(new AlignmentSVGVisualizationUpdate(worksheetId, alignment));
return c;
}

@Override
protected JSONObject getArgsJSON(Workspace workspace) {
JSONObject args = new JSONObject();
try {
args.put("command", getTitle())
.put(Arguments.alignmentId.name(), alignmentId)
.put(Arguments.initialEdges.name(), initialEdges)
.put(Arguments.worksheetId.name(),
formatWorsheetId(workspace, worksheetId))
.put(Arguments.newEdges.name(), newEdges);
} catch (JSONException e) {
e.printStackTrace();
}
return args;
}
}
Loading

0 comments on commit 3f34220

Please sign in to comment.