Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] Initial implementation of RetrievePrefixModule #176

Merged
merged 9 commits into from
Jul 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,24 @@ protected RDFNode getEffectiveValue(@NotNull Property valueProperty) {
}
}

protected ExecutionContext createOutputContext(boolean isReplace, Model inputModel, Model computedModel) {
/**
* Helper method to creates output execution context considering isReplace flag
* indicating if newly computed model should replace input model of the module
* or be appended to it.
* @param isReplace if true replace input model otherwise append to it.
* @param computedModel model to be reflected in final output of this module.
* @return Output execution context to be returned by this module.
*/
protected ExecutionContext createOutputContext(boolean isReplace, Model computedModel) {
if (isReplace) {
return ExecutionContextFactory.createContext(computedModel);
} else {
if (AuditConfig.isEnabled() || ExecutionConfig.getEnvironment().equals(Environment.development)) {
LOG.debug("Saving module's computed output to file {}.", saveModelToTemporaryFile(computedModel));
}
return ExecutionContextFactory.createContext(JenaUtils.createUnion(inputModel, computedModel));
return ExecutionContextFactory.createContext(
JenaUtils.createUnion(executionContext.getDefaultModel(), computedModel)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public ExecutionContext executeSelf() {
nNew = inferredInSingleIterationModel.size();
}

return this.createOutputContext(isReplace, defaultModel, inferredModel);
return this.createOutputContext(isReplace, inferredModel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cz.cvut.spipes.modules;

import cz.cvut.spipes.constants.KBSS_MODULE;
import cz.cvut.spipes.constants.SML;
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.manager.OntoDocManager;
import cz.cvut.spipes.manager.OntologyDocumentManager;
import org.apache.jena.assembler.JA;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RetrievePrefixesModule extends AbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(RetrievePrefixesModule.class.getName());

private static String TYPE_URI = KBSS_MODULE.getURI() + "retrieve-prefixes";

//sml:replace
private boolean isReplace;

//TODO refactor -> should be part of execution context
OntologyDocumentManager ontologyDocumentManager = OntoDocManager.getInstance();

@Override
ExecutionContext executeSelf() {
Model outputModel = ModelFactory.createDefaultModel();
blcham marked this conversation as resolved.
Show resolved Hide resolved

for (String ontologyUri : ontologyDocumentManager.getRegisteredOntologyUris()) {

Resource ontology = outputModel.createResource(ontologyUri);

ontologyDocumentManager.getOntology(ontologyUri).getNsPrefixMap().forEach((key, value) -> {
Resource singlePrefixMapping = outputModel.createResource();

outputModel.add(ontology, JA.prefixMapping, singlePrefixMapping);

outputModel.add(
singlePrefixMapping, RDF.type, JA.SinglePrefixMapping
);
outputModel.add(
singlePrefixMapping, JA.prefix, key
);
outputModel.add(
singlePrefixMapping, JA.namespace, value
);
});
}

return this.createOutputContext(isReplace, outputModel);
}

@Override
public String getTypeURI() {
return TYPE_URI;
}

@Override
public void loadConfiguration() {
isReplace = this.getPropertyValue(SML.replace, false);
}

void setOntologyDocumentManager(OntologyDocumentManager ontologyDocumentManager) {
this.ontologyDocumentManager = ontologyDocumentManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cz.cvut.spipes.modules;

import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.manager.OntologyDocumentManager;
import org.apache.jena.ontology.OntDocumentManager;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.FileUtils;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;

@ExtendWith(MockitoExtension.class)
class RetrievePrefixesModuleTest {

@Mock
OntologyDocumentManager ontoDocManager;

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

HashMap<String, OntModel> uri2ontModel;

@BeforeEach
void setUp() {
uri2ontModel = new LinkedHashMap<>();
for (String ontologyPath : ontologyResourcePaths) {
OntModel model = loadOntModel(ontologyPath);
String iri = getOntologyIri(model);
uri2ontModel.put(
iri,
model
);
}
}

@Test
void testExecuteSelf() {
given(ontoDocManager.getRegisteredOntologyUris()).willReturn(uri2ontModel.keySet());
uri2ontModel.forEach((key, value) -> {
doReturn(value).when(ontoDocManager).getOntology(key);
});

ExecutionContext inputExecutionContext = ExecutionContextFactory.createEmptyContext();

RetrievePrefixesModule retrievePrefixesModule = new RetrievePrefixesModule();
retrievePrefixesModule.setOntologyDocumentManager(ontoDocManager);
retrievePrefixesModule.setInputContext(inputExecutionContext);
ExecutionContext outputExecutionContext = retrievePrefixesModule.executeSelf();

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


private static String getOntologyIri(OntModel model) {
return model.listResourcesWithProperty(RDF.type, OWL.Ontology).nextResource().toString();
}

private static OntModel loadOntModel(String resourcePath) {
InputStream is = RetrievePrefixesModuleTest.class.getResourceAsStream(resourcePath);

if (is == null) {
throw new IllegalArgumentException("Resource " + resourcePath + " not found.");
}
return loadOntModel(is);
}

private static OntModel loadOntModel(InputStream inputStream) {
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);

OntDocumentManager dm = OntDocumentManager.getInstance();
dm.setFileManager(FileManager.get());

ontModel.read(inputStream, null, FileUtils.langTurtle);
dm.loadImports(ontModel);
return ontModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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 org.apache.commons.codec.digest.DigestUtils;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
Expand Down Expand Up @@ -68,11 +67,7 @@ ExecutionContext executeSelf() {
q -> processFormEntity(q, null, constructedModel)
);

if (isReplace) {
return ExecutionContextFactory.createContext(constructedModel);
} else {
return ExecutionContextFactory.createContext(ModelFactory.createUnion(constructedModel, inpModel));
}
return createOutputContext(isReplace, constructedModel);
}

public boolean isReplace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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.form.JenaFormUtils;
import cz.cvut.spipes.form.JopaPersistenceUtils;
import org.apache.jena.rdf.model.Model;
Expand Down Expand Up @@ -88,11 +87,7 @@ ExecutionContext executeSelf() {
);
}

if (isReplace) {
return ExecutionContextFactory.createContext(constructedModel);
} else {
return ExecutionContextFactory.createContext(ModelFactory.createUnion(constructedModel, inpModel));
}
return createOutputContext(isReplace, constructedModel);
}

public boolean isSerializeUnansweredQuestions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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.form.JenaFormUtils;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
Expand Down Expand Up @@ -70,11 +69,7 @@ ExecutionContext executeSelf() {
}
);

if (isReplace) {
return ExecutionContextFactory.createContext(constructedModel);
} else {
return ExecutionContextFactory.createContext(ModelFactory.createUnion(constructedModel, inpModel));
}
return createOutputContext(isReplace, constructedModel);
}

public String getPossibleValuesQuery(Resource question) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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.form.JenaFormUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jena.rdf.model.Model;
Expand Down Expand Up @@ -69,11 +68,7 @@ ExecutionContext executeSelf() {
}
);

if (isReplace) {
return ExecutionContextFactory.createContext(constructedModel);
} else {
return ExecutionContextFactory.createContext(ModelFactory.createUnion(constructedModel, inpModel));
}
return createOutputContext(isReplace, constructedModel);
}

public boolean isReplace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,35 @@
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.sutime.AnnforModel;
import cz.cvut.spipes.sutime.DescriptorModel;
import cz.cvut.spipes.util.JenaUtils;
import cz.cvut.spipes.util.QueryUtils;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.AnnotationPipeline;
import edu.stanford.nlp.pipeline.POSTaggerAnnotator;
import edu.stanford.nlp.pipeline.TokenizerAnnotator;
import edu.stanford.nlp.pipeline.WordsToSentencesAnnotator;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.time.TimeAnnotations;
import edu.stanford.nlp.time.TimeAnnotator;
import edu.stanford.nlp.time.TimeExpression;
import edu.stanford.nlp.util.CoreMap;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.ReifiedStatement;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.*;
import org.apache.jena.vocabulary.RDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Construct;
import org.topbraid.spin.vocabulary.SP;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class SUTimeModuleNew extends AbstractModule {

Expand Down Expand Up @@ -157,11 +140,7 @@ public ExecutionContext executeSelf() {
inferredModel = JenaUtils.createUnion(inferredModel, inferredInSingleIterationModel);
}

if (isReplace) {
return ExecutionContextFactory.createContext(inferredModel);
} else {
return ExecutionContextFactory.createContext(ModelFactory.createUnion(defaultModel, inferredModel));
}
return createOutputContext(isReplace, inferredModel);
}

private Model analyzeModel(Model m) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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.util.JenaUtils;
import cz.cvut.spipes.util.QueryUtils;
import org.apache.jena.query.Query;
Expand Down Expand Up @@ -155,11 +154,7 @@ public ExecutionContext executeSelf() {
}
}

if (isReplace) {
return ExecutionContextFactory.createContext(inferredModel);
} else {
return ExecutionContextFactory.createContext(ModelFactory.createUnion(defaultModel, inferredModel));
}
return createOutputContext(isReplace, inferredModel);
}

protected String substituteQueryMarkers(int currentIteration, String queryStr) {
Expand Down
Loading