Пример #1
0
  private Model execute(Model inputModel, String endpoint) {
    Model cube = createModel();
    Resource dataset;
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    dataset =
        cube.createResource(
            GK.uri + "Properties_per_Class" + calendar.getTimeInMillis(), QB.Dataset);
    dataset.addLiteral(RDFS.comment, "Properties per class");
    dataset.addLiteral(DCTerms.date, cube.createTypedLiteral(calendar));
    dataset.addLiteral(DCTerms.publisher, "R & D, Unister GmbH, Geoknow");
    dataset.addProperty(QB.structure, cube.createResource(STRUCTURE));

    QueryExecution qExec;
    if (inputModel != null) {
      qExec = QueryExecutionFactory.create(INSTANCES, inputModel);
    } else {
      qExec =
          QueryExecutionFactory.sparqlService(endpoint, INSTANCES, defaultGraphs, defaultGraphs);
    }
    ResultSet result = qExec.execSelect();
    int i = 0;
    while (result.hasNext()) {
      Resource owlClass = result.next().getResource("class");
      NUMBER_OF_PROPERTIES.setIri("class", owlClass.getURI());
      QueryExecution propertiesQexec;
      if (inputModel != null) {
        propertiesQexec = QueryExecutionFactory.create(NUMBER_OF_PROPERTIES.asQuery(), inputModel);
      } else {
        propertiesQexec =
            QueryExecutionFactory.sparqlService(
                endpoint, NUMBER_OF_PROPERTIES.asQuery(), defaultGraphs, defaultGraphs);
        System.out.println(NUMBER_OF_PROPERTIES.asQuery());
      }
      try {
        ResultSet propertiesResult = propertiesQexec.execSelect();
        if (propertiesResult.hasNext()) {
          System.out.println(i);
          Resource obs =
              cube.createResource(
                  "http://www.geoknow.eu/data-cube/metric2/observation" + i, QB.Observation);
          obs.addProperty(QB.dataset, dataset);
          obs.addProperty(GK.DIM.Class, owlClass);
          obs.addLiteral(GK.MEASURE.PropertyCount, propertiesResult.next().getLiteral("count"));

          i++;
        }
      } catch (Exception e) {
        System.out.println(i);
        Resource obs =
            cube.createResource(
                "http://www.geoknow.eu/data-cube/metric2/observation" + i, QB.Observation);
        obs.addProperty(QB.dataset, dataset);
        obs.addProperty(GK.DIM.Class, owlClass);
        obs.addLiteral(GK.MEASURE.PropertyCount, -1);
        obs.addLiteral(RDFS.comment, e.getMessage());
        i++;
      }
    }
    return cube;
  }
  private void generateSample() {
    logger.info("Generating sample...");
    sample = ModelFactory.createDefaultModel();

    // we have to set up a new query execution factory working on our local model
    qef = new QueryExecutionFactoryModel(sample);
    reasoner = new SPARQLReasoner(qef);

    // get the page size
    // TODO put to base class
    long pageSize = 10000; // PaginationUtils.adjustPageSize(globalQef, 10000);

    ParameterizedSparqlString sampleQueryTemplate = getSampleQuery();
    sampleQueryTemplate.setIri("p", entityToDescribe.toStringID());
    Query query = sampleQueryTemplate.asQuery();
    query.setLimit(pageSize);

    boolean isEmpty = false;
    int i = 0;
    while (!isTimeout() && !isEmpty) {
      // get next sample
      logger.debug("Extending sample...");
      query.setOffset(i++ * pageSize);
      QueryExecution qe = ksQef.createQueryExecution(query);
      Model tmp = qe.execConstruct();
      sample.add(tmp);

      // if last call returned empty model, we can leave loop
      isEmpty = tmp.isEmpty();
    }
    logger.info("...done. Sample size: " + sample.size() + " triples");
  }
Пример #3
0
  private Model execute(Model inputModel, String endpoint) {
    Model cube = createModel();

    Resource dataset;
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    dataset = cube.createResource(GK.uri + "Average_Surface", QB.Dataset);
    dataset.addLiteral(RDFS.comment, "Average Surface per class");
    dataset.addLiteral(DCTerms.date, cube.createTypedLiteral(calendar));
    dataset.addLiteral(DCTerms.publisher, "R & D, Unister GmbH, Geoknow");
    dataset.addProperty(QB.structure, cube.createResource(STRUCTURE));
    if (endpoint != null) {
      dataset.addProperty(DCTerms.source, endpoint);
    }

    QueryExecution qExec;
    if (inputModel != null) {
      qExec = QueryExecutionFactory.create(GET_CLASSES, inputModel);
    } else {
      qExec =
          QueryExecutionFactory.sparqlService(endpoint, GET_CLASSES, defaultGraphs, defaultGraphs);
    }
    ResultSet result = qExec.execSelect();
    int obsCount = 0;
    while (result.hasNext()) {

      double area = 0;
      int i = 0;
      Resource owlClass = result.next().get("class").asResource();

      if (!blacklist.contains(owlClass.toString())) {

        System.out.println(owlClass);
        GET_INSTANCES.setIri("class", owlClass.getURI());

        QueryExecution qexecInstances;
        if (inputModel != null) {
          qexecInstances = QueryExecutionFactory.create(GET_INSTANCES.asQuery(), inputModel);
        } else {
          qexecInstances =
              QueryExecutionFactory.sparqlService(
                  endpoint, GET_INSTANCES.asQuery(), defaultGraphs, defaultGraphs);
        }
        for (ResultSet instancesResult = qexecInstances.execSelect(); instancesResult.hasNext(); ) {

          QuerySolution next = instancesResult.next();
          String instance = next.get("instance").asResource().getURI();
          if (instance == null) {
            continue;
          }
          POLYGON.setIri("instance", instance);
          QueryExecution qexecMember;
          if (inputModel != null) {
            qexecMember = QueryExecutionFactory.create(POLYGON.asQuery(), inputModel);
          } else {
            qexecMember =
                QueryExecutionFactory.sparqlService(
                    endpoint, POLYGON.asQuery(), defaultGraphs, defaultGraphs);
          }
          StringBuilder polygonBuilder = new StringBuilder();
          firstLat = null;
          firstLong = null;
          for (ResultSet latLong = qexecMember.execSelect(); latLong.hasNext(); ) {
            processPoint(latLong.next(), polygonBuilder);
          }
          if (polygonBuilder.length() > 0) {
            area += calculateArea(polygonBuilder);
          } else {
            area = 0;
            polygonBuilder.setLength(0);
            this.firstLat = null;
            this.firstLong = null;
            MULTI_POLYGON.setIri("instance", instance);
            QueryExecution qexecMultiPolygon;
            if (inputModel != null) {
              qexecMultiPolygon = QueryExecutionFactory.create(MULTI_POLYGON.asQuery(), inputModel);
            } else {
              qexecMultiPolygon =
                  QueryExecutionFactory.sparqlService(
                      endpoint, MULTI_POLYGON.asQuery(), defaultGraphs, defaultGraphs);
            }
            String polygonName = "";
            for (ResultSet latLong = qexecMultiPolygon.execSelect(); latLong.hasNext(); ) {
              QuerySolution solution = latLong.next();
              if (!polygonName.equals(solution.get("polygon").asNode().getBlankNodeLabel())) {
                if (polygonBuilder.length() > 0) {
                  area += calculateArea(polygonBuilder);
                }
                this.firstLat = null;
                this.firstLong = null;
                polygonBuilder.setLength(0);
              }
              polygonName = solution.get("polygon").asNode().getBlankNodeLabel();
              processPoint(solution, polygonBuilder);
            }
          }
          i++;
        }
      }
      Resource obs = cube.createResource(structureUri + "/obs/" + obsCount, QB.Observation);
      double average = i == 0 ? 0 : area / i;
      obs.addProperty(GK.MEASURE.Average, cube.createTypedLiteral(average));
      obs.addProperty(GK.DIM.Class, owlClass);
      obs.addProperty(QB.dataset, dataset);
      obsCount++;
    }
    return cube;
  }