@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()));
  }
 ReificationStatementMask(Triple t) {
   mask = HasNada;
   Node p = t.getPredicate();
   if (p != null) {
     if (p.equals(RDF.Nodes.subject)) mask = HasSubj;
     else if (p.equals(RDF.Nodes.predicate)) mask = HasPred;
     else if (p.equals(RDF.Nodes.object)) mask = HasObj;
     else if (p.equals(RDF.Nodes.type)) {
       Node o = t.getObject();
       if (o.equals(RDF.Nodes.Statement)) mask = HasType;
     }
   }
 }
 @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);
 }
Пример #4
0
  /**
   * This method is invoked when the builtin is called in a rule body.
   *
   * @param args the array of argument values for the builtin, this is an array of Nodes, some of
   *     which may be Node_RuleVariables.
   * @param length the length of the argument list, may be less than the length of the args array
   *     for some rule engines
   * @param context an execution context giving access to other relevant data
   * @return return true if the buildin predicate is deemed to have succeeded in the current
   *     environment
   */
  public boolean bodyCall(Node[] args, int length, RuleContext context) {
    if (length != 3) {
      throw new BuiltinException(
          this, context, "builtin " + getName() + " requires 3 arguments but saw " + length);
    }
    Node obj = getArg(2, args, context);
    Node subj = getArg(0, args, context);
    // Allow variables in subject position to correspond to a wild card
    if (subj.isVariable()) {
      subj = null;
    }

    // Allow variables in the predicate position to correspond to a wild card
    Node pred = getArg(1, args, context);
    if (pred.isVariable()) {
      pred = null;
    }
    boolean bContainsObj = context.contains(subj, pred, obj);
    if (!bContainsObj) {
      return false;
    }
    // does it contain anything else?
    ClosableIterator<Triple> citr = context.find((Node) null, pred, obj);
    boolean otherSubject = false;
    while (citr.hasNext()) {
      Object o = citr.next();
      Node tmpSubj = ((Triple) o).getSubject();
      if (!tmpSubj.equals(subj)) {
        otherSubject = true;
        break;
      }
    }
    citr.close();
    return !otherSubject;
  }
Пример #5
0
 /**
  * this nodeToElement is pretty much identical to that of
  * graph.query.patternstagecompiler.compile.
  */
 private Element nodeToElement(Node X, Mapping map) {
   if (X.equals(Query.ANY)) return Element.ANY;
   if (X.isVariable()) {
     if (map.hasBound(X)) return new Bound(map.indexOf(X));
     else {
       freeVarCnt++;
       return new Free(X);
     }
   }
   return new Fixed(X);
 }
Пример #6
0
  public static boolean sameTerm(Node n1, Node n2) {
    if (n1.equals(n2)) return true;
    if (n1.isLiteral() && n2.isLiteral()) {
      // But language tags are case insensitive.
      String lang1 = n1.getLiteralLanguage();
      String lang2 = n2.getLiteralLanguage();

      if (!lang1.equals("") && lang1.equalsIgnoreCase(lang2)) {
        // Two language tags, equal by case insensitivity.
        boolean b = n1.getLiteralLexicalForm().equals(n2.getLiteralLexicalForm());
        if (b) return true;
      }
    }
    return false;
  }
Пример #7
0
  // Exact as defined by SPARQL spec.
  public static boolean rdfTermEquals(Node n1, Node n2) {
    if (n1.equals(n2)) return true;

    if (n1.isLiteral() && n2.isLiteral()) {
      // Two literals, may be sameTerm by language tag case insensitivity.
      String lang1 = n1.getLiteralLanguage();
      String lang2 = n2.getLiteralLanguage();

      if (!lang1.equals("") && lang1.equalsIgnoreCase(lang2)) {
        // Two language tags, equal by case insensitivity.
        boolean b = n1.getLiteralLexicalForm().equals(n2.getLiteralLexicalForm());
        if (b) return true;
      }
      // Two literals, different terms, different language tags.
      NodeValue.raise(new ExprEvalException("Mismatch in RDFterm-equals: " + n1 + ", " + n2));
    }
    // One or both not a literal.
    return false;
  }
