Skip to content

Commit

Permalink
Merge pull request #190 from kbss-cvut/retrieve-prefix
Browse files Browse the repository at this point in the history
Update documentation and test for retrieve prefix module
  • Loading branch information
blcham authored Aug 6, 2023
2 parents 2e6a281 + 1f97020 commit 744aa84
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Module returns prefix mappings of all loaded scripts.
* Individual mappings are represented by following graph pattern:
*
* @prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> .
*
* ?ontology ja:prefixMapping
* [ a ja:SinglePrefixMapping ;
* ja:namespace ?namespaceIRI ;
* ja:prefix ?prefix ]
* .
*
* As an example let's assume we loaded only one script:
*
* @prefix : <http://example.org/> .
* @prefix owl: <http://www.w3.org/2002/07/owl#> .
*
* :my-ontology a owl:Ontology .
*
*
* The output of this module for the example script would be:
*
* @prefix : <http://example.org/> .
* @prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> .
*
* :my-ontology ja:prefixMapping
* [ a ja:SinglePrefixMapping ;
* ja:namespace "http://example.org/" ;
* ja:prefix "" ],
* [ a ja:SinglePrefixMapping ;
* ja:namespace "http://www.w3.org/2002/07/owl#" ;
* ja:prefix "owl" ]
* .
*/
public class RetrievePrefixesModule extends AbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(RetrievePrefixesModule.class.getName());

Expand All @@ -38,13 +72,13 @@ ExecutionContext executeSelf() {
outputModel.add(ontology, JA.prefixMapping, singlePrefixMapping);

outputModel.add(
singlePrefixMapping, RDF.type, JA.SinglePrefixMapping
singlePrefixMapping, RDF.type, JA.SinglePrefixMapping
);
outputModel.add(
singlePrefixMapping, JA.prefix, key
singlePrefixMapping, JA.prefix, key
);
outputModel.add(
singlePrefixMapping, JA.namespace, value
singlePrefixMapping, JA.namespace, value
);
});
}
Expand Down
23 changes: 12 additions & 11 deletions s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package cz.cvut.spipes.util;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jena.graph.compose.MultiUnion;
import org.apache.jena.rdf.model.Model;
Expand All @@ -18,9 +9,19 @@
import org.apache.jena.util.FileUtils;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

