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

org.protege.owl.codegeneration.impl.FactoryHelper Maven / Gradle / Ivy

Go to download

Generate Java code from an OWL ontology in the Protege Desktop editing environment.

The newest version!
package org.protege.owl.codegeneration.impl;

import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.protege.owl.codegeneration.CodeGenerationRuntimeException;
import org.protege.owl.codegeneration.HandledDatatypes;
import org.protege.owl.codegeneration.WrappedIndividual;
import org.protege.owl.codegeneration.inference.CodeGenerationInference;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class FactoryHelper {
	private OWLOntology owlOntology;
	private OWLOntologyManager manager;
	private OWLDataFactory owlDataFactory;
	private CodeGenerationInference inference;
	
	public FactoryHelper(OWLOntology ontology, CodeGenerationInference inference) {
		this.owlOntology = ontology;
		this.inference = inference;
		manager = ontology.getOWLOntologyManager();
		owlDataFactory = manager.getOWLDataFactory();
	}
	
	public void flushOwlReasoner() {
	    inference.flush();
	}
	
	
	public  X createWrappedIndividual(String name, OWLClass type, Class c) {
		OWLNamedIndividual i = owlDataFactory.getOWLNamedIndividual(IRI.create(name));
		manager.addAxiom(owlOntology, owlDataFactory.getOWLClassAssertionAxiom(type, i));
		if (!inference.canAs(i, type)) {
			return null;
		}
		return getWrappedIndividual(name, c);
	}
	
	public  X getWrappedIndividual(String name, OWLClass type, Class c) {
		IRI iri = IRI.create(name);
		OWLNamedIndividual i = owlDataFactory.getOWLNamedIndividual(iri);
		if (!inference.canAs(i, type)) {
			return null;
		}
		return getWrappedIndividual(name, c);
	}
	
	private  X getWrappedIndividual(String name, Class c) {
		try {
    		Constructor constructor = c.getConstructor(CodeGenerationInference.class, IRI.class);
    		return constructor.newInstance(inference, IRI.create(name));
		}
		catch (Exception e) {
			throw new CodeGenerationRuntimeException(e);
		}
	}
	
	public  Collection getWrappedIndividuals(OWLClass owlClass, Class c) {
		Set wrappers = new HashSet();
		for (OWLNamedIndividual i : inference.getIndividuals(owlClass)) {
			wrappers.add(getWrappedIndividual(i.getIRI().toString(), c));
		}
		return wrappers;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy