Package edu.isi.karma.modeling.alignment

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


    OntologyManager ontologyManager = workspace.getOntologyManager();
    if(ontologyManager.isEmpty())
      return new UpdateContainer(new ErrorUpdate("No ontology loaded."));
   
    String alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
    Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
    if (alignment == null) {
      logger.info("Alignment is NULL for " + worksheetId);
      return new UpdateContainer(new ErrorUpdate(
          "Please align the worksheet before generating R2RML Model!"));
    }

//    Set<ColumnNode> alignmentColumnNodes = alignment.getSourceColumnNodes();
//    if (alignmentColumnNodes != null) {
//      for (ColumnNode cn : alignmentColumnNodes) {
//        if (!cn.hasUserType())
//          worksheet.getSemanticTypes().unassignColumnSemanticType(cn.getHNodeId());
//      }
//    }

    alignment = initialAlignment;
    alignment.setGraph(initialGraph);
    if(!this.isExecutedInBatch())
      alignment.align();
    AlignmentManager.Instance().addAlignmentToMap(alignmentId, alignment);
   

    try {
      // Save the semantic types in the input parameter JSON
View Full Code Here


      if (newws == null)
        c.append(WorksheetUpdateFactory.createRegenerateWorksheetUpdates(oldws.getId(), getSuperSelection(oldws)));
      if (newws != null) {
        c.append(WorksheetUpdateFactory.createRegenerateWorksheetUpdates(newws.getId(), SuperSelectionManager.DEFAULT_SELECTION));
        //c.append(WorksheetUpdateFactory.createWorksheetHierarchicalAndCleaningResultsUpdates(newws.getId()));
        Alignment alignment = AlignmentManager.Instance().getAlignmentOrCreateIt(workspace.getId(), newws.getId(), workspace.getOntologyManager());
        c.append(WorksheetUpdateFactory.createSemanticTypesAndSVGAlignmentUpdates(newws.getId(), workspace, alignment));
      }
      //c.append(WorksheetUpdateFactory.createWorksheetHierarchicalAndCleaningResultsUpdates(oldws.getId()));
      c.append(computeAlignmentAndSemanticTypesAndCreateUpdates(workspace));
      return c;
View Full Code Here

      }
    });
  }
 
  public static String gethNodeId(String alignmentId, String columnUri) {
    Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
    Set<LabeledLink> tmp = alignment.getCurrentOutgoingLinksToNode(columnUri);
    String hNodeId = null;
    for (LabeledLink link : tmp) {
      if (link.getKeyType() == LinkKeyInfo.UriOfInstance) {
        hNodeId = link.getTarget().getId();
      }
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.addForcedInternalNode(new Label(nodeUri));
      if(!this.isExecutedInBatch())
        alignment.align();
     
    } catch (JSONException e) {
      logger.error("Error adding Internal Node:" , e);
    }
View Full Code Here

  public UpdateContainer doIt(Workspace workspace) throws CommandException {
//    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, DefaultLink>) alignment
        .getGraph().clone();

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

    } catch (JSONException e) {
      e.printStackTrace();
    }
View Full Code Here

    Cloner cloner = new Cloner();
    this.worksheetBeforeInvocation = cloner.deepClone(wk);

    AlignmentManager mgr = AlignmentManager.Instance();
    String alignmentId = mgr.constructAlignmentId(workspace.getId(), worksheetId);
    Alignment al = mgr.getAlignment(alignmentId);
   
//    /**
//     *
//     */
//    // FIXME
//    String exportDir = "/Users/mohsen/Dropbox/Service Modeling/iswc2013-exp2/jgraph/";
//    try {
//      GraphUtil.serialize(al.getSteinerTree(), exportDir + wk.getTitle() + ".karma.final.jgraph");
//    } catch (Exception e1) {
//      // TODO Auto-generated catch block
//      e1.printStackTrace();
//    }
//    if (true) return null;
   
    if (al == null) {
      logger.error("The alignment model is null.");
      return new UpdateContainer(new ErrorUpdate(
        "Error occured while populating the source. The alignment model is null."));
    }
   
    DirectedWeightedMultigraph<Node, LabeledLink> tree = al.getSteinerTree();
     
    if (tree == null) {
      logger.error("The alignment tree is null.");
      return new UpdateContainer(new ErrorUpdate(
        "Error occured while populating the source. The alignment model is null."));
View Full Code Here

      c.add(new WorksheetListUpdate());
      if (newws == null)
        c.append(WorksheetUpdateFactory.createRegenerateWorksheetUpdates(oldws.getId(), getSuperSelection(oldws)));
      if (newws != null) {
        c.append(WorksheetUpdateFactory.createRegenerateWorksheetUpdates(newws.getId(), SuperSelectionManager.DEFAULT_SELECTION));
        Alignment alignment = AlignmentManager.Instance().getAlignmentOrCreateIt(workspace.getId(), newws.getId(), workspace.getOntologyManager());
        c.append(WorksheetUpdateFactory.createSemanticTypesAndSVGAlignmentUpdates(newws.getId(), workspace, alignment));
      }
      c.append(computeAlignmentAndSemanticTypesAndCreateUpdates(workspace));
      return c;
    } catch (Exception e) {
View Full Code Here

    appliedCommands.clear();
    inputColumns.clear();
    outputColumns.clear();
    UpdateContainer c =  new UpdateContainer();
    alignmentId = AlignmentManager.Instance().constructAlignmentId(workspace.getId(), worksheetId);
    Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
    RepFactory factory = workspace.getFactory();
    Worksheet worksheet = factory.getWorksheet(worksheetId);
    SuperSelection selection = getSuperSelection(worksheet);
    if (alignment.GetTreeRoot() != null)
      hNodeId = FetchHNodeIdFromAlignmentCommand.gethNodeId(alignmentId, columnUri);
    if (hNodeId == null) {
      c.append(WorksheetUpdateFactory.createRegenerateWorksheetUpdates(worksheetId, getSuperSelection(worksheet)));
      c.append(computeAlignmentAndSemanticTypesAndCreateUpdates(workspace));
      return c;
    }
    HNode hnode = factory.getHNode(hNodeId);
    List<String> hNodeIds = new LinkedList<String>();
    hNodeIds.add(hNodeId);
    inputColumns.addAll(hNodeIds);
    List<Table> dataTables = new ArrayList<Table>();
    CloneTableUtils.getDatatable(worksheet.getDataTable(), factory.getHTable(hnode.getHTableId()), dataTables, selection);
    Map<String, String> rowHashToSubjectURI = new HashMap<String, String>();
    Map<String, List<String>> SubjectURIToRowId = new HashMap<String, List<String>>();
    for(Table t : dataTables) {
      for(Row r : t.getRows(0, t.getNumRows(), selection)) {
        Node n = r.getNode(hNodeId);
        if(n != null && n.getValue() != null && !n.getValue().isEmptyValue() && n.getValue().asString() != null && !n.getValue().asString().trim().isEmpty() ) {
          String uri = n.getValue().asString().trim().replace(" ", "");
          String baseURI = worksheet.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.baseURI);
          try {
            URI t1 = new URI(uri);
            if (!t1.isAbsolute() && baseURI != null) {
              uri = baseURI + uri;
            }
          } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
          }
//          n.setValue(uri, n.getStatus(), factory);
          rowHashToSubjectURI.put(HashValueManager.getHashValue(r, hNodeIds), uri);

          if (SubjectURIToRowId.get(uri) == null)
            SubjectURIToRowId.put(uri, new ArrayList<String>());
          List<String> rowIds = SubjectURIToRowId.get(uri)
          rowIds.add(r.getId());
        }
      }
    }
    TripleStoreUtil util = new TripleStoreUtil();

    //String modelContext = worksheet.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.modelContext);
    List<String> subjects = new LinkedList<String>();
    subjects.addAll(rowHashToSubjectURI.values());
    List<String> predicates = new LinkedList<String>();
    List<String> otherClasses = new LinkedList<String>();
    Map<String, List<String>> results = new HashMap<String, List<String>>();

    URIFormatter uriFormatter = new URIFormatter(workspace.getOntologyManager(), new ErrorReport());
    if(sameAsPredicate!= null && !sameAsPredicate.trim().isEmpty())
    {
      sameAsPredicate = uriFormatter.getExpandedAndNormalizedUri(sameAsPredicate);
    }

    JSONArray predicatesarray = new JSONArray(predicate);
    JSONArray otherClassarray = new JSONArray(otherClass);

    for(int i = 0; i < predicatesarray.length(); i++) {
      predicates.add(predicatesarray.getJSONObject(i).getString("predicate"));
      otherClasses.add(otherClassarray.getJSONObject(i).getString("otherClass"));
    }

    while (subjects.size() > 0) {
      ListIterator<String> subjectsIterator = subjects.listIterator();
      LinkedList<String> tempSubjects = new LinkedList<String>();
      while(tempSubjects.size() < limit && subjects.size() > 0)
      {
        tempSubjects.add(subjectsIterator.next());
        subjectsIterator.remove();
      }
      try {
        Map<String, List<String>> temp = null;
        if (!incoming)
          temp = util.getObjectsForSubjectsAndPredicates(dataRepoUrl, null, tempSubjects , predicates, otherClasses, sameAsPredicate);
        else
          temp = util.getSubjectsForPredicatesAndObjects(dataRepoUrl, null, tempSubjects , predicates, otherClasses, sameAsPredicate);
        addMappingToResults(results, temp);
        //        predicates.clear();
        //        otherClasses.clear();
      } catch (KarmaException e) {
        LOG.error("Unable to load data to augment: ", e);
        return new UpdateContainer(new ErrorUpdate(e.getMessage()));
      }
    }


    List<String> resultSubjects = results.get("resultSubjects");
    List<String> resultPredicates = results.get("resultPredicates");
    List<String> resultObjects = results.get("resultObjects");
    List<String> resultClass = results.get("resultClasses");
    AddValuesCommandFactory addFactory = new AddValuesCommandFactory();

    for (int i = 0; i < resultPredicates.size(); i++) {
      String subject = incoming ? resultObjects.get(i) : resultSubjects.get(i);
      List<String> rowIds = SubjectURIToRowId.get(subject);
      boolean isNewNode = false;
      for (String RowId : rowIds) {
        String predicate = resultPredicates.get(i);
        String otherClass = resultClass.get(i);
        JSONArray array = new JSONArray();
        JSONObject obj = new JSONObject();
        JSONObject obj2 = new JSONObject();
        if (otherClass != null && !otherClass.trim().isEmpty())
          obj.put("URIs", incoming ? resultSubjects.get(i) : resultObjects.get(i));
        else
          obj.put("values", incoming ? resultSubjects.get(i) : resultObjects.get(i));
        obj2.put("rowId", RowId);
        obj2.put("rowIdHash", "");
        obj2.put("values", obj);
        array.put(obj2);
        JSONArray input = new JSONArray();
        JSONObject obj3 = new JSONObject();
        obj3.put("name", "AddValues");
        obj3.put("value", array.toString());
        obj3.put("type", "other");
        input.put(obj3);
        try {
          OntologyManager ontMgr = workspace.getOntologyManager();
          Label label = ontMgr.getUriLabel(incoming ? otherClass : predicate);
          AddValuesCommand command = (AddValuesCommand) addFactory.createCommand(input, workspace, hNodeId, worksheetId, hnode.getHTableId(), label.getDisplayName(), HNodeType.AugmentData, selection.getName());
          command.doIt(workspace);
          outputColumns.addAll(command.getOutputColumns());
          isNewNode |= command.isNewNode();
          if (command.isNewNode())
            appliedCommands.push(command);
          newhNodeId = command.getNewHNodeId();


        } catch(Exception e) {
          e.printStackTrace();
          return new UpdateContainer(new ErrorUpdate(e.getMessage()));
        }
      }
      if (isNewNode && alignment.GetTreeRoot() != null) {
        HNode tableHNode =workspace.getFactory().getHNode(newhNodeId);
        String nestedHNodeId = tableHNode.getNestedTable().getHNodeIdFromColumnName("values");
        if (nestedHNodeId == null)
          nestedHNodeId = tableHNode.getNestedTable().getHNodeIdFromColumnName("URIs");
        SetSemanticTypeCommandFactory sstFactory = new SetSemanticTypeCommandFactory();
        JSONArray semanticTypesArray = new JSONArray();
        JSONObject semanticType = new JSONObject();
        edu.isi.karma.rep.alignment.Node n = alignment.getNodeById(columnUri);

        semanticType.put(ClientJsonKeys.isPrimary.name(), "true");
        Set<edu.isi.karma.rep.alignment.Node> oldNodes = new HashSet<edu.isi.karma.rep.alignment.Node>();
        if(resultClass.get(i).trim().isEmpty())
        {
          semanticType.put(ClientJsonKeys.DomainId.name(), n.getId());
          semanticType.put(ClientJsonKeys.FullType.name(), resultPredicates.get(i));
          semanticType.put(ClientJsonKeys.DomainUri.name(), n.getUri());
        }
        else
        {
          if (alignment.getNodesByUri(resultClass.get(i)) != null)
            oldNodes.addAll( alignment.getNodesByUri(resultClass.get(i)));
          semanticType.put(ClientJsonKeys.DomainId.name(), resultClass.get(i));
          semanticType.put(ClientJsonKeys.FullType.name(), Uris.CLASS_INSTANCE_LINK_URI);
        }


        semanticTypesArray.put(semanticType);
        Command sstCommand = sstFactory.createCommand(workspace, worksheetId, nestedHNodeId, false, semanticTypesArray, false, "", selection.getName());
        appliedCommands.push(sstCommand);
        sstCommand.doIt(workspace);
        if(!resultClass.get(i).trim().isEmpty())
        {
          ChangeInternalNodeLinksCommandFactory cinlcf = new ChangeInternalNodeLinksCommandFactory();
          SetMetaPropertyCommandFactory smpcf = new SetMetaPropertyCommandFactory();
          JSONArray newEdges = new JSONArray();
          JSONObject newEdge = new JSONObject();
          String sourceId = n.getId();
          Set<edu.isi.karma.rep.alignment.Node> tempnodes = new HashSet<edu.isi.karma.rep.alignment.Node>();
          tempnodes.addAll(alignment.getNodesByUri(resultClass.get(i)));
          tempnodes.removeAll(oldNodes);

          edu.isi.karma.rep.alignment.Node target = tempnodes.iterator().next();
          String targetId = target.getId();
          String targetUri = target.getLabel().getUri();
View Full Code Here

        }
       
        SemanticTypeColumnModel model = new SemanticTypeUtil().predictColumnSemanticType(workspace, worksheet, currentColumnPath, 4, selection);
        if(model != null) {
          OntologyManager ontMgr = workspace.getOntologyManager();
          Alignment alignment = AlignmentManager.Instance().getAlignmentOrCreateIt(workspace.getId(), worksheetId, ontMgr);
          JSONObject json = model.getAsJSONObject(ontMgr, alignment);
          pw.print(json.toString());
        }
       
      }
View Full Code Here

    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 ***/
    newType = null;

    /** Check if a semantic type already exists for the column **/
    ColumnNode columnNode = alignment.getColumnNodeByHNodeId(hNodeId);
    columnNode.setRdfLiteralType(rdfLiteralType);
    boolean semanticTypeAlreadyExists = false;
    LabeledLink oldIncomingLinkToColumnNode = null;
    Node oldDomainNode = null;
    List<LabeledLink> columnNodeIncomingLinks = alignment
        .getIncomingLinks(columnNode.getId());
    if (columnNodeIncomingLinks != null
        && !columnNodeIncomingLinks.isEmpty()) { // SemanticType already
                              // assigned
      semanticTypeAlreadyExists = true;
      oldIncomingLinkToColumnNode = columnNodeIncomingLinks.get(0);
      oldDomainNode = oldIncomingLinkToColumnNode.getSource();
    }

    if(metaPropertyId.endsWith(" (add)"))
      metaPropertyId = metaPropertyId.substring(0, metaPropertyId.length()-5).trim();
   
  if (metaPropertyName.equals(METAPROPERTY_NAME.isUriOfClass)) {
      Node classNode = alignment.getNodeById(metaPropertyId);
      if (semanticTypeAlreadyExists) {
        clearOldSemanticTypeLink(oldIncomingLinkToColumnNode,
            oldDomainNode, alignment, classNode);
      }

      if (classNode == null) {
        Label classNodeLabel = ontMgr.getUriLabel(metaPropertyUri);
        if (classNodeLabel == null) {
          String errorMessage = "Error while setting a classLink. MetaPropertyUri '"
              + metaPropertyUri
              + "' should be in the Ontology Manager, but it is not.";
          logger.error(errorMessage);
          return new UpdateContainer(new ErrorUpdate(errorMessage));
        }
        classNode = alignment.addInternalNode(classNodeLabel);
      }

      LabeledLink newLink = alignment.addClassInstanceLink(classNode, columnNode,
          LinkKeyInfo.UriOfInstance);
      alignment.changeLinkStatus(newLink.getId(),
          LinkStatus.ForcedByUser);
     
      // Create the semantic type object
      newType = new SemanticType(hNodeId,
          ClassInstanceLink.getFixedLabel(), classNode.getLabel(),
          SemanticType.Origin.User, 1.0);
    } else if (metaPropertyName
        .equals(METAPROPERTY_NAME.isSpecializationForEdge)) {
      LabeledLink propertyLink = alignment.getLinkById(metaPropertyId);
      if (propertyLink == null) {
        String errorMessage = "Error while specializing a link. The DefaultLink '"
            + metaPropertyId
            + "' should already be in the alignment, but it is not.";
        logger.error(errorMessage);
        return new UpdateContainer(new ErrorUpdate(errorMessage));
      }

      Node classInstanceNode = alignment.getNodeById(LinkIdFactory
          .getLinkSourceId(metaPropertyId));
      if (semanticTypeAlreadyExists) {
        clearOldSemanticTypeLink(oldIncomingLinkToColumnNode,
            oldDomainNode, alignment, classInstanceNode);
      }

      if (propertyLink instanceof DataPropertyLink) {
        String targetHNodeId = ((ColumnNode) propertyLink.getTarget())
            .getHNodeId();
        LabeledLink newLink = alignment.addDataPropertyOfColumnLink(classInstanceNode,
            columnNode, targetHNodeId, propertyLink.getId());
        alignment.changeLinkStatus(newLink.getId(),
            LinkStatus.ForcedByUser);
       
        // Create the semantic type object
        newType = new SemanticType(hNodeId,
            DataPropertyOfColumnLink.getFixedLabel(),
            classInstanceNode.getLabel(), SemanticType.Origin.User,
            1.0);
      } else if (propertyLink instanceof ObjectPropertyLink) {
        LabeledLink newLink = alignment.addObjectPropertySpecializationLink(
            classInstanceNode, columnNode, propertyLink.getId());
        alignment.changeLinkStatus(newLink.getId(),
            LinkStatus.ForcedByUser);

        // Create the semantic type object
        newType = new SemanticType(hNodeId,
            ObjectPropertySpecializationLink.getFixedLabel(),
            classInstanceNode.getLabel(), SemanticType.Origin.User,
            1.0);
      }

    } else if (metaPropertyName.equals(METAPROPERTY_NAME.isSubclassOfClass)) {
      Node classNode = alignment.getNodeById(metaPropertyId);
      if (semanticTypeAlreadyExists) {
        clearOldSemanticTypeLink(oldIncomingLinkToColumnNode,
            oldDomainNode, alignment, classNode);
      }

      if (classNode == null) {
        Label classNodeLabel = ontMgr.getUriLabel(metaPropertyUri);
        if (classNodeLabel == null) {
          String errorMessage = "Error while setting an advances subclass. MetaPropertyValue '"
              + metaPropertyUri
              + "' should be in the Ontology Manager, but it is not.";
          logger.error(errorMessage);
          return new UpdateContainer(new ErrorUpdate(errorMessage));
        }
        classNode = alignment.addInternalNode(classNodeLabel);
      }
      LabeledLink newLink = alignment.addColumnSubClassOfLink(classNode, columnNode);
      alignment.changeLinkStatus(newLink.getId(),
          LinkStatus.ForcedByUser);
     
      // Create the semantic type object
      newType = new SemanticType(hNodeId,
          ColumnSubClassLink.getFixedLabel(), classNode.getLabel(),
          SemanticType.Origin.User, 1.0);
    }

    columnNode.setUserSelectedSemanticType(newType);

    // Update the alignment
    if(!this.isExecutedInBatch())
      alignment.align();


    UpdateContainer c = new UpdateContainer();

    // Save the old SemanticType object and CRF Model for undo
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.