Search in sources :

Example 16 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project platform by dashjoin.

the class RDF4J method connectAndCollectMetadata.

@Override
@SuppressWarnings("unchecked")
public Map<String, Object> connectAndCollectMetadata() throws Exception {
    if ("memory".equals(mode))
        _cp = new SailRepository(new MemoryStore());
    if ("local".equals(mode))
        _cp = new SailRepository(new NativeStore(new File(folder)));
    if ("sesame".equals(mode)) {
        _cp = new HTTPRepository(endpoint);
        ((HTTPRepository) _cp).setUsernameAndPassword(username, password);
    }
    if ("client".equals(mode)) {
        _cp = new SPARQLRepository(endpoint);
        ((SPARQLRepository) _cp).setUsernameAndPassword(username, password);
    }
    if (_cp == null)
        throw new Exception("dashjoin.database.mode must be set to one of memory, local, sesame, client");
    _cp.init();
    vf = _cp.getValueFactory();
    Map<String, Object> meta = new HashMap<>();
    try (RepositoryConnection con = _cp.getConnection()) {
        if (datasets != null && "memory".equals(mode))
            for (String s : datasets) {
                log.info("loading dataset " + s);
                InputStream ddl = Loader.open(s);
                RDFFormat format = Rio.getParserFormatForFileName(s).orElse(RDFFormat.RDFXML);
                con.add(ddl, "", format);
            }
        if (ontologies != null) {
            log.info("loading ontologies from " + ontologies);
            RDF4J tmp = new RDF4J() {

                @Override
                RepositoryConnection getConnection() {
                    return _cp.getConnection();
                }
            };
            tmp.ID = ID;
            tmp.mode = "memory";
            tmp.datasets = this.ontologies;
            return tmp.connectAndCollectMetadata();
        }
        // scan the ontology for property (domain and range info)
        log.info("loading ontologies from database");
        Map<IRI, Set<IRI>> type2props = new HashMap<>();
        Map<IRI, Set<IRI>> prop2types = new HashMap<>();
        for (IRI dr : new IRI[] { RDFS.DOMAIN, RDFS.RANGE }) {
            Map<IRI, Set<IRI>> drs = dr == RDFS.DOMAIN ? type2props : prop2types;
            try (RepositoryResult<Statement> d = con.getStatements(null, dr, null)) {
                while (d.hasNext()) {
                    Statement i = d.next();
                    if (i.getSubject() instanceof IRI && i.getObject() instanceof IRI) {
                        IRI s = dr == RDFS.DOMAIN ? (IRI) i.getObject() : (IRI) i.getSubject();
                        IRI o = dr == RDFS.DOMAIN ? (IRI) i.getSubject() : (IRI) i.getObject();
                        Set<IRI> set = drs.get(s);
                        if (set == null) {
                            set = new HashSet<>();
                            drs.put(s, set);
                        }
                        set.add(o);
                    }
                }
            }
        }
        // remember subclass tree (no multiple inheritance)
        Map<IRI, Set<IRI>> subclasses = new LinkedHashMap<>();
        // scan the ontology for classes
        for (IRI[] i : new IRI[][] { new IRI[] { RDF.TYPE, OWL.CLASS }, new IRI[] { RDF.TYPE, RDFS.CLASS }, new IRI[] { RDFS.SUBCLASSOF, null } }) try (RepositoryResult<Statement> types = con.getStatements(null, i[0], i[1])) {
            while (types.hasNext()) {
                Statement stmt = types.next();
                Resource s = stmt.getSubject();
                if (s instanceof IRI) {
                    if (stmt.getObject() instanceof IRI)
                        if (stmt.getPredicate().equals(RDFS.SUBCLASSOF)) {
                            Set<IRI> set = subclasses.get(stmt.getObject());
                            if (set == null) {
                                set = new HashSet<>();
                                subclasses.put((IRI) stmt.getObject(), set);
                            }
                            set.add((IRI) s);
                        }
                    Map<String, Object> table = new HashMap<>();
                    table.put("parent", ID);
                    table.put("name", s.stringValue());
                    table.put("ID", ID + "/" + Escape.encodeTableOrColumnName(s.stringValue()));
                    table.put("type", "object");
                    Map<String, Object> properties = new LinkedHashMap<>();
                    Map<String, Object> id = new HashMap<>();
                    id.put("pkpos", 0);
                    id.put("name", "ID");
                    id.put("type", "string");
                    id.put("format", "uri");
                    id.put("errorMessage", "Please enter a valid URI");
                    id.put("parent", table.get("ID"));
                    id.put("ID", table.get("ID") + "/ID");
                    properties.put("ID", id);
                    table.put("properties", properties);
                    table.put("required", Arrays.asList("ID"));
                    meta.put(s.stringValue(), table);
                    Set<IRI> props = type2props.get(s);
                    if (props != null)
                        for (IRI prop : props) {
                            Set<IRI> ranges = prop2types.get(prop);
                            if (ranges != null)
                                if (ranges.size() == 1) {
                                    Integer maxcard = getMaxCardinality(prop);
                                    addProp("" + table.get("ID"), prop, properties, ranges.iterator().next(), maxcard == null || maxcard > 1);
                                }
                        }
                }
            }
        }
        Set<IRI> roots = new HashSet<IRI>(subclasses.keySet());
        for (Set<IRI> sub : subclasses.values()) roots.removeAll(sub);
        for (IRI root : roots) copyProps(root, subclasses, meta);
        log.info("detected " + meta.size() + " classes");
        // scan props using one sample
        log.info("scannning data...");
        for (Entry<String, Object> cls : meta.entrySet()) try (RepositoryResult<Statement> types = con.getStatements(null, RDF.TYPE, iri(cls.getKey()))) {
            if (types.hasNext()) {
                Statement type = types.next();
                Map<String, Object> table = (Map<String, Object>) cls.getValue();
                Map<String, Object> properties = (Map<String, Object>) table.get("properties");
                try (RepositoryResult<Statement> columns = con.getStatements(type.getSubject(), null, null)) {
                    // list of detected props that will be added to / enhances the ontology
                    Map<IRI, ColType> cols = new LinkedHashMap<>();
                    while (columns.hasNext()) {
                        Statement column = columns.next();
                        if (column.getPredicate().equals(RDF.TYPE))
                            continue;
                        ColType col = cols.get(column.getPredicate());
                        if (col != null)
                            // predicate appears again => must be array
                            col.array = true;
                        else {
                            col = new ColType();
                            col.sample = column.getObject();
                            col.array = false;
                            cols.put(column.getPredicate(), col);
                        }
                    }
                    for (Entry<IRI, ColType> e : cols.entrySet()) {
                        Map<String, Object> property = (Map<String, Object>) properties.get(e.getKey().stringValue());
                        if (property == null) {
                            // prop is not yet in the ontology
                            Value value = e.getValue().sample;
                            if (value instanceof Literal)
                                addProp((String) table.get("ID"), e.getKey(), properties, ((Literal) value).getDatatype(), e.getValue().array);
                            else if (value instanceof IRI) {
                                IRI t = getType((IRI) value);
                                if (t != null)
                                    addProp((String) table.get("ID"), e.getKey(), properties, t, e.getValue().array);
                            }
                        } else {
                            // check cardinality
                            if (property.get("type").equals("array"))
                                if (!e.getValue().array) {
                                    // data suggests single value - retract array type
                                    Map<String, Object> items = (Map<String, Object>) property.remove("items");
                                    property.putAll(items);
                                    // change display props also - see addProp below
                                    // https://github.com/dashjoin/platform/issues/94
                                    property.remove("layout");
                                    if (property.remove("displayWith") != null)
                                        property.put("displayWith", "fkln");
                                }
                        }
                    }
                }
            }
        }
        log.info("done");
    }
    return meta;
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Set(java.util.Set) HashSet(java.util.HashSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLRepository(org.eclipse.rdf4j.repository.sparql.SPARQLRepository) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NativeStore(org.eclipse.rdf4j.sail.nativerdf.NativeStore) HTTPRepository(org.eclipse.rdf4j.repository.http.HTTPRepository) LinkedHashMap(java.util.LinkedHashMap) MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) Entry(java.util.Map.Entry) Literal(org.eclipse.rdf4j.model.Literal) RepositoryResult(org.eclipse.rdf4j.repository.RepositoryResult) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) HashSet(java.util.HashSet) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) Value(org.eclipse.rdf4j.model.Value) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project termit by kbss-cvut.

