/* * $Id$ * * Copyright (C) INRIA, 2010 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** * JE: 05/08/2010 * This is an example for the computations in OMBook. * Exercise C7: Measure aggregation * It aggregates in 4 different ways the results of two matchers * */ package org.ontologymatching.book; import fr.inrialpes.exmo.align.impl.Similarity; import fr.inrialpes.exmo.align.impl.MatrixMeasure; import fr.inrialpes.exmo.align.impl.DistanceAlignment; import fr.inrialpes.exmo.align.impl.method.StringDistAlignment; import fr.inrialpes.exmo.ontowrap.LoadedOntology; import fr.inrialpes.exmo.ontowrap.OntowrapException; import fr.inrialpes.exmo.ontosim.string.StringDistances; import org.semanticweb.owl.align.Alignment; import org.semanticweb.owl.align.AlignmentProcess; import org.semanticweb.owl.align.AlignmentException; import java.util.Collection; import java.util.Vector; import java.util.Set; import java.util.HashSet; import java.util.Properties; import java.net.URI; import java.io.FileReader; import java.io.StreamTokenizer; import java.io.File; import java.io.StringReader; import java.io.IOException; import java.io.FileNotFoundException; /** * OMBook answer to exercise C.7 */ public class AggregateAlignment extends DistanceAlignment implements AlignmentProcess { static final int DEFAULT = 0; static final int MAX = 1; static final int WPROD = 2; static final int WSUM = 3; static final int OWA = 4; int method = DEFAULT; double weight1 = .5; double weight2 = .5; DistanceAlignment former = null; DistanceAlignment latter = null; protected class AggregateMatrixMeasure extends MatrixMeasure { private Similarity sim1 = null; private Similarity sim2 = null; public void initialize( LoadedOntology onto1, LoadedOntology onto2, Alignment align ){ // create the matrices and all structures super.initialize( onto1, onto2, align ); // Initialize Gloss specific structures } public void init( Similarity sim1, Similarity sim2 ){ this.sim1 = sim1; this.sim2 = sim2; } // We only do aggretion public double measure( double val1, double val2 ) throws Exception { switch ( method ) { case MAX : return Math.max( val1, val2 ); case WPROD : return val1*weight1 * val2*weight2; case WSUM : return val1*weight1 + val2*weight2; case OWA : return weight1*Math.max( val1, val2 )+weight2*Math.min( val1, val2 ); case DEFAULT : return Math.min( val1, val2 ); } return 0.; } public double classMeasure( Object cl1, Object cl2 ) throws Exception { return measure( sim1.getClassSimilarity( cl1, cl2 ), sim2.getClassSimilarity( cl1, cl2 ) ); } public double propertyMeasure( Object pr1, Object pr2 ) throws Exception { return measure( sim1.getPropertySimilarity( pr1, pr2 ), sim2.getPropertySimilarity( pr1, pr2 ) ); } public double individualMeasure( Object id1, Object id2 ) throws Exception { return measure( sim1.getIndividualSimilarity( id1, id2 ), sim2.getIndividualSimilarity( id1, id2 ) ); } } /** Creation **/ public AggregateAlignment(){ setSimilarity( new AggregateMatrixMeasure() ); setType("**"); }; /** Creation **/ public void init( Object onto1, Object onto2 ) throws AlignmentException { super.init( onto1, onto2 ); // create and initialize sub alignments former = new StringDistAlignment(); latter = new InstanceBasedAlignment(); former.init( onto1, onto2 ); latter.init( onto1, onto2 ); } /** Processing **/ public void align( Alignment alignment, Properties prop ) throws AlignmentException { //loadInit( alignment ); boolean printMatrix = false; if ( prop.getProperty("printMatrix") != null ) printMatrix = true; prop.remove( "printMatrix" ); if ( prop.getProperty( "method" ) != null ) { if ( prop.getProperty( "method" ).equals("max") ) method = MAX; else if ( prop.getProperty( "method" ).equals("wprod") ) method = WPROD; else if ( prop.getProperty( "method" ).equals("wsum") ) method = WSUM; else if ( prop.getProperty( "method" ).equals("owa") ) method = OWA; } if ( prop.getProperty( "w1" ) != null ) { weight1 = Double.parseDouble( prop.getProperty( "w1" ) ); weight2 = 1. - weight1; } // Call the sub alignments prop.setProperty( "stringFunction", "levenshteinDistance" ); former.align( alignment, prop ); prop.setProperty( "imeasure", "substring" ); prop.setProperty( "cmeasure", "singlel" ); latter.align( alignment, prop ); // Aggregate the similarity AggregateMatrixMeasure sim = (AggregateMatrixMeasure)getSimilarity(); sim.init( former.getSimilarity(), latter.getSimilarity() ); sim.initialize( ontology1(), ontology2(), alignment ); // Compute the similarity sim.compute( prop ); // Print matrix if asked prop.setProperty( "algName", getClass()+"/"+method ); if ( printMatrix ) printDistanceMatrix( prop ); // Extract alignment extract( type, prop ); } }