package OWL2generator ;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File ;
import java.io.FileWriter ;
import java.io.IOException ;
import java.util.ArrayList;

/**
 *  The <b><code>WriteClassAnnotation</code></b> class is responsible for
 *  generating OWL class annotations and their corresponding individual
 *  instances within a Java framework for OWL/XML ontology creation. It
 *  relies on text file input and external utility methods. This class
 *  contributes to the ontology generation process by enriching
 *  classes with descriptive information and creating a foundation
 *  for associating data with them.
 *  <p>
 *  <b>Key Functionalities:</b>
 *  <p>
 *  Writes Class Annotations: Generates OWL code snippets using the
 *  <code>SharedUtils</code> class to define annotations (descriptive text) for
 *  specified classes.
 *  <p>
 *  Creates Individual Instances: Creates Individual objects for
 *  each annotated class, potentially used for future population
 *  with data properties.
 *  <p>
 *  Writes OWL Content: Writes the generated <code>OWL</code> code for annotations
 *  and individuals to a designated output file.
 *  <p>
 *  Handles File I/O: Manages file creation, writing, and closing
 *  operations.
 *  <p>
 *  <b>Additional Notes:</b>
 *  <p>
 *  The class relies on pre-defined file paths and utilizes comments
 *  within the code, suggesting potential improvements for
 *  user-configurable settings.
 *  <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.hlaszny.eu/ edithlaszny@gmail.com)
 */

public class AddClassAnnotation 
{ 
    private  int                   countOfAs   = 0 ;
    private  ArrayList<Individual> individuals = new ArrayList<>() ;
    private  BufferedReader        reader      = null ; 
    private  OWL2_Generator        OWL2G       = new OWL2_Generator() ;
    private  SharedUtils           shu         = null ;
    private  FileWriter            writer      = null ;

    AddClassAnnotation (String inFilePath,
                        String outFilePath,
                        String baseURI,
                        String classNameToken,
                        String classAnnotationToken,
                        String commentChar
                       )
    {
        this.shu = new SharedUtils(baseURI) ;
        
        try 
        {
            /**
             *  Append mode (set to false for overwrite)
             */
            this.writer = new FileWriter(new File(outFilePath), true) ;
            this.reader = new BufferedReader(new FileReader(inFilePath));

            String line ;
            String className  = null ;
            String annotation = null ;

            while ((line = reader.readLine()) != null) 
            {
                line = line.trim() ;

                if (line.startsWith("CLASS_NAME")) 
                {
                    className = line.substring(line.indexOf("=") + 2).trim();
                    
                    /**
                     *  Reset annotation for new class
                     */
                    annotation = null; 
                    
                } 
                else if (line.startsWith("CLASS_ANNOTATION") && className != null) 
                {
                    annotation = line.substring(line.indexOf("=") + 2).trim();
                    addClassAnnotationAndCreateIndividual(className, annotation);
                    
                    this.countOfAs++ ;
                }
            }
        } 
        catch (IOException e) 
        {
            System.err.println("Error on IO file operation: " + e.getMessage()) ;
              
        } 
        finally 
        {
            try 
            {
                this.reader.close() ; 
                this.writer.close() ;
            } 
            catch (IOException e) 
            {
                System.err.println("Error closing file(s): " + e.getMessage()) ;
            }
            
        }   //  end of try/catch/finally block
                
        /**
         *  Logging of the output volume
         */
        OWL2G.log(new StringBuilder()
                      .append("    asserted " + this.countOfAs + " class annotation(s)\n")
                      .toString()
                 ) ; 
        
    }   //  end of constructor
    
    private void addClassAnnotationAndCreateIndividual(String className,
                                                       String annotation
                                                      )  throws IOException 
    {
        this.writer.write(this.shu.genAnnotationAssertion(className, annotation)) ;
                
        Individual i = new Individual(className, 0, annotation) ; 
        
        this.individuals.add(i) ;
    }
    
    public  ArrayList<Individual> getIndividuals()
    {
        return this.individuals ;
        
    }   //  end of method getIndividuals()
        
}   //  end of class AddClassAnnotation 
