예제 #1
0
  /**
   * Extract RML Mapping object from a RML file written with Turtle syntax.
   *
   * <p>Important : The R2RML vocabulary also includes the following R2RML classes, which represent
   * various R2RML mapping constructs. Using these classes is optional in a mapping graph. The
   * applicable class of a resource can always be inferred from its properties. Consequently, in
   * order to identify each triple type, a rule will be used to extract the applicable class of a
   * resource.
   *
   * @param fileToRMLFile
   * @return
   * @throws InvalidR2RMLSyntaxException
   * @throws InvalidR2RMLStructureException
   * @throws R2RMLDataError
   * @throws IOException
   * @throws RDFParseException
   * @throws RepositoryException
   */
  public static RMLMapping extractRMLMapping(String fileToRMLFile)
      throws InvalidR2RMLStructureException, InvalidR2RMLSyntaxException, R2RMLDataError,
          RepositoryException, RDFParseException, IOException {
    // Load RDF data from R2RML Mapping document
    SesameDataSet r2rmlMappingGraph = new SesameDataSet();

    // RML document is a a URI
    if (!RMLEngine.isLocalFile(fileToRMLFile)) {
      HttpURLConnection con = (HttpURLConnection) new URL(fileToRMLFile).openConnection();
      con.setRequestMethod("HEAD");
      if (con.getResponseCode() == HttpURLConnection.HTTP_OK)
        r2rmlMappingGraph.addURI(fileToRMLFile, RDFFormat.TURTLE);
    }
    // RML document is a a local file
    else {
      // r2rmlMappingGraph.addFile(fileToRMLFile, RDFFormat.TURTLE);
      r2rmlMappingGraph.loadDataFromFile(fileToRMLFile, RDFFormat.TURTLE);
    }
    log.debug(
        "[RMLMappingFactory:extractRMLMapping] Number of R2RML triples in file "
            + fileToRMLFile
            + " : "
            + r2rmlMappingGraph.getSize());
    // Transform RDF with replacement shortcuts
    replaceShortcuts(r2rmlMappingGraph);
    // Run few tests to help user in its RDF syntax
    launchPreChecks(r2rmlMappingGraph);
    // Construct R2RML Mapping object
    Map<Resource, TriplesMap> triplesMapResources = extractTripleMapResources(r2rmlMappingGraph);

    log.debug(
        "[RMLMappingFactory:extractRMLMapping] Number of RML triples with "
            + " type "
            + R2RMLTerm.TRIPLES_MAP_CLASS
            + " in file "
            + fileToRMLFile
            + " : "
            + triplesMapResources.size());
    // Fill each triplesMap object
    for (Resource triplesMapResource : triplesMapResources.keySet()) // Extract each triplesMap
    {
      extractTriplesMap(r2rmlMappingGraph, triplesMapResource, triplesMapResources);
    }
    // Generate RMLMapping object
    RMLMapping result = new RMLMapping(triplesMapResources.values());
    return result;
  }