コード例 #1
0
  public void deduceLocation() {
    Property loc = model.getModel().getProperty(uri, "hasLocation");

    for (Resource human : humans) {

      Statement ss1 = null;
      String s = "";
      Resource r = null;

      StmtIterator locii = model.getModel().listStatements(human, loc, (RDFNode) null);
      if (locii.hasNext()) {
        ss1 = (Statement) locii.next();
        System.out.println(
            "initial statements:"
                + ss1.getSubject().getLocalName()
                + " "
                + ss1.getPredicate().getLocalName()
                + " "
                + ss1.getResource().getLocalName());
      }

      StmtIterator loci = infmodel.getDeductionsModel().listStatements(human, loc, (RDFNode) null);
      if (loci.hasNext()) {
        Statement ssl = (Statement) loci.next();
        RDFNode obj = (RDFNode) ssl.getObject();
        if (obj instanceof Resource) {
          r = (Resource) obj;
          s = r.getLocalName();
        } else {
          System.out.print("!!! Literal");
        }
        System.out.println(
            "Inferred: user: "******" changed location to room "
                + s);
      }

      if (r != null) ss1.changeObject(r);

      StmtIterator fi = model.getModel().listStatements(human, loc, (RDFNode) null);
      while (fi.hasNext()) {
        Statement ss = (Statement) fi.next();
        System.out.println(
            "After updating the ontology \n"
                + ss.getSubject().getLocalName()
                + " "
                + ss.getPredicate().getLocalName()
                + " "
                + ss.getResource().getLocalName());
      }
    }
  }
コード例 #2
0
  public void deduceAuthorization() {

    Property auth = model.getModel().getProperty(uri, "isAuthenticatedBy");

    for (Resource human : humans) {

      Resource r = null;

      StmtIterator ii = model.getModel().listStatements(human, auth, (RDFNode) null);
      if (ii.hasNext()) {
        Statement ss1 = (Statement) ii.next();
        System.out.println(
            "initial statements:"
                + ss1.getSubject().getLocalName()
                + " "
                + ss1.getPredicate().getLocalName()
                + " "
                + ss1.getResource().getLocalName());

        StmtIterator iii =
            infmodel.getDeductionsModel().listStatements(human, auth, (RDFNode) null);
        if (iii.hasNext()) {
          Statement ss2 = (Statement) iii.next();
          System.out.println(
              "inferred: "
                  + ss2.getSubject().getLocalName()
                  + " "
                  + ss2.getPredicate().getLocalName()
                  + " "
                  + ss2.getResource().getLocalName());
          r = ss2.getResource();
        }

        if (r != null) ss1.changeObject(r);

        StmtIterator fi = model.getModel().listStatements(human, auth, (RDFNode) null);
        while (fi.hasNext()) {
          Statement ss = (Statement) fi.next();
          System.out.println(
              "After updating the ontology \n"
                  + ss.getSubject().getLocalName()
                  + " "
                  + ss.getPredicate().getLocalName()
                  + " "
                  + ss.getResource().getLocalName());
        }
      }
    }
  }
コード例 #3
0
  /**
   * Deletes the given process element from the rnrm:isFlowingTo linked list without breaking the
   * path. The element is also removed from the process.
   *
   * @param processElement
   */
  public void deleteFromPath(Resource processElement) {
    Model model = process.getModel();
    List<Statement> incoming = process.getIncomingEdges(processElement);
    List<Statement> outgoing = process.getOutgoingEdges(processElement);
    if (incoming.size() > 1 || outgoing.size() > 1) {
      throw new IllegalArgumentException(
          "Error deleting element. Elements with more than 1 outgoing or incoming links cannot be deleted.");
    }
    Resource before = null;
    Resource after = null;
    Statement stmt = null;
    if (incoming.size() > 0) {
      stmt = incoming.get(0);
      before = stmt.getSubject();
      model.remove(stmt);
    }
    if (outgoing.size() > 0) {
      stmt = outgoing.get(0);
      after = stmt.getResource();
      model.remove(stmt);
    }
    if (null != before && null != after) {
      model.add(before, RNRM.isFlowingTo, after);
    }

    // now disassociate this element from the process
    removeFromProcess(processElement);
  }
