Package edu.isi.karma.modeling.alignment

Examples of edu.isi.karma.modeling.alignment.Alignment


    }
    Worksheet worksheet = workspace.getWorksheet(worksheetId);
    SuperSelection selection = getSuperSelection(worksheet);
    OntologyManager ontMgr = workspace.getOntologyManager();
    String alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
    Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
    if (alignment == null) {
      alignment = new Alignment(ontMgr);
      AlignmentManager.Instance().addAlignmentToMap(alignmentId, alignment);
    }
   
    // Save the original alignment for undo
    oldAlignment = alignment.getAlignmentClone();
    oldGraph = (DirectedWeightedMultigraph<Node, DefaultLink>)alignment.getGraph().clone();
   
    /*** Add the appropriate nodes and links in alignment graph ***/
    List<SemanticType> typesList = new ArrayList<SemanticType>();
    for (int i = 0; i < typesArr.length(); i++) {
      try {
        LabeledLink newLink = null;
        JSONObject type = typesArr.getJSONObject(i);
       
        String domainValue;
        // For property semantic types, domain uri goes to "domainValue" and link uri goes to "fullTypeValue".
        // For class semantic type, class uri goes "fullTypeValue" and "domainValue" is empty.
        if(type.has(ClientJsonKeys.DomainId.name()))
          domainValue = type.getString(ClientJsonKeys.DomainId.name());
        else
          domainValue = type.getString("Domain"); //For backward compatibility to older models
        String fullTypeValue = type.getString(ClientJsonKeys.FullType.name());
//        logger.trace("FULL TYPE:" + type.getString(ClientJsonKeys.FullType.name()));
//        logger.trace("Domain: " + type.getString(ClientJsonKeys.Domain.name()));
       
        // Look if the domain value exists. If it exists, then it is a domain of a data property. If not
        // then the value in FullType has the the value which indicates if a new class instance is needed
        // or an existing class instance should be used (this is the case when just the class is chosen as a sem type).
//        Label domainName = null;
       
        boolean isClassSemanticType = false;
        boolean semanticTypeAlreadyExists = false;
        Node domain = null;
        String domainUriOrId;
        Label linkLabel;
       
        // if domain value is empty, semantic type is a class semantic type
        if (domainValue.equals("")) {
          isClassSemanticType = true;
          domainUriOrId = fullTypeValue;
          linkLabel = ClassInstanceLink.getFixedLabel();
        } else {
          isClassSemanticType = false;
          domainUriOrId = domainValue;
          linkLabel = ontMgr.getUriLabel(fullTypeValue);
          if (linkLabel == null) {
            logger.error("URI/ID does not exist in the ontology or model: " + fullTypeValue);
            continue;
          }
        }
       
        if(domainUriOrId.endsWith(" (add)"))
          domainUriOrId = domainUriOrId.substring(0, domainUriOrId.length()-5).trim();
       
        domain = alignment.getNodeById(domainUriOrId);
        logger.info("Got domain for domainUriOrId:" + domainUriOrId + " ::" + domain);
        if (domain == null) {
          Label label = ontMgr.getUriLabel(domainUriOrId);
//          if (label == null) {
//            logger.error("URI/ID does not exist in the ontology or model: " + domainUriOrId);
//            continue;
//          }
          if (label == null) {
            if(type.has(ClientJsonKeys.DomainUri.name())) {
              label = new Label(type.getString(ClientJsonKeys.DomainUri.name()));
            } else {
              //This part of the code is for backward compatibility. Newer models should have domainUri
              int len = domainValue.length();
              if ((len > 1) && Character.isDigit(domainValue.charAt(len-1))) {
                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);
      }
    }
View Full Code Here


    nodeLabels.addAll(allClasses.values());
    return getNodesUsingAlignment(workspace, nodeLabels);
  }

  private Set<Node> getClassesInModel(Workspace workspace) {
    final Alignment alignment = AlignmentManager.Instance().getAlignment(
        workspace.getId(), worksheetId);
    return alignment.getSteinerTree().vertexSet();
  }
View Full Code Here

  }

  private Set<Node> getNodesUsingAlignment(Workspace workspace, Set<Label> nodeLabels) {
    Set<Node> nodeSet = new HashSet<>();
    final OntologyManager ontMgr = workspace.getOntologyManager();
    final Alignment alignment = AlignmentManager.Instance().getAlignmentOrCreateIt(
        workspace.getId(), worksheetId, ontMgr);

    final Set<String> steinerTreeNodeIds = new HashSet<String>();

    if (alignment != null && !alignment.isEmpty()) {
      for (Node node: alignment.getSteinerTree().vertexSet()) {
        if (node.getType() == NodeType.InternalNode) {
          steinerTreeNodeIds.add(node.getId());
        }
      }
    }
    for (Label nodeLabel : nodeLabels) {
      String nodeUri = nodeLabel.getUri();

      int graphLastIndex = -1;
      if (alignment != null) {
        graphLastIndex = alignment.getLastIndexOfNodeUri(nodeUri);
      }
      String nodeId;
      // If the node exists in graph but not in tree then use the graph node id
      if (graphLastIndex != -1) {
        int i = 1;
        for (; i <= graphLastIndex && steinerTreeNodeIds.contains(nodeUri + i); i++) ;
        nodeId = nodeUri + i + " (add)";
      } else {
        nodeId = nodeUri + "1 (add)";
      }


      InternalNode node = new InternalNode(nodeId, nodeLabel);
      nodeSet.add(node);


      // Populate the graph nodes also
      if (alignment != null) {
        Set<Node> graphNodes = alignment.getNodesByUri(nodeUri);
        if (graphNodes != null && graphNodes.size() != 0) {
          for (Node graphNode: graphNodes) {
            if (steinerTreeNodeIds.contains(graphNode.getId())) {
              nodeSet.add(graphNode);
            }
View Full Code Here

  @SuppressWarnings("unchecked")
  @Override
  public UpdateContainer doIt(Workspace workspace) throws CommandException {
    logCommand(logger, workspace);
 
    Alignment alignment = AlignmentManager.Instance().getAlignment(
        alignmentId);

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

    try {
      alignment.deleteForcedInternalNode(nodeId);
      if(!this.isExecutedInBatch())
        alignment.align();
    } catch (JSONException e) {
      logger.error("Error adding Internal Node:" , e);
    }

    return WorksheetUpdateFactory.createSemanticTypesAndSVGAlignmentUpdates(worksheetId, workspace, alignment);
View Full Code Here

    final String modelFileName = graphLabel + "-model.ttl";
    final String modelFileLocalPath = ServletContextParameterMap.getParameterValue(
        ContextParameter.R2RML_PUBLISH_DIR) +  modelFileName;

    // Get the alignment for this Worksheet
    Alignment alignment = AlignmentManager.Instance().getAlignment(AlignmentManager.
        Instance().constructAlignmentId(workspace.getId(), worksheetId));

    if (alignment == null) {
      logger.info("Alignment is NULL for " + worksheetId);
      return new UpdateContainer(new ErrorUpdate(
          "Please align the worksheet before generating R2RML Model!"));
    }
    Set<LabeledLink> links = new HashSet<LabeledLink>();
    if (alignment.getSteinerTree() != null) {
      for (LabeledLink link : alignment.getSteinerTree().edgeSet()) {
        if ((link.getStatus() == LinkStatus.Normal || link.getStatus() == LinkStatus.PreferredByUI) && (link.getType() == LinkType.ObjectPropertyLink)) {
          links.add(link);
        }
      }
    }
    JSONArray newEdges = new JSONArray();
    JSONArray initialEdges = new JSONArray();
    ChangeInternalNodeLinksCommandFactory cinlcf = new ChangeInternalNodeLinksCommandFactory();
    for (LabeledLink link : links) {
      JSONObject newEdge = new JSONObject();
      JSONObject initialEdge = new JSONObject();
      newEdge.put(ChangeInternalNodeLinksCommand.JsonKeys.edgeSourceId.name(), link.getSource().getId());
      newEdge.put(ChangeInternalNodeLinksCommand.JsonKeys.edgeTargetId.name(), link.getTarget().getId());
      newEdge.put(ChangeInternalNodeLinksCommand.JsonKeys.edgeId.name(), link.getUri());
      initialEdge.put(ChangeInternalNodeLinksCommand.JsonKeys.edgeSourceId.name(), link.getSource().getId());
      initialEdge.put(ChangeInternalNodeLinksCommand.JsonKeys.edgeTargetId.name(), link.getTarget().getId());
      initialEdge.put(ChangeInternalNodeLinksCommand.JsonKeys.edgeId.name(), link.getUri());
      newEdges.put(newEdge);
      initialEdges.put(initialEdge);
    }
    JSONArray inputJSON = new JSONArray();
    JSONObject t = new JSONObject();
    t.put("name", "worksheetId");
    t.put("type", HistoryJsonUtil.ParameterType.worksheetId.name());
    t.put("value", worksheetId);
    inputJSON.put(t);
    t = new JSONObject();
    t.put("name", "initialEdges");
    t.put("type", HistoryJsonUtil.ParameterType.other.name());
    t.put("value", initialEdges);
    inputJSON.put(t);
    t = new JSONObject();
    t.put("name", "newEdges");
    t.put("type", HistoryJsonUtil.ParameterType.other.name());
    t.put("value", newEdges);
    inputJSON.put(t);
    if (newEdges.length() > 0 || initialEdges.length() > 0) {
      try {
        Command changeInternalNodeLinksCommand = cinlcf.createCommand(inputJSON, workspace);
        workspace.getCommandHistory().doCommand(changeInternalNodeLinksCommand, workspace);
        uc.add(new HistoryUpdate(workspace.getCommandHistory()));
        uc.append(WorksheetUpdateFactory.createRegenerateWorksheetUpdates(worksheetId, getSuperSelection(worksheet)));
        uc.append(computeAlignmentAndSemanticTypesAndCreateUpdates(workspace));
      }catch(Exception e)
      {
        e.printStackTrace();
      }
    }

    // mohsen: my code to enable Karma to leran semantic models
    // *****************************************************************************************
    // *****************************************************************************************

    SemanticModel semanticModel = new SemanticModel(workspace, worksheet, worksheetName, alignment.getSteinerTree(), selection);
    semanticModel.setName(worksheetName);
    try {
      semanticModel.writeJson(ServletContextParameterMap.getParameterValue(ContextParameter.JSON_MODELS_DIR) +
          semanticModel.getName() +
          ".model.json");
View Full Code Here

  @Override
  public UpdateContainer doIt(Workspace workspace) throws CommandException {
    logCommand(logger, workspace);
 
    Alignment alignment = AlignmentManager.Instance().getAlignment(
        alignmentId);

    final UpdateContainer uc = new UpdateContainer();
    Node node = alignment.getNodeById(nodeId);
    if(node instanceof LiteralNode) {
      final LiteralNode lNode = (LiteralNode)node;
      uc.add(new AbstractUpdate() {

        @Override
View Full Code Here

    Cloner cloner = new Cloner();
    this.worksheetBeforeInvocation = cloner.deepClone(wk);
   
    OntologyManager ontMgr = workspace.getOntologyManager();
    String alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
    Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
    if (alignment == null) {
      alignment = new Alignment(ontMgr);
      AlignmentManager.Instance().addAlignmentToMap(alignmentId, alignment);
    }
   
    if (initialAlignment == null) {
      initialAlignment = alignment.getAlignmentClone();
      initialGraph = (DirectedWeightedMultigraph<Node, DefaultLink>)alignment.getGraph().clone();
    }
   
    List<String> requestURLStrings = new ArrayList<String>();
    List<Row> rows = wk.getDataTable().getRows(0, wk.getDataTable().getNumRows(), selection);
    if (rows == null || rows.size() == 0) {
View Full Code Here

//      HNodePath path = new HNodePath(node);
//      columnPaths.add(path);
//    }
   
    String alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
    Alignment alignment = initialAlignment;
    alignment.setGraph(initialGraph);
    if(!this.isExecutedInBatch())
      alignment.align();
    AlignmentManager.Instance().addAlignmentToMap(alignmentId, alignment);
    try {
      // Add the visualization update
      workspace.getFactory().replaceWorksheet(worksheetId, worksheetBeforeInvocation);
      c.add(new ReplaceWorksheetUpdate(worksheetId, worksheetBeforeInvocation));
View Full Code Here

  }

  @Override
  public UpdateContainer doIt(Workspace workspace) throws CommandException {
    UpdateContainer container = new UpdateContainer();
    Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
    container.add(new AlignmentSVGVisualizationUpdate(worksheetId, alignment));
    return container;
  }
View Full Code Here

      return new UpdateContainer(new ErrorUpdate("No ontology loaded."));
   
    worksheetName = worksheet.getTitle();
   
    String alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
    Alignment alignment = AlignmentManager.Instance().getAlignmentOrCreateIt(workspace.getId(), worksheetId, ontologyManager);
    if (alignment == null) {
      logger.info("Alignment is NULL for " + worksheetId);
      return new UpdateContainer(new ErrorUpdate(
          "Alignment is NULL for " + worksheetId));
    }

    if (initialAlignment == null)
    {
      initialAlignment = alignment.getAlignmentClone();

      initialGraph = (DirectedWeightedMultigraph<Node, DefaultLink>)alignment.getGraph().clone();
     
      List<HNode> orderedNodeIds = new ArrayList<HNode>();
      worksheet.getHeaders().getSortedLeafHNodes(orderedNodeIds);
      if (orderedNodeIds != null) {
        for (int i = 0; i < orderedNodeIds.size(); i++)
        {
          String hNodeId = orderedNodeIds.get(i).getId();
          ColumnNode cn = alignment.getColumnNodeByHNodeId(hNodeId);
         
          if (!cn.hasUserType())
          {
            worksheet.getSemanticTypes().unassignColumnSemanticType(hNodeId);
            List<SemanticType> suggestedSemanticTypes =
                new SemanticTypeUtil().getColumnSemanticSuggestions(workspace, worksheet, cn, 4, selection);
            cn.setSuggestedSemanticTypes(suggestedSemanticTypes);
          }
        }
      }
    } else {
    // Replace the current alignment with the old alignment
      alignment = initialAlignment;
      alignment.setGraph(initialGraph);
      if(!this.isExecutedInBatch())
        alignment.align();
      AlignmentManager.Instance().addAlignmentToMap(alignmentId, alignment);
    }

    steinerNodes = alignment.computeSteinerNodes();
    ModelLearner modelLearner = null;
    if (ModelingConfiguration.isLearnAlignmentEnabled())
      modelLearner = new ModelLearner(alignment.getGraphBuilder(), steinerNodes);
    else
      modelLearner = new ModelLearner(ontologyManager, alignment.getLinksByStatus(LinkStatus.ForcedByUser), steinerNodes);

//    logger.info(GraphUtil.defaultGraphToString(ModelLearningGraph.getInstance(ontologyManager, ModelLearningGraphType.Compact).getGraphBuilder().getGraph()));

    SemanticModel model = modelLearner.getModel();
    if (model == null) {
      logger.error("could not learn any model for this source!");
      return new UpdateContainer(new ErrorUpdate(
          "Error occured while generating a semantic model for the source."));
    }
   
//    logger.info(GraphUtil.labeledGraphToString(model.getGraph()));
   
    List<SemanticType> semanticTypes = new LinkedList<SemanticType>();
    alignment.updateAlignment(model, semanticTypes);
//    Set<ColumnNode> alignmentColumnNodes = alignment.getSourceColumnNodes();
//    if (alignmentColumnNodes != null) {
//      for (ColumnNode cn : alignmentColumnNodes) {
//        worksheet.getSemanticTypes().unassignColumnSemanticType(cn.getHNodeId());
//      }
View Full Code Here

TOP

Related Classes of edu.isi.karma.modeling.alignment.Alignment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.