ReadOntologies.java 4.05 KB
Newer Older
Maxime Lefrançois's avatar
Maxime Lefrançois committed
package fr.emse.gitlab.saref.jobs.library;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.Lang;

import fr.emse.gitlab.saref.jobs.AbstractJobRunner;
import fr.emse.gitlab.saref.utils.SAREF;

public class ReadOntologies extends AbstractJobRunner {

	static final Map<String, String> PREFIXES = new HashMap<String, String>();
	static {
		PREFIXES.put("owl", "http://www.w3.org/2002/07/owl#");
		PREFIXES.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
		PREFIXES.put("xsd", "http://www.w3.org/2001/XMLSchema#");
		PREFIXES.put("dcterms", "http://purl.org/dc/terms/");
		PREFIXES.put("vann", "http://purl.org/vocab/vann/");
		PREFIXES.put("foaf", "http://xmlns.com/foaf/0.1/");
		PREFIXES.put("schema", "http://schema.org/");
		PREFIXES.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
		PREFIXES.put("voaf", "http://purl.org/vocommons/voaf#");
		PREFIXES.put("dce", "http://purl.org/dc/elements/1.1/");
		PREFIXES.put("dct", "http://purl.org/dc/terms/");
		PREFIXES.put("xml", "http://www.w3.org/XML/1998/namespace/");
		PREFIXES.put("saref", "https://saref.etsi.org/core/");
	}
	
	public ReadOntologies(File dir) {
		super(dir);
	}

	@Override
	protected void doJob0() {
		String repoName = getRepoName();
		String ontologyName = repoName.equals("saref-core") ? "saref.ttl" : repoName + ".ttl";
		checkOntology(ontologyName);
		checkExamples();
	}

	private void checkOntology(String ontologyName) {
		File ontologyFile = new File(directory, "ontology/" + ontologyName);
		Model model = ModelFactory.createDefaultModel();
		try (FileInputStream input = new FileInputStream(ontologyFile)) {
			model.read(input, null, Lang.TTL.getLabel());
		} catch (Exception ex) {
			error("Exception while reading the ontology file", ex);
			return;
		}
		final Map<String, String> prefixes = model.getNsPrefixMap();
		for(String s : PREFIXES.keySet()) {
			if(prefixes.containsKey(s)) {
				if(!prefixes.get(s).equals(PREFIXES.get(s))) {
					failure(String.format("Prefix `%s:` in the ontology file is expected to be equal to `<%s>`. Got: `<%s>`", s, PREFIXES.get(s), prefixes.get(s)));
				}
			}
		}
		for(Map.Entry<String, String> entry : prefixes.entrySet()) {
			String s = entry.getKey();
			String l = entry.getValue();
			if(l.contains("saref")) {
				if(!l.matches(SAREF.REGEX_ONTO_SERIES_URI)) {
					failure(String.format("Prefix `%s:` in the ontology file contains string \"saref\", but does not seem to match the official SAREF ontologies namespaces: `\\\\%s\\\\`. Got: `<%s>`", s, SAREF.REGEX_ONTO_SERIES_URI, l));
				}
			}
		}
	}

	private void checkExamples() {
		File dir = new File(directory, "examples");
		try {
			Files.walk(dir.toPath()).filter(p -> {
				return p.endsWith(".ttl");
			}).forEach(p -> {
				File exampleFile = p.toFile();
				Model model = ModelFactory.createDefaultModel();
				try (FileInputStream input = new FileInputStream(exampleFile)) {
					model.read(input, null, Lang.TTL.getLabel());
				} catch (Exception ex) {
					failure(String.format("Exception while reading the example file %s", exampleFile.toString()),
							ex.getClass().getSimpleName(), ex.getMessage());
				}
			});
		} catch (IOException ex) {
			error("Exception while walking the example directory", ex);
		}
	}

	private String getRepoName() {
		String REGEX = "^(?<ext>saref-core|saref4[a-z][a-z][a-z][a-z])";
		String name = directory.getName();
		Pattern pattern = Pattern.compile(REGEX);
		Matcher matcher = pattern.matcher(name);
		if (!matcher.find()) {
			error(String.format(
					"The SAREF pipeline must be run inside a directory whose name begins with `saref-core`, or `saref4abcd` (where abcd can be any sequence of four letters). Got: %s",
					name));
			return null;
		}
		return matcher.group("ext");
	}

}