<commit_before>package am.application.mappingEngine.referenceAlignment;

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

import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import am.Utility;
import am.application.mappingEngine.AbstractMatcher;
import am.application.mappingEngine.AbstractMatcherParametersPanel;
import am.application.mappingEngine.Alignment;
import am.application.mappingEngine.baseSimilarity.BaseSimilarityMatcherParametersPanel;
import am.application.ontology.Node;
import am.output.OutputController;

public class ReferenceAlignmentMatcher extends AbstractMatcher {

		//Constants
		/**Formats for reference files*/
	    public final static String REF5 = "AM exported file format";
		public final static String REF0 = "OAEI standard format";
		public final static String REF1 = "OAEI-2007 i.e. weapons, wetlands...";
		public final static String REF2 = "TXT: sourcename(tab)targetname";
		public final static String REF3= "TXT: sourceDesc(tab)sourceName(tab)targetName(tab)targetDesc(tab)";

		/**Formats for output files*/
		public final static String OUTF1 = "TXT-1";
		
		private ArrayList<MatchingPair> referenceListOfPairs;
		
	public ReferenceAlignmentMatcher() {
		super();
		needsParam = true;
		maxSourceAlign = ANY_INT;
		maxTargetAlign = ANY_INT;
		threshold = 0.01;
		
	}
	
	
	
	protected void beforeAlignOperations()throws Exception{
		super.beforeAlignOperations();
		referenceListOfPairs = readReferenceFile();
		if(referenceListOfPairs == null || referenceListOfPairs.size() == 0) {
			Utility.displayMessagePane("The reference file selected doen not contain any alignment.\nPlease check the format.", null);
		}
	}
	
    protected Alignment alignTwoNodes(Node source, Node target , alignType typeOfNodes) {
    	String sname = source.getLocalName();
    	String tname = target.getLocalName();
    	Alignment a = new Alignment(source, target, 0);//if I don't find any alignment in the reference about this 2 nodes i will create an alignment with 0 similarity
    	MatchingPair mp = null;
    	if(referenceListOfPairs != null) {
    		Iterator<MatchingPair> it = referenceListOfPairs.iterator();
    		while(it.hasNext()) {
    			mp = it.next();
    			if(mp.sourcename.equals(sname)&& mp.targetname.equals(tname)) {
    				a = new Alignment(source, target, mp.similarity);
    				it.remove();
    			}
    		}
    	}
		return a;
	}
	
	
	/**
	 * Parse the reference file: the file gets opened, depending on fileformat the file gets parsed differently, invoking a specific parse for that format
	 * If a developer is going to add a new format file, should add an if case and invokes the specific new parser.
	 * @param filename
	 * @return ArrayList of refpairs
	 */
	public ArrayList<MatchingPair> readReferenceFile(){
		ArrayList<MatchingPair> result = new ArrayList<MatchingPair>();
		ReferenceAlignmentParameters parameters = (ReferenceAlignmentParameters)param;
		try {
		    //Open the reference file
			BufferedReader input;
			input = new BufferedReader(new FileReader(parameters.fileName));
			//depending on file format a different parser is invoked
			if(parameters.format.equals(REF0)) {
				result = parseRefFormat0();
			}
			else if(parameters.format.equals(REF1)) {
				result = parseRefFormat1(input);
			}
			else if(parameters.format.equals(REF2)) {
				result = parseRefFormat2(input);<commit_msg>Changed the way the ReferenceAlignmentMatcher transversed the matching pairs list.  Much faster now.<commit_after>package am.application.mappingEngine.referenceAlignment;

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

import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import am.Utility;
import am.application.mappingEngine.AbstractMatcher;
import am.application.mappingEngine.AbstractMatcherParametersPanel;
import am.application.mappingEngine.Alignment;
import am.application.mappingEngine.AlignmentMatrix;
import am.application.ontology.Node;
import am.output.OutputController;

public class ReferenceAlignmentMatcher extends AbstractMatcher {

		//Constants
		/**Formats for reference files*/
	    public final static String REF5 = "AM exported file format";
		public final static String REF0 = "OAEI standard format";
		public final static String REF1 = "OAEI-2007 i.e. weapons, wetlands...";
		public final static String REF2 = "TXT: sourcename(tab)targetname";
		public final static String REF3= "TXT: sourceDesc(tab)sourceName(tab)targetName(tab)targetDesc(tab)";

		/**Formats for output files*/
		public final static String OUTF1 = "TXT-1";
		
		private ArrayList<MatchingPair> referenceListOfPairs;
		
	public ReferenceAlignmentMatcher() {
		super();
		needsParam = true;
		maxSourceAlign = ANY_INT;
		maxTargetAlign = ANY_INT;
		threshold = 0.01;
		
	}
	
	
	
	protected void beforeAlignOperations()throws Exception{
		super.beforeAlignOperations();
		referenceListOfPairs = readReferenceFile();
		if(referenceListOfPairs == null || referenceListOfPairs.size() == 0) {
			Utility.displayMessagePane("The reference file selected doen not contain any alignment.\nPlease check the format.", null);
		}
	}
	
	
	/**
	 * The transversal will be based on the referenceListOfPairs
	 */
	
	protected AlignmentMatrix alignNodesOneByOne(ArrayList<Node> sourceList, ArrayList<Node> targetList, alignType typeOfNodes) throws Exception {
		AlignmentMatrix matrix = new AlignmentMatrix(sourceList.size(), targetList.size());
		Node source;
		Node target;
		Alignment alignment = null; //Temp structure to keep sim and relation between two nodes, shouldn't be used for this purpose but is ok
		
		
		MatchingPair mp = null;
		if( referenceListOfPairs != null ) {
			Iterator<MatchingPair> it = referenceListOfPairs.iterator();
			
			// Iterate over the list of pairs from the file
			while( it.hasNext() ) {
				mp = it.next(); // get the first matching pair from the list
				
				
				// find the source node in the source ontology
				for( int i = 0; i < sourceList.size(); i++ ) {
					source = sourceList.get(i);
			    	String sname = source.getLocalName();
					if( mp.sourcename.equals(sname) ) {
						// we have found a match for the source node
						
						for( int j = 0; j < targetList.size(); j++ ) {
							target = targetList.get(j);
							String tname = target.getLocalName();
							
							if( mp.targetname.equals(tname) ) {
								// we have found a match for the target node, it means a valid alignment
								
								if( !this.isCancelled() ) alignment = new Alignment( source, target, mp.similarity );
								matrix.set(i, j, alignment);
								
							}
							
						}
						
					}
				}
				
				it.remove();
				
				if( isProgressDisplayed() ) {
					stepDone();
					updateProgress();
				}
				
				
			}
		}
		
		return matrix;
	}
	
	
	/**
	 * Parse the reference file: the file gets opened, depending on fileformat the file gets parsed differently, invoking a specific parse for that format
	 * If a developer is going to add a new format file, should add an if case and invokes the specific new parser.
	 * @param filename
	 * @return ArrayList of refpairs
	 */
	public ArrayList<MatchingPair> readReferenceFile(){
		ArrayList<MatchingPair> result = new ArrayList<MatchingPair>();
		ReferenceAlignmentParameters parameters = (ReferenceAlignmentParameters)param;
		try {
		    //Open the reference file
			BufferedReader input;
			input = new BufferedReader(new FileReader(parameters.fileName));
			//depending on file format a different parser is invoked
			if(parameters.format.equals(REF0)) {
				result = parseRefFormat0();
			}
			else if(parameters.format.equals(REF1)) {
				result = parseRefFormat1(input);
			}
			else if(parameters.format.equals(REF2)) {
				result = parseRefFormat2(input);
{'fraction_non_alphanumeric': 0.09449404761904762, 'fraction_numerical': 0.006820436507936508, 'mean_word_length': 3.9057177615571774, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '381852', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2266, 'n_words': 680}