コード例 #4
0
 @Override
 public void processInput(Resource input, Resource output) {
   final Model model = ModelFactory.createDefaultModel();
   final List<Statement> tipURIs = input.listProperties(Vocab.has).toList();
   final SublistIterator<Statement> tipsIterator =
       new SublistIterator<Statement>(tipURIs, QUERY_CHUNK_SIZE);
   while (tipsIterator.hasNext()) {
     final List<String> ancestorQueryBlocks = new ArrayList<String>();
     final List<String> parentQueryBlocks = new ArrayList<String>();
     final List<Statement> tips = tipsIterator.next();
     for (Statement statement : tips) {
       final String tipURI =
           statement.getResource().getProperty(DC.subject).getResource().getURI();
       ancestorQueryBlocks.add(String.format(ancestorQueryBody, tipURI));
       parentQueryBlocks.add(String.format(parentQueryBody, tipURI));
     }
     final String ancestorQuery =
         String.format(ancestorQueryHead, StringUtils.join(ancestorQueryBlocks, " UNION "));
     final String parentQuery =
         String.format(parentQueryHead, StringUtils.join(parentQueryBlocks, ", "));
     QueryEngineHTTP ancestorQE = new QueryEngineHTTP(endpoint, ancestorQuery);
     QueryEngineHTTP parentQE = new QueryEngineHTTP(endpoint, parentQuery);
     ancestorQE.execConstruct(model);
     ancestorQE.close();
     parentQE.execConstruct(model);
     parentQE.close();
   }
   this.processNode(Vocab.TaxonomyRoot, model, output.getModel(), null, output);
   output.getModel().setNsPrefix("cdao", "http://www.evolutionaryontology.org/cdao/1.0/cdao.owl#");
 }
コード例 #5
0
  public Configuration(Model configurationModel) {
    model = configurationModel;
    StmtIterator it = model.listStatements(null, RDF.type, CONF.Configuration);
    if (!it.hasNext()) {
      throw new IllegalArgumentException("No conf:Configuration found in configuration model");
    }
    config = it.nextStatement().getSubject();

    datasets = new ArrayList();
    it = model.listStatements(config, CONF.dataset, (RDFNode) null);
    while (it.hasNext()) {
      datasets.add(new Dataset(it.nextStatement().getResource()));
    }
    labelProperties = new ArrayList();
    it = model.listStatements(config, CONF.labelProperty, (RDFNode) null);
    while (it.hasNext()) {
      labelProperties.add(it.nextStatement().getObject().as(Property.class));
    }
    if (labelProperties.isEmpty()) {
      labelProperties.add(RDFS.label);
      labelProperties.add(DC.title);
      labelProperties.add(model.createProperty("http://xmlns.com/foaf/0.1/name"));
    }
    commentProperties = new ArrayList();
    it = model.listStatements(config, CONF.commentProperty, (RDFNode) null);
    while (it.hasNext()) {
      commentProperties.add(it.nextStatement().getObject().as(Property.class));
    }
    if (commentProperties.isEmpty()) {
      commentProperties.add(RDFS.comment);
      commentProperties.add(DC.description);
    }
    imageProperties = new ArrayList();
    it = model.listStatements(config, CONF.imageProperty, (RDFNode) null);
    while (it.hasNext()) {
      imageProperties.add(it.nextStatement().getObject().as(Property.class));
    }
    if (imageProperties.isEmpty()) {
      imageProperties.add(model.createProperty("http://xmlns.com/foaf/0.1/depiction"));
    }

    prefixes = new PrefixMappingImpl();
    if (config.hasProperty(CONF.usePrefixesFrom)) {
      it = config.listProperties(CONF.usePrefixesFrom);
      while (it.hasNext()) {
        Statement stmt = it.nextStatement();
        prefixes.setNsPrefixes(FileManager.get().loadModel(stmt.getResource().getURI()));
      }
    } else {
      prefixes.setNsPrefixes(model);
    }
    if (prefixes.getNsURIPrefix(CONF.NS) != null) {
      prefixes.removeNsPrefix(prefixes.getNsURIPrefix(CONF.NS));
    }
  }
