Skip to content

Commit

Permalink
[Feature #25] Schema loading from JOPA model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdenek authored and blcham committed May 17, 2022
1 parent 887bbbf commit 3fbb0c9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package cz.cvut.spipes.modules;

import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.spipes.constants.CSVW;
import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.exception.ResourceNotFoundException;
import cz.cvut.spipes.exception.ResourceNotUniqueException;
import cz.cvut.spipes.modules.model.Column;
import cz.cvut.spipes.modules.model.TableSchema;
import cz.cvut.spipes.modules.template.InvalidTemplateException;
import cz.cvut.spipes.modules.template.UriTemplate;
import cz.cvut.spipes.modules.util.JopaPersistenceUtils;
import cz.cvut.spipes.registry.StreamResource;
import cz.cvut.spipes.registry.StreamResourceRegistry;
import cz.cvut.spipes.util.JenaUtils;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.iri.IRI;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.shared.PropertyNotFoundException;
Expand Down Expand Up @@ -110,6 +117,7 @@ public class TabularModule extends AbstractModule {
@Override
ExecutionContext executeSelf() {
Model inputModel = executionContext.getDefaultModel();
EntityManager em = JopaPersistenceUtils.getEntityManager("model", inputModel);

outputModel = ModelFactory.createDefaultModel();

Expand All @@ -131,6 +139,15 @@ ExecutionContext executeSelf() {
Set<String> columnNames = new HashSet<>();
List<RDFNode> columns = new LinkedList<>();

TableSchema tableSchema = em.createNativeQuery(
"PREFIX csvw: <http://www.w3.org/ns/csvw#>" +
"SELECT ?t WHERE {" +
"?t rdf:type csvw:TableSchema" +
"}",
TableSchema.class).getSingleResult();
List<Column> schemaColumns = tableSchema.getColumns();

int j = 0;
for (String columnTitle : header) {
Resource columnResource = ResourceFactory.createResource();
String columnName = normalize(columnTitle);
Expand Down Expand Up @@ -166,22 +183,15 @@ ExecutionContext executeSelf() {
ResourceFactory.createStringLiteral(columnTitle)
);

String columnAboutUrl = null; //TODO get from inputModel
if (columnAboutUrl != null && !columnAboutUrl.isEmpty()) {
outputModel.add(
columnResource,
CSVW.aboutUrl,
outputModel.createTypedLiteral(columnAboutUrl, CSVW.uriTemplate)
);
} else {
outputModel.add(
T_Schema,
CSVW.aboutUrl,
outputModel.createTypedLiteral(sourceResource.getUri() + "#row-{_row}", CSVW.uriTemplate)
);
}
Column column = schemaColumns.get(j);
String columnAboutUrlStr = column.getAboutUrl();
outputModel.add(
columnResource,
CSVW.aboutUrl,
outputModel.createTypedLiteral(columnAboutUrlStr, CSVW.uriTemplate)
);

String columnPropertyUrl = null; //TODO get from inputModel
String columnPropertyUrl = column.getPropertyUrl();
if (columnPropertyUrl != null && !columnPropertyUrl.isEmpty()) {
outputModel.add(
columnResource,
Expand All @@ -201,6 +211,8 @@ ExecutionContext executeSelf() {
ResourceFactory.createPlainLiteral(sourceResource.getUri() + "#" + URLEncoder.encode(columnName, "UTF-8"))
);
}

j++;
}

RDFList columnList = outputModel.createList(columns.iterator());
Expand Down Expand Up @@ -260,11 +272,12 @@ ExecutionContext executeSelf() {
Resource schemaColumnResource = columns.get(i).asResource();

// 4.6.8.1
String columnAboutUrl = getAboutUrlFromSchema(schemaColumnResource);
Resource S = ResourceFactory.createResource(columnAboutUrl.replace(
"{_row}",
Integer.toString(listReader.getRowNumber())
));
String columnAboutUrlStr = getAboutUrlFromSchema(schemaColumnResource);
UriTemplate aboutUrlTemplate = new UriTemplate(columnAboutUrlStr);
aboutUrlTemplate.initialize(null, Arrays.asList(header));
IRI columnAboutUrl = aboutUrlTemplate.getUri(row);

Resource S = ResourceFactory.createResource(columnAboutUrl.toString());

// 4.6.8.2
if (R != null) {
Expand All @@ -278,7 +291,8 @@ ExecutionContext executeSelf() {
String columnPropertyUrl = getPropertyUrlFromSchema(schemaColumnResource);
Property P = ResourceFactory.createProperty(columnPropertyUrl);

String valueUrl = null; //TODO get from inputModel
Column column = schemaColumns.get(i);
String valueUrl = column.getValueUrl();

if (valueUrl != null && !valueUrl.isEmpty()) {
// 4.6.8.4
Expand Down Expand Up @@ -309,7 +323,7 @@ ExecutionContext executeSelf() {
}
}

} catch (IOException e) {
} catch (IOException | InvalidTemplateException e) {
LOG.error("Error while reading file from resource uri {}", sourceResource, e);
} finally {
if( listReader != null ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.cvut.spipes.modules.model;

import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.spipes.constants.CSVW;
Expand All @@ -10,6 +11,9 @@
@OWLClass(iri = CSVW.ColumnUri)
public class Column {

@Id(generated = true)
private String id;

@OWLAnnotationProperty(iri = CSVW.nameUri)
private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class TableSchema {
@OWLAnnotationProperty(iri = CSVW.aboutUrlUri)
private String aboutUrl;

@OWLAnnotationProperty(iri = CSVW.propertyUrlUri)
private String propertyUrl;

@OWLAnnotationProperty(iri = CSVW.valueUrlUri)
private String valueUrl;

@OWLObjectProperty(iri = CSVW.columnsUri)
private List<Column> columns;

Expand All @@ -28,6 +34,22 @@ public void setAboutUrl(String aboutUrl) {
this.aboutUrl = aboutUrl;
}

public String getPropertyUrl() {
return propertyUrl;
}

public void setPropertyUrl(String propertyUrl) {
this.propertyUrl = propertyUrl;
}

public String getValueUrl() {
return valueUrl;
}

public void setValueUrl(String valueUrl) {
this.valueUrl = valueUrl;
}

public List<Column> getColumns() {
return columns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
import java.util.List;

/**
* Implementation of the first two levels of RFC6570
* Implementation of the first two levels of RFC6570.
* <p>
* This implementation of the String Template has been heavily inspired by the one that is part of the
* project Linkedpipes ETL developed by the Linkedpipes ETL Team
* (<a href="https://etl.linkedpipes.com/team/">ETL Team</a>).
*
* @see <a href="https://tools.ietf.org/html/rfc6570#section-2">RFC6570 section 2</a>
* @see <a href="https://etl.linkedpipes.com/">Linkedpipes ETL</a>
*/
public class StringTemplate {

public static final String TABLE_RESOURCE_REF = "__A83N48X1_TABLE_URI__"; //TODO what is this?
public static final String TABLE_RESOURCE_REF = "__A83N48X1_TABLE_URI__";

private interface Token {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cz.cvut.spipes.modules.template;

import org.apache.jena.iri.IRI;
import org.apache.jena.iri.IRIFactory;

import java.util.List;

public class UriTemplate {
Expand All @@ -13,4 +16,14 @@ public UriTemplate(String templateAsString) {
public void initialize(String tableUri, List<String> header) throws InvalidTemplateException {
template.initialize(tableUri, header);
}

public IRI getUri(List<String> row) {
String val = template.process(row);

if (val != null) {
return IRIFactory.iriImplementation().create(val);
}

return null;
}
}

0 comments on commit 3fbb0c9

Please sign in to comment.