// Parliament is licensed under the BSD License from the Open Source
// Initiative, http://www.opensource.org/licenses/bsd-license.php
//
// Copyright (c) 2023, BBN Technologies, Inc.
// All rights reserved.

package com.bbn.parliament.ontology_bundle;

import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;

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

public class OntAccess {
	public static enum OntType { FOR_HUMANS, FOR_MACHINES }

	public static final String ONT_RSRC_FOR_HUMANS = "OntologyForHumans.ttl";
	public static final String ONT_RSRC_FOR_MACHINES = "OntologyForMachines.ttl";

	private OntAccess() {}	// prevents instantiation

	public static String getOntResourceName(OntType ontType) {
		return (ontType == OntType.FOR_HUMANS) ? ONT_RSRC_FOR_HUMANS : ONT_RSRC_FOR_MACHINES;
	}

	public static InputStream getOntResourceAsStream(OntType ontType) {
		var rsrcName = getOntResourceName(ontType);
		var result = OntAccess.class.getResourceAsStream(rsrcName);
		if (result == null) {
			throw new MissingResourceException("Unable to load ontology resource",
				OntAccess.class.getName(), rsrcName);
		}
		return result;
	}

	public static Model getOntResourceAsModel(OntType ontType) throws IOException {
		Lang lang = RDFLanguages.filenameToLang(getOntResourceName(ontType), Lang.TURTLE);
		try (InputStream is = getOntResourceAsStream(ontType)) {
			return ModelFactory.createDefaultModel().read(is, null, lang.getName());
		}
	}
}
