diff --git a/src/fr/inrialpes/exmo/align/impl/DistanceAlignment.java b/src/fr/inrialpes/exmo/align/impl/DistanceAlignment.java index ef02be33ab401a6a13ac18582d2f2b9f8c5c5af5..9210a486e1ca1b83b11cac862e044ae93b20163a 100644 --- a/src/fr/inrialpes/exmo/align/impl/DistanceAlignment.java +++ b/src/fr/inrialpes/exmo/align/impl/DistanceAlignment.java @@ -1,7 +1,7 @@ /* * $Id$ * - * Copyright (C) INRIA Rhône-Alpes, 2003-2005 + * Copyright (C) INRIA Rhône-Alpes, 2003-2006 * * 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 @@ -100,11 +100,25 @@ public class DistanceAlignment extends BasicAlignment implements AlignmentProces // This is a 1:1 alignment in fact else setType("11"); - getSimilarity().initialize( (OWLOntology)getOntology1(), (OWLOntology)getOntology2(), alignment ); - getSimilarity().compute( params ); + sim.initialize( (OWLOntology)getOntology1(), (OWLOntology)getOntology2(), alignment ); + sim.compute( params ); + if ( params.getParameter("printMatrix") != null ) printDistanceMatrix(params); extract( getType(), params ); } + public void printDistanceMatrix( Parameters params ){ + System.out.println("\\documentclass{article}\n"); + System.out.println("\\usepackage{graphics}\n"); + System.out.println("\\begin{document}\n"); + System.out.println("\\begin{figure}"); + sim.printClassSimilarityMatrix("tex"); + System.out.println(); + sim.printPropertySimilarityMatrix("tex"); + System.out.println("\\caption{Class distance with measure "+(String)params.getParameter("stringFunction")+"}"); + System.out.println("\\end{figure}"); + System.out.println("\n\\end{document}"); + } + /** * Extract the alignment form the Similarity * There are theoretically 16 types of extractors composing the @@ -192,7 +206,6 @@ public class DistanceAlignment extends BasicAlignment implements AlignmentProces found = true; max = val; ind2 = current; } } - System.err.println(ind1+" -- "+ind2+" = "+max); if ( found ) addAlignCell(ind1,ind2, "=", max); } } diff --git a/src/fr/inrialpes/exmo/align/impl/MatrixMeasure.java b/src/fr/inrialpes/exmo/align/impl/MatrixMeasure.java index b45746166db9c2992d47bc126bb174a444c08e42..20de6aff55c88a1c35ba0c1109d391ea60753378 100644 --- a/src/fr/inrialpes/exmo/align/impl/MatrixMeasure.java +++ b/src/fr/inrialpes/exmo/align/impl/MatrixMeasure.java @@ -1,7 +1,7 @@ /* * $Id$ * - * Copyright (C) INRIA Rhône-Alpes, 2003-2005 + * Copyright (C) INRIA Rhône-Alpes, 2003-2006 * * 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 @@ -23,6 +23,7 @@ package fr.inrialpes.exmo.align.impl; import java.util.Iterator; import java.util.HashMap; import java.net.URI; +import java.text.NumberFormat; import org.semanticweb.owl.model.OWLOntology; import org.semanticweb.owl.model.OWLClass; @@ -57,15 +58,22 @@ public abstract class MatrixMeasure implements Similarity { public int nbclass2 = 0; // number of classes in onto2 public int nbprop1 = 0; // number of classes in onto1 public int nbprop2 = 0; // number of classes in onto2 + public int nbind1 = 0; // number of individuals in onto1 + public int nbind2 = 0; // number of individuals in onto2 public int i, j = 0; // index for onto1 and onto2 classes public int l1, l2 = 0; // length of strings (for normalizing) public HashMap classlist2 = null; // onto2 classes public HashMap classlist1 = null; // onto1 classes public HashMap proplist2 = null; // onto2 properties public HashMap proplist1 = null; // onto1 properties + public HashMap indlist2 = null; // onto2 individuals + public HashMap indlist1 = null; // onto1 individuals + + private NumberFormat numFormat = null; // printing public double clmatrix[][]; // distance matrix public double prmatrix[][]; // distance matrix + public double indmatrix[][]; // distance matrix public void initialize( OWLOntology onto1, OWLOntology onto2, Alignment align ){ initialize( onto1, onto2 ); @@ -79,6 +87,8 @@ public abstract class MatrixMeasure implements Similarity { classlist1 = new HashMap(); // onto1 classes proplist2 = new HashMap(); // onto2 properties proplist1 = new HashMap(); // onto1 properties + indlist2 = new HashMap(); // onto2 instances + indlist1 = new HashMap(); // onto1 instances try { // Create class lists @@ -104,6 +114,15 @@ public abstract class MatrixMeasure implements Similarity { proplist1.put( it.next(), new Integer(nbprop1) ); } prmatrix = new double[nbprop1+1][nbprop2+1]; + // Create individual lists + for ( Iterator it = onto2.getIndividuals().iterator(); it.hasNext(); nbind2++ ){ + indlist2.put( it.next(), new Integer(nbind2) ); + } + for ( Iterator it = onto1.getIndividuals().iterator(); it.hasNext(); nbind1++ ){ + indlist1.put( it.next(), new Integer(nbind1) ); + } + indmatrix = new double[nbind1+1][nbind2+1]; + } catch (OWLException e) { e.printStackTrace(); }; } @@ -117,6 +136,15 @@ public abstract class MatrixMeasure implements Similarity { clmatrix[((Integer)classlist1.get(cl1)).intValue()][((Integer)classlist2.get(cl2)).intValue()] = measure( cl1, cl2 ); } } + // Compute distances on individuals + // (this comes first because otherwise, it2 is defined) + for ( Iterator it2 = onto2.getIndividuals().iterator(); it2.hasNext(); ){ + OWLIndividual ind2 = (OWLIndividual)it2.next(); + for ( Iterator it1 = onto1.getIndividuals().iterator(); it1.hasNext(); ){ + OWLIndividual ind1 = (OWLIndividual)it1.next(); + indmatrix[((Integer)indlist1.get(ind1)).intValue()][((Integer)indlist2.get(ind2)).intValue()] = measure( ind1, ind2 ); + } + } // Compute distances on properties ConcatenatedIterator it2 = new ConcatenatedIterator(onto2.getObjectProperties().iterator(), @@ -131,13 +159,11 @@ public abstract class MatrixMeasure implements Similarity { prmatrix[((Integer)proplist1.get(pr1)).intValue()][((Integer)proplist2.get(pr2)).intValue()] = measure( pr1, pr2 ); } } - } catch (OWLException e) { e.printStackTrace(); } + } catch (Exception e) { e.printStackTrace(); } } public double getIndividualSimilarity( OWLIndividual i1, OWLIndividual i2 ){ - // JE: non finished... - int i,j = 0; - return 0.; + return indmatrix[((Integer)indlist1.get(i1)).intValue()][((Integer)indlist2.get(i2)).intValue()]; } public double getClassSimilarity( OWLClass c1, OWLClass c2 ){ return clmatrix[((Integer)classlist1.get(c1)).intValue()][((Integer)classlist2.get(c2)).intValue()]; @@ -145,4 +171,37 @@ public abstract class MatrixMeasure implements Similarity { public double getPropertySimilarity( OWLProperty p1, OWLProperty p2 ){ return prmatrix[((Integer)proplist1.get(p1)).intValue()][((Integer)proplist2.get(p2)).intValue()]; } + + // Not an efficient access... + public void printClassSimilarityMatrix( String type ){ + // Number format class to format the values + numFormat = NumberFormat.getInstance(); + numFormat.setMinimumFractionDigits( 2 ); + numFormat.setMaximumFractionDigits( 2 ); + System.out.print("\\begin{tabular}{r|"); + for ( int i = 0; i < nbclass1 ; i++ ) System.out.print("c"); + System.out.println("}"); + try { + for ( Iterator it1 = onto1.getClasses().iterator(); it1.hasNext(); ){ + OWLClass cl1 = (OWLClass)it1.next(); + System.out.print(" & \\rotatebox{90}{"+cl1.getURI().getFragment()+"}"); + } + System.out.println(" \\\\ \\hline"); + for ( Iterator it2 = onto2.getClasses().iterator(); it2.hasNext(); ){ + OWLClass cl2 = (OWLClass)it2.next(); + System.out.print(cl2.getURI().getFragment()); + for ( Iterator it1 = onto1.getClasses().iterator(); it1.hasNext(); ){ + OWLClass cl1 = (OWLClass)it1.next(); + System.out.print(" & "+numFormat.format(clmatrix[((Integer)classlist1.get(cl1)).intValue()][((Integer)classlist2.get(cl2)).intValue()])); + } + System.out.println("\\\\"); + } + } catch (OWLException e) { e.printStackTrace(); }; + System.out.println("\n\\end{tabular}"); + } + + // Considered not useful so far + public void printPropertySimilarityMatrix( String type ){}; + public void printIndividualSimilarityMatrix( String type ){}; + } diff --git a/src/fr/inrialpes/exmo/align/impl/Similarity.java b/src/fr/inrialpes/exmo/align/impl/Similarity.java index 29823a2bc84e7205ab8ac446975e7248baae1796..10491dfeb55b19c62f5382fd30077dfdd5e930c2 100644 --- a/src/fr/inrialpes/exmo/align/impl/Similarity.java +++ b/src/fr/inrialpes/exmo/align/impl/Similarity.java @@ -1,7 +1,7 @@ /* * $Id$ * - * Copyright (C) INRIA Rhône-Alpes, 2004 + * Copyright (C) INRIA Rhône-Alpes, 2004, 2006 * * 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 @@ -54,9 +54,19 @@ public interface Similarity public double getClassSimilarity( OWLClass c1, OWLClass c2 ); public double getPropertySimilarity( OWLProperty p1, OWLProperty p2); public double getIndividualSimilarity( OWLIndividual i1, OWLIndividual i2 ); + + public void printClassSimilarityMatrix( String type ); + public void printPropertySimilarityMatrix( String type ); + public void printIndividualSimilarityMatrix( String type ); + // New implementation - public double measure( OWLClass c1, OWLClass c2 ) throws OWLException; - public double measure( OWLProperty p1, OWLProperty p2) throws OWLException; - public double measure( OWLIndividual i1, OWLIndividual i2 ) throws OWLException; + // JE: this is better as a new implementation. + // however, currently the implementation does not follow it: + // the abstract matrix class provides the get- accessors and the + // concrete classes implement measure as their computation function. + // This is not clean. What should be done is: + public double measure( OWLClass c1, OWLClass c2 ) throws Exception; + public double measure( OWLProperty p1, OWLProperty p2) throws Exception; + public double measure( OWLIndividual i1, OWLIndividual i2 ) throws Exception; } diff --git a/src/fr/inrialpes/exmo/align/impl/method/StringDistAlignment.java b/src/fr/inrialpes/exmo/align/impl/method/StringDistAlignment.java new file mode 100644 index 0000000000000000000000000000000000000000..635f28ba4b4247d9444afe7ba5edb449dd56a9ae --- /dev/null +++ b/src/fr/inrialpes/exmo/align/impl/method/StringDistAlignment.java @@ -0,0 +1,108 @@ +/* + * $Id: StringDistAlignment.java 149 2005-06-17 08:25:34Z euzenat $ + * + * Copyright (C) INRIA Rhône-Alpes, 2003-2006 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU 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 + */ + +package fr.inrialpes.exmo.align.impl.method; + +import java.util.Iterator; +import java.util.Hashtable; +import java.lang.reflect.Method; + +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.model.OWLClass; +import org.semanticweb.owl.model.OWLProperty; +import org.semanticweb.owl.model.OWLIndividual; +import org.semanticweb.owl.model.OWLException; + +import fr.inrialpes.exmo.align.impl.DistanceAlignment; +import fr.inrialpes.exmo.align.impl.MatrixMeasure; +import fr.inrialpes.exmo.align.impl.Similarity; + +import org.semanticweb.owl.align.Alignment; +import org.semanticweb.owl.align.AlignmentProcess; +import org.semanticweb.owl.align.AlignmentException; +import org.semanticweb.owl.align.Parameters; + +/** + * Represents an OWL ontology alignment. An ontology comprises a number of + * collections. Each ontology has a number of classes, properties and + * individuals, along with a number of axioms asserting information + * about those objects. + * + * An improvement of that class is that, since it is based on names only, + * it can match freely property names with class names... + * + * @author Jérôme Euzenat + * @version $Id: NameEqAlignment.java 149 2005-06-17 08:25:34Z euzenat $ + */ + +public class StringDistAlignment extends DistanceAlignment implements AlignmentProcess { + + Method dissimilarity = null; + + /** Creation **/ + public StringDistAlignment( OWLOntology onto1, OWLOntology onto2 ){ + super( onto1, onto2 ); + setSimilarity( new MatrixMeasure() { + public double measure( OWLClass cl1, OWLClass cl2 ) throws Exception{ + String[] params = { cl1.getURI().getFragment(), cl2.getURI().getFragment() }; + //if ( debug > 4 ) + return ((Double)dissimilarity.invoke( null, params )).doubleValue(); + } + public double measure( OWLProperty pr1, OWLProperty pr2 ) throws Exception{ + String[] params = { pr1.getURI().getFragment(), pr2.getURI().getFragment() }; + return ((Double)dissimilarity.invoke( null, params )).doubleValue(); + } + public double measure( OWLIndividual id1, OWLIndividual id2 ) throws Exception{ + String[] params = { id1.getURI().getFragment(), id2.getURI().getFragment() }; + return ((Double)dissimilarity.invoke( null, params )).doubleValue(); + } + } ); + setType("**"); + }; + + /* Processing */ + public void align( Alignment alignment, Parameters params ) throws AlignmentException, OWLException { + //ignore alignment; + double threshold = 1.; // threshold above which distances are to high + + // Get function from params + String f = (String)params.getParameter("stringFunction"); + try { + String fname = "equalDistance"; + if ( f != null ) fname = f.trim(); + Class sClass = Class.forName("java.lang.String"); + Class[] mParams = { sClass, sClass }; + dissimilarity = Class.forName("fr.inrialpes.exmo.align.impl.method.StringDistances").getMethod( fname, mParams ); + } catch (Exception e) { throw new AlignmentException("Missing Class or method");}; + //NoSuchMethodException, ClassNotFoundException + + // Initialize matrix + getSimilarity().initialize( (OWLOntology)getOntology1(), (OWLOntology)getOntology2(), alignment ); + + // Compute similarity/dissimilarity + getSimilarity().compute( params ); + + // Print matrix if asked + if ( params.getParameter("printMatrix") != null ) printDistanceMatrix( params ); + + // Extract alignment + extract( type, params ); + } +}