@Test
 public void theEconomizerDoesntSquashOtherNodes() {
   Economizer<Node> e = new CacheEconomizer<Node>();
   Node uriA = e.economize(Node.createURI("http://slashdot.org/1"));
   Node uriB = e.economize(Node.createURI("http://slashdot.org/2"));
   assertTrue(uriA != uriB);
 }
  @Test
  public void economizingTriplesEconomizesTheNodes() {
    Node s1a = Node.createURI("http://example.com/s1");
    Node s1b = Node.createURI("http://example.com/s1");
    Node p1a = Node.createURI("http://example.com/p1");
    Node p1b = Node.createURI("http://example.com/p1");
    Node o1a = Node.createLiteral("55", XSDDatatype.XSDint);
    Node o1b = Node.createLiteral("55", XSDDatatype.XSDint);

    assertTrue(o1a != o1b);

    Triple t1 = new Triple(s1a, p1a, o1a);
    Triple t2 = new Triple(s1b, p1b, o1b);
    assertTrue(t1 != t2);
    assertEquals(t1, t2);

    Economizer<Triple> e = new TripleEconomizer();

    Triple t3 = e.economize(t1);
    Triple t4 = e.economize(t2);

    assertTrue(t3.getSubject() == t4.getSubject());
    assertTrue(t3.getObject() == t4.getObject());
    assertTrue(t3.getPredicate() == t4.getPredicate());

    assertTrue(s1a.equals(t3.getSubject()));
    assertTrue(p1a.equals(t3.getPredicate()));
    assertTrue(o1a.equals(t3.getObject()));
  }
 @Test
 public void identicallyNamedNodesAreNotSameObject() {
   Node uriA = Node.createURI("http://slashdot.org/");
   Node uriB = Node.createURI("http://slashdot.org/");
   assertTrue(uriA.equals(uriB));
   assertTrue(uriA != uriB);
 }
 @Test
 public void theEconomizerChangesThat() {
   Economizer<Node> e = new CacheEconomizer<Node>();
   Node uriA = e.economize(Node.createURI("http://slashdot.org/"));
   Node uriB = e.economize(Node.createURI("http://slashdot.org/"));
   assertTrue(uriA == uriB);
 }
示例#5
0
/** @author Rowland Watkins ([email protected]) */
public class SWP_V {
  /** The namespace of the vocabulary as a string */
  public static final String NS = "http://www.w3.org/2004/03/trix/swp-verification";

  /**
   * The namespace of the vocabulary as a string
   *
   * @see #NS
   */
  public static String getURI() {
    return NS;
  }

  /** The namespace of the vocabulary as a resource */
  public static final Node NAMESPACE = Node.createURI(NS);

  public static final Node default_graph =
      Node.createURI("http://www.w3.org/2004/03/trix/swp-verification/verifiedSignatures");

  /** The object contains the status value of a signature for the subject graph. */
  public static final Node successful =
      Node.createURI("http://www.w3.org/2004/03/trix/swp-verification/successful");

  public static final Node notSuccessful =
      Node.createURI("http://www.w3.org/2004/03/trix/swp-verification/notSuccessful");
}
  public static Node iri(Node nv, String baseIRI) {
    if (nv.isURI()) return nv;

    if (nv.isBlank()) {
      // Skolemization of blank nodes to IRIs : Don't ask, just don't ask.
      String x = nv.getBlankNodeLabel();
      return Node.createURI("_:" + x);
    }

    if (nv.isLiteral() && nv.getLiteralDatatype() == null && nv.getLiteralLanguage().equals("")) {
      // Plain literal
      IRI iri = null;
      String iriStr = nv.getLiteralLexicalForm();

      // Level of checking?
      if (baseIRI != null) {
        IRI base = iriFactory.create(baseIRI);
        iri = base.create(iriStr);
      } else iri = iriFactory.create(iriStr);

      if (!iri.isAbsolute()) throw new ExprEvalException("Relative IRI string: " + iriStr);
      if (warningsForIRIs && iri.hasViolation(false)) {
        String msg = "unknown violation from IRI library";
        Iterator<Violation> iter = iri.violations(false);
        if (iter.hasNext()) {
          Violation viol = iter.next();
          msg = viol.getShortMessage();
        }
        Log.warn(NodeFunctions.class, "Bad IRI: " + msg + ": " + iri);
      }
      return Node.createURI(iri.toString());
    }
    throw new ExprEvalException("Can't make an IRI from " + nv);
  }
