示例#1
0
  /** Find the set of resources referenced by the path */
  public static Set<RDFNode> getNodes(Model model, RDFNode first, Path path) {
    List<Step> steps = path.getSteps();

    Set<RDFNode> starts = Collections.singleton(first);

    for (Step step : steps) {
      String propertyName = step.getPropertyName();
      boolean isInverse = step.isInverse();

      Property property = model.createProperty(propertyName);

      Set<RDFNode> nodes = new HashSet<RDFNode>();

      for (RDFNode start : starts) {
        Set<RDFNode> tmp;
        if (!isInverse) {
          tmp = model.listObjectsOfProperty(start.asResource(), property).toSet();
        } else if (start.isResource()) {
          tmp = new HashSet<RDFNode>(model.listSubjectsWithProperty(property, start).toSet());
        } else {
          tmp = Collections.<RDFNode>emptySet();
        }

        nodes.addAll(tmp);
      }
      starts = nodes;
    }

    return starts;
  }
示例#2
0
  public static ArrayList<RdfModel> processRDF(InputStream in) {
    Model model = ModelFactory.createDefaultModel();
    ArrayList<RdfModel> result = new ArrayList<RdfModel>();
    if (in != null) {
      model.read(in, "RDF/XML");
      // Now, I only care these propties: has-title, year-of, full-name. All three of them must
      // exist'
      for (final ResIterator it = model.listSubjectsWithProperty(RdfPropertyList.p_hasTitle);
          it.hasNext(); ) {
        RdfModel rm = new RdfModel();
        try {
          final Resource node =
              it.next().asResource(); // node is a resource which has title property
          rm.setHasTitle(node.getProperty(RdfPropertyList.p_hasTitle).getString());

          StringBuilder authors = new StringBuilder();
          StringBuilder dates = new StringBuilder();

          for (final StmtIterator all_props = node.listProperties(); all_props.hasNext(); ) {
            try {
              Resource all_res = all_props.next().getObject().asResource();
              StmtIterator fullnames = all_res.listProperties(RdfPropertyList.p_fullName);
              StmtIterator years = all_res.listProperties(RdfPropertyList.p_year);
              // Just for now I may have mutiple author or dates in a String, seperated by comma
              RdfProcess newprocess = new RdfProcess();

              while (fullnames.hasNext()) {
                String fullname = newprocess.getValue(fullnames.next().getObject());
                if (!fullname.equals("Invalid/Lack of Information")) {
                  authors.append(fullname + " , ");
                }
              }
              while (years.hasNext()) {
                String year = newprocess.getValue(years.next().getObject());
                if (!year.equals("Invalid/Lack of Information")) {
                  dates.append(year + " , ");
                }
              }
            } catch (Exception e) {
            }
          }
          rm.setHasDate(dates.toString());
          rm.setHasAuthor(authors.toString());
        } catch (Exception e) {
        }
        result.add(rm);
      }
    }
    return result;
  }