コード例 #6
0
 /**
  * Inserts the given {@link Resource} into the rnrm:isFlowingTo path after the specified {@link
  * Resource}
  *
  * @param toInsert Process element to insert
  * @param after Process element to insert after
  */
 public void insertAfter(Resource toInsert, Resource after) {
   List<Statement> outgoing = process.getOutgoingEdges(after);
   Model model = process.getModel();
   if (outgoing.size() > 1) {
     throw new IllegalArgumentException("Cannot insert after nodes with multiple outgoing links.");
   }
   model.add(after, RNRM.isFlowingTo, toInsert);
   if (outgoing.size() > 0) {
     Statement stmt = outgoing.get(0);
     Resource next = stmt.getResource();
     model.remove(stmt);
     model.add(toInsert, RNRM.isFlowingTo, next);
   }
 }
コード例 #7
0
  public void print(PrintContext p) {

    Variable asVar = getAs();
    if (asVar != null) {
      p.print("(");
    }

    Resource aggType = getResource(RDF.type);
    String aggName = Aggregations.getName(aggType);
    p.printKeyword(aggName);
    p.print("(");

    if (isDistinct()) {
      p.print("DISTINCT ");
    }

    Statement exprS = getProperty(SP.expression);
    if (exprS != null && exprS.getObject().isResource()) {
      Resource r = exprS.getResource();
      RDFNode expr = SPINFactory.asExpression(r);
      if (expr instanceof Printable) {
        ((Printable) expr).print(p);
      } else {
        p.printURIResource(r);
      }
    } else {
      p.print("*");
    }
    String separator = getString(SP.separator);
    if (separator != null) {
      p.print("; ");
      p.printKeyword("SEPARATOR");
      p.print("='" + StrUtils.escapeString(separator) + "'");
    }
    if (asVar != null) {
      p.print(") ");
      p.printKeyword("AS");
      p.print(" ");
      p.print(asVar.toString());
    }
    p.print(")");
  }
コード例 #8
0
ファイル: SPINImports.java プロジェクト: simonstey/shacl-1
  /**
   * Checks if spin:imports have been declared and adds them to a union model. Will also register
   * any SPIN modules defined in those imports that haven't been loaded before.
   *
   * @param model the base Model to operate on
   * @return either model or the union of model and its spin:imports @
   */
  public Model getImportsModel(Model model) throws IOException {
    Set<String> uris = new HashSet<String>();
    StmtIterator it = model.listStatements(null, SPIN.imports, (RDFNode) null);
    while (it.hasNext()) {
      Statement s = it.nextStatement();
      if (s.getObject().isURIResource()) {
        uris.add(s.getResource().getURI());
      }
    }
    if (uris.isEmpty()) {
      return model;
    } else {
      Graph baseGraph = model.getGraph();

      MultiUnion union = JenaUtil.createMultiUnion();
      union.addGraph(baseGraph);
      union.setBaseGraph(baseGraph);

      boolean needsRegistration = false;
      for (String uri : uris) {
        Graph graph = getImportedGraph(uri);
        if (graph != null) {
          union.addGraph(graph);
          if (!registeredURIs.contains(uri)) {
            registeredURIs.add(uri);
            needsRegistration = true;
          }
        }
      }

      // Ensure that SP, SPIN and SPL are present
      ensureImported(union, SP.BASE_URI, SP.getModel());
      ensureImported(union, SPL.BASE_URI, SPL.getModel());
      ensureImported(union, SPIN.BASE_URI, SPIN.getModel());

      Model unionModel = ModelFactory.createModelForGraph(union);
      if (needsRegistration) {
        SPINModuleRegistry.get().registerAll(unionModel, null);
      }
      return unionModel;
    }
  }
