package am.extension.userfeedback.experiments;

import am.app.mappingEngine.Alignment;
import am.app.mappingEngine.Mapping;
import am.app.ontology.Ontology;

import am.extension.multiUserFeedback.storage.FeedbackAgregation;
import am.extension.userfeedback.ExecutionSemantics;
import am.extension.userfeedback.SaveFeedback;
import am.extension.userfeedback.UserFeedback;
import am.extension.userfeedback.UserFeedback.Validation;
import am.extension.userfeedback.evaluation.CandidateSelectionEvaluation;
import am.extension.userfeedback.evaluation.PropagationEvaluation;
import am.extension.userfeedback.inizialization.FeedbackLoopInizialization;
import am.extension.userfeedback.logic.UFLControlLogic;
import am.extension.userfeedback.propagation.FeedbackPropagation;
import am.extension.userfeedback.selection.CandidateSelection;
import am.extension.userfeedback.ui.UFLProgressDisplay;

public abstract class UFLExperiment {

	public final UFLExperimentSetup							setup;  
	
	public ExecutionSemantics 								initialMatcher;
	public FeedbackLoopInizialization<UFLExperiment>        dataInizialization;
	public CandidateSelection<UFLExperiment> 				candidateSelection;
	public CandidateSelectionEvaluation 					csEvaluation;
	public UserFeedback										userFeedback;
	public FeedbackPropagation< UFLExperiment>				feedbackPropagation;
	public PropagationEvaluation< UFLExperiment>			propagationEvaluation;
	public UFLProgressDisplay								gui;
	public SaveFeedback< UFLExperiment>						saveFeedback;
	public 	FeedbackAgregation<UFLExperiment>					feedbackAggregation;

	protected Ontology sourceOntology;	
	protected Ontology targetOntology;
	protected Alignment<Mapping> referenceAlignment = null;
	
	/**
	 * These mappings were validated by the user as being CORRECT.
	 */
    public Alignment<Mapping>				correctMappings;
    
    /**
     * These mappings were validated by the user as being INCORRECT.
     */
    public Alignment<Mapping>				incorrectMappings;
    
    private int iterationNumber = 0;

    public UFLExperiment(UFLExperimentSetup setup) {
		this.setup = setup;
	}
    
	public Ontology getSourceOntology()             { return sourceOntology; }
	public void     setSourceOntology(Ontology ont) { this.sourceOntology = ont; }
	public Ontology getTargetOntology()             { return targetOntology; }
	public void     setTargetOntology(Ontology ont) { this.targetOntology = ont; }
	
	public Alignment<Mapping> getReferenceAlignment() { return referenceAlignment; }
	public void setReferenceAlignment(Alignment<Mapping> ref) { this.referenceAlignment = ref; }
	
	
	public abstract Alignment<Mapping>  getFinalAlignment();
	public abstract void				info(String line);   // FIXME: Change this, or get rid of it. Or learn how to use log4j.
	
	public abstract UFLControlLogic<? extends UFLExperiment>	getControlLogic();
	
	public abstract boolean 			experimentHasCompleted();  // return true if the experiment is done, false otherwise.
	
	public int getIterationNumber() { return iterationNumber; }
	
	public void	newIteration() {
		if( userFeedback.getUserFeedback() == Validation.CORRECT ) {
			if( correctMappings == null ) {
				correctMappings = new Alignment<Mapping>(getSourceOntology().getID(), getTargetOntology().getID());
			}
			correctMappings.add( userFeedback.getCandidateMapping() );
		} else if( userFeedback.getUserFeedback() == Validation.INCORRECT ) {
			if( incorrectMappings == null ) {
				incorrectMappings = new Alignment<Mapping>(getSourceOntology().getID(), getTargetOntology().getID());
			}
			incorrectMappings.add( userFeedback.getCandidateMapping() );
		}		
		
		setIterationNumber(getIterationNumber() + 1); 
	}
	
	public void setGUI(UFLProgressDisplay gui) { this.gui = gui; }
	public void setIterationNumber(int iterationNumber) {
		this.iterationNumber = iterationNumber;
	}
	
	/**
	 * @return A human-readable description of the experiment that is displayed
	 *         to the user.
	 */
	public abstract String getDescription(); 
	

}

@@ -41,16 +41,49 @@
 ments;%0A%0A
+import org.apache.log4j.Logger;%0A%0A
 import a
@@ -113,16 +113,16 @@
 gnment;%0A
-
 import a
@@ -183,17 +183,16 @@
 tology;%0A
-%0A
 import a
@@ -587,32 +587,112 @@
 tionEvaluation;%0A
+import am.extension.userfeedback.experiments.UFLExperimentParameters.Parameter;%0A
 import am.extens
@@ -1032,16 +1032,92 @@
 ment %7B%0A%0A
+%09private static final Logger LOG = Logger.getLogger(UFLExperiment.class);%0A%09%0A
 %09public 
@@ -2245,24 +2245,24 @@
 up setup) %7B%0A
-
 %09%09this.setup
@@ -2271,16 +2271,355 @@
  setup;%0A
+%09%09%0A%09%09String log = setup.parameters.getParameter(Parameter.LOGFILE);%0A%09%09if( log == null ) %7B%0A%09%09%09log = %22settings/tmp/uflLog.%22 + System.currentTimeMillis() + %22.txt%22;%0A%09%09%09setup.parameters.setParameter(Parameter.LOGFILE, log);%0A%09%09%09LOG.error(%22The LOGFILE parameter has not been set for this experiment. Log file is defaulting to %5C%22%22+log+%22%5C%22.%22);%0A%09%09%7D%0A
 %09%7D%0A    %0A

Set the default LOGFILE parameter.