the class SKOSImporter method parseDataFromStreams.

private void parseDataFromStreams(String mediaType, InputStream... inputStreams) {
    final RDFFormat rdfFormat = Rio.getParserFormatForMIMEType(mediaType).orElseThrow(() -> new UnsupportedImportMediaTypeException("Media type '" + mediaType + "' not supported."));
    final RDFParser p = Rio.createParser(rdfFormat);
    final StatementCollector collector = new StatementCollector(model);
    p.setRDFHandler(collector);
    for (InputStream is : inputStreams) {
        try {
            p.parse(is, "");
        } catch (IOException e) {
            throw new VocabularyImportException("Unable to parse data for import.");
        }
    }
}
Also used : UnsupportedImportMediaTypeException(cz.cvut.kbss.termit.exception.UnsupportedImportMediaTypeException) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) InputStream(java.io.InputStream) VocabularyImportException(cz.cvut.kbss.termit.exception.VocabularyImportException) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 18 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project dotwebstack-framework by dotwebstack.

the class RmlOpenApiConfiguration method resolveMappingPerFile.

private Model resolveMappingPerFile(String mappingFile) {
    URI location = ResourceProperties.getFileConfigPath().resolve(RML_MAPPING_PATH + mappingFile);
    var path = Paths.get(location);
    InputStream mappingInputStream;
    if (Files.exists(path)) {
        try {
            mappingInputStream = new FileInputStream(path.toFile());
        } catch (FileNotFoundException fileNotFoundException) {
            throw invalidConfigurationException("Could not resolve mapping file {}", path, fileNotFoundException);
        }
    } else {
        mappingInputStream = getClass().getResourceAsStream(ResourceProperties.getResourcePath().resolve(RML_MAPPING_PATH + mappingFile).getPath());
    }
    if (mappingInputStream == null) {
        throw invalidConfigurationException("Could not resolve mapping file {}", path);
    }
    RDFFormat rdfFormat = Rio.getParserFormatForFileName(mappingFile).orElseThrow(() -> invalidConfigurationException("Could not determine rdf format for mapping filename: {}. Supported file extensions are: {}", mappingFile, RDFParserRegistry.getInstance().getKeys().stream().map(RDFFormat::getFileExtensions).flatMap(List::stream).collect(Collectors.toList())));
    return Models.parse(mappingInputStream, rdfFormat);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 19 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project dotwebstack-framework by dotwebstack.

the class RmlPostgresIntegrationTest method dereference_returnsExpectedResult_forRequest.

@ParameterizedTest
@MethodSource("createMediaTypeResponseFileNameArguments")
void dereference_returnsExpectedResult_forRequest(MediaType mediaType, String expectedResultFileName) throws IOException {
    RDFFormat rdfFormat = Rio.getParserFormatForFileName(expectedResultFileName).orElseThrow(() -> illegalArgumentException("could not determine rdf format from filename: {}", expectedResultFileName));
    Model expected = Rio.parse(getFileInputStream(expectedResultFileName), rdfFormat);
    String responseBody = client.get().uri("/doc/beer/b0e7cf18-e3ce-439b-a63e-034c8452f59c").accept(mediaType).exchange().expectBody(String.class).returnResult().getResponseBody();
    Model result = Rio.parse(IOUtils.toInputStream(responseBody, StandardCharsets.UTF_8), rdfFormat);
    assertThat(result, is(expected));
}
Also used : Model(org.eclipse.rdf4j.model.Model) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project inception by inception-project.

the class SPARQLQueryBuilderTest method importDataFromFile.

private void importDataFromFile(Repository aRepository, String aFilename) throws IOException {
    // Detect the file format
    RDFFormat format = Rio.getParserFormatForFileName(aFilename).orElse(RDFFormat.RDFXML);
    System.out.printf("Loading %s data fron %s%n", format, aFilename);
    // Load files into the repository
    try (InputStream is = new FileInputStream(aFilename)) {
        importData(aRepository, format, is);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Aggregations

RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)62 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 IOException (java.io.IOException)17 WriteRdf4j (mom.trd.opentheso.core.exports.rdf4j.WriteRdf4j)14 InputStream (java.io.InputStream)12 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)11 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)8 FileInputStream (java.io.FileInputStream)7 Model (org.eclipse.rdf4j.model.Model)6 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)6 RDFParser (org.eclipse.rdf4j.rio.RDFParser)5 UnsupportedRDFormatException (org.eclipse.rdf4j.rio.UnsupportedRDFormatException)5 Rdf2GoCore (de.knowwe.rdf2go.Rdf2GoCore)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 NodePreference (mom.trd.opentheso.bdd.helper.nodes.NodePreference)4 ExportRdf4jHelper (mom.trd.opentheso.core.exports.rdf4j.ExportRdf4jHelper)4 IRI (org.eclipse.rdf4j.model.IRI)4 Statement (org.eclipse.rdf4j.model.Statement)4 Test (org.junit.Test)4