Ejemplo n.º 1
0
 /**
  * Answer the string described by the value of the unique optional <code>classProperty</code>
  * property of <code>root</code>, or null if there's no such property. The value may be a URI, in
  * which case it must be a <b>java:</b> URI with content the class name; or it may be a literal,
  * in which case its lexical form is its class name; otherwise, BOOM.
  */
 public static String getOptionalClassName(Resource root, Property classProperty) {
   RDFNode classNode = getUnique(root, classProperty);
   return classNode == null
       ? null
       : classNode.isLiteral()
           ? classNode.asNode().getLiteralLexicalForm()
           : classNode.isResource() ? mustBeJava(classNode.asNode().getURI()) : null;
 }
Ejemplo n.º 2
0
 private void writeNode(RDFNode node) throws IOException {
   Node n = node.asNode();
   if (n.isURI()) {
     write("      <uri>" + escape(n.getURI()) + "</uri>\n");
     return;
   }
   if (n.isBlank()) {
     write("      <id>" + escape(n.getBlankNodeId().toString()) + "</id>\n");
     return;
   }
   if (!n.isLiteral()) {
     throw new JenaException("Don't know how to serialize node " + n);
   }
   if (n.getLiteral().getDatatypeURI() != null) {
     write(
         "      <typedLiteral datatype=\""
             + escape(n.getLiteral().getDatatypeURI())
             + "\">"
             + escape(n.getLiteral().getLexicalForm())
             + "</typedLiteral>\n");
     return;
   }
   if (n.getLiteral().language() == null || "".equals(n.getLiteral().language())) {
     write("      <plainLiteral>" + escape(n.getLiteral().getLexicalForm()) + "</plainLiteral>\n");
     return;
   }
   write(
       "      <plainLiteral xml:lang=\""
           + n.getLiteral().language()
           + "\">"
           + escape(n.getLiteral().getLexicalForm())
           + "</plainLiteral>\n");
 }
Ejemplo n.º 3
0
  private void fillTree(
      Resource root,
      RDFResourceTree tree,
      Map<Resource, SortedSet<Statement>> resource2Statements,
      int currentDepth,
      int maxDepth) {
    currentDepth++;
    if (resource2Statements.containsKey(root)) {
      RDFResourceTree subTree;

      for (Statement st : resource2Statements.get(root)) {
        Node predicate = st.getPredicate().asNode();
        RDFNode object = st.getObject();

        if (object.isLiteral()) {
          subTree = new RDFResourceTree(nodeId++, object.asNode());
          tree.addChild(subTree, predicate);
        } else if (object.isURIResource()) {
          subTree = new RDFResourceTree(nodeId++, object.asNode());
          tree.addChild(subTree, predicate);
          //					System.out.println(root + "::" + object + "::" + (currentDepth < maxDepth));
          if (currentDepth < maxDepth) {
            if (currentDepth < maxDepth) {
              fillTree(object.asResource(), subTree, resource2Statements, currentDepth, maxDepth);
            }
          }
        } else if (object.isAnon()) {
          subTree = new RDFResourceTree(nodeId++);
          tree.addChild(subTree, predicate);
          if (currentDepth < maxDepth) {
            fillTree(object.asResource(), subTree, resource2Statements, currentDepth, maxDepth);
          }
        }
      }
    }
    currentDepth--;
  }
Ejemplo n.º 4
0
 @Override
 public QueryResult query(String queryString, String language, String baseURI) {
   Model graph = null;
   GraphConnection graphConnection = null;
   QueryResult res = null;
   QueryExecution qe = null;
   try {
     graphConnection = openGraph();
     graph = graphConnection.getGraph();
     graph.enterCriticalSection(Lock.READ);
     log.debug(String.format("Running query %s", queryString));
     // XXX AT: ignore language for now
     if (language != null && !language.equals("sparql")) {
       log.warn(String.format("Unknown language %s for query, using SPARQL", language));
     }
     Query query = QueryFactory.create(queryString);
     query.setBaseURI(baseURI);
     qe = QueryExecutionFactory.create(query, graph);
     res = new QueryResultImpl(0, new ArrayList<String>(), new ArrayList<Map<String, Node>>());
     ResultSet jenaResults = qe.execSelect();
     Integer count = 0;
     List<String> variableNames = jenaResults.getResultVars();
     List<Map<String, Node>> nuxResults = new ArrayList<Map<String, Node>>();
     while (jenaResults.hasNext()) {
       QuerySolution soln = jenaResults.nextSolution();
       Map<String, Node> nuxSol = new HashMap<String, Node>();
       for (String varName : variableNames) {
         RDFNode x = soln.get(varName);
         nuxSol.put(varName, getNXRelationsNode(x.asNode()));
       }
       nuxResults.add(nuxSol);
       count++;
     }
     res = new QueryResultImpl(count, variableNames, nuxResults);
   } finally {
     if (qe != null) {
       // Important - free up resources used running the query
       qe.close();
     }
     if (graph != null) {
       graph.leaveCriticalSection();
     }
     if (graphConnection != null) {
       graphConnection.close();
     }
   }
   return res;
 }
  protected boolean populateKeyValue(Text key, Triple value) {

    Resource subject;
    Property predicate = null;
    RDFNode object;

    inErr = false;

    skipWhiteSpace();
    if (in.eof()) {
      return false;
    }

    subject = readResource();
    if (inErr) return false;

    skipWhiteSpace();
    try {
      predicate = model.createProperty(readResource().getURI());
    } catch (Exception e1) {
      e1.printStackTrace();
      errorHandler.fatalError(e1);
    }
    if (inErr) return false;

    skipWhiteSpace();
    object = readNode();
    if (inErr) return false;

    skipWhiteSpace();
    if (badEOF()) return false;

    if (!expect(".")) return false;

    try {
      key.set(getCurrentFileName());
      value.setTriple(subject.asNode(), predicate.asNode(), object.asNode());
      recordCounter++;
      // System.out.println("Triple value:"+value);
      return true;
    } catch (Exception e2) {
      e2.printStackTrace();
      errorHandler.fatalError(e2);
    }

    return false;
  }
