String newDomainValue = domainValue.substring(0, len-1);
label = ontMgr.getUriLabel(newDomainValue);
}
if (label == null) {
logger.error("No graph node found for the node: " + domainValue);
return new UpdateContainer(new ErrorUpdate("" +
"Error occured while setting semantic type!"));
}
}
}
domain = alignment.addInternalNode(label);
}
// Check if a semantic type already exists for the column
ColumnNode columnNode = alignment.getColumnNodeByHNodeId(hNodeId);
columnNode.setRdfLiteralType(rdfLiteralType);
List<LabeledLink> columnNodeIncomingLinks = alignment.getIncomingLinks(columnNode.getId());
LabeledLink oldIncomingLinkToColumnNode = null;
Node oldDomainNode = null;
if (columnNodeIncomingLinks != null && !columnNodeIncomingLinks.isEmpty()) { // SemanticType already assigned
semanticTypeAlreadyExists = true;
oldIncomingLinkToColumnNode = columnNodeIncomingLinks.get(0);
oldDomainNode = oldIncomingLinkToColumnNode.getSource();
}
if (type.getBoolean(ClientJsonKeys.isPrimary.name())) {
if (isClassSemanticType) {
if (semanticTypeAlreadyExists && oldDomainNode == domain) {
newLink = oldIncomingLinkToColumnNode;
// do nothing;
} else if (semanticTypeAlreadyExists) {
alignment.removeLink(oldIncomingLinkToColumnNode.getId());
// alignment.removeNode(oldDomainNode.getId());
newLink = alignment.addClassInstanceLink(domain, columnNode, LinkKeyInfo.None);
} else {
newLink = alignment.addClassInstanceLink(domain, columnNode, LinkKeyInfo.None);
}
}
// Property semantic type
else {
// When only the link changes between the class node and the internal node (domain)
if (semanticTypeAlreadyExists && oldDomainNode == domain) {
alignment.removeLink(oldIncomingLinkToColumnNode.getId());
newLink = alignment.addDataPropertyLink(domain, columnNode, linkLabel);
}
// When there was an existing semantic type and the new domain is a new node in the graph and semantic type already existed
else if (semanticTypeAlreadyExists) {
alignment.removeLink(oldIncomingLinkToColumnNode.getId());
// alignment.removeNode(oldDomainNode.getId());
newLink = alignment.addDataPropertyLink(domain, columnNode, linkLabel);
} else {
newLink = alignment.addDataPropertyLink(domain, columnNode, linkLabel);
}
}
} else { // Synonym semantic type
SemanticType synType = new SemanticType(hNodeId, linkLabel, domain.getLabel(), SemanticType.Origin.User, 1.0);
typesList.add(synType);
}
// Create the semantic type object
newType = new SemanticType(hNodeId, linkLabel, domain.getLabel(), SemanticType.Origin.User, 1.0);
// newType = new SemanticType(hNodeId, classNode.getLabel(), null, SemanticType.Origin.User, 1.0,isPartOfKey);
columnNode.setUserSelectedSemanticType(newType);
if(newLink != null) {
alignment.changeLinkStatus(newLink.getId(),
LinkStatus.ForcedByUser);
}
// Update the alignment
if(!this.isExecutedInBatch())
alignment.align();
} catch (JSONException e) {
logger.error("JSON Exception occured", e);
}
}
UpdateContainer c = new UpdateContainer();
// Save the old SemanticType object and CRF Model for undo
oldType = worksheet.getSemanticTypes().getSemanticTypeForHNodeId(hNodeId);
oldSynonymTypes = worksheet.getSemanticTypes().getSynonymTypesForHNodeId(hNodeId);
if (newType != null) {
// Update the SemanticTypes data structure for the worksheet
worksheet.getSemanticTypes().addType(newType);
// Update the synonym semanticTypes
newSynonymTypes = new SynonymSemanticTypes(typesList);
worksheet.getSemanticTypes().addSynonymTypesForHNodeId(newType.getHNodeId(), newSynonymTypes);
}
// Identify the outliers if the semantic type exists in the crfmodel
// List<String> existingLabels = new ArrayList<String>();
// crfModelHandler.getLabels(existingLabels);
// if (existingLabels.contains(newType.getCrfModelLabelString())) {
// identifyOutliers(worksheet, vWorkspace, crfModelHandler, newType);
// c.add(new TagsUpdate());
// }
if(trainAndShowUpdates) {
new SemanticTypeUtil().trainOnColumn(workspace, worksheet, newType, selection);
}
c.add(new SemanticTypesUpdate(worksheet, worksheetId, alignment));
c.add(new AlignmentSVGVisualizationUpdate(worksheetId,
alignment));
return c;
}