示例#7
0
  @Override
  public void newIndividual(String individualURI, String individualTypeURI, String graphURI)
      throws RDFServiceException {

    StringBuffer containsQuery = new StringBuffer("ASK { \n");
    if (graphURI != null) {
      containsQuery.append("  GRAPH <" + graphURI + "> { ");
    }
    containsQuery.append("<");
    containsQuery.append(individualURI);
    containsQuery.append("> ");
    containsQuery.append("?p ?o");
    if (graphURI != null) {
      containsQuery.append(" } \n");
    }
    containsQuery.append("\n}");

    if (sparqlAskQuery(containsQuery.toString())) {
      throw new RDFServiceException("individual already exists");
    } else {
      Triple triple =
          new Triple(
              Node.createURI(individualURI), RDF.type.asNode(), Node.createURI(individualTypeURI));
      // addTriple(triple, graphURI);
      ChangeSet cs = this.manufactureChangeSet();
      cs.addAddition(
          new ByteArrayInputStream(sparqlTriple(triple).getBytes()),
          ModelSerializationFormat.N3,
          graphURI);
      changeSetUpdate(cs);
    }
  }
示例#8
0
 @Override
 protected Node rename(Node uri) {
   if (uri.getNameSpace().equals(replaceNS))
     return Node.createURI(namespace + uri.getLocalName());
   if (uri.getURI().equals(replace))
     return Node.createURI(namespace.substring(0, namespace.length() - 1));
   return uri;
 }
  /*
   * See http://www.w3.org/TR/rdf-sparql-query paragraph 11.4.7
   *
   * "(DATATYPE) Returns the datatype IRI of typedLit; returns xsd:string if the parameter is a simple literal."
   *
   * @see http://www.w3.org/TR/rdf-sparql-query
   */
  private void convertDataType(E_Datatype expr) {
    logger.debug("convertDataType " + expr.toString());

    expr.getArg().visit(this);

    Expression arg = expression.pop();

    if (arg instanceof AttributeExprEx) {
      AttributeExprEx variable = (AttributeExprEx) arg;
      NodeMaker nm = variable.getNodeMaker();
      DetermineNodeType filter = new DetermineNodeType();
      nm.describeSelf(filter);
      if (!filter.isLimittedToLiterals()) {
        // type error, return false?
        logger.warn("type error: " + variable + " is not a literal, returning FALSE");
        expression.push(Expression.FALSE);
        return;
      }
      RDFDatatype datatype = filter.getDatatype();
      logger.debug("datatype " + datatype);

      Node node =
          Node.createURI((datatype != null) ? datatype.getURI() : XSDDatatype.XSDstring.getURI());

      ConstantEx constantEx = new ConstantEx(NodeValue.makeNode(node).asString(), node);
      logger.debug("pushing " + constantEx);
      expression.push(constantEx);
    } else if (arg instanceof ConstantEx) {
      ConstantEx constant = (ConstantEx) arg;
      Node node = constant.getNode();
      if (!node.isLiteral()) {
        // type error, return false?
        logger.warn("type error: " + node + " is not a literal, returning FALSE");
        expression.push(Expression.FALSE);
        return;
      }
      RDFDatatype datatype = node.getLiteralDatatype();
      logger.debug("datatype " + datatype);
      node =
          Node.createURI((datatype != null) ? datatype.getURI() : XSDDatatype.XSDstring.getURI());
      ConstantEx constantEx = new ConstantEx(NodeValue.makeNode(node).asString(), node);
      logger.debug("pushing " + constantEx);
      expression.push(constantEx);
    } else {
      conversionFailed(expr);
    }
  }
示例#10
0
 @Override
 public Iterator<Node> listGraphNodes() {
   List<Node> graphNodeList = new ArrayList<Node>();
   try {
     for (String graphURI : rdfService.getGraphURIs()) {
       graphNodeList.add(Node.createURI(graphURI));
     }
   } catch (RDFServiceException rdfse) {
     throw new RuntimeException(rdfse);
   }
   return graphNodeList.iterator();
 }
示例#11
0
  public static Node datatype(Node node) {
    if (!node.isLiteral()) {
      NodeValue.raise(new ExprTypeException("datatype: Not a literal: " + node));
      return null;
    }

    String s = node.getLiteralDatatypeURI();
    boolean plainLiteral = (s == null || s.equals(""));

    if (plainLiteral) {
      boolean simpleLiteral =
          (node.getLiteralLanguage() == null || node.getLiteralLanguage().equals(""));
      if (!simpleLiteral)
        NodeValue.raise(new ExprTypeException("datatype: Literal has language tag: " + node));
      return XSD.xstring.asNode();
    }
    return Node.createURI(s);
  }
示例#12
0
public class LarqAnnotationFilter extends AnnotationFilterBase implements AnnotationFilter {

  static final Node TEXTMATCH = Node.createURI("http://jena.hpl.hp.com/ARQ/property#textMatch");
  public final String searchTerm;

  public LarqAnnotationFilter(String searchParam) {
    if (searchParam.contains("\""))
      throw new IllegalArgumentException("Quote mark in search param");
    this.searchTerm = searchParam;
  }