public class JenaUtils {

private static Logger LOG = LoggerFactory.getLogger(JenaUtils.class);
Expand Down Expand Up @@ -105,11 +106,11 @@ public static Model createUnion(Model... model) {
return outputModel;
}

public static void saveModelToTemporaryFile(Model model) {
public static void saveModelToTemporaryFile(@NotNull Model model) {
try {
Path file = Files.createTempFile("model-output-", ".ttl");
LOG.debug("Saving model to temporary file " + file.toString() + " ...");
model.write(new FileOutputStream(file.toFile()), FileUtils.langTurtle);
model.write(Files.newOutputStream(file.toFile().toPath()), FileUtils.langTurtle);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.jena.ontology.OntDocumentManager;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.FileUtils;
Expand All @@ -16,11 +17,17 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedHashMap;

import static cz.cvut.spipes.test.JenaTestUtils.assertIsomorphic;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;

Expand All @@ -30,9 +37,11 @@ class RetrievePrefixesModuleTest {
@Mock
OntologyDocumentManager ontoDocManager;

private static final Logger LOG = LoggerFactory.getLogger(RetrievePrefixesModuleTest.class);

private final static String[] ontologyResourcePaths = new String[]{
"/manager/import-closure/indirect-import.ttl",
"/manager/import-closure/direct-import.ttl"
"/manager/import-closure/indirect-import.ttl",
"/manager/import-closure/direct-import.ttl"
};

HashMap<String, OntModel> uri2ontModel;
Expand All @@ -44,14 +53,14 @@ void setUp() {
OntModel model = loadOntModel(ontologyPath);
String iri = getOntologyIri(model);
uri2ontModel.put(
iri,
model
iri,
model
);
}
}

@Test
void executeSelfReturnPrefixes() {
void executeSelfReturnPrefixes() throws URISyntaxException {
given(ontoDocManager.getRegisteredOntologyUris()).willReturn(uri2ontModel.keySet());
uri2ontModel.forEach((key, value) -> {
doReturn(value).when(ontoDocManager).getOntology(key);
Expand All @@ -64,9 +73,13 @@ void executeSelfReturnPrefixes() {
retrievePrefixesModule.setInputContext(inputExecutionContext);
ExecutionContext outputExecutionContext = retrievePrefixesModule.executeSelf();

outputExecutionContext.getDefaultModel().write(System.out, FileUtils.langTurtle, null);
}
Model actualModel = outputExecutionContext.getDefaultModel();

Model expectedModel = ModelFactory.createDefaultModel()
.read(getFilePath("module/retrieve-prefixes/expected-output.ttl").toString());

assertIsomorphic(actualModel, expectedModel);
}

private static String getOntologyIri(OntModel model) {
return model.listResourcesWithProperty(RDF.type, OWL.Ontology).nextResource().toString();
Expand All @@ -91,4 +104,9 @@ private static OntModel loadOntModel(InputStream inputStream) {
dm.loadImports(ontModel);
return ontModel;
}


public Path getFilePath(String fileName) throws URISyntaxException {
return Paths.get(getClass().getResource("/" + fileName).toURI());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> .

<http://onto.fel.cvut.cz/ontologies/test/direct-import-test> ja:prefixMapping [ a ja:SinglePrefixMapping ;
ja:namespace "http://onto.fel.cvut.cz/ontologies/test/loading-test#" ;
ja:prefix "" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/2002/07/owl#" ;
ja:prefix "owl" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
ja:prefix "rdf" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/XML/1998/namespace" ;
ja:prefix "xml" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/2001/XMLSchema#" ;
ja:prefix "xsd" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/2000/01/rdf-schema#" ;
ja:prefix "rdfs" ] .

<http://onto.fel.cvut.cz/ontologies/test/indirect-import-test> ja:prefixMapping [ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/XML/1998/namespace" ;
ja:prefix "xml" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/2001/XMLSchema#" ;
ja:prefix "xsd" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/2000/01/rdf-schema#" ;
ja:prefix "rdfs" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://onto.fel.cvut.cz/ontologies/test/loading-test#" ;
ja:prefix "" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/2002/07/owl#" ;
ja:prefix "owl" ],
[ a ja:SinglePrefixMapping ;
ja:namespace "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
ja:prefix "rdf" ] .
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import cz.cvut.spipes.exception.ResourceNotUniqueException;
import cz.cvut.spipes.modules.exception.TableSchemaException;
import cz.cvut.spipes.test.JenaTestUtils;
import cz.cvut.spipes.util.JenaUtils;
import cz.cvut.spipes.util.StreamResourceUtils;
import org.apache.jena.rdf.model.*;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -18,14 +17,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static cz.cvut.spipes.test.JenaTestUtils.assertIsomorphic;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class TabularModuleTest extends AbstractModuleTestHelper {
Expand Down Expand Up @@ -228,15 +229,6 @@ void executeSelfWithHTMLFileInput() throws URISyntaxException, IOException {
}
}

void assertIsomorphic(Model actualModel, Model expectedModel){
if (! actualModel.isIsomorphicWith(expectedModel)) {
LOG.debug("Saving actual model ... ");
JenaUtils.saveModelToTemporaryFile(actualModel);
LOG.debug("Saving expected model ... ");
JenaUtils.saveModelToTemporaryFile(expectedModel);
fail("Actual model is not isomorphic with expected model (see additional information above).");
}
}

@Override
public String getModuleName() {
Expand Down
5 changes: 4 additions & 1 deletion s-pipes-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>${org.apache.jena}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
</dependencies>
</project>
29 changes: 29 additions & 0 deletions s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.fail;

public class JenaTestUtils {

private static Logger LOG = LoggerFactory.getLogger(JenaTestUtils.class);

public static void mapLocalSPipesDefinitionFiles() {
OntDocumentManager dm = OntDocumentManager.getInstance();
dm.setFileManager(FileManager.getInternal());
Expand Down Expand Up @@ -52,4 +61,24 @@ public static Model laodModelFromResource(String path) {

return model;
}

public static void assertIsomorphic(Model actualModel, Model expectedModel) {
if (!actualModel.isIsomorphicWith(expectedModel)) {
LOG.debug("Saving actual model ... ");
saveModelToTemporaryFile(actualModel);
LOG.debug("Saving expected model ... ");
saveModelToTemporaryFile(expectedModel);
fail("Actual model is not isomorphic with expected model (see additional information above).");
}
}

private static void saveModelToTemporaryFile(Model model) {
try {
Path file = Files.createTempFile("model-output-", ".ttl");
LOG.debug("Saving model to temporary file " + file.toString() + " ...");
model.write(Files.newOutputStream(file.toFile().toPath()), FileUtils.langTurtle);
} catch (IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 744aa84

Please sign in to comment.