Ejemplo n.º 6
0
  private Node walkTree(
      Model model,
      Dataset oldDataset,
      Node node,
      Node predicate,
      Query query,
      QuerySolution initialBinding,
      Set<Node> reached) {
    QuerySolutionMap localBinding = new QuerySolutionMap();
    localBinding.addAll(initialBinding);
    localBinding.add("arg1", model.asRDFNode(node));
    Dataset dataset = new DatasetWithDifferentDefaultModel(model, oldDataset);
    QueryExecution qexec = ARQFactory.get().createQueryExecution(query, dataset, localBinding);
    ResultSet rs = qexec.execSelect();
    try {
      if (rs.hasNext()) {
        List<String> resultVars = rs.getResultVars();
        String varName = resultVars.get(0);
        RDFNode resultNode = rs.next().get(varName);
        if (resultNode != null) {
          return resultNode.asNode();
        }
      }
    } finally {
      qexec.close();
    }

    // Recurse into parents
    ExtendedIterator<Triple> it = createIterator(model.getGraph(), node, predicate);
    try {
      while (it.hasNext()) {
        Node next = getNext(it.next());
        if ((next.isBlank() || next.isURI()) && !reached.contains(next)) {
          reached.add(next);
          Node nextResult =
              walkTree(model, oldDataset, next, predicate, query, initialBinding, reached);
          if (nextResult != null) {
            return nextResult;
          }
        }
      }
    } finally {
      it.close();
    }

    return null;
  }
Ejemplo n.º 7
0
 protected static String nice(RDFNode n) {
   return nice(n.asNode());
 }
Ejemplo n.º 8
0
  public void doctorModel(Model report, Model m, Model config) {
    this.input = m;
    this.repair = report;
    this.output = this.input;
    this.config = config;

    ResIterator it = repair.listSubjectsWithProperty(EYE.repairType, EYE.replaceNamespace);
    while (it.hasNext()) {
      /*
       * We are able to make one or more of the following assumptions;
       * (1)	o The Prefix is probably desired                                       \\
       * (3)	o The URI is likely misspelt                                          ===>  Work these with the precedence specified
       * (2)	o The URI is right, but the user mistakenly used a 'reserved' prefix   //
       */
      Resource curr = it.nextResource();
      String prefix =
          repair
              .listObjectsOfProperty(curr, EYE.onPrefix)
              .nextNode()
              .asNode()
              .getLiteralLexicalForm();
      String currUri = output.getNsPrefixURI(prefix);
      String configsUri = getConfigsUriFromPrefix(prefix);
      String configsPre = null;
      if (currUri != null) configsPre = getConfigsPrefixFromUri(currUri);
      if (configsUri != null) // Change the URI to a preferred one
      changeNsUri(currUri, configsUri);
      else if (configsPre != null) // Use that URI, but set the prefix according to config
      changeNsPrefix(prefix, configsPre);
      else // Use the 'expected' URI
      output.setNsPrefix(
            prefix,
            repair
                .listObjectsOfProperty(curr, EYE.expected)
                .nextNode()
                .asNode()
                .getLiteralLexicalForm());
    }

    it = repair.listSubjectsWithProperty(EYE.repairType, EYE.removeDuplicatePrefixes);
    while (it.hasNext()) {
      Resource curr = it.nextResource();
      NodeIterator it2 = repair.listObjectsOfProperty(curr, EYE.onPrefix);
      String uri =
          repair
              .listObjectsOfProperty(curr, EYE.multiplePrefixesForNamespace)
              .nextNode()
              .asNode()
              .getLiteralLexicalForm();
      if (it2.hasNext()) {
        boolean oneKept = false;
        while (it2.hasNext()) {
          RDFNode curra = it2.nextNode();
          if (m.getNsPrefixURI(curra.asNode().getLiteralLexicalForm()) != null)
            if (m.getNsPrefixURI(curra.asNode().getLiteralLexicalForm())
                .equals(uri)) // The prefix is still used in the model
            {
              if (oneKept) m.removeNsPrefix(curra.asNode().getLiteralLexicalForm());
              else oneKept = true;
            }
        }
      }
    }
  }
Ejemplo n.º 9
0
 /* (non-Javadoc)
  * @see com.hp.hpl.jena.graph.FrontsNode#asNode()
  */
 @Override
 public com.hp.hpl.jena.graph.Node asNode() {
   return rdfNode.asNode();
 }