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

org.protege.owl.codegeneration.impl.ProtegeJavaMapping 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.HashMap;
import java.util.Map;

import org.protege.owl.codegeneration.WrappedIndividual;
import org.protege.owl.codegeneration.inference.CodeGenerationInference;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;

public class ProtegeJavaMapping {
    private CodeGenerationInference inference;
    private OWLDataFactory dataFactory;
    private OWLOntology ontology;
    
    private Map protegeMap  = new HashMap();
    private Map, JavaAndOwlBean> interfaceMap = new HashMap, JavaAndOwlBean>();
    private Map, JavaAndOwlBean> implementationMap = new HashMap, JavaAndOwlBean>();

    public void initialize(OWLOntology ontology, CodeGenerationInference inference) {
        this.ontology = ontology;
        this.inference = inference;
        dataFactory = ontology.getOWLOntologyManager().getOWLDataFactory();
    }
    
    public void add(String protegeClassName, 
                    Class javaInterface, 
                    Class javaImplementation) {
        OWLClass protegeClass = OWLManager.getOWLDataFactory().getOWLClass(IRI.create(protegeClassName));
        add(protegeClass, javaInterface, javaImplementation);
    }
    
    public void add(OWLClass protegeClass, 
                    Class javaInterface, 
                    Class javaImplementation) {
        JavaAndOwlBean entry = new JavaAndOwlBean(protegeClass, javaInterface, javaImplementation);
        protegeMap.put(protegeClass, entry);
        interfaceMap.put(javaInterface, entry);
        implementationMap.put(javaImplementation, entry);
    }
    
    public Class getJavaInterfaceFromOwlClass(OWLClass cls) {
        JavaAndOwlBean bean = protegeMap.get(cls);
        return bean != null ? bean.getJavaInterface() : null;
    }
    
    public OWLClass getOwlClassFromJavaInterface(Class javaInterface) {
        JavaAndOwlBean bean = interfaceMap.get(javaInterface);
        return bean != null ? bean.getProtegeClass() : null;
    }
    
    @SuppressWarnings("unchecked")
    public  X create(Class javaInterface, String name) {
        JavaAndOwlBean entry = interfaceMap.get(javaInterface);
        if (entry == null) {
            return null;
        }
        OWLClass cls = entry.getProtegeClass();
        OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(name));
        ontology.getOWLOntologyManager().addAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(cls, individual));
        return constructImplementation((Class) entry.getJavaImplementation(), individual.getIRI());
    }
    
    public boolean canAs(WrappedIndividual resource, Class javaInterface) {
        if (javaInterface.isAssignableFrom(resource.getClass())) {
            return true;
        }
        return getJavaImplementation(resource.getOwlIndividual(), javaInterface) != null;
    }
    
    public   X as(WrappedIndividual resource, Class javaInterface) {
        if (javaInterface.isAssignableFrom(resource.getClass())) {
            return javaInterface.cast(resource);
        }
        Class type = getJavaImplementation(resource.getOwlIndividual(), javaInterface);
        return type != null ? constructImplementation(type, resource.getOwlIndividual().getIRI()) : null;
    }
    
    private  X constructImplementation(Class implType, IRI id) {
        try {
            Constructor con = implType.getConstructor(new Class[] { CodeGenerationInference.class, IRI.class});
            return con.newInstance(new Object[] { inference, id });
        }
        catch (Throwable t) {
            ClassCastException classcast = new ClassCastException("Resource " + id.toString() + " could not be cast to type " + implType);
            classcast.initCause(t);
            throw classcast;
        }
    }
    
    private  Class getJavaImplementation(OWLNamedIndividual resource, Class javaInterface) {
        Collection protegeTypes = inference.getTypes(resource);
        for (OWLClass type  : protegeTypes) {
            JavaAndOwlBean entry = protegeMap.get(type);
            if (entry == null) {
                continue;
            }
            Class javaImplementationClass = entry.getJavaImplementation();
            if (javaInterface.isAssignableFrom(javaImplementationClass)) {
                return javaImplementationClass.asSubclass(javaInterface);
            }
        }
        return null;
    }
    
    private static class JavaAndOwlBean {
        private OWLClass protegeClass;
        private Class javaInterface;
        private Class javaImplementation;
        
        public JavaAndOwlBean(OWLClass protegeClass,
                     Class javaInterface,
                     Class javaImplementation) {
            this.protegeClass = protegeClass;
            this.javaInterface = javaInterface;
            this.javaImplementation = javaImplementation;
        }

        public OWLClass getProtegeClass() {
            return protegeClass;
        }

        public Class getJavaInterface() {
            return javaInterface;
        }

        public Class getJavaImplementation() {
            return javaImplementation;
        }
        
        @Override
        public String toString() {
            return "";
        }
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy