JavaCodeGenerationUsingAPI

From Protege Wiki

Jump to: navigation, search

This is example code for a test class that uses the Protege-OWL API to invoke the OWL Java Code Generator.

import java.io.File;

import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.javacode.JavaCodeGenerator;
import edu.stanford.smi.protegex.owl.javacode.ProjectBasedJavaCodeGeneratorOptions;
import edu.stanford.smi.protegex.owl.model.OWLModel;


public class JavaCodeGenerationTest {

    public static void main(String[] args) {
        try {
	    //create OWL Model
            OWLModel owlModel = 
                ProtegeOWL.createJenaOWLModelFromURI("file:///C:/Program%20Files/Protege_3.4_beta/examples/pizza/pizza.owl");
			
            //create and set options for the Java Code Generator 
	    ProjectBasedJavaCodeGeneratorOptions options = new ProjectBasedJavaCodeGeneratorOptions(owlModel);

	    options.setOutputFolder(new File("C:\\test"));
	    options.setPackage("protege.codegenerator.test");

            //the following 2 lines have no effect because the specified 
            //values are the default values for those properties
	    options.setFactoryClassName("MyFactory");
	    options.setAbstractMode(false);

	    //new option since Protege 3.4 Beta Build 130
	    options.setPrefixMode(true);

	    //for the options not specified (e.g. setSetMode(boolean value)) the default value will be used 
			
	    //create and run Java Code Generator
	    JavaCodeGenerator code_generator = new JavaCodeGenerator(owlModel, options);
	    code_generator.createAll();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

Personal tools