Пример #8
0
 /**
  * This method splits services as bindings would be ask to them.
  *
  * @param bindingList
  * @param serviceOp
  * @param serviceList
  * @return
  */
 private static List<List<NumericBinding>> generateUnionGroups(
     List<NumericBinding> bindingList, OpService serviceOp, List<Node> serviceList) {
   List<List<NumericBinding>> bindingUnionGroups = new ArrayList<List<NumericBinding>>();
   // generate Ops for each service value
   for (int j = 0; j < serviceList.size(); j++) {
     List<NumericBinding> bindingGroupToUnion = new ArrayList<NumericBinding>();
     Node currentService = serviceList.get(j);
     for (int i = 0; i < bindingList.size(); i++) {
       BindingMap binding = (BindingMap) bindingList.get(i).getBinding();
       Node serviceValue = binding.get((Var) serviceOp.getService());
       // if this serviceValue same as one with service list add it to
       // binding group.
       if (currentService.equals(serviceValue)) {
         bindingGroupToUnion.add(bindingList.get(i));
       }
     }
     bindingUnionGroups.add(bindingGroupToUnion);
   }
   return bindingUnionGroups;
 }
Пример #9
0
 /**
  * This method is invoked when the builtin is called in a rule body.
  *
  * @param args the array of argument values for the builtin, this is an array of Nodes, some of
  *     which may be Node_RuleVariables.
  * @param length the length of the argument list, may be less than the length of the args array
  *     for some rule engines
  * @param context an execution context giving access to other relevant data
  * @return return true if the buildin predicate is deemed to have succeeded in the current
  *     environment
  */
 @SuppressWarnings("unused")
 public boolean bodyCall(Node[] args, int length, RuleContext context) {
   checkArgs(length, context);
   BindingEnvironment env = context.getEnv();
   Object maxVal = null;
   Node min = null;
   boolean allLongs = true;
   if (length >= 3) {
     for (int i = 0; i < length; i++) {
       Node n1 = getArg(i, args, context);
       if (n1.isLiteral()) {
         Object v1 = n1.getLiteralValue();
         if (v1 instanceof Number) {
           Number nv1 = (Number) v1;
           if (maxVal == null) {
             maxVal = nv1;
           } else {
             if (v1 instanceof Float || v1 instanceof Double) {
               double pwd = nv1.doubleValue();
               if (pwd > ((Number) maxVal).doubleValue()) {
                 maxVal = nv1;
               }
               allLongs = false;
             } else if (v1 instanceof Integer || v1 instanceof Long) {
               long pwd = nv1.longValue();
               if (pwd > ((Number) maxVal).doubleValue()) {
                 maxVal = nv1;
               }
             }
           }
         } else if (v1 instanceof XSDDateTime) {
           if (maxVal == null) {
             maxVal = v1;
           } else if (maxVal instanceof XSDDateTime) {
             if (((XSDDateTime) maxVal).compareTo((XSDDateTime) v1) < 0) {
               maxVal = v1;
             }
           } else {
             throw new BuiltinException(
                 this,
                 context,
                 "Can't compare datetime ("
                     + v1.toString()
                     + ") with non-datetime value ("
                     + maxVal.toString()
                     + ")");
           }
         } else {
           return false;
         }
       }
     } // end for
   } else {
     Node n1 = getArg(0, args, context);
     if (n1 == null || n1.equals(RDF.Nodes.nil)) {
       return false;
     } else {
       maxVal = maxOfList(n1, context);
     }
   }
   if (maxVal == null) {
     return false;
   }
   if (maxVal instanceof Float) {
     min = Utils.makeFloatNode(((Number) maxVal).floatValue());
   } else if (maxVal instanceof Double) {
     min = Util.makeDoubleNode(((Number) maxVal).doubleValue());
   } else if (maxVal instanceof XSDDateTime) {
     min = Utils.makeXSDDateTimeNode((XSDDateTime) maxVal);
   } else if (maxVal instanceof Integer) {
     min = Util.makeIntNode(((Number) maxVal).intValue());
   } else {
     min = Util.makeLongNode(((Number) maxVal).longValue());
   }
   return env.bind(args[length - 1], min);
 }
 /**
  * Answer true iff both <code>a</code> and <code>b</code> are ANY wildcards or are null (legacy).
  */
 private boolean bothANY(Node a, Node b) {
   return (a == null || a.equals(Node.ANY)) && (b == null || b.equals(Node.ANY));
 }