Beispiel #1
0
  // FIXME: use digest instead of size
  static void getReachable(Resource r, Model m, Model result) throws ModelException {

    int oldSize = result.size();
    Model directlyReachable = m.find(r, null, null);
    SetOperations.unite(result, directlyReachable);
    if (result.size() == oldSize) return;
    for (Enumeration en = directlyReachable.elements(); en.hasMoreElements(); ) {
      Statement t = (Statement) en.nextElement();
      if (t.object() instanceof Resource) getReachable((Resource) t.object(), m, result);
    }
  }
Beispiel #2
0
  public static Resource getSubject(Model r, Resource predicate, RDFNode object)
      throws ModelException {

    Model m = r.find(null, predicate, object);
    if (m == null || m.size() == 0) return null;
    //    if(m.size() > 1)
    //      throw new RuntimeException("Model contains more than one triple");
    return ((Statement) m.elements().nextElement()).subject();
  }
Beispiel #3
0
  public static RDFNode getObject(Model r, Resource subject, Resource predicate)
      throws ModelException {

    Model m = r.find(subject, predicate, null);
    if (m == null || m.size() == 0) return null;
    //    if(m.size() > 1)
    //      throw new RuntimeException("Model contains more than one triple");
    // FIXME: we do not check whether it is a resource or literal
    return ((Statement) m.elements().nextElement()).object();
  }
Beispiel #4
0
  /** Collects the triples of a model into an array. */
  public static Statement[] getStatementArray(Model m) throws ModelException {

    Statement[] v = new Statement[m.size()];
    int i = 0;
    for (Enumeration en = m.elements(); en.hasMoreElements(); ) {

      Statement t = (Statement) en.nextElement();
      v[i++] = t;
    }
    return v;
  }
Beispiel #5
0
  public static List getObjects(Model m, Resource subject, Resource predicate)
      throws ModelException {

    List result = new ArrayList();

    if (m == null || m.size() == 0) return result;

    for (Enumeration en = m.find(subject, predicate, null).elements(); en.hasMoreElements(); ) {

      Statement st = (Statement) en.nextElement();
      result.add(st.object());
    }
    return result;
  }
Beispiel #6
0
  /**
   * @return a new model in which all occurrences of the old resources are replaced by the new ones.
   *     Returns number replacements done.
   */
  public static int replaceResources(Model m, Map o2n) throws ModelException {

    NodeFactory f = m.getNodeFactory();
    Enumeration en = m.elements();

    Model toRemove = m.create();
    Model toAdd = m.create();

    while (en.hasMoreElements()) {

      Statement st = (Statement) en.nextElement();
      Statement st_n = replaceResources(st, f, o2n);

      if (st_n != st) { // yes, pointer comparison
        toAdd.add(st_n);
        toRemove.add(st);
      }
    }

    SetOperations.subtract(m, toRemove);
    SetOperations.unite(m, toAdd);

    return toAdd.size();
  }