diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java index e077f5c6..2f8a018b 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/modules/RetrievePrefixesModule.java @@ -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: . + * + * ?ontology ja:prefixMapping + * [ a ja:SinglePrefixMapping ; + * ja:namespace ?namespaceIRI ; + * ja:prefix ?prefix ] + * . + * + * As an example let's assume we loaded only one script: + * + * @prefix : . + * @prefix owl: . + * + * :my-ontology a owl:Ontology . + * + * + * The output of this module for the example script would be: + * + * @prefix : . + * @prefix ja: . + * + * :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()); @@ -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 ); }); } diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java b/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java index a02f34cb..13c5fc77 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/util/JenaUtils.java @@ -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; @@ -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); @@ -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(); } diff --git a/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java b/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java index 955ac64f..2e71464c 100644 --- a/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java +++ b/s-pipes-core/src/test/java/cz/cvut/spipes/modules/RetrievePrefixesModuleTest.java @@ -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; @@ -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; @@ -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 uri2ontModel; @@ -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); @@ -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(); @@ -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()); + } } \ No newline at end of file diff --git a/s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl b/s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl new file mode 100644 index 00000000..aac67747 --- /dev/null +++ b/s-pipes-core/src/test/resources/module/retrieve-prefixes/expected-output.ttl @@ -0,0 +1,39 @@ +@prefix ja: . + + 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" ] . + + 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" ] . \ No newline at end of file diff --git a/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java b/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java index 2ca2948e..11208334 100644 --- a/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java +++ b/s-pipes-modules/module-tabular/src/test/java/cz/cvut/spipes/modules/TabularModuleTest.java @@ -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; @@ -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 { @@ -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() { diff --git a/s-pipes-test/pom.xml b/s-pipes-test/pom.xml index f8040869..daa4a803 100644 --- a/s-pipes-test/pom.xml +++ b/s-pipes-test/pom.xml @@ -29,7 +29,10 @@ org.apache.jena jena-core - ${org.apache.jena} + + + org.junit.jupiter + junit-jupiter-api \ No newline at end of file diff --git a/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java b/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java index 23044acc..c8e6b58e 100644 --- a/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java +++ b/s-pipes-test/src/main/java/cz/cvut/spipes/test/JenaTestUtils.java @@ -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()); @@ -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(); + } + } }