Пример #1
0
  /**
   * Map a Jena graph node to a Mulgara value.
   *
   * @param x The Jena Node to convert.
   * @param session A session to use for blank node persistence.
   * @return A Mulgara Value.
   * @throws URISyntaxException When creating a URIReference that refers to an invalid URI.
   */
  static Value n2v(Node x, Session session) throws URISyntaxException {
    if (x.isURI()) return new URIReferenceImpl(new URI(x.getURI()));

    if (x.isLiteral()) {
      // The return types are Mulgara LiteralImpl
      if (x.getLiteralDatatypeURI() != null) {
        return new LiteralImpl(x.getLiteralLexicalForm(), new URI(x.getLiteralDatatypeURI()));
      }
      if (x.getLiteralLanguage() != null) {
        return new LiteralImpl(x.getLiteralLexicalForm(), x.getLiteralLanguage());
      }
      return new LiteralImpl(x.getLiteralLexicalForm());
    }

    if (x.isBlank()) {
      // is this a previously encountered Jena-allocated node?
      Value bn = nodesToValues.get(x);
      if (bn != null) return bn;

      // May be a Mulgara-allocated bNode (and so we we have seen before)
      String blankLabel = x.getBlankNodeLabel();
      if (blankLabel.startsWith(LABEL)) {
        long id = Long.parseLong(blankLabel.substring(LABEL_LEN));
        return new BlankNodeImpl(BlankNodeImpl.counterToNode(id));
      }

      // It's not - it's a Jena one.
      if (skolemizedBlankNodes) {
        String skol = bNodeScheme + x.getBlankNodeLabel();
        return new URIReferenceImpl(new URI(skol));
      }

      // Not a Mulgara-allocated bNode.  Create a new mapping.
      BlankNodeImpl v = new BlankNodeImpl();

      nodesToValues.put(x, v);
      valuesToNodes.put(v.getNodeId(), x);
      return v;
    }

    throw new RuntimeException("Can't convert from Jena node : " + x);
  }
Пример #2
0
 protected static String sparqlNode(Node node, String varName) {
   if (node == null || node.isVariable()) {
     return varName;
   } else if (node.isBlank()) {
     return "<fake:blank>"; // or throw exception?
   } else if (node.isURI()) {
     StringBuffer uriBuff = new StringBuffer();
     return uriBuff.append("<").append(node.getURI()).append(">").toString();
   } else if (node.isLiteral()) {
     StringBuffer literalBuff = new StringBuffer();
     literalBuff.append("\"");
     pyString(literalBuff, node.getLiteralLexicalForm());
     literalBuff.append("\"");
     if (node.getLiteralDatatypeURI() != null) {
       literalBuff.append("^^<").append(node.getLiteralDatatypeURI()).append(">");
     } else if (node.getLiteralLanguage() != null && node.getLiteralLanguage().length() > 0) {
       literalBuff.append("@").append(node.getLiteralLanguage());
     }
     return literalBuff.toString();
   } else {
     return varName;
   }
 }
Пример #3
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);
  }
Пример #4
0
 /**
  * Gets NXRelations node instance given Jena node.
  *
  * @param jenaNodeInst
  * @return NXRelations node instance
  */
 private Node getNXRelationsNode(com.hp.hpl.jena.graph.Node jenaNodeInst) {
   if (jenaNodeInst == null) {
     return null;
   }
   Node nuxNode = null;
   if (jenaNodeInst.isBlank()) {
     AnonId anonId = jenaNodeInst.getBlankNodeId();
     String id = anonId.getLabelString();
     nuxNode = NodeFactory.createBlank(id);
   } else if (jenaNodeInst.isLiteral()) {
     LiteralLabel label = jenaNodeInst.getLiteral();
     String value = label.getLexicalForm();
     String type = jenaNodeInst.getLiteralDatatypeURI();
     String language = jenaNodeInst.getLiteralLanguage();
     if (type != "") {
       nuxNode = NodeFactory.createTypedLiteral(value, type);
     } else if (language != "") {
       nuxNode = NodeFactory.createLiteral(value, language);
     } else {
       nuxNode = NodeFactory.createLiteral(value);
     }
   } else if (jenaNodeInst.isURI()) {
     String uri = jenaNodeInst.getURI();
     // try to find corresponding prefix
     // TODO AT: maybe take namespaces from relation service?
     for (Map.Entry<String, String> ns : namespaces.entrySet()) {
       String base = ns.getValue();
       if (uri.startsWith(base)) {
         String localName = uri.substring(base.length());
         nuxNode = NodeFactory.createQNameResource(base, localName);
         break;
       }
     }
     if (nuxNode == null) {
       // default to resource
       nuxNode = NodeFactory.createResource(uri);
     }
   } else {
     throw new IllegalArgumentException(
         "Cannot translate non concrete Jena node into NXRelations node");
   }
   return nuxNode;
 }
