<commit_before>package am.app.mappingEngine.FedericoCaimiMatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.CardinalityRestriction;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
import com.hp.hpl.jena.ontology.MinCardinalityRestriction;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.ontology.Restriction;
import com.hp.hpl.jena.ontology.UnionClass;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import am.Utility;
import am.app.Core;
import am.app.mappingEngine.AbstractMatcher;
import am.app.mappingEngine.Alignment;
import am.app.mappingEngine.AlignmentMatrix;
import am.app.mappingEngine.AlignmentSet;
import am.app.mappingEngine.MatcherFactory;
import am.app.mappingEngine.MatchersRegistry;
import am.app.mappingEngine.referenceAlignment.ReferenceAlignmentMatcher;
import am.app.mappingEngine.referenceAlignment.ReferenceEvaluationData;
import am.app.mappingEngine.referenceAlignment.ReferenceEvaluator;
import am.app.ontology.Node;
import am.app.ontology.Ontology;
import am.userInterface.MatcherParametersDialog;

public class FedericoMatcher extends AbstractMatcher {
	double LABEL_TRESHOLD = 1.0; 
	double SUBSTRING_TRESHOLD = 1.0;
	double SUBCLASS_TRESHOLD = 0.6;
	double INDIVIDUAL_THRESHOLD = 0.5;
	double RANGE_DOMAIN_THRESHOLD = 0.9;
	double PROP_USAGE_THRESHOLD = 0.6;
	
	double PROPERTY_THRESHOLD = 0.8;
	double CLASS_THRESHOLD = 0.8;
	
	boolean individuals = true;
	boolean localname = false;
	boolean comment = false;
	boolean label = false;
	
	static boolean verbose = false; 
	
	private ArrayList<Node> sourceClassList;
	private ArrayList<Node> targetClassList;
	private ArrayList<Node> sourcePropList;
	private ArrayList<Node> targetPropList;
	
	private HashMap<Node, List<Restriction>> sourceRestrictions;
	private HashMap<Node, List<Restriction>> targetRestrictions;
	private HashMap<Restriction, Node> restrictions;
	private HashMap<OntProperty, List<Literal>> sourcePropValues;
	private HashMap<OntProperty, List<Literal>> targetPropValues;
	
	
	public FedericoMatcher(){
		super();
		minInputMatchers = 1;
		maxInputMatchers = 1;
	}
	
	@Override
	protected void matchEnd() {
		// TODO Auto-generated method stub
		super.matchEnd();
		//evaluate();
	}
	
	@SuppressWarnings("unchecked")
	protected void align() throws Exception {
		if (sourceOntology == null || targetOntology == null)
			return; // cannot align just one ontology
		
		sourceClassList = sourceOntology.getClassesList();
		targetClassList = targetOntology.getClassesList();
		sourcePropList = sourceOntology.getPropertiesList();
		targetPropList = targetOntology.getPropertiesList();
		
		//Initialize maps for information about restrictions
		initHashMaps();
		
		receiveInputMatrixes();		
		
		sourcePropValues = initPropValues(sourcePropList,sourceOntology);
		targetPropValues = initPropValues(targetPropList,targetOntology);
	
		//printPropValues();
		
		//Match by string and individuals
		classesMatrix = alignNodesOneByOne(sourceClassList,
				targetClassList, alignType.aligningClasses);
		
		propertiesMatrix = alignNodesOneByOne(sourcePropList,
				targetPropList, alignType.aligningProperties);
		
		//Match properties by similar values
		matchPropertyValues();		
				
		//Iterative part
		for (int i = 0; ; i++) {
			double totAlign = getNumberOfClassAlignments() + getNumberOfPropAlignments();
			//Match by superclasses and restriction on properties
			matchSuperclasses();
			//Match properties by range and domain	
			matchRangeAndDomain();
			//Match properties by their presence in restrictions
			matchPropertyUsage();
			//match sons of aligned classes
			matchSubClasses();
			//match sons of aligned properties
			matchSubProperties();
			double totAlign2 = getNumberOfClassAlignments() + getNumberOfPropAlignments();
			if(totAlign2==totAlign){
				if( Core.DEBUG_FCM ) System.out.println("CONVERGED IN "+(i+1)+" ITERATIONS");
				break;
			}
				
		}
		
		filterNonOntologyAlignments();
		//evaluate();
		
	}
	
