Commit 862907e9 authored by Maxime Lefrançois's avatar Maxime Lefrançois

guess versionname from versionIRI metadata

parent ddec1f0f
......@@ -25,14 +25,24 @@
*/
package fr.emse.gitlab.saref;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.jena.atlas.RuntimeIOException;
import org.apache.jena.riot.Lang;
import org.slf4j.Logger;
import fr.emse.gitlab.saref.entities.SAREFCore;
import fr.emse.gitlab.saref.entities.SAREFExtension;
import fr.emse.gitlab.saref.entities.SAREFNamedGraph;
import fr.emse.gitlab.saref.entities.SAREFProject;
import fr.emse.gitlab.saref.entities.SAREFVersionName;
......@@ -143,4 +153,26 @@ public class SAREF {
}
return msg;
}
public static void loadModel(SAREFNamedGraph namedGraph, File file, Logger logger) {
try (FileInputStream input = new FileInputStream(file)) {
namedGraph.getModel().read(input, SAREF.BASE, Lang.TTL.getLabel());
} catch (Exception ex) {
if (ex instanceof RuntimeIOException && ex.getCause() instanceof MalformedInputException) {
namedGraph.getModel().removeAll();
try (InputStreamReader input2 = new InputStreamReader(new FileInputStream(file),
Charset.defaultCharset())) {
namedGraph.getModel().read(input2, SAREF.BASE, Lang.TTL.getLabel());
} catch (Exception ex2) {
namedGraph.getModel().removeAll();
String msg = getMessage("turtle", namedGraph.getIRI());
logger.error(msg, ex2);
}
} else {
String msg = getMessage("turtle", namedGraph.getIRI());
logger.error(msg, ex);
}
}
}
}
......@@ -29,6 +29,9 @@ import java.io.IOException;
import java.util.Set;
import java.util.function.Supplier;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.OWL2;
import org.apache.jena.vocabulary.RDF;
import org.semanticweb.HermiT.ReasonerFactory;
import org.semanticweb.owl.explanation.api.Explanation;
import org.semanticweb.owl.explanation.api.ExplanationGenerator;
......@@ -65,6 +68,7 @@ import fr.emse.gitlab.saref.SAREFPipeline;
import fr.emse.gitlab.saref.SAREFPipeline.Mode;
import fr.emse.gitlab.saref.entities.SAREFExample;
import fr.emse.gitlab.saref.entities.SAREFVersion;
import fr.emse.gitlab.saref.entities.SAREFVersionName;
import fr.emse.gitlab.saref.managers.parsers.JenaModelDocumentFormatFactory;
import fr.emse.gitlab.saref.managers.parsers.SourceOntologyParser;
......
......@@ -33,6 +33,7 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.jena.vocabulary.OWL2;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
......@@ -46,10 +47,12 @@ import fr.emse.gitlab.saref.SAREFPipeline.Mode;
import fr.emse.gitlab.saref.SAREFPipelineException;
import fr.emse.gitlab.saref.entities.SAREFCore;
import fr.emse.gitlab.saref.entities.SAREFExtension;
import fr.emse.gitlab.saref.entities.SAREFNamedGraph;
import fr.emse.gitlab.saref.entities.SAREFProject;
import fr.emse.gitlab.saref.entities.SAREFRepository;
import fr.emse.gitlab.saref.entities.SAREFVersion;
import fr.emse.gitlab.saref.entities.SAREFVersionName;
import fr.emse.gitlab.saref.utils.Languages;
/**
* Reads the repository and all the branches
......@@ -130,11 +133,15 @@ public class RepositoryFactory extends SAREFErrorLogger {
final String msg = String.format(getMessage(MESSAGE.release_branch, originalBranch));
errorLogger.warn(msg);
throw new SAREFPipelineException(msg);
} else if (isTarget) {
}
SAREFVersionName vn = guessVersion(project, directory);
if (isTarget && vn == null) {
final String msg = getMessage(MESSAGE.develop_branch, originalBranch);
errorLogger.warn(msg);
originalVersionName = SAREFVersionName.DEFAULT;
} else {
originalVersionName = vn;
}
originalVersionName = SAREFVersionName.DEFAULT;
} else {
int major = Integer.parseInt(m.group(SAREF.REGEX_VERSION_MAJOR_VAR));
int minor = Integer.parseInt(m.group(SAREF.REGEX_VERSION_MINOR_VAR));
......@@ -158,6 +165,19 @@ public class RepositoryFactory extends SAREFErrorLogger {
}
}
private SAREFVersionName guessVersion(SAREFProject project, File directory) {
try {
File ontologyDirectory = new File(directory, "ontology");
File ontologyFile = new File(ontologyDirectory, project.getOntologyFileName(Languages.TEXT_TURTLE));
SAREFNamedGraph namedGraph = new SAREFNamedGraph("null");
SAREF.loadModel(namedGraph, ontologyFile, errorLogger);
String uri = namedGraph.getModel().listObjectsOfProperty(OWL2.versionIRI).next().asResource().getURI();
return SAREF.extractVersionName(uri);
} catch (Exception ex) {
return null;
}
}
private void readVersions(Git git, SAREFRepository repository) throws GitAPIException {
final List<Ref> allBranches;
final Pattern pattern;
......
......@@ -185,7 +185,7 @@ public class RepositoryManager extends SAREFErrorLogger {
} else {
versionName = version.getVersionName().toString();
}
loadModel(version, ontologyFile, pipeline.getLogger(SAREF.getMessage("clause", repositoryName, versionName, "9.4.1")));
SAREF.loadModel(version, ontologyFile, pipeline.getLogger(SAREF.getMessage("clause", repositoryName, versionName, "9.4.1")));
forEachTerm(version.getModel(), (term) -> {
if(term.getProject().equals(project)) {
version.definesTerm(term);
......@@ -202,7 +202,7 @@ public class RepositoryManager extends SAREFErrorLogger {
}).forEach(p -> {
SAREFExample example = new SAREFExample(version, p);
File exampleFile = p.toFile();
loadModel(example, exampleFile, pipeline.getLogger(SAREF.getMessage("clause_for", repositoryName, versionName, exampleFile, "9.6.1")));
SAREF.loadModel(example, exampleFile, pipeline.getLogger(SAREF.getMessage("clause_for", repositoryName, versionName, exampleFile, "9.6.1")));
version.getExamples().put(example.getName(), example);
forEachTerm(example.getModel(), (term) -> {
example.exemplifiesTerm(term);
......@@ -214,28 +214,6 @@ public class RepositoryManager extends SAREFErrorLogger {
}
}
private void loadModel(SAREFNamedGraph namedGraph, File file, Logger logger) {
try (FileInputStream input = new FileInputStream(file)) {
namedGraph.getModel().read(input, SAREF.BASE, Lang.TTL.getLabel());
} catch (Exception ex) {
if (ex instanceof RuntimeIOException && ex.getCause() instanceof MalformedInputException) {
namedGraph.getModel().removeAll();
try (InputStreamReader input2 = new InputStreamReader(new FileInputStream(file),
Charset.defaultCharset())) {
namedGraph.getModel().read(input2, SAREF.BASE, Lang.TTL.getLabel());
} catch (Exception ex2) {
namedGraph.getModel().removeAll();
String msg = getMessage(MESSAGE.turtle, namedGraph.getIRI());
logger.error(msg, ex2);
}
} else {
String msg = getMessage(MESSAGE.turtle, namedGraph.getIRI());
logger.error(msg, ex);
}
}
}
private void forEachTerm(Model model, Consumer<SAREFTerm> consumer) {
model.listStatements().forEachRemaining(stmt -> {
Resource s = stmt.getSubject();
......
reset=Error when reseting repository to original branch %s
checkout=Error when checking out version %s
turtle=The document %s shall contain the sources of an ontology in the Turtle 1.1 format. See detailed error below.
ioexception=Error while checking the `examples` directory for %s %s
load_exception=Error while loading the repository %s
......@@ -28,3 +28,4 @@ ignoreExamples=ignore examples
ignoreExamples_tooltip=Only generates the report for the ontology
options=3. Select execution options
run=4. Run the Pipeline
turtle=The document %s shall contain the sources of an ontology in the Turtle 1.1 format. See detailed error below.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment