/**
 * Class Property
 * This class manages the conversion of an RDF triplet (subject / predicate / 
 * object) to an English sentence.
 * An instance of this class will point to an Element Node of the RDF code
 * whose name is a property name (ie a 'property node'). Moreover, such an
 * instance will have another attribute given as a parameter while being
 * constructed which is the id of the statement object (the 'about' attribute
 * of the generator node of the AElementRDF which calls the construction).
 * Eventually, given the convention that says that the object of a statement
 * must be identified as an attribute of the property node, and not as a child
 * node of this one, an instance of Property has effectivly access to the 3
 * elements of the RDF statement.
 * Each element of the triplet (or statement) has a dedicated method in charge
 * of converting it in English, (subjectDesc, printPredicate, objectDesc)
 * thanx to methods of classes Sentence and AElementSVG.
 **/

package axsvg;

import java.io.*;
import fr.dyade.koala.dom.*;
import fr.dyade.koala.dom.util.*;
import org.w3c.dom.*;
import java.util.*; 


public class Property {
    private Element generator;
    private String id;
    private Hashtable tableSVG;
    private String propertyName;
    private String resourceId;


    /**
     * method Property
     * The constructor of the class
     * @param gene : The Element node of the RDF code whose name is the
     * property that will be represented by the Property being constructed.
     * Basically, a child of the AElementRDF generator node being described
     * (with spitDesc method).
     * @param ident : the id of the entity being described, corresponding to
     * the object of the RDF statement that will be managed by the Property
     * being constructed.
     * @param table : the hashtable holding the SVG entities of the document,
     * identified by their 'id' attribute. Knowing this table is useful for
     * the Property being constructed:
     * It allows this one to access the SVG description of any entity, and to
     * know wether or not an SVG entity has already been mentionned in the
     * global description.
     **/
    public Property(Element gene, String ident, Hashtable table) {
	generator = gene;
	id = ident;
	tableSVG = table;
	propertyName = generator.getTagName();
	resourceId = generator.getAttribute("resource");
    }


    /**
     * method getResourceId
     * @return the resourceId attribute of the calling object.
     * This attribute corresponds to the id of the object in the
     * RDF statement managed by the calling object.
     **/
    public String getResourceId() {
	return(resourceId);
    }


    /**
     * method subjectDesc
     * @param a boolean which is true if the RDF statement processed is the
     * first one to involve the subject of this statement.
     * This method presents the subject of the RDF statement processed by the
     * calling instance :
     * First of all, if firstLoop is false, it prints a linkword (Moreover,
     * Furthermore or Also), followed by ", the entity $id", where $id is the
     * value of the id private attribute of the calling instance (ie the id of
     * the object in the RDF statement). Else :
     * If the entity corresponding to this subject has not yet been mentionned
     * (ie if there is a key equals to its id in the hashtable tableSVG, which
     * at the beginning contains all the SVG nodes which have an id), it
     * prints "An entity called $id".
     * It then prints the SVG description (ie the content of the node 'desc'
     * in the SVG code, thanx to the method AElementSVG.getDesc) preceded by
     * "which" and removes the key corresponding to $id in tableSVG (the
     * entity has then been already 'mentionned').
     * Else it only prints "The entity $id".
     **/
    public void subjectDesc(boolean firstLoop) {
	if(firstLoop == true) {
	    AElementSVG eltSVG = (AElementSVG)tableSVG.get(id);
	    if(eltSVG == null) {
		System.out.print("The entity "+id+" ");
	    }
	    else {
		if(eltSVG.getDesc().equals("")) {
		    System.out.print("An entity called "+id+" ");
		}
		else {
		    System.out.print("An entity called "+id+", which "+
				     eltSVG.getDesc()+", ");
		}    
		 tableSVG.remove(id);
	    }
	}
	else {
	    System.out.print(linkWord()+", the entity "+id+" ");
	}
    return;
    }
    

    /**
     * method printPredicate
     * @param the object of class sentence holding the information
     * relative to the RDF Schema corresponding to the namespace axsvg.
     * Calls the sentence methods that will print the predicate
     * of the RDF statement being processed.
     * If the property is intransitive, also calls the impression
     * of the attributes of the node (ie the objects of the satement).
     **/
    public boolean printPredicate(Sentence sentence) {
	boolean transitive;

	transitive = sentence.print(propertyName);
	if(!transitive) {
	    sentence.print(generator);
	}
	return(transitive);
    }
    
    /**
     * method linkWord
     * @return a String randomly chosen among "Also", "Moreover", and
     * "Furthermore"..
     **/
    private String linkWord() {
	Random de = new Random();
	int i = de.nextInt(3);

	if(i == 0) return("Moreover");
	if(i == 1) return("Furthermore");
	if(i == 2) return("Also");
	
	return("De casse, relancez");
    }


    /**
     * method objectDesc
     * @param the object of class sentence holding the information
     * relative to the RDF Schema corresponding to the namespace axsvg.
     * Presents the object of the RDF statement being processed, in case
     * this one is transitive (resourceId != null).
     * Also calls the Sentence method that will print the attributes of
     * the property node generator of the calling Property instance.
     * (prints all the objects of the statement, becoming then a
     * multiple-statement).
     **/
    public void objectDesc(Sentence attributeSentence) {
	AElementSVG eltSVG = (AElementSVG)tableSVG.get(resourceId);
		    if(eltSVG == null) {
			System.out.print("the entity "+resourceId);
		    }
		    else {
			if(eltSVG.getDesc().equals("")) {
			    System.out.print("an entity called "+resourceId);
			}
			else {
			    System.out.print("an entity called "+resourceId+
					     ",which "+eltSVG.getDesc());
			}    
			tableSVG.remove(resourceId);
		    }
	attributeSentence.print(generator);
    }
    
}