	private void receiveInputMatrixes() {
		if(inputMatchers.size()>0){
			AbstractMatcher input = inputMatchers.get(0);
			classesMatrix = input.getClassesMatrix();
			propertiesMatrix = input.getPropertiesMatrix();
			//System.out.println();
		}
		
	}

	private void filterNonOntologyAlignments() {
		for (int i = 0; i < sourcePropList.size(); i++) {
			if(!sourcePropList.get(i).getUri().startsWith(sourceOntology.getURI())){
				for (int j = 0; j < targetPropList.size(); j++) {
					propertiesMatrix.set(i, j, null);
				}
			}
		}
		for (int j = 0; j < targetPropList.size(); j++) {
			if(!targetPropList.get(j).getUri().startsWith(targetOntology.getURI())){
				for (int i = 0; i < sourcePropList.size(); i++) {
					propertiesMatrix.set(i, j, null);
				}
			}
		}
		
	}

	private void evaluate() {
		ReferenceAlignmentMatcher refMatcher = (ReferenceAlignmentMatcher)MatcherFactory.getMatcherInstance(MatchersRegistry.ImportAlignment,0);
		MatcherParametersDialog dialog = new MatcherParametersDialog(refMatcher);
		if(dialog.parametersSet()) {
			refMatcher.setParam(dialog.getParameters());
			refMatcher.setThreshold(refMatcher.getDefaultThreshold());
			refMatcher.setMaxSourceAlign(refMatcher.getDefaultMaxSourceRelations());
			refMatcher.setMaxTargetAlign(refMatcher.getDefaultMaxTargetRelations());<commit_msg>Filter out classes with different URI from the ontology.<commit_after>package am.app.mappingEngine.FedericoCaimiMatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.CardinalityRestriction;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
import com.hp.hpl.jena.ontology.MinCardinalityRestriction;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.ontology.Restriction;
import com.hp.hpl.jena.ontology.UnionClass;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import am.Utility;
import am.app.Core;
import am.app.mappingEngine.AbstractMatcher;
import am.app.mappingEngine.Alignment;
import am.app.mappingEngine.AlignmentMatrix;
import am.app.mappingEngine.AlignmentSet;
import am.app.mappingEngine.MatcherFactory;
import am.app.mappingEngine.MatchersRegistry;
import am.app.mappingEngine.referenceAlignment.ReferenceAlignmentMatcher;
import am.app.mappingEngine.referenceAlignment.ReferenceEvaluationData;
import am.app.mappingEngine.referenceAlignment.ReferenceEvaluator;
import am.app.ontology.Node;
import am.app.ontology.Ontology;
import am.userInterface.MatcherParametersDialog;

public class FedericoMatcher extends AbstractMatcher {
	double LABEL_TRESHOLD = 1.0; 
	double SUBSTRING_TRESHOLD = 1.0;
	double SUBCLASS_TRESHOLD = 0.6;
	double INDIVIDUAL_THRESHOLD = 0.5;
	double RANGE_DOMAIN_THRESHOLD = 0.9;
	double PROP_USAGE_THRESHOLD = 0.6;
	
	double PROPERTY_THRESHOLD = 0.8;
	double CLASS_THRESHOLD = 0.8;
	
	boolean individuals = true;
	boolean localname = false;
	boolean comment = false;
	boolean label = false;
	
	static boolean verbose = false; 
	
	private ArrayList<Node> sourceClassList;
	private ArrayList<Node> targetClassList;
	private ArrayList<Node> sourcePropList;
	private ArrayList<Node> targetPropList;
	
	private HashMap<Node, List<Restriction>> sourceRestrictions;
	private HashMap<Node, List<Restriction>> targetRestrictions;
	private HashMap<Restriction, Node> restrictions;
	private HashMap<OntProperty, List<Literal>> sourcePropValues;
	private HashMap<OntProperty, List<Literal>> targetPropValues;
	
	
	public FedericoMatcher(){
		super();
		minInputMatchers = 1;
		maxInputMatchers = 1;
	}
	
	@Override
	protected void matchEnd() {
		// TODO Auto-generated method stub
		super.matchEnd();
		//evaluate();
	}
	
	@SuppressWarnings("unchecked")
	protected void align() throws Exception {
		if (sourceOntology == null || targetOntology == null)
			return; // cannot align just one ontology
		
		sourceClassList = sourceOntology.getClassesList();
		targetClassList = targetOntology.getClassesList();
		sourcePropList = sourceOntology.getPropertiesList();
		targetPropList = targetOntology.getPropertiesList();
		
		//Initialize maps for information about restrictions
		initHashMaps();
		
		receiveInputMatrixes();		
		
		sourcePropValues = initPropValues(sourcePropList,sourceOntology);
		targetPropValues = initPropValues(targetPropList,targetOntology);
	
		//printPropValues();
		
		//Match by string and individuals
		classesMatrix = alignNodesOneByOne(sourceClassList,
				targetClassList, alignType.aligningClasses);
		
		propertiesMatrix = alignNodesOneByOne(sourcePropList,
				targetPropList, alignType.aligningProperties);
		
		//Match properties by similar values
		matchPropertyValues();		
				
		//Iterative part
		for (int i = 0; ; i++) {
			double totAlign = getNumberOfClassAlignments() + getNumberOfPropAlignments();
			//Match by superclasses and restriction on properties
			matchSuperclasses();
			//Match properties by range and domain	
			matchRangeAndDomain();
			//Match properties by their presence in restrictions
			matchPropertyUsage();
			//match sons of aligned classes
			matchSubClasses();
			//match sons of aligned properties
			matchSubProperties();
			double totAlign2 = getNumberOfClassAlignments() + getNumberOfPropAlignments();
			if(totAlign2==totAlign){
				if( Core.DEBUG_FCM ) System.out.println("CONVERGED IN "+(i+1)+" ITERATIONS");
				break;
			}
				
		}
		
		filterNonOntologyAlignments();
		
		
		
		//evaluate();
		
	}
	
	private void receiveInputMatrixes() {
		if(inputMatchers.size()>0){
			AbstractMatcher input = inputMatchers.get(0);
			classesMatrix = input.getClassesMatrix();
			propertiesMatrix = input.getPropertiesMatrix();
			//System.out.println();
		}
		
	}

	private void filterNonOntologyAlignments() {
		
		for (int i = 0; i < sourceClassList.size(); i++) {
			if(!sourceClassList.get(i).getUri().startsWith(sourceOntology.getURI())){
				for (int j = 0; j < targetClassList.size(); j++) {
					classesMatrix.set(i, j, null);
				}
			}
		}
		for (int j = 0; j < targetClassList.size(); j++) {
			if(!targetClassList.get(j).getUri().startsWith(targetOntology.getURI())){
				for (int i = 0; i < sourceClassList.size(); i++) {
					classesMatrix.set(i, j, null);
				}
			}
		}
		
		
		for (int i = 0; i < sourcePropList.size(); i++) {
			if(!sourcePropList.get(i).getUri().startsWith(sourceOntology.getURI())){
				for (int j = 0; j < targetPropList.size(); j++) {
					propertiesMatrix.set(i, j, null);
				}
			}
		}
		for (int j = 0; j < targetPropList.size(); j++) {
			if(!targetPropList.get(j).getUri().startsWith(targetOntology.getURI())){
				for (int i = 0; i < sourcePropList.size(); i++) {
					propertiesMatrix.set(i, j, null);
				}
			}
		}
		
	}

	private void evaluate() {
		ReferenceAlignmentMatcher refMatcher = (ReferenceAlignmentMatcher)MatcherFactory.getMatcherInstance(MatchersRegistry.ImportAlignment,0);
		MatcherParametersDialog dialog = new MatcherParametersDialog(refMatcher);
		if(dialog.parametersSet()) {
			refMatcher.setParam(dialog.getParameters());
			refMatcher.setThreshold(refMatcher.getDefaultThreshold());
			refMatcher.setMaxSourceAlign(refMatcher.getDefaultMaxSourceRelations());
			refMatcher.setMaxTargetAlign(refMatcher.getDefaultMaxTargetRelations());
{'fraction_non_alphanumeric': 0.11245802277008764, 'fraction_numerical': 0.0050782209845196165, 'mean_word_length': 4.9619140625, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 31, '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': '7153999', 'n_tokens_mistral': 4278, 'n_tokens_neox': 3799, 'n_words': 743}