示例#1
0
  private static void insertKWH(String kwhReadingFile, String buildingCode, int buildingNo)
      throws IOException {

    System.out.println(
        "Starting insertKWH(): BuildingCode = "
            + buildingCode
            + "  Building# = "
            + buildingNo
            + "  file = "
            + kwhReadingFile);

    FileReader reader = new FileReader(kwhReadingFile);
    BufferedReader read = new BufferedReader(reader);

    StringBuilder insertQuery = new StringBuilder("");
    insertQuery.append("PREFIX sgns: <http://www.smartgrid.usc.edu/Ontology/2012.owl#> \n");
    insertQuery.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n");
    insertQuery.append("PREFIX gmlns: <http://purl.org/ifgi/gml/0.1/> \n");
    insertQuery.append("INSERT DATA { \n");

    String date = null;
    String time = null;
    String kwhReading = null;

    int readingCount = 0;
    String line = read.readLine(); // First line is the column headers
    line = read.readLine();
    while (line != null) {

      // System.out.println(line);

      StringTokenizer tok = new StringTokenizer(line, ", ");

      if (!tok.hasMoreTokens()) {
        line = read.readLine();
        continue;
      }
      if (!(buildingNo + "").equals(tok.nextToken())) {
        line = read.readLine();
        continue;
      }

      if (!tok.hasMoreTokens()) {
        line = read.readLine();
        continue;
      }
      date = tok.nextToken();

      if (!tok.hasMoreTokens()) {
        line = read.readLine();
        continue;
      }
      time = tok.nextToken();

      if (!tok.hasMoreTokens()) {
        line = read.readLine();
        continue;
      }
      kwhReading = tok.nextToken();

      readingCount++;
      System.out.println(date + "   " + time + "   " + kwhReading);

      // buildingCode = "testBuildingCode111";

      String buildingURI = "ran" + System.nanoTime();
      String kwhURI = "ran" + System.nanoTime();

      String triple =
          " 	sgns:" + buildingURI + " rdf:type <http://dbpedia.org/ontology/Building> . \n";
      insertQuery.append(triple);

      triple =
          " 	sgns:"
              + buildingURI
              + " <http://dbpedia.org/ontology/hasBuildingCode> '"
              + buildingCode
              + "' . \n";
      insertQuery.append(triple);

      triple = " 	sgns:" + buildingURI + " sgns:KiloWattLoadObservable sgns:" + kwhURI + " . \n";
      insertQuery.append(triple);

      triple = " 	sgns:" + kwhURI + " rdf:type sgns:ElectricalMeasurement . \n";
      insertQuery.append(triple);

      triple =
          " 	sgns:"
              + kwhURI
              + " sgns:hasMeasurementRecordedDate  '"
              + date
              + "' . \n"; // ^^xsd:date
      insertQuery.append(triple);

      triple =
          " 	sgns:"
              + kwhURI
              + " sgns:hasMeasurementRecordedTime  '"
              + time
              + "' . \n"; // ^^xsd:time
      insertQuery.append(triple);

      triple = " 	sgns:" + kwhURI + " sgns:hasMeasuredValue  '" + kwhReading + "' . \n\n";
      insertQuery.append(triple);

      line = read.readLine();
    }

    insertQuery.append(" 	} \n");
    String sparql = insertQuery.toString();
    System.out.println("InsertQuery: " + sparql);

    System.out.println("# of Readings = " + readingCount);

    Store store = null;
    try {
      store = new Store(IIPProperties.getStoreURL());
      String response = store.insert(sparql);
      System.out.println("Insert response: " + response);
    } catch (MalformedURLException e) {
      e.getMessage();
      e.printStackTrace();
    }
  }
示例#2
0
  private static void insertLatLong() throws IOException {

    System.out.println("Starting insertLatLong()");

    FileReader reader = new FileReader("data/latlong.csv");
    BufferedReader read = new BufferedReader(reader);

    StringBuilder insertQuery = new StringBuilder("");
    insertQuery.append("PREFIX sgns: <http://www.smartgrid.usc.edu/Ontology/2012.owl#> \n");
    insertQuery.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n");
    insertQuery.append("PREFIX gmlns: <http://purl.org/ifgi/gml/0.1/> \n");
    insertQuery.append("INSERT DATA { \n");

    String buildingCode = null;
    String lat = null;
    String longi = null;
    String line = read.readLine(); // First line is the column headers
    line = read.readLine();
    while (line != null) {

      System.out.println(line);
      if (line == null || line.startsWith(",")) continue;

      StringTokenizer tok = new StringTokenizer(line, ",");

      buildingCode = tok.nextToken();
      lat = tok.nextToken();
      longi = tok.nextToken();

      // buildingCode = "testBuildingCode111";

      String buildingURI = "ran" + System.nanoTime();
      String gmlURI = "ran" + System.nanoTime();

      String triple =
          " sgns:" + buildingURI + " rdf:type <http://dbpedia.org/ontology/Building> . \n";
      insertQuery.append(triple);

      triple =
          " sgns:"
              + buildingURI
              + " <http://dbpedia.org/ontology/hasBuildingNo> '"
              + buildingCode
              + "' . \n";
      insertQuery.append(triple);

      triple = " sgns:" + buildingURI + " gmlns:GeometryProperty sgns:" + gmlURI + " . \n";
      insertQuery.append(triple);

      triple = " sgns:" + gmlURI + " sgns:hasLatitude  '" + lat + "' . \n";
      insertQuery.append(triple);

      triple = " sgns:" + gmlURI + " sgns:hasLongitude  '" + longi + "' . \n";
      insertQuery.append(triple);

      line = read.readLine();
    }

    insertQuery.append(" 	} \n");
    String sparql = insertQuery.toString();
    System.out.println("InsertQuery: " + sparql);

    Store store = null;
    try {
      store = new Store(IIPProperties.getStoreURL());
      String response = store.insert(sparql);
      System.out.println("Insert response: " + response);
    } catch (MalformedURLException e) {
      e.getMessage();
      e.printStackTrace();
    }
  }