All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.protege.webprotege.forms.AxiomTemplatesParser Maven / Gradle / Ivy

The newest version!
package edu.stanford.protege.webprotege.forms;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.functional.parser.OWLFunctionalSyntaxOWLParser;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

/**
 * Matthew Horridge
 * Stanford Center for Biomedical Informatics Research
 * 2019-11-15
 */
public class AxiomTemplatesParser {

    private final Logger logger = LoggerFactory.getLogger(AxiomTemplatesParser.class);

    @Inject
    public AxiomTemplatesParser() {
    }

    public Set parseAxiomTemplate(Collection axiomTemplates) {
        var templatesDocument = String.join("\n", axiomTemplates);
        var ontologyDocument = String.format("Ontology(%s)", templatesDocument);
        try {
            var parser = new OWLFunctionalSyntaxOWLParser();
            var ontologyManager = OWLManager.createOWLOntologyManager();
            var ontology = ontologyManager.createOntology();
            parser.parse(new StringDocumentSource(ontologyDocument),
                         ontology,
                         new OWLOntologyLoaderConfiguration());
            return ontology.getAxioms();
        } catch(OWLOntologyCreationException e) {
            logger.error("Error creating ontology", e);
            return Collections.emptySet();
        } catch(IOException e) {
            logger.error("Error parsing ontology", e);
            return Collections.emptySet();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy