Skip to content

Commit

Permalink
[Upd] Refactor tabular module
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Aug 29, 2022
1 parent 496e686 commit 7ebb3b2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 50 deletions.
6 changes: 4 additions & 2 deletions s-pipes-core/src/main/java/cz/cvut/spipes/constants/CSVW.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

public class CSVW {

private CSVW(){
}

/**
* The namespace of the vocabulary as a string
*/
public static final String uri = "http://www.w3.org/ns/csvw#";
public static final String extendedUri = "https://onto.fel.cvut.cz/ontologies/csvw-extension/";

protected static Resource resource(String local )
{ return ResourceFactory.createResource( uri + local ); }
Expand Down Expand Up @@ -54,7 +56,7 @@ public static Property extendedProperty(String local )
public static final String TableUri = uri + "Table";
public static final String tableSchemaUri = uri + "tableSchema";
public static final String propertyUri = uri + "property";
public static final String extendedPropertyUri = extendedUri + "property";

/**
returns the URI for this schema
@return the URI for this schema
Expand Down
34 changes: 34 additions & 0 deletions s-pipes-core/src/main/java/cz/cvut/spipes/constants/KBSS_CSVW.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cz.cvut.spipes.constants;

import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;

public class KBSS_CSVW {

private KBSS_CSVW() {
}

/**
* The namespace of the vocabulary as a string
*/
public static final String uri = "https://onto.fel.cvut.cz/ontologies/extension/csvw";

protected static Resource resource(String local )
{ return ResourceFactory.createResource( uri + local ); }

protected static Property property(String local )
{ return ResourceFactory.createProperty( uri, local ); }

public static final String propertyUri = uri + "propertyUri";

public static final Property property = ResourceFactory.createProperty(propertyUri);

/**
returns the URI for this schema
@return the URI for this schema
*/
public static String getURI() {
return uri;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ ExecutionContext executeSelf() {
EntityManager em = JopaPersistenceUtils.getEntityManager("cz.cvut.spipes.modules.model", inputModel);
em.getTransaction().begin();

// TODO add references to specification (they are already in git somewhere!!!)
tableGroup = onTableGroup(null);
table = onTable(null);

Expand All @@ -148,17 +149,17 @@ ExecutionContext executeSelf() {
}
Set<String> columnNames = new HashSet<>();

boolean hasTableSchema = getTableSchema(em);
this.inputTableSchema = getTableSchema(em);
if(skipHeader){
header = getHeaderFromSchema(inputModel, header, hasTableSchema ? 1 : 0);
header = getHeaderFromSchema(inputModel, header, inputTableSchema != null ? 1 : 0);
listReader = new CsvListReader(getReader(), csvPreference);
}else if (hasTableSchema) {
}else if (inputTableSchema != null) {
header = getHeaderFromSchema(inputModel, header, 1);
}


String mainErrorMsg = "CSV table schema is not compliant with provided custom schema.";
if (hasTableSchema && header.length != inputTableSchema.getColumnsSet().size()) {
if (inputTableSchema != null && header.length != inputTableSchema.getColumnsSet().size()) {

String mergedMsg = mainErrorMsg + "\n" +
"The number of columns in the table schema does not match the number of columns in the table." + "\n"
Expand All @@ -177,13 +178,17 @@ ExecutionContext executeSelf() {
for (String columnTitle : header) {
String columnName = normalize(columnTitle);
boolean isDuplicate = !columnNames.add(columnName);
if(hasTableSchema) checkMissingColumns(mainErrorMsg, schemaColumns, columnName);
if(inputTableSchema != null) checkMissingColumns(mainErrorMsg, schemaColumns, columnName);

Column column = new Column(columnName, columnTitle);
outputColumns.add(column);

setColumnAboutUrl(hasTableSchema, tableSchema, schemaColumns, j, column);
setColumnPropertyUrl(hasTableSchema, schemaColumns, j, columnName, column);
setColumnAboutUrl(inputTableSchema != null, tableSchema, schemaColumns, j, column);

Column schemaColumn = inputTableSchema != null ? schemaColumns.get(j) : null;
String propertyUrl = getColumnPropertyUrl(inputTableSchema != null, schemaColumn, columnName);
column.setProperty(propertyUrl);
column.setPropertyUrl(propertyUrl);
if(isDuplicate) throwNotUniqueException(column,columnTitle, columnName);
j++;
}
Expand Down Expand Up @@ -281,8 +286,7 @@ private void setColumnAboutUrl(boolean hasTableSchema, TableSchema tableSchema,
}
}

private boolean getTableSchema(EntityManager em) {
boolean hasTableSchema = false;
private TableSchema getTableSchema(EntityManager em) {
TypedQuery<TableSchema> query = em.createNativeQuery(
"PREFIX csvw: <http://www.w3.org/ns/csvw#>\n" +
"SELECT ?t WHERE { \n" +
Expand All @@ -293,43 +297,33 @@ private boolean getTableSchema(EntityManager em) {

int tableSchemaCount = query.getResultList().size();

if(tableSchemaCount == 1) {
hasTableSchema = true;
inputTableSchema = query.getSingleResult();
LOG.debug("Custom table schema found.");
}else if(tableSchemaCount > 1) {
if(tableSchemaCount > 1) {
LOG.warn("More than one table schema found. Ignoring schemas {}. ", query.getResultList());
}else {
return null;
}
if(tableSchemaCount == 0) {
LOG.debug("No custom table schema found.");
return null;
}
return hasTableSchema;
LOG.debug("Custom table schema found.");
return query.getSingleResult();
}

private void setColumnPropertyUrl(boolean hasTableSchema, List<Column> schemaColumns, int j,
String columnName, Column column) throws UnsupportedEncodingException {
String columnPropertyUrl = null;
private String getColumnPropertyUrl(boolean hasTableSchema,
Column schemaColumn,
String columnName) throws UnsupportedEncodingException {;
if (hasTableSchema) {
if (schemaColumns.get(j).getPropertyUrl() != null) {
columnPropertyUrl = schemaColumns.get(j).getPropertyUrl();
}else if(schemaColumns.get(j).getProperty() != null){
columnPropertyUrl = schemaColumns.get(j).getProperty();
if (schemaColumn.getPropertyUrl() != null) {
return schemaColumn.getPropertyUrl();
}

if (schemaColumns.get(j).getExtendedProperty() != null) {
column.setExtendedProperty(schemaColumns.get(j).getExtendedProperty());
if (columnPropertyUrl == null) {
columnPropertyUrl = column.getExtendedProperty();
}
if (schemaColumn.getProperty() != null) {
return schemaColumn.getProperty();
}
}

if (columnPropertyUrl != null && !columnPropertyUrl.isEmpty()) {
column.setPropertyUrl(columnPropertyUrl);
} else if (dataPrefix != null && !dataPrefix.isEmpty()) {
column.setPropertyUrl(dataPrefix + URLEncoder.encode(columnName, "UTF-8"));
} else {
column.setPropertyUrl(sourceResource.getUri() + "#" + URLEncoder.encode(columnName, "UTF-8"));
if (dataPrefix != null && !dataPrefix.isEmpty()) {
return dataPrefix + URLEncoder.encode(columnName, "UTF-8");
}
return sourceResource.getUri() + "#" + URLEncoder.encode(columnName, "UTF-8");
}

private void throwNotUniqueException(Column column, String columnTitle, String columnName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cz.cvut.kbss.jopa.model.annotations.*;
import cz.cvut.spipes.constants.CSVW;
import cz.cvut.spipes.constants.KBSS_CSVW;

/**
* Part of {@link TableSchema}, each column can have different metadata.
Expand All @@ -22,10 +23,7 @@ public Column(String name, String title) {
@OWLAnnotationProperty(iri = CSVW.titleUri)
private String title;

@OWLAnnotationProperty(iri = CSVW.extendedPropertyUri)
private String extendedProperty;

@OWLAnnotationProperty(iri = CSVW.propertyUri)
@OWLAnnotationProperty(iri = KBSS_CSVW.propertyUri)
private String property;

@OWLAnnotationProperty(iri = CSVW.requiredUri)
Expand Down Expand Up @@ -115,14 +113,6 @@ public void setSuppressOutput(Boolean suppressOutput) {
this.suppressOutput = suppressOutput;
}

public String getExtendedProperty() {
return extendedProperty;
}

public void setExtendedProperty(String extendedProperty) {
this.extendedProperty = extendedProperty;
}

public String getProperty() {
return property;
}
Expand Down

0 comments on commit 7ebb3b2

Please sign in to comment.