package OWL2generator;

/**
 *  The <b><code>OWL2_Generator</code></b> class serves as the core application logic
 *  within a Java framework for generating OWL/XML ontologies: It
 *  leverages text files containing configuration details to
 *  construct the ontology structure. This class acts as the
 *  central controller, directing the overall process of ontology
 *  generation based on user-provided configuration files.
 *  <p>
 *  <b>Key Functionalities:</b>
 *  <p>
 *  Processes Command Line Arguments: Evaluates user-provided
 *  arguments to determine the input/output directory for text
 *  files used during generation.
 *  <p>
 *  Controls Ontology Generation Sequence: Orchestrates the
 *  creation of the ontology by calling dedicated modules responsible for:
 *  <p>
 *  <ul>
 *  <li>Generating the OWL header
 *  <li>Defining class structures
 *  <li>Setting class disjointness
 *  <li>Defining object property triplets (relationships between individuals)
 *  </ul>
 *  <p>
 *  Manages Configuration Files: Employs separate text files for
 *  each generation step, allowing for modularity and customization.
 *  <p>
 *  Logs Console Messages: Provides informative messages during
 *  execution to track progress.
 *  <p>
 *  <b>Additional Notes:</b>
 *  <p>
 *  The class utilizes pre-defined file paths and comments within the
 *  code, suggesting potential improvements for user-configurable settings.
 *  <p>
 *  The comments indicate the framework's reliance on external classes.
 *  <p>
 *  <b>Environment:</b> 
 *  <ul>
 *  <li>    IDE:              Eclipse IDE for Java Developers
 *  <li>    Version:          2021-12 (4.22.0)
 *  <li>    Build id:         20211202-1639
 *  <li>    HW Model Name:    iMac, MacOS Monterey, 12.5.1
 *  <li>    Processor Name:   Quad-Core Intel Core i5
 *  <li>    Processor Speed:  3.2 GHz
 *  <li>    Memory:           32 GB 1867 MHz DDR3
 *  <li>    Disk:             APPLE SSD SM0256G    
 *  <li>    Serial:           DGKRC080GG7V
 *  </ul>
 *  @version 1-001
 *  @since   2024/05/28
 *  @author  Edit Hlaszny (https://www.edithlaszny.eu/) 
 */

public class OWL2_Generator 
{ 
	/**
	 *  The base URI plays a significant role in managing namespaces, resolving 
	 *  references, and potentially aiding in disambiguation within OWL/XML 
	 *  encoded ontologies.
	 */
	private final String baseURI                   = "https://www.hlaszny.com/ontology/OWLschemafy/data/" ;

	/**
	 *  Any text following the <code>comment</code> string on the first position 
	 *  is ignored by this simple parser in the class
	 */
	private final String commentChar               = "!" ;

	/**
	 *  This app is a Java-based, text file controlled OWL2 ontology generator. 
	 *  The text files should have a common directory where they are stored.
     */
    private final String defaultIOdirName          = "/Users/hlasznyedit/Documents/OWLschemafy_V_2_002/RunnableEnv/" ;

	/**
	 *  The OWL class individuals are with leading zeroes serial numbers suffixed.
	 *  The <code>leadingZeroesInNameSuffix</code> defines the length the serial number.
	 */
	private final int    leadingZeroesInNameSuffix = 5 ;   //  e.g. individual name = actor_ID00012

	/**
	 *  Name of the JAR-file making the app stand alone runnable
	 */
    private final String runableJarfile            = "owl2gen.jar" ;
	
    /**
     *  Text file name, which defines the disjoint OWL classes
     */
	private       String disjointClassNameFile     = defaultIOdirName + "P2_DisjointClassDefs.txt" ; 
	
    /**
     *  Text file name, which defines the parameters of the ontology header
     */
	private       String headerDefFile             = defaultIOdirName + "P2_HeaderDef.txt" ; 
	
    /**
     *  Text file name, which defines the ontology OWL class names. The file is TAB-indented,
     *  being able to use it to Protégé
     */
    private       String indentedClassNames        = defaultIOdirName + "P2_classStructure.txt" ; 
    
    /**
     *  Text file name, which defines the ontology OWL class names. The file is TAB-indented,
     *  being able to use it to Protégé
     */
    private       String classAnnotations          = defaultIOdirName + "P2_classAnnotation.txt" ; 
	
    /**
     *  Text file name, which defines the name of the OWL2/XML encoded result file
     */
	private       String OWLresultFile             = defaultIOdirName + "resultOntology/prince2_ontology.owl" ;
	
    /**
     *  Text file name, which defines the triplets
     */
	private       String tripletDefFile            = defaultIOdirName + "P2_tripletDefs.txt" ; 

	/**
	 *  You can overwrite this value with the 1st calling argument.
	 *  See the method evalCallingArgs()
	 */
	private       String IOdirectoryName           = "" ;

	/**
	 *  Keywords in the control files
	 */
    private final String keyWord_START_OF_DISJOINT_CLASS_SET = "START_OF_DISJOINT_CLASS_SET" ;
    private final String keyWord_END_OF_DISJOINT_CLASS_SET   = "END_OF_DISJOINT_CLASS_SET"   ;
    private final String keyWord_START_OF_TRIPLET_SET        = "START_OF_TRIPLET_SET";
    private final String keyWord_END_OF_TRIPLET_SET          = "END_OF_TRIPLET_SET"  ;
    private final String keyWord_CLASS_NAME                  = "CLASS_NAME"         ;
    private final String keyWord_CLASS_ANNOTATIONS           = "CLASS_ANNOTATIONS"  ;
	
    /**
     *  Silent mode> no log messages will be displayed
     */
    private final boolean silentMode                         = false ;
    
	/**
	 *  The main method starts the app.
	 *  
	 *  @param args Java standard calling arguments 
	 */
	public final static void main(String args[])
	{
		OWL2_Generator o1g = new OWL2_Generator() ;

		o1g.generateOWL2ontology(args) ;
		
	}   //  end of method main() 
 
	/**
	 *  The method evaluates the calling arguments, and set the default directory,
	 *  if it is set by the $1 arg.
	 *  
	 *  @param args Java standard calling arguments
	 */
	private void evalCallingArgs(String args[])
	{
		if (args.length == 0) 
		{
			this.IOdirectoryName = this.defaultIOdirName ;
		}
		else if (args.length == 1)
		{
			this.IOdirectoryName = args[0] ;
		}
		else
		{
            log("Usage: java -jar " + runableJarfile + " [IOdirectoryName]\n" +
	        "                 the IO directory name should contain the following files: \n" +
	        "                 P2_headerTemplate.owl     -  the default OWL geader \n" +
	        "                 P2_classStructure.txt     -  tab-indented OWL class names \n" +
	        "                 P2_DisjointClassDefs.txt  -  tab-indented disjoint class names \n" +
	        "                 P2_tripletDefs.txt        -  triplet definitions \n") ;
			System.exit(-1);
		}

        this.disjointClassNameFile = this.IOdirectoryName + "P2_DisjointClassDefs.txt" ;
        this.classAnnotations      = this.IOdirectoryName + "P2_classAnnotation.txt" ;
        this.indentedClassNames    = this.IOdirectoryName + "P2_classStructure.txt" ; 
        this.headerDefFile         = this.IOdirectoryName + "P2_HeaderDef.txt" ; 
        this.tripletDefFile        = this.IOdirectoryName + "P2_tripletDefs.txt" ; 
        this.OWLresultFile         = this.IOdirectoryName + "resultOntology/prince2_ontology.owl" ;

	}   //  end of method evalCallingArgs()

	/**
	 *  The method controls the sequence of the ontology generation. The app consists of 
	 *  the following modules (each of them is controlled by the corresponding text 
	 *  parameter files): 
	 *  <br />
	 *  1) generating the OWL-header
	 *  <br />
	 *  2) defining the class structures
	 *  <br />
	 *  3) setting class disjointness
	 *  <br />
	 *  4) defining triplets
	 *  
	 *  @param args Java standard calling arguments
	 */
    private void generateOWL2ontology(String args[])
    {
        evalCallingArgs(args) ; 

        log("OWLschemafy V1-002 started from " + this.IOdirectoryName + "\n") ;
     
            //  -------------------------------------------------------------
            new WriteOWLHeader(this.headerDefFile, 
            		           this.OWLresultFile,
            		           this.baseURI,
                               this.commentChar) ;

            //  -------------------------------------------------------------
            new WriteClassStructure(this.indentedClassNames,   
                                    this.OWLresultFile, 
                                    this.baseURI,
                                    false) ;     

            //  -------------------------------------------------------------
            new WriteDisjointClassDefs(this.disjointClassNameFile          ,
                                       this.OWLresultFile                  ,
                                       this.baseURI                        ,
                                       keyWord_START_OF_DISJOINT_CLASS_SET ,
                                       keyWord_END_OF_DISJOINT_CLASS_SET   ,
                                       this.commentChar) ;

            //  -------------------------------------------------------------
            new WriteTriplets(this.classAnnotations       ,
            		          this.tripletDefFile         , 
                              this.OWLresultFile          ,
                              this.baseURI                ,
                              keyWord_START_OF_TRIPLET_SET,
                              keyWord_END_OF_TRIPLET_SET  ,
                              keyWord_CLASS_NAME          ,
                              keyWord_CLASS_ANNOTATIONS   ,
                              this.commentChar     ,
                              this.leadingZeroesInNameSuffix
                             ) ; 

            log("OWLschemafy V1-002 has succesfully been completed:     \n" +
                "Result: " + this.OWLresultFile + "\n") ;
            
            System.exit(0);
          
    }   //  and of method generateOWL2ontology()
	
    /**
     *  Writes text message to the console
     *  
     *  @param logEntry displayed message  
     */
    public void log(String logEntry)
    {
        if (!this.silentMode)
            System.out.print(logEntry) ;  
        
    }   //  end of log()
 
}   //  end of class OWL2_Generator