Пример #5
0
  private void convertNotEquals(E_NotEquals expr) {
    logger.debug("convertNotEquals " + expr.toString());

    expr.getArg1().visit(this);
    expr.getArg2().visit(this);
    Expression e2 = expression.pop();
    Expression e1 = expression.pop();

    // TODO Expression.FALSE and Expression.TRUE are not constants
    if (e1.equals(Expression.FALSE)) e1 = CONSTANT_FALSE;
    else if (e1.equals(Expression.TRUE)) e1 = CONSTANT_TRUE;
    if (e2.equals(Expression.FALSE)) e2 = CONSTANT_FALSE;
    else if (e2.equals(Expression.TRUE)) e2 = CONSTANT_TRUE;

    if (e1 instanceof AttributeExprEx && e2 instanceof Constant
        || e2 instanceof AttributeExprEx && e1 instanceof Constant) {
      AttributeExprEx variable;
      ConstantEx constant;

      if (e1 instanceof AttributeExprEx) {
        variable = (AttributeExprEx) e1;
        constant = (ConstantEx) e2;
      } else {
        variable = (AttributeExprEx) e2;
        constant = (ConstantEx) e1;
      }

      logger.debug("isNotEqual(" + variable + ", " + constant + ")");

      NodeMaker nm = variable.getNodeMaker();

      if (nm instanceof TypedNodeMaker) {
        ValueMaker vm = ((TypedNodeMaker) nm).valueMaker();
        Node node = constant.getNode();
        logger.debug("checking " + node + " with " + nm);
        if (XSD.isNumeric(node)) {
          DetermineNodeType filter = new DetermineNodeType();
          nm.describeSelf(filter);
          RDFDatatype datatype = filter.getDatatype();
          if (datatype != null && XSD.isNumeric(datatype)) {
            RDFDatatype numericType = XSD.getNumericType(datatype, node.getLiteralDatatype());
            nm = cast(nm, numericType);
            node = XSD.cast(node, numericType);
          }
        }
        boolean empty = nm.selectNode(node, RelationalOperators.DUMMY).equals(NodeMaker.EMPTY);
        logger.debug("result " + new Boolean(empty));
        if (!empty) {
          if (node.isURI()) expression.push(new Negation(vm.valueExpression(node.getURI())));
          else if (node.isLiteral()) {
            if (XSD.isSupported(node.getLiteralDatatype()))
              expression.push(new Negation(vm.valueExpression(constant.value())));
            else // type = boolean or an unknown type
            conversionFailed("cannot compare values of type " + node.getLiteralDatatypeURI(), expr);
          } else conversionFailed(expr); // TODO blank nodes?
          return;
        } else {
          expression.push(Expression.TRUE);
          return;
        }
      }
    } else if (e1 instanceof ConstantEx && e2 instanceof ConstantEx) {
      logger.debug("isNotEqual(" + e1 + ", " + e2 + ")");
      Node c1 = ((ConstantEx) e1).getNode();
      Node c2 = ((ConstantEx) e2).getNode();
      boolean equals;
      if (XSD.isNumeric(c1) && XSD.isNumeric(c2)) {
        RDFDatatype datatype = XSD.getNumericType(c1.getLiteralDatatype(), c2.getLiteralDatatype());
        equals = XSD.cast(c1, datatype).equals(XSD.cast(c2, datatype));
      } else if (isSimpleLiteral(c1) && isSimpleLiteral(c2)) {
        equals = c1.getLiteralValue().equals(c2.getLiteralValue());
      } else if (XSD.isString(c1) && XSD.isString(c2)) {
        equals = c1.getLiteralValue().equals(c2.getLiteralValue());
      } else {
        try {
          equals = NodeFunctions.rdfTermEquals(c1, c2);
        } catch (ExprEvalException e) {
          equals = false;
        }
      }
      logger.debug("constants equal? " + new Boolean(equals));
      expression.push(equals ? Expression.FALSE : Expression.TRUE);
      return;
    } else if (e1 instanceof AttributeExprEx && e2 instanceof AttributeExprEx) {
      logger.debug("isNotEqual(" + e1 + ", " + e2 + ")");
      AttributeExprEx variable1 = (AttributeExprEx) e1;
      AttributeExprEx variable2 = (AttributeExprEx) e2;

      NodeMaker nm1 = variable1.getNodeMaker();
      NodeMaker nm2 = variable2.getNodeMaker();

      DetermineNodeType filter1 = new DetermineNodeType();
      nm1.describeSelf(filter1);
      RDFDatatype datatype1 = filter1.getDatatype();

      DetermineNodeType filter2 = new DetermineNodeType();
      nm2.describeSelf(filter2);
      RDFDatatype datatype2 = filter2.getDatatype();

      if (datatype1 != null
          && XSD.isNumeric(datatype1)
          && datatype2 != null
          && XSD.isNumeric(datatype2)) {
        RDFDatatype numericType = XSD.getNumericType(filter1.getDatatype(), filter2.getDatatype());
        nm1 = cast(nm1, numericType);
        nm2 = cast(nm2, numericType);
      }

      NodeSetConstraintBuilder nodeSet = new NodeSetConstraintBuilder();
      nm1.describeSelf(nodeSet);
      nm2.describeSelf(nodeSet);

      if (nodeSet.isEmpty()) {
        logger.debug("nodes " + nm1 + " " + nm2 + " incompatible");
        expression.push(Expression.TRUE);
        return;
      }
    }

    expression.push(new Negation(Equality.create(e1, e2)));
  }