Exemplo n.º 1
0
  public Map<String, String> read(String in) {
    List<String> result = new ArrayList<String>();
    HttpClient client = new HttpClient();
    //	client.getHostConfiguration().setProxy("wwwcache.open.ac.uk", 80);
    ModelFactory modelFactory = RDF2Go.getModelFactory();
    Model model = modelFactory.createModel();

    String serviceEndPoint = "http://ws.geonames.org/search?q=" + "London" + "&type=rdf";

    GetMethod geoNameSearch = new GetMethod(serviceEndPoint);

    String rdf = "";
    try {
      client.executeMethod(geoNameSearch);
      rdf = geoNameSearch.getResponseBodyAsString();
      String rdf8 = new String(rdf.getBytes(), "UTF-8");

      System.out.println(rdf8);

      String modelQueryString =
          "SELECT DISTINCT ?name ?country ?location ?map ?lat ?long WHERE { ?location <http://www.geonames.org/ontology#name> ?name ."
              + "?location <http://www.geonames.org/ontology#locationMap> ?map ."
              + "?location <http://www.geonames.org/ontology#countryCode> ?country ."
              + "?location <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat ."
              + "?location <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long}";

      model.open();

      model.readFrom(new ByteArrayInputStream(rdf8.getBytes()));

      QueryResultTable results = model.sparqlSelect(modelQueryString);
      System.out.println(results.toString());
      ClosableIterator<QueryRow> iter = results.iterator();
      while (iter.hasNext()) {
        QueryRow row = iter.next();
        String name = row.getValue("name").toString();
        String country = row.getValue("country").toString();
        String location = row.getValue("location").toString();
        String map = row.getValue("location").toString();
        String lat = row.getValue("lat").toString();
        String longatti = row.getValue("long").toString();
        result.add(
            name + " -> " + country + ";" + location + ";" + lat + ";" + longatti + ";" + name + ";"
                + name + ":" + country + ":" + location + ":" + map);
      }

    } catch (HttpException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    model.close();
    //	return result;
    return null;
  }
Exemplo n.º 2
0
  public Model toRDF2GoModel() {
    Model model = RDF2Go.getModelFactory().createModel();
    model.open();

    Resource sample = this.getUri();
    model.addStatement(sample, RDF.type, this.type);
    for (PropertyValue property : properties) {
      Node object;
      if (property.getLang() != null) {
        object = model.createLanguageTagLiteral(property.getValue(), property.getLang());
      } else if (property.getDatatype() != null) {
        object = model.createDatatypeLiteral(property.getValue(), property.getDatatype());
      } else {
        object = model.createPlainLiteral(property.getValue());
      }
      model.addStatement(sample, property.getUri(), object);
    }

    return model;
  }