From abf101db728404455c2fbc7b94f3685f9444d0c5 Mon Sep 17 00:00:00 2001 From: Maria-Elena Rosoiu <rosoiu.maria@gmail.com> Date: Fri, 22 Jul 2011 14:13:57 +0000 Subject: [PATCH] 'modular architecture' --- .../exmo/align/gen/ArbitraryTest.java | 200 +++++++ .../exmo/align/gen/ClassHierarchy.java | 11 +- .../exmo/align/gen/GenerateBenchmark.java | 545 ++++++++++++++++++ .../exmo/align/gen/OntologyModifier.java | 476 ++++++++------- .../exmo/align/gen/ParametersIds.java | 35 +- .../exmo/align/gen/TestGenerator.java | 97 +++- src/fr/inrialpes/exmo/align/gen/URITree.java | 61 +- 7 files changed, 1144 insertions(+), 281 deletions(-) create mode 100644 src/fr/inrialpes/exmo/align/gen/ArbitraryTest.java create mode 100644 src/fr/inrialpes/exmo/align/gen/GenerateBenchmark.java diff --git a/src/fr/inrialpes/exmo/align/gen/ArbitraryTest.java b/src/fr/inrialpes/exmo/align/gen/ArbitraryTest.java new file mode 100644 index 00000000..fcf234f1 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/gen/ArbitraryTest.java @@ -0,0 +1,200 @@ +/* + * $Id: ArbitraryTest.java + * + * Copyright (C) 2003-2010, INRIA + * + * 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. + */ + +/* + * Generates an arbitrary test. + */ + +package fr.inrialpes.exmo.align.gen; + +//Java classes +import java.io.FileOutputStream; +import java.io.InputStream; + +//Jena API classes +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.ontology.OntModelSpec; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.RDFWriter; +import com.hp.hpl.jena.util.FileManager; + +// Alignment API implementation classes +import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; +import java.io.File; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.util.Properties; +import fr.inrialpes.exmo.ontowrap.jena25.JENAOntology; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import org.semanticweb.owl.align.Alignment; +import org.semanticweb.owl.align.AlignmentVisitor; + +public class ArbitraryTest { + private String fileName; //the initial file + private String testNumber; //the test number + private String [] params; //vector to build the parameters + private Properties parameters; //the parameters + public final String namespace = "http://oaei.ontologymatching.org/2011/gen/"; + + //constructor + public ArbitraryTest(String fileName, String testNumber, String [] params) { + this.fileName = fileName; + this.testNumber = testNumber; + this.params = params; + } + + //build the parameters + public void buildParameters() { + parameters = new Properties(); //initialize the parameters + + for ( int i=0; i<params.length; i+=2 ) { + if ( params[i].equals("addClasses") ) /* add percentage classes */ + parameters.setProperty(ParametersIds.ADD_CLASSES, params[i+1]); + + if ( params[i].equals("addProperties") ) /* add percentage properties */ + parameters.setProperty(ParametersIds.ADD_PROPERTIES, params[i+1]); + + //add c classes beginning from level l -> the value of this parameters should be: + //beginning_level.number_of_classes_to_add + if ( params[i].equals("addClassesLevel") ) /* add c classes beginning from level l */ + parameters.setProperty(ParametersIds.ADD_CLASSES, params[i+1]); + + if ( params[i].equals("removeClasses") ) /* remove percentage classes */ + parameters.setProperty(ParametersIds.REMOVE_CLASSES, params[i+1]); + + if ( params[i].equals("removeProperties") ) /* remove percentage properties */ + parameters.setProperty(ParametersIds.REMOVE_PROPERTIES, params[i+1]); + + if ( params[i].equals("removeComments") ) /* remove percentage comments */ + parameters.setProperty(ParametersIds.REMOVE_COMMENTS, params[i+1]); + + if ( params[i].equals("removeRestrictions") ) /* remove percentage restrictions */ + parameters.setProperty(ParametersIds.REMOVE_RESTRICTIONS, params[i+1]); + + if ( params[i].equals("removeIndividuals") ) /* remove percentage individuals */ + parameters.setProperty(ParametersIds.REMOVE_INDIVIDUALS, params[i+1]); + + if ( params[i].equals("renameClasses") ) /* rename percentage classes */ + parameters.setProperty(ParametersIds.RENAME_CLASSES, params[i+1]); + + if ( params[i].equals("renameProperties") ) /* rename percentage properties */ + parameters.setProperty(ParametersIds.RENAME_PROPERTIES, params[i+1]); + + if ( params[i].equals("levelFlattened") ) /* flattened level */ + parameters.setProperty(ParametersIds.LEVEL_FLATTENED, params[i+1]); + + if ( params[i].equals( ("noHierarchy")) ) /* no hierarchy */ + parameters.setProperty( ParametersIds.NO_HIERARCHY, ParametersIds.NO_HIERARCHY); + } + } + + //get the prefix to build the namespace + public String getPrefix(String fileName) { + return fileName.substring(0, fileName.lastIndexOf(".")); + } + + //get the uri + public String getURI(String fileName, String testNumber) { + return this.namespace + this.getPrefix(fileName) + "/" + testNumber + "/" + fileName + "#"; + } + + //load ontology + public OntModel loadOntology (String fileName) { + InputStream in = FileManager.get().open( fileName ); + OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); + model.read(in, null); + return model; + } + + //write ontology + public static void writeOntology(OntModel model, String destFile, String ns) { + try { + File f = new File(destFile); + FileOutputStream fout = new FileOutputStream(f); + Charset defaultCharset = Charset.forName("UTF8"); + RDFWriter writer = model.getWriter("RDF/XML-ABBREV"); + writer.setProperty("showXmlDeclaration","true"); + model.setNsPrefix("", ns); + writer.setProperty( "xmlbase", ns ); + writer.write(model.getBaseModel(), new OutputStreamWriter(fout, defaultCharset), ""); + fout.close(); + } catch (Exception ex) { + System.out.println("Exception " + ex.getMessage()); + } + } + + + //modify ontology + public void modifyOntology(){ + //build the list of parameters + this.buildParameters(); + //modify the model + try { + OntModel model = loadOntology(fileName); //load the initial ontology + TestGenerator t = new TestGenerator(); //build an instance of TestGenerator + JENAOntology onto = new JENAOntology(); //cast the model into Ontology + onto.setOntology( (OntModel)model ); + //set the TestGenerator ontology + t.setOntology(onto); + //set the namespace + t.setNamespace( this.getURI(this.fileName, this.testNumber) ); + //t.setNamespace( "http://oaei.ontologymatching.org/2011/gen/onto/101/onto.rdf#" ); + + Alignment align = t.generate(onto, parameters); //generate the alignment + + JENAOntology modified = (JENAOntology)t.getModifiedOntology(); //get the modified ontology + + + //build the directory to save the file + boolean create; + create = new File(testNumber).mkdir(); + if ( create ) System.out.println(" Succesufully created the directory "); + else System.out.println(" Error creating the directory "); + + //new File(testNumber).mkdir(); + //write the ontology into the directory + if ( modified.getOntology() instanceof OntModel ) + writeOntology(modified.getOntology(), testNumber + "/" + fileName, this.getURI(this.fileName, this.testNumber)); //write the ontology + //write the alignment into the directory + OutputStream stream = new FileOutputStream( testNumber + "/" + "refalign.rdf" ); + // Outputing + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new RDFRendererVisitor( writer ); + align.render(renderer); + writer.flush(); + writer.close(); + } + catch ( Exception ex ) { + System.err.println( "Error " + ex.getMessage() ); + } + + System.out.println( "***" ); + System.out.println( "END" ); + System.out.println( "***" ); + } + + +} diff --git a/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java b/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java index bc99d2de..253444ea 100644 --- a/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java +++ b/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java @@ -1,7 +1,7 @@ /* - * $Id$ + * $Id: ClassHierarchy.java * - * Copyright (C) 2010-2011, INRIA + * Copyright (C) 2003-2010, INRIA * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -23,7 +23,6 @@ Iterates through all the classes in the hierarchy and builds the class hierarchy */ - package fr.inrialpes.exmo.align.gen; //Jena @@ -66,9 +65,7 @@ public class ClassHierarchy { public void addClass (String childURI, String parentURI) { URITree node = null; - //node = this.root.searchURITree( this.root, parentURI ); //the node from the hierarchy with the identifier parentURI this.getRootClassHierarchy().add(this.root, parentURI, childURI); //we add the new childURI to the hierarchy - //node._addChildToNode(this.root, parentURI, childURI); } //updates the class hierarchy @@ -97,7 +94,7 @@ public class ClassHierarchy { //remove the URI of the class from the hierarchy //returns the parent of the class if it exists or 0 other way -> not owl:Thing - public OntClass removeClass ( OntModel model, OntClass cls ) { + public OntClass removeClass (OntModel model, OntClass cls) { URITree node = this.root.searchURITree( this.root, cls.getURI() ); //search for the class URI in the class hierarchy URITree parentNode = null; @@ -284,5 +281,3 @@ public class ClassHierarchy { } - - diff --git a/src/fr/inrialpes/exmo/align/gen/GenerateBenchmark.java b/src/fr/inrialpes/exmo/align/gen/GenerateBenchmark.java new file mode 100644 index 00000000..8dc4a7c8 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/gen/GenerateBenchmark.java @@ -0,0 +1,545 @@ +/* + * $Id: GenerateBenchmark.java + * + * Copyright (C) 2003-2010, INRIA + * + * 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. + */ + +/* + * Generates the Benchmark dataset. + */ + +package fr.inrialpes.exmo.align.gen; + +//Java classes +import java.io.FileOutputStream; +import java.io.InputStream; + +//Jena API classes +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.ontology.OntModelSpec; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.RDFWriter; +import com.hp.hpl.jena.util.FileManager; + +// Alignment API implementation classes +import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; +import java.io.File; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.util.Properties; +import fr.inrialpes.exmo.ontowrap.jena25.JENAOntology; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import org.semanticweb.owl.align.Alignment; +import org.semanticweb.owl.align.AlignmentVisitor; + +public class GenerateBenchmark { + private String fileName; //the initial file + private String testNumber; //the test number + private Properties parameters; //the parameters + public final String namespace = "http://oaei.ontologymatching.org/2011/gen/"; + + //constructor + public GenerateBenchmark(String fileName) { + this.fileName = fileName; + } + + //gets the prefix to build the namespace + public String getPrefix(String fileName) { + return fileName.substring(0, fileName.lastIndexOf(".")); + } + + //gets the uri + public String getURI(String fileName, String testNumber) { + return this.namespace + this.getPrefix(fileName) + "/" + testNumber + "/" + fileName + "#"; + } + + //loads ontology + public OntModel loadOntology (String fileName) { + InputStream in = FileManager.get().open( fileName ); + OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); + model.read(in, null); + return model; + } + + //writes ontology + public static void writeOntology(OntModel model, String destFile, String ns) { + try { + File f = new File(destFile); + FileOutputStream fout = new FileOutputStream(f); + Charset defaultCharset = Charset.forName("UTF8"); + RDFWriter writer = model.getWriter("RDF/XML-ABBREV"); + writer.setProperty("showXmlDeclaration","true"); + model.setNsPrefix("", ns); + writer.setProperty( "xmlbase", ns ); + writer.write(model.getBaseModel(), new OutputStreamWriter(fout, defaultCharset), ""); + fout.close(); + } catch (Exception ex) { + System.out.println("Exception " + ex.getMessage()); + } + } + + + //modifies an ontology + public void modifyOntology(TestGenerator t, String fileName, String testNumber, Properties params) { + //modify the model + try { + + System.out.println("From test " + fileName); + System.out.println("Test " + testNumber); + OntModel model = loadOntology(fileName); //load the initial ontology + JENAOntology onto = new JENAOntology(); //cast the model into Ontology + onto.setOntology( (OntModel)model ); + //set the TestGenerator ontology + t.setOntology(onto); + //set the namespace + t.setNamespace( this.getURI(this.fileName, testNumber) ); + //t.setNamespace( "http://oaei.ontologymatching.org/2011/gen/onto/101/onto.rdf#" ); + + Alignment align = null; + if ( fileName.equals(this.fileName) ) + align = t.generate(onto, params); //generate the alignment + else + align = t.generate(onto, params, this.parameters); //generate the alignment + + JENAOntology modified = (JENAOntology)t.getModifiedOntology(); //get the modified ontology + + //build the directory to save the file + boolean create; + create = new File(testNumber).mkdir(); + /* if ( create ) System.out.println(" Succesufully created the directory "); + else System.out.println(" Error creating the directory "); */ + + //write the ontology into the directory + if ( modified.getOntology() instanceof OntModel ) + writeOntology(modified.getOntology(), testNumber + "/" + this.fileName, this.getURI( this.fileName, testNumber)); //write the ontology + + //write the alignment into the directory + OutputStream stream = new FileOutputStream( testNumber + "/" + "refalign.rdf" ); + // Outputing + PrintWriter writer = new PrintWriter ( + new BufferedWriter( + new OutputStreamWriter( stream, "UTF-8" )), true); + AlignmentVisitor renderer = new RDFRendererVisitor( writer ); + align.render(renderer); + writer.flush(); + writer.close(); + + //retrieve the parameters + this.parameters = t.getParameters(); + + + //System.out.println("Alignmnent"); + + + + } + catch ( Exception ex ) { + System.err.println( "Error " + ex.getMessage() ); + } + + } + + public String directoryName(float i1, String dir) { + String dirName = ""; + if ( ((Float)i1).toString().substring(0, 1).equals("0") ) { + dirName = dir + "-" + ((Float)i1).toString().substring(2, 3); + } + else { + dirName = dir; + } + + return dirName; + } + + //generates the Benchmark + public void generate() { + //keep track of the alignment + Properties paramsRenameResources; Properties paramsNoComments; + Properties paramsNoInstance; Properties paramsNoProperty; + Properties paramsFlatten; Properties paramsExpand; + Properties paramsInitial; + Properties p = new Properties(); //modify the ontology according to this parameters + + String prevDirName; + String crtDirName; + + this.parameters = new Properties(); + crtDirName = this.fileName; + + TestGenerator t = new TestGenerator(); //build an instance of TestGenerator + + /* + * Test 101 + */ + this.modifyOntology(t, this.fileName, "101", p); + paramsInitial = (Properties)parameters.clone(); + paramsRenameResources = (Properties)parameters.clone(); + prevDirName = "101" + "/"; + + + /* + * Tests 201, 202, 248, 249, 253, 258, 259, 250, 254, + * 262, 257, 260, 265, 261, 266, 251, 252 + */ + + for ( float i1=0.20f; i1<=1.00f; i1+=0.20f ) { + + /* + * 201-x *** no names + */ + p = new Properties(); + p.setProperty(ParametersIds.RENAME_CLASSES, ((Float)i1).toString()); + p.setProperty(ParametersIds.RENAME_PROPERTIES, ((Float)i1).toString()); + crtDirName = directoryName(i1, "201"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 202-x *** no names + no comments + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_COMMENTS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "201") + "/"; + crtDirName = directoryName(i1, "202"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 248-x *** no names + no comments + no hierarchy + */ + p = new Properties(); + p.setProperty(ParametersIds.NO_HIERARCHY, ParametersIds.NO_HIERARCHY); + prevDirName = directoryName(i1, "202") + "/"; + crtDirName = directoryName(i1, "248"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 253-x *** no names + no comments + no hierarchy + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "248") + "/"; + crtDirName = directoryName(i1, "253"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 249-x *** no names + no comments + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "202") + "/"; + crtDirName = directoryName(i1, "249"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + ////get the parameters + paramsRenameResources = (Properties)this.parameters.clone(); + + /* + * 250-x *** no names + no comments + no property + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_PROPERTIES, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "202") + "/"; + crtDirName = directoryName(i1, "250"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 254-x *** no names + no comments + no property + no hierarchy + */ + p = new Properties(); + p.setProperty(ParametersIds.NO_HIERARCHY, ParametersIds.NO_HIERARCHY); + prevDirName = directoryName(i1, "250") + "/"; + crtDirName = directoryName(i1, "254"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 262-x *** no names + no comments + no property + no hierarchy + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "254") + "/"; + crtDirName = directoryName(i1, "262"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 257-x *** no names + no comments + no property + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "250") + "/"; + crtDirName = directoryName(i1, "257"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 261-x *** no names + no comments + no property + expand + */ + p = new Properties(); + p.setProperty(ParametersIds.ADD_CLASSES, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "250") + "/"; + crtDirName = directoryName(i1, "261"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 266-x *** no names + no comments + no property + expand + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "261") + "/"; + crtDirName = directoryName(i1, "266"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 260-x *** no names + no comments + no property + flatten + */ + p = new Properties(); + p.setProperty(ParametersIds.LEVEL_FLATTENED, "2"); + prevDirName = directoryName(i1, "250") + "/"; + crtDirName = directoryName(i1, "260"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 265-x *** no names + no comments + no property + flatten + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "260") + "/"; + crtDirName = directoryName(i1, "265"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + //re-establish the parameters + this.parameters = (Properties)paramsRenameResources.clone(); + + /* + * 251-x *** no names + no comments + flatten + */ + p = new Properties(); + p.setProperty(ParametersIds.LEVEL_FLATTENED, "2"); + prevDirName = directoryName(i1, "202") + "/"; + crtDirName = directoryName(i1, "251"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 258-x *** no names + no comments + flatten + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "251") + "/"; + crtDirName = directoryName(i1, "258"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + //re-establish the parameters + this.parameters = (Properties)paramsRenameResources.clone(); + + /* + * 252-x *** no names + no comments + expand + */ + p = new Properties(); + p.setProperty(ParametersIds.ADD_CLASSES, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "202") + "/"; + crtDirName = directoryName(i1, "252"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 259-x *** no names + no comments + expand + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(i1, "252") + "/"; + crtDirName = directoryName(i1, "259"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + +// paramsNoProperty = modifier.getProperties(); //get the modifed properties + + this.parameters = (Properties)paramsRenameResources.clone(); + prevDirName = directoryName(i1, "201") + "/"; + } + + //re-establish the parameters + this.parameters = (Properties)paramsInitial.clone(); + + /* + * Tests 221, 232, 233, 241 + */ + + /* + * 221 *** no hierarchy + */ + p = new Properties(); + p.setProperty(ParametersIds.NO_HIERARCHY, ParametersIds.NO_HIERARCHY); + prevDirName = directoryName(1.00f, "101") + "/"; + crtDirName = directoryName(1.00f, "221"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 232 *** no hierarchy + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "221") + "/"; + crtDirName = directoryName(1.00f, "232"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 233 *** no hierarchy + no property + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_PROPERTIES, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "221") + "/"; + crtDirName = directoryName(1.00f, "233"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 241 *** no hierarchy + no property + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "233") + "/"; + crtDirName = directoryName(1.00f, "241"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + //re-establish the parameters + this.parameters = (Properties)paramsInitial.clone(); + + /* + * Tests 222, 237 + */ + + /* + * 222 *** flatten + */ + p = new Properties(); + p.setProperty(ParametersIds.LEVEL_FLATTENED, "2"); + prevDirName = directoryName(1.00f, "101") + "/"; + crtDirName = directoryName(1.00f, "222"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 237 *** flatten + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "222") + "/"; + crtDirName = directoryName(1.00f, "237"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + + //re-establish the parameters + this.parameters = (Properties)paramsInitial.clone(); + + /* + * Tests 223, 238 + */ + + /* + * 223 *** expand + */ + p = new Properties(); + p.setProperty(ParametersIds.ADD_CLASSES, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "101") + "/"; + crtDirName = directoryName(1.00f, "223"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 238 *** expand + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "223") + "/"; + crtDirName = directoryName(1.00f, "238"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 224 *** no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "101") + "/"; + crtDirName = directoryName(1.00f, "224"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 225 *** no restrictions + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_RESTRICTIONS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "101") + "/"; + crtDirName = directoryName(1.00f, "225"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * Tests 228, 239, 246, 236, 240, 247 + */ + + /* + * 228 *** no property + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_PROPERTIES, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "101") + "/"; + crtDirName = directoryName(1.00f, "228"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 236 *** no property + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "228") + "/"; + crtDirName = directoryName(1.00f, "236"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 240 *** no property + expand + */ + p = new Properties(); + p.setProperty(ParametersIds.ADD_CLASSES, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "228") + "/"; + crtDirName = directoryName(1.00f, "240"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 247 *** no property + expand + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "240") + "/"; + crtDirName = directoryName(1.00f, "247"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 239 *** no property + flatten + */ + p = new Properties(); + p.setProperty(ParametersIds.LEVEL_FLATTENED, "2"); + prevDirName = directoryName(1.00f, "228") + "/"; + crtDirName = directoryName(1.00f, "239"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + /* + * 246 *** no property + flatten + no instance + */ + p = new Properties(); + p.setProperty(ParametersIds.REMOVE_INDIVIDUALS, ((Float)1.00f).toString()); + prevDirName = directoryName(1.00f, "239") + "/"; + crtDirName = directoryName(1.00f, "246"); + this.modifyOntology(t, prevDirName + this.fileName, crtDirName, p); + + } + +} diff --git a/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java b/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java index db99f2f0..96fe8275 100644 --- a/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java +++ b/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java @@ -1,7 +1,7 @@ /* - * $Id$ + * $Id: OntologyModifier.java * - * Copyright (C) 2010-2011, INRIA + * Copyright (C) 2003-2010, INRIA * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -25,15 +25,9 @@ The file in which we store the alignment is "referenceAlignment.rdf" */ - package fr.inrialpes.exmo.align.gen; //Java classes -import java.io.BufferedWriter; -import java.io.FileOutputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; import java.net.URI; import java.util.HashMap; import java.util.Iterator; @@ -41,16 +35,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Random; -import java.util.Set; //Alignment API classes import org.semanticweb.owl.align.Alignment; import org.semanticweb.owl.align.AlignmentException; -import org.semanticweb.owl.align.AlignmentVisitor; -import fr.inrialpes.exmo.align.impl.Annotations; -import fr.inrialpes.exmo.align.impl.Namespace; -import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; import fr.inrialpes.exmo.align.service.jade.messageontology.Parameter; //Google API classes @@ -76,7 +65,6 @@ import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.RDFReader; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.util.ResourceUtils; @@ -84,6 +72,7 @@ import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDFS; import com.hp.hpl.jena.vocabulary.XSD; import fr.inrialpes.exmo.align.impl.URIAlignment; +import java.util.Enumeration; /* //WordNet API classes import edu.smu.tspell.wordnet.Synset; @@ -105,7 +94,8 @@ public class OntologyModifier { private boolean isBuild; //keep track if the class hierarchy is build private boolean isAlign; //keep track it the initial alignment has already been computed private boolean isChanged; //keep track if the namespace of the new ontology is changed - public static String fileName = "referenceAlignment.rdf"; //the reference alignment + public static String fileName = "refalign.rdf"; //the reference alignment + private String base; // //Ontology init, Ontology modified, Alignment align public OntologyModifier ( OntModel model, OntModel modifiedModel, Alignment alignment ) { @@ -116,14 +106,7 @@ public class OntologyModifier { this.isBuild = false; this.isAlign = false; this.isChanged = false; - this.namespace = model.getNsPrefixURI(""); - //if has no namespace, choose a default one - if ( this.namespace == null ) { - //RDFReader reader = this.model.getReader(); - this.model.setNsPrefix("", "http://oaei.ontologymatching.org/2010/benchmarks/101/onto.rdf#"); - this.namespace = model.getNsPrefixURI(""); - System.out.println( "Set default namespace [" + this.namespace + "]" ); - } + this.namespace = model.getNsPrefixURI(""); this.namespaceNew = ""; this.params = new Properties(); } @@ -138,7 +121,7 @@ public class OntologyModifier { //this.classHierarchy.printClassHierarchy(); } - //generate random string with the length "length" + //generates a random string with the length "length" public String getRandomString() { Random generator = new Random(); String characters = "abcdefghijklmnopqrstuvwxyz"; @@ -150,7 +133,7 @@ public class OntologyModifier { return new String(text).toUpperCase(); } - //remove spaces from a string + //removes spaces from a string public String removeSpaces ( String str ) { //return str.replaceAll("\\s+", ""); if ( !str.contains( " " ) ) @@ -173,7 +156,7 @@ public class OntologyModifier { return str; } - //translate the string from English to French + //translates the string from English to French public String translateString( String source ) { String translatedText = ""; GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/"); @@ -249,11 +232,10 @@ public class OntologyModifier { newString = newString.concat( getSynonym(str.substring(0)) ); return newString; } - - + //count - the number of elements from the vector //the random numElems that must be selected - //use the Fisher and Yates method to shuffle integers from an array + //uses the Fisher and Yates method to shuffle integers from an array public int [] randNumbers (int count, int numElems) { int [] vect = new int[count]; int [] n = new int[numElems]; @@ -272,7 +254,7 @@ public class OntologyModifier { return n; } - //replace the label of the property + //replaces the label of the property public void replacePropertyLabel( String uri, String newLabel, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation ) { OntProperty prop = this.modifiedModel.getOntProperty( uri ); if ( prop.getLabel( "en" ) != null ) { @@ -287,35 +269,37 @@ public class OntologyModifier { //gets the URIs of the properties and their translation public HashMap<String, String> getPropertiesIdentifiers ( float percentage, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation) { HashMap<String, String> propertiesIdentifiers = new HashMap<String, String>(); //the HashMap of the properties identifiers - List<OntProperty> properties = getOntologyProperties(); //the list of all the properties List<String> propertiesName = new ArrayList<String>(); //the properties identifiers - List<OntProperty> propertiesTo = new ArrayList<OntProperty>(); - int nbProperties, toBeRenamed; - List<OntProperty> notRenamedProperties = new ArrayList<OntProperty>(); - List<OntProperty> modelProperties = this.getOntologyProperties(); + List<OntProperty> propertiesTo = new ArrayList<OntProperty>(); //the list of properties to be renamed + List<OntProperty> notRenamedProperties = new ArrayList<OntProperty>(); //the list of not renamed properties + List<OntProperty> properties = getOntologyProperties(); //the list of all the properties + + int nbProperties, toBeRenamed, renamedProperties; + //builds the list of all unrenamed properties from the model - for ( OntProperty p : modelProperties ) { - String key = p.getURI(); - if ( this.params.containsKey( key ) ) { + for ( OntProperty p : properties ) { + String uri = base + p.getLocalName(); + if ( this.params.containsKey( uri ) ) { + String key = uri; String value = this.params.getProperty( key ); - if ( key.equals( value ) ) { - notRenamedProperties.add( p ); //add the property to not renamed properties - } + if ( key.equals( value ) ) + notRenamedProperties.add( p ); //add the property to not renamed properties } } + + nbProperties = properties.size(); //the number of renamed properties - toBeRenamed = (int)( percentage*nbProperties ); + renamedProperties = nbProperties - notRenamedProperties.size(); + toBeRenamed = (int)(percentage*nbProperties) - renamedProperties; // -renamedProperties -> for Benchmark - if ( notRenamedProperties.size() < 2*toBeRenamed ) - toBeRenamed = notRenamedProperties.size(); //builds the list of properties to be renamed int [] n = this.randNumbers(notRenamedProperties.size(), toBeRenamed); for ( int i=0; i<toBeRenamed; i++ ) { OntProperty p = notRenamedProperties.get(n[i]); propertiesTo.add(p); - if ( p.getNameSpace().equals( this.namespace ) ) + if ( p.getNameSpace().equals( this.namespace ) ) propertiesName.add( p.getLocalName() ); } @@ -330,16 +314,16 @@ public class OntologyModifier { propertiesIdentifiers.put( localName , translateStrg ); replacePropertyLabel( prop.getURI(), translateStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( prop.getURI() ) ) { //this.params.remove( prop.getURI() ); - this.params.put( prop.getURI() , this.namespace + translateStrg );//the reference alignment + if ( params.containsKey( this.base + prop.getLocalName() ) ) { //this.params.remove( prop.getURI() ); + this.params.put( this.base + prop.getLocalName() , this.base + translateStrg );//the reference alignment } } else if ( activeRandomString ) { //replace the URI with a random string String newStrg = getRandomString(); propertiesIdentifiers.put( localName , newStrg ); replacePropertyLabel( prop.getURI(), newStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( prop.getURI() ) ) { //this.params.remove( prop.getURI() ); - this.params.put( prop.getURI() , this.namespace + newStrg);//the reference alignment + if ( params.containsKey( this.base + prop.getLocalName() ) ) { //this.params.remove( prop.getURI() ); + this.params.put( this.base + prop.getLocalName() , this.base + newStrg);//the reference alignment } } else if ( activeSynonym ) { @@ -349,30 +333,30 @@ public class OntologyModifier { else { propertiesIdentifiers.put( localName, synonym ); replacePropertyLabel( prop.getURI(), synonym, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( prop.getURI() ) ) { //this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + synonym ); //the reference alignment + if ( params.containsKey( this.base + prop.getLocalName() ) ) { //this.params.remove( prop.getURI() ); + this.params.put( this.base + prop.getLocalName() , this.base + synonym ); //the reference alignment } } } else if ( activeStringOperation == 1 ) { //replace the URI with the UpperCase URI propertiesIdentifiers.put( localName , toUpperCase( localName ) ); replacePropertyLabel( prop.getURI(), toUpperCase( localName ), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( prop.getURI() ) ) { //this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + toUpperCase( localName ) ); //the reference alignment + if ( params.containsKey( this.base + prop.getLocalName() ) ) { //this.params.remove( prop.getURI() ); + this.params.put( this.base + prop.getLocalName() , this.base + toUpperCase( localName ) ); //the reference alignment } } else if ( activeStringOperation == 2 ) { propertiesIdentifiers.put( localName , toLowerCase( localName ) ); replacePropertyLabel( prop.getURI(), toLowerCase( localName ), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( prop.getURI() ) ) { // this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + toLowerCase( localName ) ); //the reference alignment + if ( params.containsKey( this.base + prop.getLocalName() ) ) { // this.params.remove( prop.getURI() ); + this.params.put( this.base + prop.getLocalName() , this.base + toLowerCase( localName ) ); //the reference alignment } } else { propertiesIdentifiers.put( localName, localName + "PROPERTY" ); replacePropertyLabel( prop.getURI(), localName + "PROPERTY", activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( prop.getURI() ) ) { //this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + localName + "PROPERTY" ); + if ( params.containsKey( this.base + prop.getLocalName() ) ) { //this.params.remove( prop.getURI() ); + this.params.put( this.base + prop.getLocalName() , this.base + localName + "PROPERTY" ); } } } @@ -380,8 +364,8 @@ public class OntologyModifier { } return propertiesIdentifiers; } - - //replace the label of the class + + //replaces the label of the class public void replaceClassLabel( String uri, String newLabel, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation ) { OntClass c = this.modifiedModel.getOntClass( uri ); @@ -396,30 +380,30 @@ public class OntologyModifier { //gets the URIs of the classes and their translation public HashMap<String, String> getClassesIdentifiers ( float percentage, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation ) { HashMap<String, String> classesIdentifiers = new HashMap<String, String>(); //the HashMap of classes identifiers - List<OntClass> classes = this.getOntologyClasses(); - List<OntClass> classesTo = new ArrayList<OntClass>(); - int nbClasses, toBeRenamed ; - List<OntClass> notRenamedClasses = new ArrayList<OntClass>(); - List<OntClass> modelClasses = this.getOntologyClasses(); + int nbClasses, toBeRenamed, renamedClasses; + + List<OntClass> notRenamedClasses = new ArrayList<OntClass>(); //the list of not renamed classes + List<OntClass> classes = this.getOntologyClasses(); //the list of ontology classes + List<OntClass> classesTo = new ArrayList<OntClass>(); //the list of classes to be renamed //builds the list of all unrenamed classes from the model - for ( OntClass c : modelClasses ) { - String uri = c.getURI(); + for ( OntClass c : classes ) { + String uri = base + c.getLocalName(); //gets the pair <key, value> if ( this.params.containsKey( uri ) ) { String key = uri; String value = this.params.getProperty( uri ); //they didnt change - if ( key.equals( value ) ) + if ( key.equals( value ) ) notRenamedClasses.add( c ); //add the class to not renamed classes } } + + nbClasses = classes.size(); //the number of renamed classes + renamedClasses = nbClasses - notRenamedClasses.size(); + toBeRenamed = (int)(percentage*nbClasses) - renamedClasses; // -renamedClasses -> for Benchmark - nbClasses = classes.size(); //the number of renamed classes - toBeRenamed = (int)( percentage*nbClasses ); //the number of classes to be renamed - if ( notRenamedClasses.size() < 2 * toBeRenamed ) - toBeRenamed = notRenamedClasses.size(); //build the list of classes to be renamed int [] n = this.randNumbers(notRenamedClasses.size(), toBeRenamed); @@ -441,45 +425,45 @@ public class OntologyModifier { String translateStrg = parseString (localName, true, false); classesIdentifiers.put( localName , translateStrg ); replaceClassLabel( cls.getURI(), translateStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( cls.getURI() ) ) { //this.params.remove( cls.getURI() ); - this.params.put( cls.getURI() , this.namespace + translateStrg); //the reference alignment + if ( params.containsKey( this.base + cls.getLocalName() ) ) { //this.params.remove( cls.getURI() ); + this.params.put( this.base + cls.getLocalName() , this.base + translateStrg); //the reference alignment } } else if ( activeRandomString ) { //replace the URI with a random string String newStrg = getRandomString(); classesIdentifiers.put( localName , newStrg ); replaceClassLabel( cls.getURI(), newStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( cls.getURI() ) ) { //this.params.remove( cls.getURI() ); - this.params.put( cls.getURI(), this.namespace + newStrg ); //the reference alignment + if ( params.containsKey( this.base + cls.getLocalName() ) ) { //this.params.remove( cls.getURI() ); + this.params.put( this.base + cls.getLocalName() , this.base + newStrg ); //the reference alignment } } else if ( activeSynonym ) { //replace the URI with a synonym String synonym = parseString (localName, false, true); classesIdentifiers.put( localName, synonym ); replaceClassLabel( cls.getURI(), synonym, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( cls.getURI() ) ) { //this.params.remove( cls.getURI() ); - this.params.put( cls.getURI(), this.namespace + synonym );//the reference alignment + if ( params.containsKey( this.base + cls.getLocalName() ) ) { //this.params.remove( cls.getURI() ); + this.params.put( this.base + cls.getLocalName() , this.base + synonym );//the reference alignment } } else if ( activeStringOperation == 1 ){ //replace the URI with the UpperCase URI classesIdentifiers.put( localName , toUpperCase( localName ) ); replaceClassLabel( cls.getURI(), toUpperCase(localName), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( cls.getURI() ) ) { //this.params.remove( cls.getURI() ); - this.params.put( cls.getURI(), this.namespace + toUpperCase( localName ) ); //the reference alignment + if ( params.containsKey( this.base + cls.getLocalName() ) ) { //this.params.remove( cls.getURI() ); + this.params.put( this.base + cls.getLocalName() , this.base + toUpperCase( localName ) ); //the reference alignment } } else if ( activeStringOperation == 2 ){ //replace the URI with the LowerCase URI classesIdentifiers.put( localName , toLowerCase( localName ) ); replaceClassLabel( cls.getURI(), toLowerCase(localName), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( cls.getURI() ) ) { //this.params.remove( cls.getURI() ); - this.params.put( cls.getURI(), this.namespace + toLowerCase( localName ) ); //the reference alignment + if ( params.containsKey( this.base + cls.getLocalName() ) ) { //this.params.remove( cls.getURI() ); + this.params.put( this.base + cls.getLocalName() , this.base + toLowerCase( localName ) ); //the reference alignment } } else { classesIdentifiers.put( localName, localName + "CLASS" ); replaceClassLabel( cls.getURI(), localName + "CLASS", activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - if ( params.containsKey( cls.getURI() ) ) { //this.params.remove( cls.getURI() ); - this.params.put( cls.getURI(), this.namespace + localName + "CLASS" ); + if ( params.containsKey( this.base + cls.getLocalName() ) ) { //this.params.remove( cls.getURI() ); + this.params.put( this.base + cls.getLocalName() , this.base + localName + "CLASS" ); } } } @@ -497,11 +481,14 @@ public class OntologyModifier { List<Statement> statements = null; //the list of all statements HashMap<String, String> propertiesIdentifiers = null; //the HashMap of the properties identifiers HashMap<String, String> classesIdentifiers = null; //the HashMap of the classes identifiers + String subjectLocalName, subjectNameSpace; String predicateLocalName, predicateNameSpace; String objectLocalName, objectNameSpace; + boolean isPred, isSubj, isObj; isPred = isSubj = isObj = false; + OntModel newModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);//create new Model //get properties and classes identifiers if ( activeProperties ) @@ -509,16 +496,19 @@ public class OntologyModifier { if ( activeClasses ) classesIdentifiers = getClassesIdentifiers ( percentage, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation); statements = this.modifiedModel.listStatements().toList(); //get all the statements of the model - + //iterate and modify the identifiers for ( Statement stm : statements ) { + Resource subject = stm.getSubject(); //the subject Property predicate = stm.getPredicate(); //the predicate RDFNode object = stm.getObject(); //the object + Resource subj = null; Property pred = null; Resource obj = null; isPred = isSubj = isObj = false; + //if it is the subject of the statement if ( subject.getLocalName() != null ) { if ( activeProperties ) { @@ -546,6 +536,7 @@ public class OntologyModifier { } } } + //if it is the predicate of the statement if ( activeProperties ) { if ( propertiesIdentifiers.containsKey( predicate.getLocalName() ) ) { @@ -572,6 +563,7 @@ public class OntologyModifier { } } } + //if it is the object of the statement if ( object.canAs( Resource.class ) ) if ( object.isURIResource() ) { @@ -603,21 +595,29 @@ public class OntologyModifier { if ( isSubj ) { if ( isPred ) { - if ( isObj ) newModel.add( subj, pred, obj ); - else newModel.add( subj, pred, object ); + if ( isObj ) + newModel.add( subj, pred, obj ); + else + newModel.add( subj, pred, object ); } else { - if ( isObj ) newModel.add( subj, predicate, obj ); - else newModel.add( subj, predicate, object ); + if ( isObj ) + newModel.add( subj, predicate, obj ); + else + newModel.add( subj, predicate, object ); } } else { if ( isPred ) { - if ( isObj ) newModel.add( subject, pred, obj ); - else newModel.add( subject, pred, object ); + if ( isObj ) + newModel.add( subject, pred, obj ); + else + newModel.add( subject, pred, object ); } else { - if ( isObj ) newModel.add( subject, predicate, obj ); - else newModel.add( subject, predicate, object ); + if ( isObj ) + newModel.add( subject, predicate, obj ); + else + newModel.add( subject, predicate, object ); } } } @@ -629,7 +629,7 @@ public class OntologyModifier { return newModel; } - //checks if the class hierarchy is build + //check if the class hierarchy is build public void checkClassHierarchy() { if ( !this.isBuild ) { buildClassHierarchy(); @@ -677,7 +677,7 @@ public class OntologyModifier { List<OntClass> classes = getOntologyClasses(); //get the list of classes from the Ontology int nbClasses = classes.size(); //number of classes from the Ontology int toAdd = (int) ( percentage * nbClasses ); - + checkClassHierarchy(); //check if the classHierarchy is built //build the list of properties to be renamed int [] n = this.randNumbers(nbClasses, toAdd); @@ -735,7 +735,7 @@ public class OntologyModifier { String subjectNameSpace, subjectLocalName; String objectNameSpace, objectLocalName; String predicateNameSpace, predicateLocalName; - + //iterate and modify the identifiers for ( Statement stm : statements ) { Resource subject = stm.getSubject(); //the subject @@ -747,12 +747,12 @@ public class OntologyModifier { isSubj = isObj = isPred = false; //if the class appears as the subject of a proposition - if ( subject.getURI() != null ) + if ( subject.getURI() != null ) if ( uris.containsKey( subject.getURI() ) ) { isSubj = true; subj = newModel.createResource( uris.get( subject.getURI() ) ); } - + //if appears as the predicate - never if ( predicate.getURI() != null ) if ( uris.containsKey( predicate.getURI() ) ) { @@ -789,11 +789,11 @@ public class OntologyModifier { else newModel.add( subject, predicate, object ); } } - } + } return newModel; } - //checks if the removed class appears as AllValueFrom or SomeValueFrom in a restriction + //check if the removed class appears as AllValueFrom or SomeValueFrom in a restriction @SuppressWarnings("unchecked") public void checkClassesRestrictions ( OntClass childClass, OntClass parentClass ) { Restriction restr = null; @@ -837,12 +837,12 @@ public class OntologyModifier { for (OntClass clss : subClasses) //new superclass => clss.setSuperClass( parentClass ); //=>the superclass of the node - checkClassesRestrictions( cls, parentClass ); + checkClassesRestrictions( cls, parentClass ); cls.remove(); //remove the class from the Ontology return parentClass.getURI(); } - //removes the subClasses from the list + //remove the subClasses from the list public void removeSubClasses ( float percentage ) { List<OntClass> classes = this.getOntologyClasses(); //the list of classes from Ontologu List<OntClass> removedClasses = new ArrayList<OntClass>(); @@ -868,18 +868,17 @@ public class OntologyModifier { //checks if the class appears like unionOf.. and replaces its appearence with the superclass this.modifiedModel = changeDomainRange( uris ); - + //remove the URI of the class from the reference alignment for ( String key : this.params.stringPropertyNames() ) { String value = this.params.getProperty( key ); - if ( cl.contains( key ) ) + if ( cl.contains( this.namespace + key.substring(key.indexOf(this.base) + this.base.length() )) ) this.params.remove( key ); - if ( cl.contains( value ) ) //we iterate to check if the class appears only like value + if ( cl.contains( this.namespace + value.substring(value.indexOf(this.base) + this.base.length() )) ) //we iterate to check if the class appears only like value this.params.remove( key ); } } - //remove all the classes from a specific level public void removeClassesFromLevel ( int level ) { HashMap<String, String> uris = new HashMap<String, String>(); @@ -890,25 +889,12 @@ public class OntologyModifier { List<OntClass> classes = new ArrayList<OntClass>(); checkClassHierarchy(); //check if the class hierarchy is built classes = this.classHierarchy.getClassesFromLevel(this.modifiedModel, level); - - for ( OntClass cls : classes ) { - parentURI = removeClass ( cls ); - OntClass parentClass = this.modifiedModel.getOntClass( parentURI ); - checkClassesRestrictions( cls, parentClass ); - uris.put( cls.getURI(), parentURI); + for ( int i=0; i<classes.size(); i++ ) { //remove the classes from the hierarchy + parentURI = removeClass ( classes.get(i) ); + uris.put(classes.get(i).getURI(), parentURI); } - //checks if the class appears like unionOf .. and replaces its appearence with the superclass this.modifiedModel = changeDomainRange( uris ); - //remove the URI of the class from the reference alignment - for ( String key : this.params.stringPropertyNames() ) { - String value = this.params.getProperty( key ); - if ( uris.containsKey( key ) ) - this.params.remove( key ); - if ( uris.containsValue( value ) ) //we iterate to check if the class appears only like value - this.params.remove( key ); - } - } //remove percentage individuals from Ontology @@ -919,7 +905,7 @@ public class OntologyModifier { List<Statement> statements = this.modifiedModel.listStatements().toList(); int nbIndividuals = individuals.size(); //the number of individuals int toBeRemoved = (int)( percentage*nbIndividuals ); //the number of individuals to be removed - + int [] n = this.randNumbers(nbIndividuals, toBeRemoved); //build the list of individuals to be removed for ( int i=0; i<toBeRemoved; i++ ) { Individual indiv = individuals.get(n[i]); //remove the individual from the reference alignment @@ -965,6 +951,7 @@ public class OntologyModifier { propertiesToBeRemoved.add( p ); pr.add( p.getURI() ); + //this.params.remove( p.getURI() ); for ( Iterator it = p.listReferringRestrictions(); it.hasNext(); ) {//get the restrictions of that property restrictions.add( (Restriction)it.next() ); @@ -1001,10 +988,10 @@ public class OntologyModifier { //since we don't respect any order the value can appear as a value, thus we must iterate among all params to delete it for ( String key : this.params.stringPropertyNames() ) { String value = this.params.getProperty(key); - if ( pr.contains( key ) ) { //System.out.println( "Elimin " + key ); + if ( pr.contains( this.namespace + key.substring(key.indexOf(this.base) + this.base.length()) ) ) { //System.out.println( "Elimin " + key ); this.params.remove( key ); } - if ( pr.contains( value ) ) { //System.out.println( "Elimin " + key ); + if ( pr.contains( this.namespace + value.substring(key.indexOf(this.base) + this.base.length()) ) ) { //System.out.println( "Elimin " + key ); this.params.remove( key ); } } @@ -1033,7 +1020,6 @@ public class OntologyModifier { } } - //add object properties to the Ontology public void addProperties ( float percentage ) { List<OntProperty> properties = this.getOntologyProperties(); @@ -1195,14 +1181,22 @@ public class OntologyModifier { for ( OntClass cls : supCls ) { if ( cls.isRestriction() ) { Restriction r = cls.asRestriction(); - if ( r.isAllValuesFromRestriction() ) restr.add(r); - if ( r.isCardinalityRestriction() ) restr.add(r); - if ( r.isHasValueRestriction() ) restr.add(r); - if ( r.isMaxCardinalityRestriction() ) restr.add(r); - if ( r.isMinCardinalityRestriction() ) restr.add(r); - if ( r.isSomeValuesFromRestriction() ) restr.add(r); + if ( r.isAllValuesFromRestriction() ) + restr.add(r); + if ( r.isCardinalityRestriction() ) + restr.add(r); + if ( r.isHasValueRestriction() ) + restr.add(r); + if ( r.isMaxCardinalityRestriction() ) + restr.add(r); + if ( r.isMinCardinalityRestriction() ) + restr.add(r); + if ( r.isSomeValuesFromRestriction() ) + restr.add(r); + //System.out.println( cls.getURI() + cls.getLocalName() ); } } + //System.out.println( restr.size() ); if ( !restrictions.containsKey( parentClass.getURI() ) ) { restrictions.put( parentClass.getURI(), restr ); @@ -1213,6 +1207,10 @@ public class OntologyModifier { if ( active ) { //if ( !parentClass.getURI().equals( "Thing" ) ) { OntClass superClass = superLevelClasses.get( i ); //parent class of the child class parents + //System.out.println("SuperClass class [" + superClass.getURI() + "]"); + //System.out.println("Parent class [" + parentClass.getURI() + "]"); + //System.out.println("Child class [" + childClass.getURI() + "]"); + if ( this.modifiedModel.containsResource(parentClass) ) { //to check if the class appears as unionOf, someValuesFrom, allValuesFrom .. unionOf.put(parentClass.getURI(), superClass.getURI()); @@ -1223,15 +1221,18 @@ public class OntologyModifier { parentClass.removeSubClass( childClass ); } else { OntClass superClass = this.modifiedModel.createClass( OWL.Thing.getURI() ); //Thing class + if ( this.modifiedModel.containsResource(parentClass) ) { //to check if the class appears as unionOf.. unionOf.put(parentClass.getURI(), superClass.getURI()); checkClassesRestrictions ( parentClass, superClass ); parentClass.remove(); } + parentClass.removeSubClass( childClass ); - } + } } + int i = 0; for ( String uri : parentURI ) { OntClass childClass = levelClasses.get( i ); @@ -1241,18 +1242,21 @@ public class OntologyModifier { } i++; } + //checks if the class appears like unionOf, someValuesFrom, allValuesFrom .. and replaces its appearence with the superclass this.modifiedModel = changeDomainRange(unionOf); + //remove the parentClass from the alignment for ( String key : this.params.stringPropertyNames() ) { String value = this.params.getProperty(key); - if ( parentURI.contains(key) ) { //this.classHierarchy.removeUri("Thing", key); + if ( parentURI.contains( this.namespace + key.substring(key.indexOf(this.base) + this.base.length()) ) ) { //this.classHierarchy.removeUri("Thing", key); this.params.remove(key); } - if ( parentURI.contains( value ) ) { //this.classHierarchy.removeUri("Thing", value); + if ( parentURI.contains( this.namespace + value.substring(key.indexOf(this.base) + this.base.length() ))) { //this.classHierarchy.removeUri("Thing", value); this.params.remove(key); } } + } //flatten level - for noHierarchy @@ -1288,6 +1292,7 @@ public class OntologyModifier { //this.classHierarchy.printClassHierarchy(); _noHierarchy ( level ); level--; + } } @@ -1304,6 +1309,7 @@ public class OntologyModifier { Restriction res = restrictions.get(n[i]); restrictionsTo.add( res ); } + for ( Restriction res : restrictionsTo ) res.remove(); } @@ -1314,8 +1320,12 @@ public class OntologyModifier { List<OntProperty> properties = this.modifiedModel.listAllOntProperties().toList();//all properties List<Individual> individuals = this.modifiedModel.listIndividuals().toList();//all individuals List<Ontology> ontologies = this.modifiedModel.listOntologies().toList();//all Ontologies - this.isAlign = true; //the alignment has been computed for the first time + this.params = new Properties(); + + this.isAlign = true; //the alignment has been computed for the first time + this.base = this.namespace; + for ( OntClass cls : classes ) //list all classes if ( cls.getNameSpace().equals( this.namespace ) ) this.params.put( cls.getURI() , cls.getURI() ); //add them to the initial alignment @@ -1331,68 +1341,63 @@ public class OntologyModifier { URI onto1 = null; URI onto2 = null; - if ( !this.isChanged ) { //if the namespace of the ontology is not changed - int index = this.namespace.indexOf("#"); - this.namespaceNew = this.namespace.substring(0, index) + "new#"; - //this.namespaceNew = "http://oaei.ontologymatching.org/2010/benchmarks/101/onto1.rdf#"; - } - this.alignment = new URIAlignment(); + try { - onto1 = new URI ( this.namespace ); - onto2 = new URI ( this.namespaceNew ); - this.alignment.init(onto1, onto2); - long time = System.currentTimeMillis(); + onto1 = new URI ( this.base.substring(0, this.base.lastIndexOf("#")) ); + onto2 = new URI( this.namespaceNew.substring(0, this.namespaceNew.lastIndexOf("#")) ); + this.alignment.init(onto1, onto2); + this.alignment.setFile1(onto1); + this.alignment.setFile2(onto2); + //Cell addAlignCell(Object ob1, Object ob2, String relation, double measure) throws AlignmentException { for ( String key : this.params.stringPropertyNames() ) { String value = this.params.getProperty(key); - URI uri1 = URI.create( key ); - String newValue = null; - URI uri2 = URI.create( value ); - if ( value.contains( this.namespace ) ) { - newValue = value.substring( this.namespace.length() ); - uri2 = URI.create( this.namespaceNew + newValue ); - } - else { - newValue = value; - uri2 = URI.create( newValue ); - } + URI uri1 = URI.create(key); + URI uri2 = URI.create(this.namespaceNew + value.substring(value.indexOf(this.base) + this.base.length())); + //System.out.println( "[" + key + "][" + value + "]" ); this.alignment.addAlignCell( uri1, uri2, "=", 1 ); } - long newTime = System.currentTimeMillis(); - this.alignment.setExtension( Namespace.ALIGNMENT.uri, Annotations.TIME, Long.toString(newTime - time) ); - + /* OutputStream stream ;//= new FileOutputStream(filename); if ( fileName == null ) { stream = System.out; } else { stream = new FileOutputStream( fileName ); } + // Outputing PrintWriter writer = new PrintWriter ( new BufferedWriter( new OutputStreamWriter( stream, "UTF-8" )), true); AlignmentVisitor renderer = new RDFRendererVisitor( writer ); this.alignment.render(renderer); + + //RDFRendererVisitor renderer = new RDFRendererVisitor(writer); + //renderer.visit(this.alignment); + writer.flush(); writer.close(); + * + */ } catch (AlignmentException aex) { System.out.println( "Exception " + aex.getMessage() ); } catch (Exception ex) { System.err.println("Exception " + ex.getMessage()); } } + public void setNewNamespace(String newNamespace) { + this.namespaceNew = newNamespace; + //System.out.println("New namespace [" + this.namespaceNew + "]"); + } + //change the namespace of the modified ontology public OntModel changeNamespace () { List<Statement> statements = this.modifiedModel.listStatements().toList(); //the statements of the model - String newNamespace; - - int index = this.namespace.indexOf("#"); - newNamespace = this.namespace.substring(0, index) + "new#"; - this.namespaceNew = newNamespace; + this.isChanged = true; - boolean isSubj, isPred, isObj; + boolean isSubj, isPred, isObj;; OntModel newModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );//create new Model @@ -1401,7 +1406,7 @@ public class OntologyModifier { Resource subject = stm.getSubject(); //the subject Property predicate = stm.getPredicate(); //the predicate RDFNode object = stm.getObject(); //the object - + Resource subj = null; Property pred = null; Resource obj = null; @@ -1411,7 +1416,7 @@ public class OntologyModifier { if ( subject.getLocalName() != null ) if ( !subject.isLiteral() ) if ( subject.getNameSpace().equals( this.namespace ) ) { - subj = newModel.createResource( newNamespace + subject.getLocalName() ); + subj = newModel.createResource( this.namespaceNew + subject.getLocalName() ); isSubj = true; } @@ -1419,54 +1424,91 @@ public class OntologyModifier { if ( object.canAs( Resource.class ) ) if ( object.isURIResource() ) if ( object.asResource().getNameSpace().equals( this.namespace ) ) { - obj = newModel.createResource( newNamespace + object.asResource().getLocalName() ); + obj = newModel.createResource( this.namespaceNew + object.asResource().getLocalName() ); isObj = true; } if ( !predicate.isLiteral() ) if ( predicate.getNameSpace().equals( this.namespace ) ) { - pred = newModel.createProperty( newNamespace + predicate.getLocalName() ); + pred = newModel.createProperty( this.namespaceNew + predicate.getLocalName() ); isPred = true; } if ( isSubj ) { if ( isPred ) { - if ( isObj ) newModel.add( subj, pred, obj ); - else newModel.add(subj, pred, object); + if ( isObj ) + newModel.add( subj, pred, obj ); + else + newModel.add(subj, pred, object); } else { - if ( isObj ) newModel.add( subj, predicate, obj ); - else newModel.add( subj, predicate, object ); + if ( isObj ) + newModel.add( subj, predicate, obj ); + else + newModel.add( subj, predicate, object ); } } else { if ( isPred ) { - if ( isObj ) newModel.add( subject, pred, obj ); - else newModel.add( subject, pred, object ); + if ( isObj ) + newModel.add( subject, pred, obj ); + else + newModel.add( subject, pred, object ); } else { - if ( isObj ) newModel.add( subject, predicate, obj ); - else newModel.add( subject, predicate, object ); + if ( isObj ) + newModel.add( subject, predicate, obj ); + else + newModel.add( subject, predicate, object ); } } } + //rename the namespace of owl:Ontology List<Ontology> ontos = newModel.listOntologies().toList(); for ( Ontology o : ontos ) { - ResourceUtils.renameResource( o, newNamespace ); + ResourceUtils.renameResource( o, this.namespaceNew ); } - //this.namespace = newNamespace; //change the namespace + return newModel; } //returns the modified ontology after changing the namespace public OntModel getModifiedOntology () { //System.out.println( "->change namespace" ); - if ( !this.isChanged ) - this.modifiedModel = changeNamespace(); //change the namespace of the modified ontology + this.modifiedModel = changeNamespace(); //change the namespace of the modified ontology //System.out.println( "->namespace changed" ); return this.modifiedModel; } + //set the model + public void setModel( OntModel model ) { + this.model = model; + } + + public void setModifiedModel(OntModel model) { + this.modifiedModel = model; + } + + //ca ii schimbi namespace-ul, de-asta trebuie sa il intorci pe asta separat + public OntModel getModifiedModel() { + return this.modifiedModel; + } + + //get properties + public Properties getProperties() { + return this.params; + } + + public void setProperties( Properties params ) { + this.params = params; + + Enumeration e = this.params.propertyNames(); + String aux = (String)e.nextElement(); + base = aux.substring(0, aux.lastIndexOf("#")+1); + + this.isAlign = true; + } + //returns the alignment public Alignment getAlignment() { return this.alignment; @@ -1495,33 +1537,44 @@ public class OntologyModifier { value = Float.valueOf( this.parameter.getValue() ).floatValue(); else aux = this.parameter.getValue(); + if ( !this.isAlign ) { initializeAlignment(); //determine the elements from the initial reference alignment } //add percentage classes - if ( name.equals( ParametersIds.ADD_SUBCLASS ) ) { + if ( name.equals( ParametersIds.ADD_CLASSES ) ) { System.out.println( "Add Class" + "[" + value + "]"); addSubClasses( value ); - } //remove percentage classes - if ( name.equals( ParametersIds.REMOVE_SUBCLASS ) ) { + } + + //remove percentage classes + if ( name.equals( ParametersIds.REMOVE_CLASSES ) ) { System.out.println( "Remove Class" + "[" + value + "]"); removeSubClasses( value ); - } //remove percentage comments - if ( name.equals( ParametersIds.REMOVE_COMMENT ) ) { + } + + //remove percentage comments + if ( name.equals( ParametersIds.REMOVE_COMMENTS ) ) { System.out.println( "Remove Comments" + "[" + value + "]"); removeComments ( value ); - } //remove percentage properties - if ( name.equals( ParametersIds.REMOVE_PROPERTY ) ) { + } + + //remove percentage properties + if ( name.equals( ParametersIds.REMOVE_PROPERTIES ) ) { System.out.println( "Remove Property" + "[" + value + "]"); removeProperties ( value ); - } //add percentage properties - if ( name.equals( ParametersIds.ADD_PROPERTY ) ) { + } + + //add percentage properties + if ( name.equals( ParametersIds.ADD_PROPERTIES ) ) { System.out.println( "Add Property" + "[" + value + "]"); addProperties ( value ); - } //recursive add nbClasses starting from level level - if ( name.equals( ParametersIds.ADD_CLASSES ) ) { + } + + //recursive add nbClasses starting from level level + if ( name.equals( ParametersIds.ADD_CLASSESLEVEL ) ) { aux = ((Float)value).toString(); int index = aux.indexOf("."); int level = Integer.valueOf( aux.substring(0, index) ); @@ -1530,40 +1583,59 @@ public class OntologyModifier { System.out.println( "nbClasses " + nbClasses ); float percentage = 1.00f; addClasses ( level, nbClasses, percentage ); - } //remove all the classes from the level level - if ( name.equals( ParametersIds.REMOVE_CLASSES ) ) { + } + + //remove all the classes from the level level + if ( name.equals( ParametersIds.REMOVE_CLASSESLEVEL ) ) { System.out.println("Remove all classes from level" + (int)value ); removeClassesFromLevel ( (int)value ); - } //flatten a level + } + + //flatten a level if ( name.equals( ParametersIds.LEVEL_FLATTENED ) ) { //levelFlattened ( level ); levelFlattened ( (int)value ); System.out.println( "New class hierarchy level " + getMaxLevel() ) ; - } //rename classes + } + + //rename classes if ( name.equals( ParametersIds.RENAME_CLASSES ) ) { System.out.println("Rename classes" + "[" + value + "]" ); //System.out.println("\nValue = " + value + "\n"); //activeProperties, activeClasses, .. this.modifiedModel = renameResource ( false, true, value, true, false, false, 0); - } //rename properties + } + + //rename properties if ( name.equals( ParametersIds.RENAME_PROPERTIES ) ) { System.out.println("Rename properties " + "[" + value + "]" ); //System.out.println("\nValue = " + value + "\n"); //activeProperties, activeClasses, .. this.modifiedModel = renameResource ( true, false, value, true, false, false, 0); - } //remove percentage restrictions - if ( name.equals( ParametersIds.REMOVE_RESTRICTION ) ) { + } + + //remove percentage restrictions + if ( name.equals( ParametersIds.REMOVE_RESTRICTIONS ) ) { System.out.println("Remove restrictions" + "[" + value + "]"); removeRestrictions( value ); - } //remove percentage individuals + } + + //remove percentage individuals if ( name.equals( ParametersIds.REMOVE_INDIVIDUALS ) ) { System.out.println("Remove individuals" + "[" + value + "]"); removeIndividuals( value ); - } //no hierarchy + } + + //no hierarchy if ( name.equals( ParametersIds.NO_HIERARCHY ) ) { System.out.println( "NoHierarchy" ); noHierarchy(); } + + //rebuild the class hierarchy every time + this.isBuild = false; + this.isChanged = false; + } } -} +} \ No newline at end of file diff --git a/src/fr/inrialpes/exmo/align/gen/ParametersIds.java b/src/fr/inrialpes/exmo/align/gen/ParametersIds.java index 77dc8c42..64e94049 100644 --- a/src/fr/inrialpes/exmo/align/gen/ParametersIds.java +++ b/src/fr/inrialpes/exmo/align/gen/ParametersIds.java @@ -1,7 +1,7 @@ /* - * $Id$ + * $Id: ParametersIds.java * - * Copyright (C) 2010, INRIA + * Copyright (C) 2003-2010, INRIA * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -26,19 +26,20 @@ package fr.inrialpes.exmo.align.gen; public class ParametersIds { - public static String ADD_SUBCLASS = "addSubClass"; //adds random classes - public static String REMOVE_SUBCLASS = "removeSubClass"; //removes random classes - public static String REMOVE_PROPERTY = "removeProperty"; //removes random properties - public static String REMOVE_COMMENT = "removeComment"; //removes random comments - public static String LEVEL_FLATTENED = "levelFlattened"; //flattens a level - public static String ADD_PROPERTY = "addProperty"; //adds random propeties - public static String REMOVE_CLASSES = "removeClasses"; //remove classes from level - public static String ADD_CLASSES = "addClasses"; //add c classes beginning from level l -> the value of this parameters should be: - //beginning_level.number_of_classes_to_add - public static String RENAME_PROPERTIES="renameProperties"; //renames properties - public static String RENAME_CLASSES ="renameClasses"; //renames classes - public static String RENAME_RESOURCES = "renameResources"; //renames properties + classes - public static String REMOVE_RESTRICTION= "removeRestriction"; //removes restrictions - public static String REMOVE_INDIVIDUALS= "removeIndividuals"; //removes individuals - public static String NO_HIERARCHY = "noHierarchy"; //no hierarchy + public static String ADD_CLASSES = "addClasses"; //adds random classes + public static String REMOVE_CLASSES = "removeClasses"; //removes random classes + public static String REMOVE_PROPERTIES = "removeProperties"; //removes random properties + public static String REMOVE_COMMENTS = "removeComments"; //removes random comments + public static String LEVEL_FLATTENED = "levelFlattened"; //flattens a level + public static String ADD_PROPERTIES = "addProperties"; //adds random propeties + public static String REMOVE_CLASSESLEVEL= "removeClasses"; //remove classes from level + public static String ADD_CLASSESLEVEL = "addClassesLevel"; //add c classes beginning from level l -> the value of this parameters should be: + //beginning_level.number_of_classes_to_add + public static String RENAME_PROPERTIES = "renameProperties"; //renames properties + public static String RENAME_CLASSES = "renameClasses"; //renames classes + public static String RENAME_RESOURCES = "renameResources"; //renames properties + classes + public static String REMOVE_RESTRICTIONS= "removeRestrictions"; //removes restrictions + public static String REMOVE_INDIVIDUALS = "removeIndividuals"; //removes individuals + public static String NO_HIERARCHY = "noHierarchy"; //no hierarchy } + diff --git a/src/fr/inrialpes/exmo/align/gen/TestGenerator.java b/src/fr/inrialpes/exmo/align/gen/TestGenerator.java index 9acbd901..3e88a003 100644 --- a/src/fr/inrialpes/exmo/align/gen/TestGenerator.java +++ b/src/fr/inrialpes/exmo/align/gen/TestGenerator.java @@ -1,7 +1,7 @@ /* - * $Id$ + * $Id: TestGenerator.java * - * Copyright (C) 2010-2011, INRIA + * Copyright (C) 2003-2010, INRIA * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -38,12 +38,16 @@ import org.semanticweb.owl.align.Alignment; public class TestGenerator implements AlignedOntologyGenerator { private Properties parameters; //the set of parameters + private Properties params; //the modifications private OntModel model; //initial ontology private OntModel modifiedModel; //modified ontology private Alignment alignment; //the reference alignment + private String namespace = ""; //the namespace + private OntologyModifier modifier = null; //the modifier + public TestGenerator() {} - + //returns the modified ontology public Ontology getModifiedOntology() { JENAOntology onto = new JENAOntology(); //cast the model into Ontology @@ -53,17 +57,6 @@ public class TestGenerator implements AlignedOntologyGenerator { //set the initial ontology public void setOntology( Ontology o ) { - try { - this.model = ((JENAOntology)o).getOntology(); - this.modifiedModel = ((JENAOntology)o).getOntology(); - } catch ( Exception ex ) { - System.err.println( "Exception " + ex.getMessage() ); - } - } - - - public Alignment generate(Ontology o, Properties p) { - //cast the model into a Jena OntModel if ( o instanceof JENAOntology ) { this.model = ((JENAOntology)o).getOntology(); this.modifiedModel = ((JENAOntology)o).getOntology(); @@ -72,16 +65,41 @@ public class TestGenerator implements AlignedOntologyGenerator { System.err.println("Error : The object given is not an OntModel"); System.exit(-1); } + } + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + //return the newNamespace + public String getNamespace() { + return this.namespace; + } + + //set the parameters + public void setParameters(Properties p) { this.parameters = p; - this.alignment = new URIAlignment(); - OntologyModifier modifier = new OntologyModifier( this.model, this.modifiedModel, this.alignment); //build the ontology modifier for the first time + modifier.setProperties(this.parameters); + } + + //get the parameters + public Properties getParameters() { + this.parameters = modifier.getProperties(); //get the properties + return this.parameters; + } + + //generate the alingnment + public Alignment generate(Ontology o, Properties p) { + this.params = p; + this.alignment = new URIAlignment(); + modifier = new OntologyModifier( this.model, this.modifiedModel, this.alignment); //build the ontology modifier for the first time modifier.initializeAlignment(); //initialize the reference alignment - int level = modifier.getMaxLevel(); //get the max level of the class hierarchy of the ontology + //set the new namespace + modifier.setNewNamespace( this.namespace ); System.out.println( "[-------------------------------------------------]" ); - for(String key : this.parameters.stringPropertyNames()) { - String value = this.parameters.getProperty(key); //System.out.println( "[" +key + "] => [" + value + "]"); + for(String key : this.params.stringPropertyNames()) { + String value = this.params.getProperty(key); //System.out.println( "[" +key + "] => [" + value + "]"); //iterate through all the parameters Parameter param = new Parameter(); //build a parameter param.setName( key ); @@ -91,10 +109,43 @@ public class TestGenerator implements AlignedOntologyGenerator { System.out.println( "[-------------------------------------------------]" ); //saves the alignment into the file "referenceAlignment.rdf", null->System.out - modifier.computeAlignment( "referenceAlignment.rdf" ); //at the end, compute the reference alignment - this.alignment = modifier.getAlignment(); //get the reference alignment - this.modifiedModel = modifier.getModifiedOntology(); //get the modified ontology + modifier.computeAlignment( "refalign.rdf" ); //at the end, compute the reference alignment + this.alignment = modifier.getAlignment(); //get the reference alignment + this.modifiedModel = modifier.getModifiedOntology(); //get the modified ontology return this.alignment; } -} + + + //generate the alingnment + //newParams => keeps track of the previous modifications + public Alignment generate(Ontology o, Properties p, Properties newParams) { + this.params = p; + this.alignment = new URIAlignment(); + modifier = new OntologyModifier( this.model, this.modifiedModel, this.alignment); //build the ontology modifier for the first time + //modifier.initializeAlignment(); //initialize the reference alignment + + modifier.setProperties(newParams); + //set the new namespace + modifier.setNewNamespace( this.namespace ); + + System.out.println( "[-------------------------------------------------]" ); + for(String key : this.params.stringPropertyNames()) { + String value = this.params.getProperty(key); //System.out.println( "[" +key + "] => [" + value + "]"); + //iterate through all the parameters + Parameter param = new Parameter(); //build a parameter + param.setName( key ); + param.setValue( value ); + modifier.modifyOntology( param ); //modify the ontology according to it + } + System.out.println( "[-------------------------------------------------]" ); + + //saves the alignment into the file "referenceAlignment.rdf", null->System.out + modifier.computeAlignment( "refalign.rdf" ); //at the end, compute the reference alignment + this.alignment = modifier.getAlignment(); //get the reference alignment + this.modifiedModel = modifier.getModifiedOntology(); //get the modified ontology + return this.alignment; + } + + +} \ No newline at end of file diff --git a/src/fr/inrialpes/exmo/align/gen/URITree.java b/src/fr/inrialpes/exmo/align/gen/URITree.java index d69a9f49..9f055441 100644 --- a/src/fr/inrialpes/exmo/align/gen/URITree.java +++ b/src/fr/inrialpes/exmo/align/gen/URITree.java @@ -1,7 +1,7 @@ /* - * $Id$ + * $Id: URITree.java * - * Copyright (C) 2011, INRIA + * Copyright (C) 2003-2010, INRIA * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -35,7 +35,7 @@ public class URITree { private URITree parent; //the parent of the node int depth; //the depth of the node int maxDepth; //the max depth of the node - + public URITree(String URI) { this.URI = URI; this.children = new ArrayList<URITree>(); @@ -48,47 +48,47 @@ public class URITree { public String getURI () { return this.URI; } - + //set the URI of the node public void setURI(String URI) { this.URI = URI; } - + //set the depth of the node public void setDepth (int depth) { this.depth = depth; } - + //get the depth of the node public int getDepth() { return this.depth; } - + //return the max depth public int getMaxDepth() { return this.maxDepth; } - + //set the parent of the node public void setParent ( URITree parent ) { this.parent = parent; } - + //get the parent of the node public URITree getParent () { return this.parent; } - + //returns a child from a specific position public URITree getChildAt ( int index ) { return this.children.get( index ); } - + //return the list of children nodes public ArrayList<URITree> getChildrenList() { return this.children; } - + //returns the size of the children public int getChildrenSize() { return this.children.size(); @@ -96,7 +96,7 @@ public class URITree { //add the node with the childURI to the parent with the URI parentURI public void add(URITree root, String childURI, String parentURI) { - //adds the node to the class hierarchy -> a class might have more than one + //adds the node to the class hierarchy -> a class might have more than one //superclass we have to add it to each superclass in part, not only to one _addChildToNode( root, parentURI, childURI ); } @@ -105,7 +105,7 @@ public class URITree { public void addChild (URITree root, URITree node, String URI) { int index = 0; URITree n = null; - + while ( index < node.getChildrenSize() ) { //if the child is already in the list if ( node.getChildAt( index ).getURI().equals( URI ) ) return; @@ -123,15 +123,15 @@ public class URITree { } addChildToNode( node, URI ); } - + //add child to a specific node public void addChildToNode(URITree node, String URI) { URITree child = new URITree( URI ); //creates a new node child.setDepth( node.getDepth() + 1 ); //set the depth of the node - + if ( this.maxDepth < node.getDepth()+1 ) //keeps track of the max depth of the hierarchy this.maxDepth = node.getDepth() + 1; - + child.setParent( node ); //sets the parent of the node node.getChildrenList().add( child ); //adds the node to the parent children list } @@ -223,10 +223,10 @@ public class URITree { int index = 0; URITree node = root; URITree ans = null; - + if ( root.getURI().equals( URI ) ) //if the root has the URI as the URI searched return root; - + while ( index < root.getChildrenSize() ) { //we start to search recursively if ( root.getChildAt( index ).getURI().equals( URI ) ) return root.getChildAt( index ); //we found the node with the given URI @@ -271,12 +271,12 @@ public class URITree { root.getChildrenList().remove( index ); //found the node to delete //return; } - + remove(root.getChildAt( index ), new ArrayList(), 0, URI); index++; } } - + @SuppressWarnings("unchecked") public void remove(URITree node, List occurs, int depth, String URI) { int index = 0; @@ -292,10 +292,10 @@ public class URITree { child.setParent( parent ); //modify the parent parent.getChildrenList().add( child ); //add the child to the parent of node cnt++; - } + } parent.getChildrenList().remove( node ); //remove the node from the children list } - + while( index < node.getChildrenSize()) { URITree n = node.getChildAt( index ); occurs.add( node ); @@ -334,7 +334,7 @@ public class URITree { index++; } } - + //change the depth if the nodes lower the level to node.getDepth()-1 @SuppressWarnings("unchecked") public void changeDepth (URITree root, int level) { @@ -349,12 +349,12 @@ public class URITree { @SuppressWarnings("unchecked") public void change (URITree node, List occurs, int depth, int level) { int index = 0; - + if ( node.getDepth() > level ) { //if it's on the level that we want, we add it to the hierarchy int dept = node.getDepth(); node.setDepth( dept-1 ); } - + while( index < node.getChildrenList().size() ) { URITree n = node.getChildrenList().get(index); occurs.add( node ); @@ -363,13 +363,13 @@ public class URITree { index++; } } - + //print the tree @SuppressWarnings("unchecked") public void printURITree( URITree root ) { int index = 0; //System.out.println( "[" + root.getURI() + "]" + "->" + root.getDepth() ); - + while ( index < root.getChildrenList().size() ) { //recursively print all the children URITrees print(root.getChildAt(index), new ArrayList(), 0); @@ -382,7 +382,7 @@ public class URITree { int index = 0; indent( node.getDepth() ); System.out.println( "[" + node.getURI() + "]" + "->" + node.getDepth() ); - + while( index < node.getChildrenList().size() ) { URITree n = node.getChildrenList().get( index ); occurs.add( node ); @@ -397,8 +397,7 @@ public class URITree { System.out.print( " " ); } } - + } - -- GitLab