  @Override
  public void augmentBlock(
      TripleCollector arg0, String annotationBodyVar, String annotationHeadVar) {
    // This assumes subject indexing
    arg0.addTriple(
        Triple.create(Var.alloc(annotationBodyVar), TEXTMATCH, Node.createLiteral(searchTerm)));
  }
}
示例#13
0
 /* (non-Javadoc)
  * @see com.hp.hpl.jena.sparql.function.FunctionBase2#exec(com.hp.hpl.jena.sparql.expr.NodeValue, com.hp.hpl.jena.sparql.expr.NodeValue)
  */
 @Override
 public NodeValue exec(NodeValue uri, NodeValue pattern) {
   if (uri.asNode().isVariable()) {
     return uri;
   }
   // Simulate the identity
   Node nuri = uri.asNode();
   String result = null;
   String regexPattern = pattern.asUnquotedString();
   try {
     result = getSameAs(nuri.getURI(), regexPattern);
     if (result == null) {
       Logger.getAnonymousLogger().log(Level.INFO, "No results found for uri:" + nuri);
       return NodeValue.makeNode(nuri);
     } else {
       return NodeValue.makeNode(Node.createURI(result));
     }
   } catch (Exception e) {
     return uri;
   }
 }
示例#14
0
  /**
   * Gets the Jena node for given NXRelations Node instance.
   *
   * @param nuxNode NXrelations Node instance
   * @return Jena node instance
   */
  private static com.hp.hpl.jena.graph.Node getJenaNode(Node nuxNode) {
    if (nuxNode == null) {
      return null;
    }

    com.hp.hpl.jena.graph.Node jenaNodeInst;
    if (nuxNode.isBlank()) {
      Blank blank = (Blank) nuxNode;
      String id = blank.getId();
      if (id == null) {
        jenaNodeInst = com.hp.hpl.jena.graph.Node.createAnon();
      } else {
        jenaNodeInst = com.hp.hpl.jena.graph.Node.createAnon(new AnonId(id));
      }
    } else if (nuxNode.isLiteral()) {
      Literal lit = (Literal) nuxNode;
      String value = lit.getValue();
      if (value == null) {
        throw new IllegalArgumentException(String.format("Invalid literal node %s", nuxNode));
      }
      String language = lit.getLanguage();
      String type = lit.getType();
      if (language != null) {
        jenaNodeInst = com.hp.hpl.jena.graph.Node.createLiteral(value, language, false);
      } else if (type != null) {
        jenaNodeInst =
            com.hp.hpl.jena.graph.Node.createLiteral(value, null, new BaseDatatype(type));
      } else {
        jenaNodeInst = com.hp.hpl.jena.graph.Node.createLiteral(value);
      }
    } else if (nuxNode.isResource()) {
      Resource resource = (Resource) nuxNode;
      String uri = resource.getUri();
      jenaNodeInst = com.hp.hpl.jena.graph.Node.createURI(uri);

    } else {
      throw new IllegalArgumentException(String.format("Invalid NXRelations node %s", nuxNode));
    }
    return jenaNodeInst;
  }
示例#15
0
  /**
   * Convert a Mulgara Value to a Jena graph node.
   *
   * @param obj The Mulgara value to convert.
   * @return A new Jena graph node.
   */
  static Node o2n(org.jrdf.graph.Node obj) {
    if (obj == null) return Node.ANY;

    // testing for org.jrdf.graph.URIReference
    if (obj.isURIReference()) {
      URIReference uRef = (URIReference) obj;
      if (skolemizedBlankNodes) {
        String x = uRef.getURI().toString();
        if (x.startsWith(bNodeScheme)) {
          x = x.substring(bNodeScheme.length());
          Node n = Node.createAnon(new AnonId(x));
          return n;
        }
      }
      return Node.createURI(uRef.getURI().toString());
    }

    // testing for org.jrdf.graph.Literal
    if (obj.isLiteral()) {
      Literal literal = (Literal) obj;
      if (literal.getDatatypeURI() != null) {
        RDFDatatype type =
            TypeMapper.getInstance().getSafeTypeByName(literal.getDatatypeURI().toString());
        return Node.createLiteral(literal.getLexicalForm(), null, type);
      }

      return Node.createLiteral(literal.getLexicalForm(), literal.getLanguage(), null);
    }

    if (obj.isBlankNode()) {
      BlankNodeImpl b = (BlankNodeImpl) obj;
      // check if this was a Jena-allocated node
      Node jenaNode = valuesToNodes.get(b);
      // if not known, then create a Jena node from the Mulgara ID
      return jenaNode != null ? jenaNode : Node.createAnon(new AnonId(b.getID()));
    }
    throw new RuntimeException("Can't convert to Jena Node : " + obj);
  }
 @Override
 public Node createURI(String uriStr, long line, long col) {
   return Node.createURI(uriStr);
 }