コード例 #9
0
ファイル: Spec.java プロジェクト: mmmmmrob/Vertere
 public int[] getSourceColumns(Resource resource) {
   if (_model.contains(resource, Vertere.source_column)) {
     Statement sourceColumn = _model.getProperty(resource, Vertere.source_column);
     return new int[] {sourceColumn.getInt()};
   } else if (_model.contains(resource, Vertere.source_columns)) {
     Statement sourceColumns = _model.getProperty(resource, Vertere.source_columns);
     Resource listResource = sourceColumns.getResource();
     RDFList list = listResource.as(RDFList.class);
     List<RDFNode> javalist = list.asJavaList();
     int[] sourceColumnNumbers = new int[javalist.size()];
     for (int i = 0; i < javalist.size(); i++) {
       RDFNode node = javalist.get(i);
       Literal value = node.asLiteral();
       sourceColumnNumbers[i] = value.getInt();
     }
     return sourceColumnNumbers;
   } else {
     return new int[0];
   }
 }
コード例 #10
0
  public Organization getOrganization(String organizationUri) {
    long startTime = System.currentTimeMillis();
    Organization org = new Organization();
    // ResIterator iter =
    // ontModel.listSubjectsWithProperty(ontModel.getProperty(ORGID),organizationId);

    Resource r = ontModel.getResource(organizationUri);
    if (r != null) {
      // Resource r = iter.nextResource();

      org.setUri(organizationUri);

      StmtIterator typeStmtIter = r.listProperties(ontModel.getProperty(TYPE));
      Resource organizationalCollaboration = ontModel.getResource(ORGANIZATIONALCOLLABORATION);
      while (typeStmtIter.hasNext()) {
        Statement typeStmt = typeStmtIter.next();
        if (typeStmt != null) {
          String organizationClass = typeStmt.getResource().getURI();
          if (organizationClass.equalsIgnoreCase(organizationalCollaboration.getURI()))
            org.setOrganizationalCollaboration(true);
        }
      }

      Statement id = r.getProperty(ontModel.getProperty(ORGID));
      if (id != null) org.setId(id.getString());

      Statement title = r.getProperty(ontModel.getProperty(TITLE));
      if (title != null) org.setTitle(title.getString());

      Statement preflabel = r.getProperty(ontModel.getProperty(PREFLABEL));
      if (preflabel != null) org.setPrefLabel(preflabel.getString());

      Statement purpose = r.getProperty(ontModel.getProperty(PURPOSE));
      if (purpose != null) org.setPurpose(purpose.getString());

      Statement description = r.getProperty(ontModel.getProperty(DESCRIPTION));
      if (description != null) org.setDescription(description.getString());

      Statement classification = r.getProperty(ontModel.getProperty(CLASSIFICATION));
      if (classification != null) {
        Resource classificationObj = classification.getObject().asResource();
        Statement preflabelClassification =
            classificationObj.getProperty(ontModel.getProperty(PREFLABEL));
        org.setClassification(preflabelClassification.getString());
      }

      // depiction
      Statement depiction = r.getProperty(ontModel.getProperty(DEPICTION));
      if (depiction != null) {
        Statement depicts = depiction.getProperty(ontModel.getProperty(DEPICTS));
        if (depicts != null) {
          Resource imgRes = depicts.getResource();
          if (imgRes != null) org.setDepicts(depicts.getResource().getURI());
        }
      }

      // memberOrganizations
      StmtIterator memberOrgIter = r.listProperties(ontModel.getProperty(HASMEMBERORGANIZATION));
      ArrayList<String> hasMemberOrganization = new ArrayList<String>();
      while (memberOrgIter.hasNext()) {
        Statement stmtMembOrg = memberOrgIter.next();
        Resource memberOrgRes = stmtMembOrg.getResource();
        if (memberOrgRes != null) {
          //			    	Statement memberOrgStmt = memberOrgRes.getProperty(ontModel.getProperty(ORGID));
          //			    	if (memberOrgStmt!=null)
          //			    	    hasMemberOrganization.add(memberOrgStmt.getString());
          hasMemberOrganization.add(memberOrgRes.getURI());
        }
      }
      org.setHasMemberOrganization(hasMemberOrganization);

      // hasProject
      StmtIterator projectOrgIter = r.listProperties(ontModel.getProperty(HASPROJECT));
      ArrayList<String> hasProject = new ArrayList<String>();
      while (projectOrgIter.hasNext()) {
        Statement stmtProjOrg = projectOrgIter.next();
        Resource projRes = stmtProjOrg.getResource();
        if (projRes != null) {
          hasProject.add(projRes.getURI());
        }
      }
      org.setHasProject(hasProject);

      // hasProduct
      StmtIterator productOrgIter = r.listProperties(ontModel.getProperty(HASPRODUCT));
      ArrayList<String> hasProduct = new ArrayList<String>();
      while (productOrgIter.hasNext()) {
        Statement stmtProdOrg = productOrgIter.next();
        Resource prodRes = stmtProdOrg.getResource();
        if (prodRes != null) {
          hasProduct.add(prodRes.getURI());
        }
      }
      org.setHasProduct(hasProduct);

      // hasMember
      StmtIterator hasMemberIter = r.listProperties(ontModel.getProperty(HASMEMBER));
      ArrayList<String> hasMember = new ArrayList<String>();
      while (hasMemberIter.hasNext()) {
        Statement stmtHasMember = hasMemberIter.next();
        Resource personRes = stmtHasMember.getResource();
        if (personRes != null) {
          //			    	Statement personIdStmt = personRes.getProperty(ontModel.getProperty(PERSONID));
          //			    	if (personIdStmt!=null)
          //			    	    hasMember.add(personIdStmt.getString());
          hasMember.add(personRes.getURI());
        }
      }
      org.setHasMember(hasMember);

      // organizationPosition
      StmtIterator orgPositionIter = r.listProperties(ontModel.getProperty(ORGPOSITION));
      ArrayList<String> position = new ArrayList<String>();
      while (orgPositionIter.hasNext()) {
        Statement stmtOrgPosition = orgPositionIter.next();
        Resource positionRes = stmtOrgPosition.getResource();
        if (positionRes != null) {
          position.add(positionRes.getURI());
        }
      }
      org.setPosition(position);

      // organizationRole
      StmtIterator orgRoleIter = r.listProperties(ontModel.getProperty(ORGROLE));
      ArrayList<String> role = new ArrayList<String>();
      while (orgRoleIter.hasNext()) {
        Statement stmtOrgRole = orgRoleIter.next();
        Resource roleRes = stmtOrgRole.getResource();
        if (roleRes != null) {
          role.add(roleRes.getURI());
        }
      }
      org.setRole(role);

      // membership
      StmtIterator membershipIter = r.listProperties(ontModel.getProperty(MEMBERSHIP));
      ArrayList<String> membership = new ArrayList<String>();
      while (membershipIter.hasNext()) {
        Statement stmtMembership = membershipIter.next();
        Resource membershipRes = stmtMembership.getResource();
        if (membershipRes != null) {
          membership.add(membershipRes.getURI());
        }
      }
      org.setMembership(membership);

      long stopTime = System.currentTimeMillis();
      long elapsedTime = stopTime - startTime;
      LOGGER.debug("- Load the organization, elapsed time (ms)..: {}", elapsedTime);
    }
    return org;
  }