diff --git a/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java b/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java index 839e9f9258a1a6a94e8175134ffa04168a2e63d8..75ce22a85635841bc5ba59a3bd7d79712e9c8c8b 100644 --- a/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java +++ b/src/fr/inrialpes/exmo/align/gen/ClassHierarchy.java @@ -1,266 +1,276 @@ +/* + * $Id: ClassHierarchy.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. + */ + +/* This program is based on the ClassHierarchy.java example. + Iterates through all the classes in the hierarchy and builds the class hierarchy + */ + package fr.inrialpes.exmo.align.gen; +//Jena import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.Restriction; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.shared.PrefixMapping; import com.hp.hpl.jena.vocabulary.OWL; - +//Java import java.util.*; +public class ClassHierarchy { + private URITree root; //the root of the tree + private Map m_anonIDs = new HashMap(); + private int m_anonCount = 0; + private int maxDepth; //the max depth of class hierarchy -/* - * This implementation is based on the ClassHierarchy.java example - * iterates through all the classes in the hierarchy and - * builds the class hierarchy - */ + public ClassHierarchy() {} -public class ClassHierarchy { - private URITree root; //the root of the tree - @SuppressWarnings("unchecked") - private Map m_anonIDs = new HashMap(); - private int m_anonCount = 0; - private int maxDepth; //the max depth of class hierarchy - - public ClassHierarchy() {} - - //return the root of the hierarchy - public URITree getRootClassHierarchy () { - return this.root; - } - - //return the max level of the hierarchy - public int getMaxLevel() { - this.maxDepth = this.root.getMaxDepth(); - return this.maxDepth; - } - - //print class hierarchy - public void printClassHierarchy() { - System.out.println( "[-------------------------------------------------]" ); - System.out.println( "The class hierarchy" ); - root.printURITree(this.root); //we print the tree - System.out.println( "The class hierachy" ); - System.out.println( "[-------------------------------------------------]" ); - } - - 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 - node.addChildToNode( node, childURI ); //we add the new childURI to the hierarchy - } - - //updates the class hierarchy - public void updateClassHierarchy ( Properties params ) { - this.root.renameTree(this.root, params); - } + //return the root of the hierarchy + public URITree getRootClassHierarchy () { + return this.root; + } - //returns the list of classes from level level - public List<OntClass> getClassesFromLevel (OntModel model, int level) { - List <URITree> nodes = this.getNodesFromLevel(level); //get all the nodes from the specific level - ArrayList<OntClass> classes = new ArrayList<OntClass>(); - for ( int i=0; i<nodes.size(); i++ ) - classes.add( model.getOntClass( nodes.get(i).getURI() ) ); //builds the list of classes - return classes; - } + //return the max level of the hierarchy + public int getMaxLevel() { + this.maxDepth = this.root.getMaxDepth(); + return this.maxDepth; + } - //get the nodes from a specific level - public List<URITree> getNodesFromLevel (int level) { - return this.getRootClassHierarchy().getNodesFromLevel(this.root, level); - } - - //remove URI from the class hierarchy - public static void removeUri(URITree root, String uri) { - root.removeFromURITree(root, uri); //remove URI uri from the class hierarchy - } + //print class hierarchy + public void printClassHierarchy() { + System.out.println( "[--------------------]" ); + System.out.println( "The class hierarchy" ); + root.printURITree(this.root); //we print the tree + System.out.println( "The class hierachy" ); + System.out.println( "[---------------------]" ); + } + + 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 + node.addChildToNode( node, childURI ); //we add the new childURI to the hierarchy + } + + //updates the class hierarchy + public void updateClassHierarchy (Properties params) { + this.root.renameTree(this.root, params); + } + + //returns the list of classes from level level + public List<OntClass> getClassesFromLevel (OntModel model, int level) { + List <URITree> nodes = this.getNodesFromLevel(level); //get all the nodes from the specific level + ArrayList<OntClass> classes = new ArrayList<OntClass>(); + for ( int i=0; i<nodes.size(); i++ ) + classes.add( model.getOntClass( nodes.get(i).getURI() ) ); //builds the list of classes + return classes; + } - //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 ) { - URITree node = this.root.searchURITree( this.root, cls.getURI() ); //search for the class URI in the class hierarchy - URITree parentNode = null; - - int depth = node.getDepth(); //get the depth of the node - parentNode = node.getParent(); //get the parent of the node - for ( URITree child : node.getChildrenList() ) { //change the parent of the subclasses of the node - child.setDepth( depth ); //change the depth of the child - child.setParent( parentNode ); //set the parent of the child - parentNode.getChildrenList().add( child ); //add the child to the parent children - } - parentNode.getChildrenList().remove( node ); //remove the node from children list + //get the nodes from a specific level + public List<URITree> getNodesFromLevel (int level) { + return this.getRootClassHierarchy().getNodesFromLevel(this.root, level); + } + + //remove URI from the class hierarchy + public void removeUri(URITree root, String uri) { + root.removeFromURITree(root, uri); //remove URI uri from the class hierarchy + } + + //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 ) { + URITree node = this.root.searchURITree( this.root, cls.getURI() ); //search for the class URI in the class hierarchy + URITree parentNode = null; - if ( depth == 1 ) { //the parent is owl:Thing - OntClass thing = model.createClass( OWL.Thing.getURI() );//Thing class - return thing; - } - else - return model.getOntClass( node.getParent().getURI() ); //return the parent class - } - - //return a random class from the level - level - public OntClass getRandomClassFromLevel( OntModel model, int level ) { - Random rdm = new Random(); - List<URITree> childrenNodes = getNodesFromLevel( level ); //get the list of nodes from the level level - int index = rdm.nextInt( childrenNodes.size() ); //get a random number between 0 and the number_of_classes_from_that_level - return model.getOntClass( childrenNodes.get(index).getURI() ); //returns the class from the position that we have selected -> the random number - } + int depth = node.getDepth(); //get the depth of the node + parentNode = node.getParent(); //get the parent of the node + for ( URITree child : node.getChildrenList() ) { //change the parent of the subclasses of the node + child.setDepth( depth ); //change the depth of the child + child.setParent( parentNode ); //set the parent of the child + parentNode.getChildrenList().add( child ); //add the child to the parent children + } + + parentNode.getChildrenList().remove( node ); //remove the node from children list + if ( depth == 1 ) { //the parent is owl:Thing + OntClass thing = model.createClass( OWL.Thing.getURI() ); //Thing class + return thing; + } + else + return model.getOntClass( node.getParent().getURI() ); //return the parent class + } + + //return a random class from the level - level + public OntClass getRandomClassFromLevel( OntModel model, int level ) { + Random rdm = new Random(); + List<URITree> childrenNodes = getNodesFromLevel( level ); //get the list of nodes from the level level + int index = rdm.nextInt( childrenNodes.size() ); //get a random number between 0 and the number_of_classes_from_that_level + return model.getOntClass( childrenNodes.get(index).getURI() ); //returns the class from the position that we have selected -> the random number + } - //modifies the class hierarchy after we have flattened it - public boolean flattenClassHierarchy ( OntModel model, int level, ArrayList<OntClass> childClasses, - ArrayList<OntClass> parentClasses, ArrayList<OntClass> superLevelClasses) { - - List<URITree> childrenNodes = getNodesFromLevel( level ); - URITree parentNode = null; - URITree superNode = null; - boolean active = true; - for ( URITree childNode : childrenNodes ) { //for each child class - parentNode = childNode.getParent(); //get the parent node - superNode = parentNode.getParent(); //get the parents of the parent node + + //modifies the class hierarchy after we have flattened it + public boolean flattenClassHierarchy ( OntModel model, int level, ArrayList<OntClass> childClasses, + ArrayList<OntClass> parentClasses, ArrayList<OntClass> superLevelClasses) { + + List<URITree> childrenNodes = getNodesFromLevel( level ); + URITree parentNode = null; + URITree superNode = null; + boolean active = true; + for ( URITree childNode : childrenNodes ) { //for each child class + parentNode = childNode.getParent(); //get the parent node + superNode = parentNode.getParent(); //get the parents of the parent node - childClasses.add( model.getOntClass( childNode.getURI() ) ); //build the list of children classes - parentClasses.add( model.getOntClass( parentNode.getURI() ) ); //build the list of parent classes + childClasses.add( model.getOntClass( childNode.getURI() ) ); //build the list of children classes + parentClasses.add( model.getOntClass( parentNode.getURI() ) ); //build the list of parent classes - if ( !superNode.getURI().equals( "Thing" ) ) { //if the parent of the child class is not owl:Thing (1st level class) - superLevelClasses.add( model.getOntClass( superNode.getURI() ) );//build the list of the parents of the parent classes - active = true; //set the flag -> we don't have a 1st level class - } - else { - active = false; //set the flag -> we have a 1st level class - } - } - - flattenHierarchy( childrenNodes ); //modify the links among the nodes from the class hierarchy - this.getRootClassHierarchy().changeDepth ( this.getRootClassHierarchy(), level ); //change the depth - return active; - } - - //modify the links between the nodes from the class hierarchy - public void flattenHierarchy (List<URITree> childrenNodes ) { - URITree parentNode; + if ( !superNode.getURI().equals( "Thing" ) ) { //if the parent of the child class is not owl:Thing (1st level class) + superLevelClasses.add( model.getOntClass( superNode.getURI() ) );//build the list of the parents of the parent classes + active = true; //set the flag -> we don't have a 1st level class + } + else { + active = false; //set the flag -> we have a 1st level class + } + } - for ( URITree childNode : childrenNodes ) { - parentNode = childNode.getParent(); //get the parent of the node - childNode.setParent( parentNode.getParent() ); //change the parent of my superclass to the [parent of the "parent node"] - - childNode.setDepth( parentNode.getDepth() ); //change it's depth + flattenHierarchy( childrenNodes ); //modify the links among the nodes from the class hierarchy + this.getRootClassHierarchy().changeDepth ( this.getRootClassHierarchy(), level );//change the depth + return active; + } + + //modify the links between the nodes from the class hierarchy + public void flattenHierarchy (List<URITree> childrenNodes ) { + URITree parentNode; + + for ( URITree childNode : childrenNodes ) { + parentNode = childNode.getParent(); //get the parent of the node + childNode.setParent( parentNode.getParent() ); //change the parent of my superclass to the [parent of the "parent node"] + + childNode.setDepth( parentNode.getDepth() ); //change it's depth - parentNode.getChildrenList().remove( childNode ); //remove it from the children list of parentNode - parentNode.getParent().getChildrenList().add( childNode ); //add it to the children list of superClass - } - } - - //builds the class hierarchy - @SuppressWarnings("unchecked") - public void buildClassHierarchy(OntModel model) { - Iterator i = model.listHierarchyRootClasses(); - this.root = new URITree( "Thing" ); //the root of the hierarchy is always owl:Thing - - //get the list of root classes - List<OntClass> ontologyClasses = model.listClasses().toList(); - List<OntClass> rootClasses = model.listClasses().toList(); - for ( OntClass cls : ontologyClasses ) { - if ( cls.isHierarchyRoot() ) - rootClasses.add( cls ); - } + parentNode.getChildrenList().remove( childNode ); //remove it from the children list of parentNode + parentNode.getParent().getChildrenList().add( childNode ); //add it to the children list of superClass + } + } + + //builds the class hierarchy + @SuppressWarnings("unchecked") + public void buildClassHierarchy(OntModel model) { + Iterator i = model.listHierarchyRootClasses(); + this.root = new URITree( "Thing" ); //the root of the hierarchy is always owl:Thing + + //get the list of root classes + List<OntClass> ontologyClasses = model.listClasses().toList(); + List<OntClass> rootClasses = model.listClasses().toList(); + for ( OntClass cls : ontologyClasses ) { + if ( cls.isHierarchyRoot() ) + rootClasses.add( cls ); + } - /* - System.out.println("\n*****"); - System.out.println("Root classes "); - for ( OntClass cls : rootClasses ) { - System.out.println("ROOT = [" + cls.getLocalName() + "]"); + for ( OntClass rootClass : rootClasses ) { + if ( !rootClass.isAnon() ) //if a root class is not an anonymous class + getClass( rootClass, new ArrayList(), 0 ) ; + else { + for ( Iterator it = rootClass.listSubClasses(); it.hasNext(); ) { + getClass ( (OntClass)it.next(), new ArrayList(), 1 ); } - System.out.println("\n*****\n"); - */ - for ( OntClass rootClass : rootClasses ) { - if ( !rootClass.isAnon() ) //if a root class is not an anonymous class - getClass( rootClass, new ArrayList(), 0 ) ; - else { - for ( Iterator it = rootClass.listSubClasses(); it.hasNext(); ) { - getClass ( (OntClass)it.next(), new ArrayList(), 1 ); - } + } + } + this.maxDepth = this.root.getMaxDepth(); + } + + @SuppressWarnings("unchecked") + public void getClass (OntClass cls, List occurs, int depth) { + renderClassDescription( cls, depth ); + + if ( cls.canAs( OntClass.class ) && !occurs.contains( cls ) ) { // recurse to the next level down + for (Iterator i = cls.listSubClasses( true ); i.hasNext(); ) { + OntClass sub = (OntClass) i.next(); + occurs.add( cls ); // we push this expression on the occurs list before we recurse + getClass( sub, occurs, depth + 1 ); + occurs.remove( cls ); + } + } + } + + @SuppressWarnings("unchecked") + public void renderClassDescription( OntClass c, int depth ) { + if (c.isRestriction()) { + renderRestriction( (Restriction) c.as( Restriction.class ) ); + } + else { + if ( !c.isAnon() ) { + String uri; //the URI of the child + String parentURI = ""; //the URI of the parent + int found = 0; + OntClass aux = null; + uri = c.getURI(); + + for ( Iterator it = c.listSuperClasses(); it.hasNext(); ) { //search to see if the class has a superclass which is not anonymous + aux = (OntClass)it.next(); + if ( !aux.isAnon() ) { //is not an anonymous class + found = 1; //got the parent + parentURI = aux.getURI(); + this.root.add(this.root, uri, parentURI); } } - this.maxDepth = this.root.getMaxDepth(); - } - - @SuppressWarnings("unchecked") - public void getClass (OntClass cls, List occurs, int depth) { - renderClassDescription( cls, depth ); - - if ( cls.canAs( OntClass.class ) && !occurs.contains( cls ) ) { // recurse to the next level down - for (Iterator i = cls.listSubClasses( true ); i.hasNext(); ) { - OntClass sub = (OntClass) i.next(); - occurs.add( cls ); // we push this expression on the occurs list before we recurse - getClass( sub, occurs, depth + 1 ); - occurs.remove( cls ); - } - } - } - - @SuppressWarnings("unchecked") - public void renderClassDescription( OntClass c, int depth ) { - if (c.isRestriction()) { - renderRestriction( (Restriction) c.as( Restriction.class ) ); - } - else { - if ( !c.isAnon() ) { - String uri; //the URI of the child - String parentURI = ""; //the URI of the parent - int found = 0; - OntClass aux = null; - uri = c.getURI(); - - for ( Iterator it = c.listSuperClasses(); it.hasNext(); ) { //search to see if the class has a superclass which is not anonymous - aux = (OntClass)it.next(); - if ( !aux.isAnon() ) { //is not an anonymous class - found = 1; //got the parent - parentURI = aux.getURI(); - this.root.add(this.root, uri, parentURI); - } - } - if ( found == 0 ) //has no parent until now - this.getRootClassHierarchy().add(this.root, uri, "Thing"); - } - else { - renderAnonymous( c, "class" ); //an anonymous class - } - } - } - - // Render a URI - protected String renderURI( PrefixMapping prefixes, String uri ) { - //System.out.println("URI = [" + uri + "]"); - return prefixes.shortForm( uri ); - } - - protected void renderRestriction( Restriction r ) { - if ( !r.isAnon() ) { - renderURI( r.getModel(), r.getURI() ); - } - else { - renderAnonymous( r, "restriction" ); - } - renderURI( r.getModel(), r.getOnProperty().getURI() ); - } - - // Render an anonymous class or restriction - @SuppressWarnings("unchecked") - protected void renderAnonymous( Resource anon, String name ) { - String anonID = (String) m_anonIDs.get( anon.getId() ); - if (anonID == null) { - anonID = "a-" + m_anonCount++; - m_anonIDs.put( anon.getId(), anonID ); - } - } + if ( found == 0 ) //has no parent until now + this.getRootClassHierarchy().add(this.root, uri, "Thing"); + } + else { + renderAnonymous( c, "class" ); //an anonymous class + } + } + } + + // Render a URI + protected String renderURI( PrefixMapping prefixes, String uri ) { + return prefixes.shortForm( uri ); + } + + protected void renderRestriction( Restriction r ) { + if ( !r.isAnon() ) { + renderURI( r.getModel(), r.getURI() ); + } + else { + renderAnonymous( r, "restriction" ); + } + renderURI( r.getModel(), r.getOnProperty().getURI() ); + } + + // Render an anonymous class or restriction + @SuppressWarnings("unchecked") + protected void renderAnonymous( Resource anon, String name ) { + String anonID = (String) m_anonIDs.get( anon.getId() ); + if (anonID == null) { + anonID = "a-" + m_anonCount++; + m_anonIDs.put( anon.getId(), anonID ); + } + } - // Generate the indentation - protected void indent( int depth ) { - for (int i = 0; i < depth; i++) { - System.out.print( " " ); - } - } + // Generate the indentation + protected void indent( int depth ) { + for (int i = 0; i < depth; i++) { + System.out.print( " " ); + } + } } diff --git a/src/fr/inrialpes/exmo/align/gen/Main.java b/src/fr/inrialpes/exmo/align/gen/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..3b71063f215cac2b6c80025c8ecbd6fd953f637c --- /dev/null +++ b/src/fr/inrialpes/exmo/align/gen/Main.java @@ -0,0 +1,190 @@ +/* + * $Id: Main.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. + */ + +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 java.io.File; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.util.Properties; +import fr.inrialpes.exmo.ontowrap.jena25.JENAOntology; +import org.semanticweb.owl.align.Alignment; + +public class Main { + + //load ontology + public static 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 dest) { + try { + File f = new File(dest); + FileOutputStream fout = new FileOutputStream(f); + Charset defaultCharset = Charset.forName("UTF8"); + RDFWriter writer = model.getWriter("RDF/XML-ABBREV"); + writer.setProperty("showXmlDeclaration","true"); + writer.write(model.getBaseModel(), new OutputStreamWriter(fout, defaultCharset), ""); + fout.close(); + } catch (Exception ex) { + System.out.println("Exception " + ex.getMessage()); + } + } + + + public static void printUsage() { + System.out.println( "inputOntology outputOntology parameters" ); + System.out.println( "[--------------------------------------------------------------------------]" ); + System.out.println( "[------------- The list of all modification is the following --------------]" ); + + System.out.println( "[1. Remove percentage subclasses \"removeSubClass\" --------------]" ); + System.out.println( "[2. Remove percentage properties \"removeProperty\" --------------]" ); + System.out.println( "[3. Remove percentage comments \"removeComment\" --------------]" ); + System.out.println( "[4. Remove percentage restrictions \"removeRestriction\" --------------]" ); + System.out.println( "[5. Add percentage subclasses \"addSubClass\" --------------]" ); + System.out.println( "[6. Add percentage properties \"addProperty\" --------------]" ); + System.out.println( "[7. Rename percentage classes \"renameClasses\" --------------]" ); + System.out.println( "[8. Rename percentage properties \"renameProperties\" --------------]" ); + + System.out.println( "[9. Remove all the classes from a level\"removeClasses\" ---------------]" ); + System.out.println( "[10. Add nbClasses to a specific level \"addClasses\" ---------------]" ); + System.out.println( "[11. Level flattened \"levelFlattened\" ---------------]" ); + System.out.println( "[12. Remove individuals \"removeIndividuals\" ------------]" ); + //noHierarchy + System.out.println( "[--------------------------------------------------------------------------]" ); + System.exit(-1); + } + + public static void main (String [] args) { + String fileName = "", destFile = ""; + Properties parameters; + + if ( args.length < 2 ) { + System.out.println("Usage"); + printUsage(); + } + else { + System.out.println( "Input ontology: [" + args[0] + "]" ); + System.out.println( "Output ontology: [" + args[1] + "]" ); + + fileName = args[0]; + destFile = args[1]; + parameters = new Properties(); //initialize the parameters + + for ( int i=2; i<args.length; i+=2 ) { + if ( args[i].equals("addSubClass") ) /* add percentage classes */ + parameters.setProperty(ParametersIds.ADD_SUBCLASS, args[i+1]); + + //add c classes beginning from level l -> the value of this parameters should be: + //beginning_level.number_of_classes_to_add + if ( args[i].equals("addClasses") ) /* add c classes beginning from level l */ + parameters.setProperty(ParametersIds.ADD_CLASSES, args[i+1]); + + if ( args[i].equals("removeSubClass") ) /* remove percentage classes */ + parameters.setProperty(ParametersIds.REMOVE_SUBCLASS, args[i+1]); + + if ( args[i].equals("removeClasses") ) /* remove classes from level */ + parameters.setProperty(ParametersIds.REMOVE_CLASSES, args[i+1]); + + if ( args[i].equals("addProperty") ) /* add percentage properties */ + parameters.setProperty(ParametersIds.ADD_PROPERTY, args[i+1]); + + if ( args[i].equals("removeProperty") ) /* remove percentage properties */ + parameters.setProperty(ParametersIds.REMOVE_PROPERTY, args[i+1]); + + if ( args[i].equals("renameProperties") ) /* rename percentage properties */ + parameters.setProperty(ParametersIds.RENAME_PROPERTIES, args[i+1]); + + if ( args[i].equals("removeComment") ) /* remove percentage comments */ + parameters.setProperty(ParametersIds.REMOVE_COMMENT, args[i+1]); + + if ( args[i].equals("levelFlattened") ) /* flattened level */ + parameters.setProperty(ParametersIds.LEVEL_FLATTENED, args[i+1]); + + if ( args[i].equals("renameClasses") ) /* rename percentage classes */ + parameters.setProperty(ParametersIds.RENAME_CLASSES, args[i+1]); + + if ( args[i].equals("renameResources") ) /* rename percentage resources */ + parameters.setProperty(ParametersIds.RENAME_RESOURCES, args[i+1]); + + if ( args[i].equals("removeRestriction") ) /* remove percentage restrictions */ + parameters.setProperty(ParametersIds.REMOVE_RESTRICTION, args[i+1]); + + if ( args[i].equals("removeIndividuals") ) /* remove percentage individuals */ + parameters.setProperty(ParametersIds.REMOVE_INDIVIDUALS, args[i+1]); + + if ( args[i].equals( ("noHierarchy")) ) /* no hierarchy */ + parameters.setProperty( ParametersIds.NO_HIERARCHY, ParametersIds.NO_HIERARCHY); + } + + 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 ); + + Alignment align = t.generate(onto, parameters); //generate the alignment + + JENAOntology modified = (JENAOntology)t.getModifiedOntology(); //get the modified ontology + if ( modified.getOntology() instanceof OntModel ) + writeOntology(modified.getOntology(), destFile); //write the ontology + + /* Prints the reference alignment + OutputStream stream = new FileOutputStream( "fancyAlignment.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/OntologyModifier.java b/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java index 211eed024359c888cd28546faf44d1b2356a5b65..a5bc18793439e4423f77d1a480386f95c7e805a7 100644 --- a/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java +++ b/src/fr/inrialpes/exmo/align/gen/OntologyModifier.java @@ -1,3 +1,30 @@ +/* + * $Id: OntologyModifier.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. + */ + +/* This program receives as input two ontologies (the original ontology, the ontology that must be modified), + an alignment and a parameter with the modification that must be applied to the input ontology. + After the modification of the initial ontology the alignment must be computed + The file in which we store the alignment is "referenceAlignment.rdf" +*/ + package fr.inrialpes.exmo.align.gen; //Java classes @@ -63,114 +90,105 @@ import edu.smu.tspell.wordnet.WordNetDatabase; //activeRandomString is true -> we replace the label with a random string //activeTranslateString is true -> we translate the label -/* -* This class receives as input two ontologies (the original ontology, the ontology that must be modified), -* an alignment and a parameter with the modification that must be applied to the input ontology. -* After the modification of the initial ontology the alignment must be computed -* The file in which we store the alignment is "referenceAlignment.rdf" -*/ - public class OntologyModifier { - private ClassHierarchy classHierarchy; //the class hierarchy - private OntModel model; //the model - the original Ontology - private OntModel modifiedModel; //the modified Ontology - private String namespace; //the Namespace - private String namespaceNew; - private Parameter parameter; //the parameter for which we must apply the changes - private Alignment alignment; //the alignment of the two Ontologies - private Properties params; //the alignment - 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 - - //Ontology init, Ontology modified, Alignment align - public OntologyModifier ( OntModel model, OntModel modifiedModel, Alignment alignment ) { - this.model = model; - this.modifiedModel = modifiedModel; - this.alignment = alignment; - this.parameter = null; - this.isBuild = false; - this.isAlign = false; - this.isChanged = false; - this.namespace = model.getNsPrefixURI(""); - this.namespaceNew = ""; - this.params = new Properties(); - } - - //no-args constructor - public OntologyModifier () { - } - - //if we add / remove a class we need to keep track of the class hierarchy - public void buildClassHierarchy () { - this.classHierarchy = new ClassHierarchy(); - this.classHierarchy.buildClassHierarchy( this.modifiedModel ); - //this.classHierarchy.printClassHierarchy(); - } + private ClassHierarchy classHierarchy; //the class hierarchy + private OntModel model; //the model - the original Ontology + private OntModel modifiedModel; //the modified Ontology + private String namespace; //the Namespace + private String namespaceNew; + private Parameter parameter; //the parameter for which we must apply the changes + private Alignment alignment; //the alignment of the two Ontologies + private Properties params; //the alignment + 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 - //generate random string with the length "length" - public String getRandomString() { - Random generator = new Random(); - String characters = "abcdefghijklmnopqrstuvwxyz"; - int length = characters.length(); - char[] text = new char[length]; - for (int i = 0; i < length; i++) { - text[i] = characters.charAt( generator.nextInt(length) ); - } - return new String(text).toUpperCase(); + //Ontology init, Ontology modified, Alignment align + public OntologyModifier ( OntModel model, OntModel modifiedModel, Alignment alignment ) { + this.model = model; + this.modifiedModel = modifiedModel; + this.alignment = alignment; + this.parameter = null; + this.isBuild = false; + this.isAlign = false; + this.isChanged = false; + this.namespace = model.getNsPrefixURI(""); + this.namespaceNew = ""; + this.params = new Properties(); + } + + //no-args constructor + public OntologyModifier () { } + + //if we add / remove a class we need to keep track of the class hierarchy + public void buildClassHierarchy () { + this.classHierarchy = new ClassHierarchy(); + this.classHierarchy.buildClassHierarchy( this.modifiedModel ); + //this.classHierarchy.printClassHierarchy(); + } + + //generate random string with the length "length" + public String getRandomString() { + Random generator = new Random(); + String characters = "abcdefghijklmnopqrstuvwxyz"; + int length = characters.length(); + char[] text = new char[length]; + for (int i = 0; i < length; i++) { + text[i] = characters.charAt( generator.nextInt(length) ); } + return new String(text).toUpperCase(); + } - //remove spaces from a string - public String removeSpaces ( String str ) { - //return str.replaceAll("\\s+", ""); - if ( !str.contains( " " ) ) - return str; - else { - String aux = "", aux1=""; - int index; - - if ( str.contains( " " ) ) { - while ( str.indexOf( " " ) != -1 ) { - index = str.indexOf( " " ); - aux += str.substring( 0, index ); - aux1 = str.substring(index+2); - str = str.substring(index+1, index+2).toUpperCase().concat( aux1 ); - } - aux += str; - return aux; - } - } + //remove spaces from a string + public String removeSpaces ( String str ) { + //return str.replaceAll("\\s+", ""); + if ( !str.contains( " " ) ) return str; - } + else { + String aux = "", aux1=""; + int index; - - //translate the string from English to French - public String translateString( String source ) { - String translatedText = ""; - GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/"); - //Translate.setHttpReferrer("http://code.google.com/p/google-api-translate-java/"); - try { - translatedText = Translate.execute(source, Language.ENGLISH, Language.FRENCH); - } catch (Exception e) { - System.out.println( "Exception " + e.getMessage() ); - } - return removeSpaces ( translatedText ); - } - - //string to upperCase - public String toUpperCase ( String source ) { - return source.toUpperCase(); - } - - //string to lowerCase - public String toLowerCase ( String source ) { - return source.toLowerCase(); - } + if ( str.contains( " " ) ) { + while ( str.indexOf( " " ) != -1 ) { + index = str.indexOf( " " ); + aux += str.substring( 0, index ); + aux1 = str.substring(index+2); + str = str.substring(index+1, index+2).toUpperCase().concat( aux1 ); + } + aux += str; + return aux; + } + } + return str; + } - public String getSynonym( String source ) { - return source; + //translate the string from English to French + public String translateString( String source ) { + String translatedText = ""; + GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/"); + //Translate.setHttpReferrer("http://code.google.com/p/google-api-translate-java/"); + try { + translatedText = Translate.execute(source, Language.ENGLISH, Language.FRENCH); + } catch (Exception e) { + System.out.println( "Exception " + e.getMessage() ); } + return removeSpaces ( translatedText ); + } + + //string to upperCase + public String toUpperCase ( String source ) { + return source.toUpperCase(); + } + + //string to lowerCase + public String toLowerCase ( String source ) { + return source.toLowerCase(); + } + + public String getSynonym( String source ) { + return source; + } /* //synonym of the word public String getSynonym ( String source ) { @@ -188,1198 +206,1170 @@ public class OntologyModifier { synonym = removeSpaces ( wordForms[j] ); return synonym; } - } + } } } - else - return source; + else + return source; return source; } */ - public String parseString (String str, boolean activeTranslateString, boolean activeSynonym) { - // System.out.println ( "str = [" + str + "]" ); - char [] parsed = str.toCharArray(); - String newString = ""; + public String parseString (String str, boolean activeTranslateString, boolean activeSynonym) { + // System.out.println ( "str = [" + str + "]" ); + char [] parsed = str.toCharArray(); + String newString = ""; - for ( int i=1; i<parsed.length; i++ ) { - if( Character.isUpperCase( parsed[i] ) ) { - String aux = str.substring(0, i); + for ( int i=1; i<parsed.length; i++ ) { + if( Character.isUpperCase( parsed[i] ) ) { + String aux = str.substring(0, i); - if ( activeTranslateString ) - newString = newString.concat( translateString( str.substring(0, i) ) ); - if ( activeSynonym ) - newString = newString.concat( getSynonym( str.substring(0, i) ) ); + if ( activeTranslateString ) + newString = newString.concat( translateString( str.substring(0, i) ) ); + if ( activeSynonym ) + newString = newString.concat( getSynonym( str.substring(0, i) ) ); - str = str.substring(i); - } + str = str.substring(i); } + } + if ( activeTranslateString ) + newString = newString.concat( translateString(str.substring(0)) ); + if ( activeSynonym ) + newString = newString.concat( getSynonym(str.substring(0)) ); + return newString; + } - if ( activeTranslateString ) - newString = newString.concat( translateString(str.substring(0)) ); - if ( activeSynonym ) - 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 + public int [] randNumbers (int count, int numElems) { + int [] vect = new int[count]; + int [] n = new int[numElems]; + int aux, rand; + Random generator = new Random(); + + for ( int i=0; i<count; i++ ) //fill the array with sorted elements + vect[i] = i; + for ( int j=0; j<numElems; j++ ) { + rand = generator.nextInt( count-j ); //choose a random number from the interval + n[j] = vect[rand]; //build the new vector + aux = vect[rand]; //swap + vect[rand] = vect[count-j-1]; + vect[count-j-1] = aux; } + return n; + } - //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 - public int [] randNumbers (int count, int numElems) { - int [] vect = new int[count]; - int [] n = new int[numElems]; - int aux, rand; - Random generator = new Random(); - - for ( int i=0; i<count; i++ ) //fill the array with sorted elements - vect[i] = i; - for ( int j=0; j<numElems; j++ ) { - rand = generator.nextInt( count-j ); //choose a random number from the interval - n[j] = vect[rand]; //build the new vector - aux = vect[rand]; //swap - vect[rand] = vect[count-j-1]; - vect[count-j-1] = aux; - } /* for ( int j=0; j<numElems; j++ ) System.out.print( " [" + n[j] + "]" );*/ - return n; - } - - //replace 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 ) { - if ( activeTranslateString ) { - prop.setLabel( newLabel, "fr" ); - } else { - prop.setLabel( newLabel, "en" ); - } - } - } - - //get 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 = properties.size(); - int toBeRenamed = (int)( percentage*nbProperties ); - //build the list of properties to be renamed - int [] n = this.randNumbers(nbProperties, toBeRenamed); - for ( int i=0; i<toBeRenamed; i++ ) { - OntProperty p = properties.get(n[i]); - propertiesTo.add(p); - } - - for ( OntProperty prop : propertiesTo ) - if ( prop.getNameSpace().equals( this.namespace ) ) - propertiesName.add( prop.getLocalName() ); - - for ( OntProperty prop : propertiesTo ) { - String nameSpace = prop.getNameSpace(); - String localName = prop.getLocalName(); - //has the same Namespace as the Ontology Namespace - if ( nameSpace.equals( this.namespace ) ) { - if ( !propertiesIdentifiers.containsKey( localName ) ) { - if ( activeTranslateString ) { //replace the URI with the translated one - String translateStrg = parseString ( localName, true, false); - propertiesIdentifiers.put( localName , translateStrg ); - replacePropertyLabel( prop.getURI(), translateStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.remove( prop.getURI() ); - this.params.put( prop.getURI() , this.namespace + 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 ); - this.params.remove( prop.getURI() ); - this.params.put( prop.getURI() , this.namespace + newStrg); //the reference alignment - } - else if ( activeSynonym ) { //replace the URI with a synonym - String synonym = parseString (localName, false, true); - if ( propertiesName.contains( synonym ) ) - propertiesIdentifiers.put( localName, localName ); - else { - propertiesIdentifiers.put( localName, synonym ); - replacePropertyLabel( prop.getURI(), synonym, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + 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 ); - this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + toUpperCase( localName ) ); //the reference alignment - } - else if ( activeStringOperation == 2 ) { //replace the URI with the LowerCase URI - propertiesIdentifiers.put( localName , toLowerCase( localName ) ); - replacePropertyLabel( prop.getURI(), toLowerCase( localName ), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + toLowerCase( localName ) ); //the reference alignment - } - else { - propertiesIdentifiers.put( localName, localName + "PROPERTY" ); - replacePropertyLabel( prop.getURI(), localName + "PROPERTY", activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.remove( prop.getURI() ); - this.params.put( prop.getURI(), this.namespace + localName + "PROPERTY" ); - } - } - } - } - /* for debugging - System.out.println( "\n\nPROPERTIES_TRANSLATION" ); - System.out.println("percentage " + percentage ); - Set<String> e1 = propertiesIdentifiers.keySet(); - for ( Iterator it = e1.iterator(); it.hasNext(); ) { - String key = (String)it.next(); - String value = propertiesIdentifiers.get( key ); - System.out.println( "key = [" + key + "][" + value + "]" ); - } - System.out.println( "PROPERTIES_TRANSLATION\n\n" ); - */ - return propertiesIdentifiers; - } + //replace 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 ) { + if ( activeTranslateString ) { + prop.setLabel( newLabel, "fr" ); + } else { + prop.setLabel( newLabel, "en" ); + } + } + } - //replace 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 ); - - if ( c.getLabel( "en" ) != null ) { - if ( activeTranslateString ) { - c.setLabel( newLabel, "fr" ); - } else - c.setLabel( newLabel, "en" ); - } + //get 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 = properties.size(); + int toBeRenamed = (int)( percentage*nbProperties ); + //build the list of properties to be renamed + int [] n = this.randNumbers(nbProperties, toBeRenamed); + for ( int i=0; i<toBeRenamed; i++ ) { + OntProperty p = properties.get(n[i]); + propertiesTo.add(p); } - //get 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 = classes.size(); - int toBeRenamed = (int)( percentage*nbClasses ); - //build the list of classes to be renamed - int [] n = this.randNumbers(nbClasses, toBeRenamed); - for ( int i=0; i<toBeRenamed; i++ ) { - OntClass cls = classes.get(n[i]); - classesTo.add(cls); - } - - for ( OntClass cls : classesTo ) { - if ( !cls.isRestriction() ) { - if ( !cls.isAnon() ) { - String nameSpace = cls.getNameSpace(); - String localName = cls.getLocalName(); - - //has the same Namespace as the Ontology Namespace - if ( nameSpace.equals( this.namespace ) ) { - if ( !classesIdentifiers.containsKey( localName ) ) { - if ( activeTranslateString ) { //replace the URI with the translated one - String translateStrg = parseString (localName, true, false); - classesIdentifiers.put( localName , translateStrg ); - replaceClassLabel( cls.getURI(), translateStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.remove( cls.getURI() ); - this.params.put( cls.getURI() , this.namespace + translateStrg); //the reference alignment - } - else if ( activeRandomString ) { //replace the URI with a random string - String newStrg = getRandomString(); - classesIdentifiers.put( localName , newStrg ); - this.params.remove( cls.getURI() ); - replaceClassLabel( cls.getURI(), newStrg, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - //setProperty calls the Hashtable method put. - this.params.put( cls.getURI(), this.namespace + newStrg ); //the reference alignment - } - else if ( activeSynonym ) { //replace the URI with a synonym - String synonym = parseString (localName, false, true); - classesIdentifiers.put( localName, synonym ); - this.params.remove( cls.getURI() ); - replaceClassLabel( cls.getURI(), synonym, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.put( cls.getURI(), this.namespace + synonym ); //the reference alignment - } - else if ( activeStringOperation == 1 ){ //replace the URI with the UpperCase URI - classesIdentifiers.put( localName , toUpperCase( localName ) ); - this.params.remove( cls.getURI() ); - replaceClassLabel( cls.getURI(), toUpperCase(localName), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.put( cls.getURI(), this.namespace + toUpperCase( localName ) ); //the reference alignment - } - else if ( activeStringOperation == 2 ){ //replace the URI with the LowerCase URI - classesIdentifiers.put( localName , toLowerCase( localName ) ); - this.params.remove( cls.getURI() ); - replaceClassLabel( cls.getURI(), toLowerCase(localName), activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.put( cls.getURI(), this.namespace + toLowerCase( localName ) ); //the reference alignment - } - else { - classesIdentifiers.put( localName, localName + "CLASS" ); - this.params.remove( cls.getURI() ); - replaceClassLabel( cls.getURI(), localName + "CLASS", activeRandomString, activeTranslateString, activeSynonym, activeStringOperation ); - this.params.put( cls.getURI(), this.namespace + localName + "CLASS" ); - } - } - } - } - } - } - return classesIdentifiers; - } - - //rename percentage properties and classes - //activeProperties -> if true, then rename properties - //activeClasses -> if true, then rename classes - public OntModel renameResource ( boolean activeProperties, boolean activeClasses, float percentage, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation) { - 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 ) - propertiesIdentifiers = getPropertiesIdentifiers( percentage, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation); - 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 ) { - if ( propertiesIdentifiers.containsKey( subject.getLocalName() ) ) { - //if the namespace of the subject is the same as the namespace of the property identifier - if ( subject.getNameSpace().equals( this.namespace ) ) { //that we want to remove - isSubj = true; - subjectNameSpace = subject.getNameSpace(); - subjectLocalName = subject.getLocalName(); - subj = newModel.createResource( subjectNameSpace + propertiesIdentifiers.get( subjectLocalName ) ); - } - } - } - - if ( activeClasses ) { - if ( classesIdentifiers.containsKey( subject.getLocalName() ) ) { - //if the namespace of the subject is the same as the namespace of the property identifier - //that we want to remove - if (subject.getNameSpace().equals( this.namespace ) ) { - isSubj = true; - subjectNameSpace = subject.getNameSpace(); - subjectLocalName = subject.getLocalName(); - subj = newModel.createResource( subjectNameSpace + classesIdentifiers.get( subjectLocalName ) ); - } - } - } - } - //if it is the predicate of the statement - if ( activeProperties ) { - if ( propertiesIdentifiers.containsKey( predicate.getLocalName() ) ) { - //if the namespace of the predicate is the same as the namespace of the property identifier - //that we want to remove - if ( predicate.getNameSpace().equals( this.namespace ) ) { - isPred = true; - predicateNameSpace = predicate.getNameSpace(); - predicateLocalName = predicate.getLocalName(); - pred = newModel.createProperty(predicateNameSpace, propertiesIdentifiers.get( predicateLocalName ) ); - } - } - } - - if ( activeClasses ) { - if ( classesIdentifiers.containsKey( predicate.getLocalName() ) ) { - //if the namespace of the predicate is the same as the namespace of the property identifier - //that we want to remove - if ( predicate.getNameSpace().equals( this.namespace ) ) { - isPred = true; - predicateNameSpace = predicate.getNameSpace(); - predicateLocalName = predicate.getLocalName(); - pred = newModel.createProperty(predicateNameSpace, classesIdentifiers.get( predicateLocalName ) ); - } - } - } - //if it is the object of the statement - if ( object.canAs( Resource.class ) ) - if ( object.isURIResource() ) { - if ( activeProperties ) { - if ( propertiesIdentifiers.containsKey( object.asResource().getLocalName() ) ) { - //if the namespace of the object is the same as the namespace of the property identifier - //that we want to remove - if ( object.asResource().getNameSpace().equals( this.namespace ) ) { - isObj = true; - objectNameSpace = object.asResource().getNameSpace(); - objectLocalName = object.asResource().getLocalName(); - obj = newModel.createResource(objectNameSpace + propertiesIdentifiers.get( objectLocalName ) ); - } - } - } - - if ( activeClasses ) { - if ( classesIdentifiers.containsKey( object.asResource().getLocalName() ) ) { - //if the namespace of the object is the same as the namespace of the property identifier - //that we want to remove - if ( object.asResource().getNameSpace().equals( this.namespace ) ) { - isObj = true; - objectNameSpace = object.asResource().getNameSpace(); - objectLocalName = object.asResource().getLocalName(); - obj = newModel.createResource(objectNameSpace + classesIdentifiers.get( objectLocalName ) ); - } - } - } - } - - if ( isSubj ) { - if ( isPred ) { - 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 ); - } - } else { - if ( isPred ) { - 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 ); - } - } - } + for ( OntProperty prop : propertiesTo ) + if ( prop.getNameSpace().equals( this.namespace ) ) + propertiesName.add( prop.getLocalName() ); - if ( activeClasses ) { - checkClassHierarchy(); - //we update the class hierarchy according to the new modifications - this.classHierarchy.updateClassHierarchy( params ); - //this.classHierarchy.printClassHierarchy(); + for ( OntProperty prop : propertiesTo ) { + String nameSpace = prop.getNameSpace(); + String localName = prop.getLocalName(); + //has the same Namespace as the Ontology Namespace + if ( nameSpace.equals( this.namespace ) ) { + if ( !propertiesIdentifiers.containsKey( localName ) ) { + if ( activeTranslateString ) { //replace the URI with the translated one + String translateStrg = parseString ( localName, true, false); + 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 + } + } + 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 + } + } + else if ( activeSynonym ) { + String synonym = parseString (localName, false, true); + if ( propertiesName.contains( synonym ) ) + propertiesIdentifiers.put( localName, localName ); + 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 + } + } + } + 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 + } + } + 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 + } + } + 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" ); + } + } + } } + } + return propertiesIdentifiers; + } + + //replace 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 ); + + if ( c.getLabel( "en" ) != null ) { + if ( activeTranslateString ) { + c.setLabel( newLabel, "fr" ); + } else + c.setLabel( newLabel, "en" ); + } + } - return newModel; - } - - //check if the class hierarchy is build - public void checkClassHierarchy() { - if ( !this.isBuild ) { - buildClassHierarchy(); - this.isBuild = true; - } - } - - //get the Ontology classes - @SuppressWarnings("unchecked") - public List<OntClass> getOntologyClasses () { - List<OntClass> classes = new ArrayList<OntClass>(); - for ( Iterator it = this.modifiedModel.listNamedClasses(); it.hasNext(); ) { - OntClass aux = (OntClass)it.next(); - if ( ( aux ).getNameSpace().equals( this.namespace ) ) { - classes.add( aux ); - } - } - return classes; - } - - //get the Ontology properties - @SuppressWarnings("unchecked") - public List<OntProperty> getOntologyProperties () { - List<OntProperty> properties = new ArrayList<OntProperty>(); - for ( Iterator it = this.modifiedModel.listAllOntProperties(); it.hasNext(); ) { - OntProperty prop = (OntProperty)it.next(); - if ( prop.getNameSpace().equals( this.namespace ) ) - properties.add( prop ); - } - return properties; - } - - //adds a class with a random URI to the parent class parentURI - public OntClass addClass ( OntClass parentClass, String childURI ) { - OntClass childClass = this.modifiedModel.createClass( this.namespace + childURI ); //create a new class to the model - this.classHierarchy.addClass( this.namespace + childURI, parentClass.getURI() ); //add the node in the hierarchy of classes - parentClass.addSubClass( childClass ); //add the childClass as subclass of parentClass - this.modifiedModel.add( childClass, RDFS.subClassOf, parentClass ); //add the class to the model - return childClass; - } - - //add the percentage of the specified subclasses - public void addSubClasses ( float percentage ) { - 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); - for ( int i=0; i<toAdd; i++ ) { - String childURI = this.getRandomString(); //give a random URI to the new class - addClass( classes.get(n[i]), childURI ); - } - } - - //add nbClasses beginning from level - public void addClasses ( int level, int nbClasses, float percentage ) { - String classURI; - //the parent class -> if level is 1 then we create a new class - //else we get a random class from the level : level-1 to be the parent of the class - OntClass parentClass; - OntClass childClass; - List<OntClass> parentClasses = new ArrayList<OntClass>(); - List<OntClass> childClasses = new ArrayList<OntClass>(); - - checkClassHierarchy(); //check if the class hierarchy is built - - if ( level == 1 ) { //the parent of the class is Thing, we add the class and then the rest of the classes - classURI = this.getRandomString(); - parentClass = this.modifiedModel.createClass( this.namespace + classURI ); //create a new class to the model - this.classHierarchy.addClass( this.namespace + classURI, "Thing" ); //add the node in the hierarchy of classes - childClasses.add(parentClass); - } - else { - parentClasses = this.classHierarchy.getClassesFromLevel(this.modifiedModel, level); - int nbParentClasses = parentClasses.size(); //number of classes from the Ontology - int toAdd = (int) ( percentage * nbClasses ); // 1 can be replaced by percentage + //get 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 = classes.size(); + int toBeRenamed = (int)( percentage*nbClasses ); + //build the list of classes to be renamed + int [] n = this.randNumbers(nbClasses, toBeRenamed); + for ( int i=0; i<toBeRenamed; i++ ) { + OntClass cls = classes.get(n[i]); + classesTo.add(cls); + } - for ( OntClass pClass : parentClasses ) { - classURI = this.getRandomString(); - childClass = addClass (pClass, classURI ); - pClass = childClass; - childClasses.add( childClass ); + for ( OntClass cls : classesTo ) { + if ( !cls.isRestriction() ) { + if ( !cls.isAnon() ) { + String nameSpace = cls.getNameSpace(); + String localName = cls.getLocalName(); + + //has the same Namespace as the Ontology Namespace + if ( nameSpace.equals( this.namespace ) ) { + if ( !classesIdentifiers.containsKey( localName ) ) { + if ( activeTranslateString ) { //replace the URI with the translated one + 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 + } + } + 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 + } + } + 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 + } + } + 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 + } + } + 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 + } + } + 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" ); + } + } } - } + } + } + } + } + return classesIdentifiers; + } - for ( OntClass pClass : childClasses ) { - classURI = pClass.getLocalName(); - for ( int i=level+1; i<level + nbClasses; i++ ) { - classURI = "IS_" + classURI; - childClass = addClass (pClass, classURI); - pClass = childClass; - } //this.classHierarchy.printClassHierarchy(); + + //rename percentage properties and classes + //activeProperties -> if true, then rename properties + //activeClasses -> if true, then rename classes + public OntModel renameResource ( boolean activeProperties, boolean activeClasses, float percentage, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation) { + 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 ) + propertiesIdentifiers = getPropertiesIdentifiers( percentage, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation); + 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 ) { + if ( propertiesIdentifiers.containsKey( subject.getLocalName() ) ) { + //if the namespace of the subject is the same as the namespace of the property identifier + if ( subject.getNameSpace().equals( this.namespace ) ) {//that we want to remove + isSubj = true; + subjectNameSpace = subject.getNameSpace(); + subjectLocalName = subject.getLocalName(); + subj = newModel.createResource( subjectNameSpace + propertiesIdentifiers.get( subjectLocalName ) ); + } + } + } + + if ( activeClasses ) { + if ( classesIdentifiers.containsKey( subject.getLocalName() ) ) { + //if the namespace of the subject is the same as the namespace of the property identifier + //that we want to remove + if (subject.getNameSpace().equals( this.namespace ) ) { + isSubj = true; + subjectNameSpace = subject.getNameSpace(); + subjectLocalName = subject.getLocalName(); + subj = newModel.createResource( subjectNameSpace + classesIdentifiers.get( subjectLocalName ) ); + } + } + } } - } - - //the class to be removed appears in the domain / range of the property -> change with the parent class - @SuppressWarnings("unchecked") - public void changeDomainRange ( OntClass child, OntClass parent ) { - boolean isDomain, isRange; - List<OntProperty> properties = this.modifiedModel.listAllOntProperties().toList();//get the list of all the model properties - String URI = child.getURI(); + //if it is the predicate of the statement + if ( activeProperties ) { + if ( propertiesIdentifiers.containsKey( predicate.getLocalName() ) ) { + //if the namespace of the predicate is the same as the namespace of the property identifier + //that we want to remove + if ( predicate.getNameSpace().equals( this.namespace ) ) { + isPred = true; + predicateNameSpace = predicate.getNameSpace(); + predicateLocalName = predicate.getLocalName(); + pred = newModel.createProperty(predicateNameSpace, propertiesIdentifiers.get( predicateLocalName ) ); + } + } + } + + if ( activeClasses ) { + if ( classesIdentifiers.containsKey( predicate.getLocalName() ) ) { + //if the namespace of the predicate is the same as the namespace of the property identifier + //that we want to remove + if ( predicate.getNameSpace().equals( this.namespace ) ) { + isPred = true; + predicateNameSpace = predicate.getNameSpace(); + predicateLocalName = predicate.getLocalName(); + pred = newModel.createProperty(predicateNameSpace, classesIdentifiers.get( predicateLocalName ) ); + } + } + } + //if it is the object of the statement + if ( object.canAs( Resource.class ) ) + if ( object.isURIResource() ) { + if ( activeProperties ) { + if ( propertiesIdentifiers.containsKey( object.asResource().getLocalName() ) ) { + //if the namespace of the object is the same as the namespace of the property identifier + //that we want to remove + if ( object.asResource().getNameSpace().equals( this.namespace ) ) { + isObj = true; + objectNameSpace = object.asResource().getNameSpace(); + objectLocalName = object.asResource().getLocalName(); + obj = newModel.createResource(objectNameSpace + propertiesIdentifiers.get( objectLocalName ) ); + } + } + } + + if ( activeClasses ) { + if ( classesIdentifiers.containsKey( object.asResource().getLocalName() ) ) { + //if the namespace of the object is the same as the namespace of the property identifier that we want to remove + if ( object.asResource().getNameSpace().equals( this.namespace ) ) { + isObj = true; + objectNameSpace = object.asResource().getNameSpace(); + objectLocalName = object.asResource().getLocalName(); + obj = newModel.createResource(objectNameSpace + classesIdentifiers.get( objectLocalName ) ); + } + } + } + } + + if ( isSubj ) { + if ( isPred ) { + 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 ); + } + } else { + if ( isPred ) { + 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 ( activeClasses ) { + checkClassHierarchy(); + //we update the class hierarchy according to the new modifications + this.classHierarchy.updateClassHierarchy( params ); + //this.classHierarchy.printClassHierarchy(); + } + + return newModel; + } + + //check if the class hierarchy is build + public void checkClassHierarchy() { + if ( !this.isBuild ) { + buildClassHierarchy(); + this.isBuild = true; + } + } + + //get the Ontology classes + @SuppressWarnings("unchecked") + public List<OntClass> getOntologyClasses () { + List<OntClass> classes = new ArrayList<OntClass>(); + for ( Iterator it = this.modifiedModel.listNamedClasses(); it.hasNext(); ) { + OntClass aux = (OntClass)it.next(); + if ( ( aux ).getNameSpace().equals( this.namespace ) ) { + classes.add( aux ); + } + } + return classes; + } + + //get the Ontology properties + @SuppressWarnings("unchecked") + public List<OntProperty> getOntologyProperties () { + List<OntProperty> properties = new ArrayList<OntProperty>(); + for ( Iterator it = this.modifiedModel.listAllOntProperties(); it.hasNext(); ) { + OntProperty prop = (OntProperty)it.next(); + if ( prop.getNameSpace().equals( this.namespace ) ) + properties.add( prop ); + } + return properties; + } + + //adds a class with a random URI to the parent class parentURI + public OntClass addClass ( OntClass parentClass, String childURI ) { + OntClass childClass = this.modifiedModel.createClass( this.namespace + childURI );//create a new class to the model + + this.classHierarchy.addClass( this.namespace + childURI, parentClass.getURI() );//add the node in the hierarchy of classes + parentClass.addSubClass( childClass ); //add the childClass as subclass of parentClass + this.modifiedModel.add( childClass, RDFS.subClassOf, parentClass ); //add the class to the model + return childClass; + } + + //add the percentage of the specified subclasses + public void addSubClasses ( float percentage ) { + 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 ); - for ( OntProperty prop : properties ) { - isDomain = isRange = false; - if ( prop.isObjectProperty() ) { //if the prop is ObjectProperty - if ( prop.hasDomain(null) ) { //if it has domain - OntResource res = prop.getDomain(); //the domain - if ( res.canAs(UnionClass.class) ) { //the domain is a union of classes - UnionClass uncls = res.as( UnionClass.class ); - for ( Iterator it = uncls.listOperands(); it.hasNext(); ) { - OntClass aux = (OntClass)it.next(); - if ( aux.getURI().equals( URI ) ) - isDomain = true; - } - if ( isDomain ) { //if the domain is a union of classes - uncls.removeOperand( child ); //remove the child from the union - uncls.addOperand( parent ); //add the parent to the union - } - } - } - } - if ( prop.isDatatypeProperty() ) { //if prop is DatatypeProperty - if ( prop.hasDomain(null) ) { //if it has domain - Resource res = prop.getDomain(); //get the domain - if ( res.canAs(UnionClass.class) ) { //if domain is a union of classes - UnionClass uncls = res.as( UnionClass.class ); - for ( Iterator it = uncls.listOperands(); it.hasNext(); ) { - OntClass aux = (OntClass)it.next(); - if ( aux.getURI().equals( URI ) ) - isDomain = true; - } - if ( isDomain ) { //if the domain is a union of classes - uncls.removeOperand( child ); //remove the child class from the union - uncls.addOperand( parent ); //add the parent class to the union - } - } - } - if ( prop.hasRange(null) ) { //if the prop has range - Resource res = prop.getRange(); //get the resource - if ( res.canAs(UnionClass.class) ) { //the range of property is a union of classes - UnionClass uncls = res.as( UnionClass.class ); - for ( Iterator it = uncls.listOperands(); it.hasNext(); ) { - OntClass aux = (OntClass)it.next(); - if ( aux.getURI().equals( URI ) ) { - isRange = true; - } - } - if ( isRange ) { //if the range is a union of classes - uncls.removeOperand( child ); //remove the child class from the union - uncls.addOperand( parent ); //add the parent class to the union - } - } - } + checkClassHierarchy(); //check if the classHierarchy is built + //build the list of properties to be renamed + int [] n = this.randNumbers(nbClasses, toAdd); + for ( int i=0; i<toAdd; i++ ) { + String childURI = this.getRandomString(); //give a random URI to the new class + addClass( classes.get(n[i]), childURI ); + } + } - } - } - } - - //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; - for ( Iterator it = this.modifiedModel.listRestrictions(); it.hasNext(); ) { - restr = (Restriction)it.next(); //get the restriction - if ( restr.isAllValuesFromRestriction() ) { - AllValuesFromRestriction av = restr.asAllValuesFromRestriction(); - if ( av.getAllValuesFrom().getURI() != null ) //if points to the childClass - if ( av.getAllValuesFrom().getURI().equals( childClass.getURI() ) ) - av.setAllValuesFrom( parentClass ); //change to point to the parentClass + //add nbClasses beginning from level + public void addClasses ( int level, int nbClasses, float percentage ) { + String classURI; + //the parent class -> if level is 1 then we create a new class + //else we get a random class from the level : level-1 to be the parent of the class + OntClass parentClass; + OntClass childClass; + List<OntClass> parentClasses = new ArrayList<OntClass>(); + List<OntClass> childClasses = new ArrayList<OntClass>(); + + + checkClassHierarchy(); //check if the class hierarchy is built + if ( level == 1 ) { //the parent of the class is Thing, we add the class and then the rest of the classes + classURI = this.getRandomString(); + parentClass = this.modifiedModel.createClass( this.namespace + classURI );//create a new class to the model + this.classHierarchy.addClass( this.namespace + classURI, "Thing" ); //add the node in the hierarchy of classes + childClasses.add(parentClass); + } + else { + parentClasses = this.classHierarchy.getClassesFromLevel(this.modifiedModel, level); + int nbParentClasses = parentClasses.size(); //number of classes from the Ontology + int toAdd = (int) ( percentage * nbClasses ); // 1 can be replaced by percentage + + for ( OntClass pClass : parentClasses ) { + classURI = this.getRandomString(); + childClass = addClass (pClass, classURI ); + pClass = childClass; + childClasses.add( childClass ); + } + } + + for ( OntClass pClass : childClasses ) { + classURI = pClass.getLocalName(); + for ( int i=level+1; i<level + nbClasses; i++ ) { + classURI = "IS_" + classURI; + childClass = addClass (pClass, classURI); + pClass = childClass; + } //this.classHierarchy.printClassHierarchy(); + } + } + + //the class to be removed appears in the domain / range of the property -> change with the parent class + @SuppressWarnings("unchecked") + public void changeDomainRange ( OntClass child, OntClass parent ) { + boolean isDomain, isRange; + List<OntProperty> properties = this.modifiedModel.listAllOntProperties().toList();//get the list of all the model properties + String URI = child.getURI(); + + for ( OntProperty prop : properties ) { + isDomain = isRange = false; + if ( prop.isObjectProperty() ) { //if the prop is ObjectProperty + if ( prop.hasDomain(null) ) { //if it has domain + OntResource res = prop.getDomain(); //the domain + if ( res.canAs(UnionClass.class) ) { //the domain is a union of classes + UnionClass uncls = res.as( UnionClass.class ); + for ( Iterator it = uncls.listOperands(); it.hasNext(); ) { + OntClass aux = (OntClass)it.next(); + if ( aux.getURI().equals( URI ) ) + isDomain = true; + } + if ( isDomain ) { //if the domain is a union of classes + uncls.removeOperand( child ); //remove the child from the union + uncls.addOperand( parent ); //add the parent to the union + } + } + } + } + if ( prop.isDatatypeProperty() ) { //if prop is DatatypeProperty + if ( prop.hasDomain(null) ) { //if it has domain + Resource res = prop.getDomain(); //get the domain + if ( res.canAs(UnionClass.class) ) { //if domain is a union of classes + UnionClass uncls = res.as( UnionClass.class ); + for ( Iterator it = uncls.listOperands(); it.hasNext(); ) { + OntClass aux = (OntClass)it.next(); + if ( aux.getURI().equals( URI ) ) + isDomain = true; + } + if ( isDomain ) { //if the domain is a union of classes + uncls.removeOperand( child ); //remove the child class from the union + uncls.addOperand( parent ); //add the parent class to the union + } + } + } + if ( prop.hasRange(null) ) { //if the prop has range + Resource res = prop.getRange(); //get the resource + if ( res.canAs(UnionClass.class) ) { //the range of property is a union of classes + UnionClass uncls = res.as( UnionClass.class ); + for ( Iterator it = uncls.listOperands(); it.hasNext(); ) { + OntClass aux = (OntClass)it.next(); + if ( aux.getURI().equals( URI ) ) { + isRange = true; + } + } + if ( isRange ) { //if the range is a union of classes + uncls.removeOperand( child ); //remove the child class from the union + uncls.addOperand( parent ); //add the parent class to the union + } + } + } + } + } + } + + //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; + for ( Iterator it = this.modifiedModel.listRestrictions(); it.hasNext(); ) { + restr = (Restriction)it.next(); //get the restriction + if ( restr.isAllValuesFromRestriction() ) { + AllValuesFromRestriction av = restr.asAllValuesFromRestriction(); + if ( av.getAllValuesFrom().getURI() != null ) //if points to the childClass + if ( av.getAllValuesFrom().getURI().equals( childClass.getURI() ) ) + av.setAllValuesFrom( parentClass ); //change to point to the parentClass } - if ( restr.isSomeValuesFromRestriction() ) { - SomeValuesFromRestriction sv = restr.asSomeValuesFromRestriction(); - if ( sv.getSomeValuesFrom().getURI() != null ) //if points to the childClass - if ( sv.getSomeValuesFrom().getURI().equals( childClass.getURI() ) ) - sv.setSomeValuesFrom( parentClass ); //change to point to the parentClass - } - } - } - - //remove class - @SuppressWarnings("unchecked") - public void removeClass ( OntClass cls ) { - OntClass parentClass; - ArrayList<OntClass> subClasses = new ArrayList<OntClass>(); //the list of all the subclasses of the class - OntClass thing = this.modifiedModel.createClass( OWL.Thing.getURI() ); //Thing class - checkClassHierarchy(); //check if the class hierarchy is built - parentClass = this.classHierarchy.removeClass( this.modifiedModel, cls ); //get the parent of the class - - for (Iterator it1 = cls.listSubClasses(); it1.hasNext(); ) { //build the list of subclasses - OntClass subCls = (OntClass)it1.next(); //because we can't change the - subClasses.add( subCls ); //model while we are iterating - } - - if ( parentClass != thing ) //now we change the superclass of classes - for (OntClass clss : subClasses) //new superclass => - clss.setSuperClass( parentClass ); //=>the superclass of the node - - changeDomainRange(cls, parentClass); //change the domain and the range for each property - checkClassesRestrictions( cls, parentClass ); //change the union of different classes - this.params.remove( cls.getURI() ); //remove the URI of the class from the reference alignment - cls.remove(); //remove the class from the Ontology - } - - //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>(); - int nbClasses = classes.size(); //number of classes - checkClassHierarchy(); //check if the class hierarchy is built - int toBeRemoved = (int) (percentage*nbClasses); //the number of classes to be removed - - //build the list of classes to be removed - int [] n = this.randNumbers(nbClasses, toBeRemoved); - for ( int i=0; i<toBeRemoved; i++ ) { - OntClass cls = classes.get(n[i]); - removedClasses.add( cls ); - } - - for ( OntClass cls : removedClasses ) { //remove the classes from the list - removeClass (cls); - } - } + if ( restr.isSomeValuesFromRestriction() ) { + SomeValuesFromRestriction sv = restr.asSomeValuesFromRestriction(); + if ( sv.getSomeValuesFrom().getURI() != null ) //if points to the childClass + if ( sv.getSomeValuesFrom().getURI().equals( childClass.getURI() ) ) + sv.setSomeValuesFrom( parentClass ); //change to point to the parentClass + } + } + } - //remove all the classes from a specific level - public void removeClassesFromLevel ( int level ) { - //System.out.println( "Level " + level ); - /* if ( level == 1 ) //except classes from level 1 - return; */ - List<OntClass> classes = new ArrayList<OntClass>(); - checkClassHierarchy(); //check if the class hierarchy is built - classes = this.classHierarchy.getClassesFromLevel(this.modifiedModel, level); - for ( int i=0; i<classes.size(); i++ ) { //remove the classes from the hierarchy - removeClass ( classes.get(i) ); - } - } + //remove class + @SuppressWarnings("unchecked") + public void removeClass ( OntClass cls ) { + OntClass parentClass; + ArrayList<OntClass> subClasses = new ArrayList<OntClass>(); //the list of all the subclasses of the class + OntClass thing = this.modifiedModel.createClass( OWL.Thing.getURI() ); //Thing class + checkClassHierarchy(); //check if the class hierarchy is built + parentClass = this.classHierarchy.removeClass( this.modifiedModel, cls );//get the parent of the class - //remove percentage individuals from Ontology - public void removeIndividuals( float percentage ) { - boolean isSubj, isObj; //the individual can appear as subject or object - List<Individual> individuals = this.modifiedModel.listIndividuals().toList(); - List<Individual> individualsTo = new ArrayList<Individual>(); //the list of individuals to be removed - 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 - individualsTo.add( indiv ); - //this.params.remove( indiv.getURI() ); - } - - for ( Statement st : statements ) { - Resource subject = st.getSubject(); - RDFNode object = st.getObject(); - isSubj = isObj = false; - - if ( individualsTo.contains( subject ) ) - isSubj = true; - if ( object.canAs( Resource.class ) ) - if ( individualsTo.contains( object.asResource() ) ) - isObj = true; - if ( isSubj ) //the individual appears as subject in the statement - this.modifiedModel.remove( st ); - if ( isObj ) //the individual appears as object in the statement - this.modifiedModel.remove( st ); - } - } - //remove properties from the model - @SuppressWarnings("unchecked") - public void removeProperties ( float percentage ) { - List <OntProperty> properties = this.getOntologyProperties(); //the list of all properties from the model - ArrayList <OntProperty> propertiesToBeRemoved = new ArrayList<OntProperty>(); - ArrayList<Restriction> restrictions = new ArrayList<Restriction>(); - boolean isObj, isSubj, isPred; - - List<Statement> statements = this.modifiedModel.listStatements().toList(); //get all of the statements - int nbProperties = properties.size(); //the number of properties - int toBeRemoved = (int)( percentage*nbProperties ); //the number of properties to be removed - - //build the list of classes to be removed - int [] n = this.randNumbers(nbProperties, toBeRemoved); - for ( int i=0; i<toBeRemoved; i++ ) { //build the list of properties to be removed - OntProperty p = properties.get(n[i]); - propertiesToBeRemoved.add( p ); - this.params.remove( p.getURI() ); - for ( Iterator it = p.listReferringRestrictions(); it.hasNext(); ) { //get the restrictions of that property - restrictions.add( (Restriction)it.next() ); - } - for ( Restriction r : restrictions ) //delete all the restrictions - r.remove(); - } - - for ( Statement st : statements ) { //remove the declaration of properties from the model - Resource subject = st.getSubject(); - Property predicate = st.getPredicate(); - RDFNode object = st.getObject(); - isSubj = isPred = isObj = false; - - if ( propertiesToBeRemoved.contains( subject ) ) //if appears as subject - if ( subject.getNameSpace().equals( this.namespace ) ) - isSubj = true; - - if ( propertiesToBeRemoved.contains( predicate ) ) //if appears as predicate - if ( predicate.getNameSpace().equals( this.namespace ) ) - isPred = true; - - if ( object.canAs( Resource.class ) ) //if appears as object - if ( propertiesToBeRemoved.contains( object ) ) - if ( object.asResource().getNameSpace().equals( this.namespace ) ) - isObj = true; - - if ( isSubj || isPred || isObj ) //remove the statement in which the prop - this.modifiedModel.remove( st ); //appears as subject, predicate or object - } - } + for (Iterator it1 = cls.listSubClasses(); it1.hasNext(); ) { //build the list of subclasses + OntClass subCls = (OntClass)it1.next(); //because we can't change the + subClasses.add( subCls ); //model while we are iterating + } - //add object properties to the Ontology - public void addProperties ( float percentage ) { - List<OntProperty> properties = this.getOntologyProperties(); - List<OntClass> classes = this.getOntologyClasses(); - ObjectProperty p = null; - DatatypeProperty d = null; - Random classRand = new Random(); - int index; - int nbClasses = classes.size(); //the number of classes - int nbProperties = properties.size(); //the number of properties - int toBeAdd = (int)( percentage*nbProperties ); //the number of properties to be add - - for ( int i=0; i<toBeAdd/2; i++ ) { //add object properties - //p = this.modifiedModel.createObjectProperty( this.namespace + "OBJECT_PROPERTY_" + getRandomString() ); - p = this.modifiedModel.createObjectProperty( this.namespace + getRandomString() ); - index = classRand.nextInt( nbClasses ); //pick random domain - p.addDomain( classes.get( index ) ); - index = classRand.nextInt( nbClasses ); //pick random range - p.addRange( classes.get( index ) ); - } - - for ( int i=toBeAdd/2; i<toBeAdd; i++ ) { //add datatype properties - //d = this.modifiedModel.createDatatypeProperty( this.namespace + "DATATYPE_PROPERTY_" + getRandomString() ); - d = this.modifiedModel.createDatatypeProperty( this.namespace + getRandomString() ); - index = classRand.nextInt( nbClasses ); //add domain - d.addDomain( classes.get( index ) ); - d.addRange( XSD.xstring ); //add range -> string - } - } + if ( parentClass != thing ) //now we change the superclass of classes + for (OntClass clss : subClasses) //new superclass => + clss.setSuperClass( parentClass ); //=>the superclass of the node - //remove classes comments - @SuppressWarnings("unchecked") - public void removeClassesComments ( float percentage ) { - ArrayList<Literal> comments = new ArrayList<Literal>(); - List<OntClass> classes = this.modifiedModel.listNamedClasses().toList(); - ArrayList<OntClass> classesTo = new ArrayList<OntClass>(); - int nbClasses = classes.size(); - int toBeRemoved = (int)( percentage * nbClasses ); //number of classes comments to be removed - - int [] n = this.randNumbers(nbClasses, toBeRemoved); - for ( int i=0; i<toBeRemoved; i++ ) { - OntClass cls = classes.get(n[i]); - classesTo.add( cls ); - } - - for ( OntClass c : classesTo ) { - for (Iterator it2 = c.listComments(null); it2.hasNext();) - comments.add(((Literal) it2.next())); - for (Literal lit : comments) // remove comments - c.removeComment( lit ); - comments.clear(); - } - } + changeDomainRange(cls, parentClass); //change the domain and the range for each property + checkClassesRestrictions( cls, parentClass ); //change the union of different classes + this.params.remove( cls.getURI() ); //remove the URI of the class from the reference alignment + cls.remove(); //remove the class from the Ontology + } - //remove properties comments - @SuppressWarnings("unchecked") - public void removePropertiesComments ( float percentage ) { - ArrayList<Literal> comments = new ArrayList<Literal>(); // an array list to hold all the comments - List<OntProperty> properties = this.modifiedModel.listAllOntProperties().toList(); - ArrayList<OntProperty> propertiesTo = new ArrayList<OntProperty>(); - int nbProperties = properties.size(); - int toBeRemoved = (int)( percentage * nbProperties ); //the number of properties comments to be removed - - int [] n = this.randNumbers(nbProperties, toBeRemoved); - for ( int i=0; i<toBeRemoved; i++ ) { - OntProperty p = properties.get(n[i]); - propertiesTo.add( p ); - } - - for ( OntProperty prop : propertiesTo ) { - for (Iterator it2 = prop.listComments(null); it2.hasNext();) // get all comments - comments.add(((Literal) it2.next())); - for (Literal lit : comments) //remove comments - prop.removeComment( lit ); - comments.clear(); - } - } + //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>(); + int nbClasses = classes.size(); //number of classes + checkClassHierarchy(); //check if the class hierarchy is built + int toBeRemoved = (int) (percentage*nbClasses); //the number of classes to be removed - //remove individuals comments - @SuppressWarnings("unchecked") - public void removeIndividualsComments ( float percentage ) { - ArrayList<Literal> comments = new ArrayList<Literal>(); // an array list to hold all the comments - List<Individual> individuals = this.modifiedModel.listIndividuals().toList(); - ArrayList<Individual> individualsTo = new ArrayList<Individual>(); - int nbIndividuals = individuals.size(); - int toBeRemoved = (int)( percentage * nbIndividuals ); //number of classes to be removed - - int [] n = this.randNumbers(nbIndividuals, toBeRemoved); - for ( int i=0; i<toBeRemoved; i++ ) { - Individual indiv = individuals.get(n[i]); - individualsTo.add( indiv ); - } - for ( Individual indiv : individuals ) { - for (Iterator it2 = indiv.listComments(null); it2.hasNext(); ) //get all comments - comments.add( ((Literal) it2.next()) ); - for (Literal lit : comments ) //remove comments - indiv.removeComment( lit ); - comments.clear(); - } - } + //build the list of classes to be removed + int [] n = this.randNumbers(nbClasses, toBeRemoved); + for ( int i=0; i<toBeRemoved; i++ ) { + OntClass cls = classes.get(n[i]); + removedClasses.add( cls ); + } - //remove Ontologies comments - @SuppressWarnings("unchecked") - public void removeOntologiesComments ( float percentage ) { - ArrayList<Literal> comments = new ArrayList<Literal>(); // an array list to hold all the comments - List<Ontology> ontologies = this.modifiedModel.listOntologies().toList(); - ArrayList<Ontology> ontologiesTo = new ArrayList<Ontology>(); - int nbOntologies = ontologies.size(); - int toBeRemoved = (int)( percentage * nbOntologies ); //the number of Ontologies comments to be removed - - int [] n = this.randNumbers(nbOntologies, toBeRemoved); - for ( int i=0; i<toBeRemoved; i++ ) { - Ontology onto = ontologies.get(n[i]); - ontologiesTo.add( onto ); - } + for ( OntClass cls : removedClasses ) { //remove the classes from the list + removeClass (cls); + } + } - for ( Ontology onto : ontologies ) { - for (Iterator it2 = onto.listComments(null); it2.hasNext(); ) // get all comments - comments.add(((Literal) it2.next())); - for ( Literal lit : comments ) //remove all comments - onto.removeComment( lit ); - comments.clear(); - } - } + //remove all the classes from a specific level + public void removeClassesFromLevel ( int level ) { + //System.out.println( "Level " + level ); + /* if ( level == 1 ) //except classes from level 1 + return; */ + List<OntClass> classes = new ArrayList<OntClass>(); + checkClassHierarchy(); //check if the class hierarchy is built + classes = this.classHierarchy.getClassesFromLevel(this.modifiedModel, level); + for ( int i=0; i<classes.size(); i++ ) { //remove the classes from the hierarchy + removeClass ( classes.get(i) ); + } + } - //remove percentage comments - public void removeComments ( float percentage ) { - removeClassesComments ( percentage ); - removeIndividualsComments ( percentage ); - removePropertiesComments ( percentage ); - removeOntologiesComments ( percentage ); - } + //remove percentage individuals from Ontology + public void removeIndividuals( float percentage ) { + boolean isSubj, isObj; //the individual can appear as subject or object + List<Individual> individuals = this.modifiedModel.listIndividuals().toList(); + List<Individual> individualsTo = new ArrayList<Individual>(); //the list of individuals to be removed + 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 + individualsTo.add( indiv ); + //this.params.remove( indiv.getURI() ); + } - //flatten level - public void levelFlattened ( int level ) { - int size; - boolean active = false; - ArrayList<OntClass> levelClasses = new ArrayList<OntClass>(); //the list of classes from that level - ArrayList<OntClass> parentLevelClasses = new ArrayList<OntClass>(); //the list of parent of the child classes from that level - ArrayList<OntClass> superLevelClasses = new ArrayList<OntClass>(); //the list of parent of the parent classes from that level - if ( level == 1 ) //no change - return; - checkClassHierarchy(); //check if the class hierarchy is built - active = this.classHierarchy.flattenClassHierarchy( this.modifiedModel, level, levelClasses, parentLevelClasses, superLevelClasses); - size = levelClasses.size(); - - for ( int i=0; i<size; i++ ) { - OntClass childClass = levelClasses.get( i ); //child class - OntClass parentClass = parentLevelClasses.get( i ); //parent class - //all the classes are subclasses of owl: Thing - if ( active ) { //if ( !parentClass.getURI().equals( "Thing" ) ) { - OntClass superClass = superLevelClasses.get( i ); //parent class of the child class parents - childClass.addSuperClass( superClass ); - parentClass.removeSubClass( childClass ); - } else { - parentClass.removeSubClass( childClass ); - } - } - } + for ( Statement st : statements ) { + Resource subject = st.getSubject(); + RDFNode object = st.getObject(); + isSubj = isObj = false; - //AICI MAI TREBUIE MODIFICAT!!! - //IN CLASS HIERARCHY NU FACE BINE - public void noHierarchy () { - int level = this.getMaxLevel(); - //this.classHierarchy.printClassHierarchy(); - while ( this.getMaxLevel() != 1 ) { - //System.out.println("\n\n"); - //this.classHierarchy.printClassHierarchy(); - //System.out.println("\n\n"); - levelFlattened ( level ); - level--; + if ( individualsTo.contains( subject ) ) + isSubj = true; + if ( object.canAs( Resource.class ) ) + if ( individualsTo.contains( object.asResource() ) ) + isObj = true; + if ( isSubj ) //the individual appears as subject in the statement + this.modifiedModel.remove( st ); + if ( isObj ) //the individual appears as object in the statement + this.modifiedModel.remove( st ); + } + } + + //remove properties from the model + @SuppressWarnings("unchecked") + public void removeProperties ( float percentage ) { + List <OntProperty> properties = this.getOntologyProperties(); //the list of all properties from the model + ArrayList <OntProperty> propertiesToBeRemoved = new ArrayList<OntProperty>(); + ArrayList<Restriction> restrictions = new ArrayList<Restriction>(); + boolean isObj, isSubj, isPred; + + List<Statement> statements = this.modifiedModel.listStatements().toList();//get all of the statements + int nbProperties = properties.size(); //the number of properties + int toBeRemoved = (int)( percentage*nbProperties ); //the number of properties to be removed + + //build the list of classes to be removed + int [] n = this.randNumbers(nbProperties, toBeRemoved); + for ( int i=0; i<toBeRemoved; i++ ) { //build the list of properties to be removed + OntProperty p = properties.get(n[i]); + propertiesToBeRemoved.add( p ); + this.params.remove( p.getURI() ); + for ( Iterator it = p.listReferringRestrictions(); it.hasNext(); ) {//get the restrictions of that property + restrictions.add( (Restriction)it.next() ); } + for ( Restriction r : restrictions ) //delete all the restrictions + r.remove(); } - - //remove percentage restrictions from the model - public void removeRestrictions( float percentage ) { - List<Restriction> restrictions = new ArrayList<Restriction>(); - List<Restriction> restrictionsTo = new ArrayList<Restriction>(); //the array list of restrictions to be removed - restrictions = this.modifiedModel.listRestrictions().toList(); - int nbRestrictions = restrictions.size(); //the number of restrictions - int toBeRemoved = (int)( percentage*nbRestrictions ); //the number of restrictions to be removed - - int [] n = this.randNumbers(nbRestrictions, toBeRemoved); //build the list of restrictions to be removed - for ( int i=0; i<toBeRemoved; i++ ) { - Restriction res = restrictions.get(n[i]); - restrictionsTo.add( res ); - } - - for ( Restriction res : restrictionsTo ) - res.remove(); - } + - //the initial reference alignment - public void initializeProperties() { - List<OntClass> classes = this.modifiedModel.listNamedClasses().toList(); //all classes - 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 - - 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 - - for ( OntProperty prop : properties ) //list all properties - if ( prop.getNameSpace().equals( this.namespace ) ) - this.params.put( prop.getURI() , prop.getURI() );//add them to the initial alignment - /* - for ( Individual indiv : individuals ) { //list all individuals - if ( indiv.getURI() != null ) { - System.out.println( "[" + indiv.getURI() + "]" ); - this.params.put( indiv.getURI() , indiv.getURI() );//add them to the initial alignment - } - } - - for ( Ontology onto : ontologies ) //list all ontologies - if ( onto.getNameSpace().equals( this.namespace ) ) - this.params.put( onto.getURI() , onto.getURI() );//add them to the initial alignment - */ - } - - //compute the alignment after the modifications - @SuppressWarnings("unchecked") - public void computeAlignment( String fileName ) { - URI onto1 = null; - URI onto2 = null; - //System.out.println("Namespace = " + this.namespace); - - if ( !this.isChanged ) //if the namespace of the ontology is not changed - this.namespaceNew = "http://oaei.ontologymatching.org/2010/benchmarks/101/onto1.rdf#"; - - try { - onto1 = new URI ( this.namespace ); - onto2 = new URI ( this.namespaceNew ); - - this.alignment.init(onto1, onto2); - - long time = System.currentTimeMillis(); - - //Cell addAlignCell(Object ob1, Object ob2, String relation, double measure) throws AlignmentException { - //System.out.println( "\nPARAMS" ); - Set<Object> e1 = params.keySet(); - for ( Iterator it = e1.iterator(); it.hasNext(); ) { - String key = (String)it.next(); - String value = (String)params.get( key ); - URI uri1 = URI.create( key ); - //the namespace of value - //System.out.println( "key = [" + key + "]"); //System.out.println( "value = [" + value + "]" ); - - 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 ); - } - this.alignment.addAlignCell( uri1, uri2, "=", 1 ); - } + for ( Statement st : statements ) { //remove the declaration of properties from the model + Resource subject = st.getSubject(); + Property predicate = st.getPredicate(); + RDFNode object = st.getObject(); + isSubj = isPred = isObj = false; - long newTime = System.currentTimeMillis(); - this.alignment.setExtension( Namespace.ALIGNMENT.uri, Annotations.TIME, Long.toString(newTime - time) ); + if ( propertiesToBeRemoved.contains( subject ) ) //if appears as subject + if ( subject.getNameSpace().equals( this.namespace ) ) + isSubj = true; - 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); - writer.flush(); - writer.close(); - } catch (AlignmentException aex) { System.out.println( "Exception " + aex.getMessage() ); - } catch (Exception ex) { System.err.println("Exception " + ex.getMessage()); - } - } + if ( propertiesToBeRemoved.contains( predicate ) ) //if appears as predicate + if ( predicate.getNameSpace().equals( this.namespace ) ) + isPred = true; - //change the namespace of the modified ontology - public OntModel changeNamespace () { - List<Statement> statements = this.modifiedModel.listStatements().toList(); //the statements of the model - String newNamespace; - - //newNamespace = this.namespace.substring(0, this.namespace.length()-1 ) + "1#"; - newNamespace = "http://oaei.ontologymatching.org/2010/benchmarks/101/onto1.rdf#"; - this.namespaceNew = newNamespace; - this.isChanged = true; - boolean isSubj, isPred, isObj; - - OntModel newModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); //create new Model - - //iterate through all the statements and change the namespace - 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 ( subject.getLocalName() != null ) - if ( !subject.isLiteral() ) - if ( subject.getNameSpace().equals( this.namespace ) ) { //System.out.print("[s][" + subject.getLocalName() + "]"); - subj = newModel.createResource( newNamespace + subject.getLocalName() ); - isSubj = true; - } - - if ( !object.isLiteral() ) - if ( object.canAs( Resource.class ) ) - if ( object.isURIResource() ) - if ( object.asResource().getNameSpace().equals( this.namespace ) ) { - //System.out.print("[o][" + object.asResource().getLocalName() + "]"); - obj = newModel.createResource( newNamespace + object.asResource().getLocalName() ); - isObj = true; - } - - if ( !predicate.isLiteral() ) - if ( predicate.getNameSpace().equals( this.namespace ) ) { - //System.out.print( "[p][" + predicate.getLocalName() + "]" ); - pred = newModel.createProperty( newNamespace + predicate.getLocalName() ); - isPred = true; - } - - //System.out.println(); - - if ( isSubj ) { - if ( isPred ) { - 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 ); - } - } else { - if ( isPred ) { - 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 ); - } - } - } - - //rename the namespace of owl:Ontology - List<Ontology> ontos = newModel.listOntologies().toList(); - for ( Ontology o : ontos ) { - ResourceUtils.renameResource( o, newNamespace ); - } - - //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 - System.out.println( "->namespace changed" ); - return this.modifiedModel; - } - - //returns the alignment - public Alignment getAlignment() { - return this.alignment; - } - - //must have the max level of the class hierarchy - public int getMaxLevel() { - checkClassHierarchy(); //check if the class hierarchy is built - //System.out.println( "MaxLevelOfClassHierarchy = [" + this.classHierarchy.getMaxLevel() + "]" ); - return this.classHierarchy.getMaxLevel(); - + if ( object.canAs( Resource.class ) ) //if appears as object + if ( propertiesToBeRemoved.contains( object ) ) + if ( object.asResource().getNameSpace().equals( this.namespace ) ) + isObj = true; + + if ( isSubj || isPred || isObj ) //remove the statement in which the prop + this.modifiedModel.remove( st ); //appears as subject, predicate or object + } + } + + //add object properties to the Ontology + public void addProperties ( float percentage ) { + List<OntProperty> properties = this.getOntologyProperties(); + List<OntClass> classes = this.getOntologyClasses(); + ObjectProperty p = null; + DatatypeProperty d = null; + Random classRand = new Random(); + int index; + int nbClasses = classes.size(); //the number of classes + int nbProperties = properties.size(); //the number of properties + int toBeAdd = (int)( percentage*nbProperties ); //the number of properties to be add + + for ( int i=0; i<toBeAdd/2; i++ ) { //add object properties + //p = this.modifiedModel.createObjectProperty( this.namespace + "OBJECT_PROPERTY_" + getRandomString() ); + p = this.modifiedModel.createObjectProperty( this.namespace + getRandomString() ); + index = classRand.nextInt( nbClasses ); //pick random domain + p.addDomain( classes.get( index ) ); + index = classRand.nextInt( nbClasses ); //pick random range + p.addRange( classes.get( index ) ); + } + + for ( int i=toBeAdd/2; i<toBeAdd; i++ ) { //add datatype properties + //d = this.modifiedModel.createDatatypeProperty( this.namespace + "DATATYPE_PROPERTY_" + getRandomString() ); + d = this.modifiedModel.createDatatypeProperty( this.namespace + getRandomString() ); + index = classRand.nextInt( nbClasses ); //add domain + d.addDomain( classes.get( index ) ); + d.addRange( XSD.xstring ); //add range -> string + } + } + + //remove classes comments + @SuppressWarnings("unchecked") + public void removeClassesComments ( float percentage ) { + ArrayList<Literal> comments = new ArrayList<Literal>(); + List<OntClass> classes = this.modifiedModel.listNamedClasses().toList(); + ArrayList<OntClass> classesTo = new ArrayList<OntClass>(); + int nbClasses = classes.size(); + int toBeRemoved = (int)( percentage * nbClasses ); //number of classes comments to be removed + + int [] n = this.randNumbers(nbClasses, toBeRemoved); + for ( int i=0; i<toBeRemoved; i++ ) { + OntClass cls = classes.get(n[i]); + classesTo.add( cls ); + } + + for ( OntClass c : classesTo ) { + for (Iterator it2 = c.listComments(null); it2.hasNext();) + comments.add(((Literal) it2.next())); + for (Literal lit : comments) // remove comments + c.removeComment( lit ); + comments.clear(); + } + } + + //remove properties comments + @SuppressWarnings("unchecked") + public void removePropertiesComments ( float percentage ) { + ArrayList<Literal> comments = new ArrayList<Literal>(); // an array list to hold all the comments + List<OntProperty> properties = this.modifiedModel.listAllOntProperties().toList(); + ArrayList<OntProperty> propertiesTo = new ArrayList<OntProperty>(); + int nbProperties = properties.size(); + int toBeRemoved = (int)( percentage * nbProperties ); //the number of properties comments to be removed + + int [] n = this.randNumbers(nbProperties, toBeRemoved); + for ( int i=0; i<toBeRemoved; i++ ) { + OntProperty p = properties.get(n[i]); + propertiesTo.add( p ); + } + + for ( OntProperty prop : propertiesTo ) { + for (Iterator it2 = prop.listComments(null); it2.hasNext();) // get all comments + comments.add(((Literal) it2.next())); + for (Literal lit : comments) //remove comments + prop.removeComment( lit ); + comments.clear(); + } + } + + //remove individuals comments + @SuppressWarnings("unchecked") + public void removeIndividualsComments ( float percentage ) { + ArrayList<Literal> comments = new ArrayList<Literal>(); // an array list to hold all the comments + List<Individual> individuals = this.modifiedModel.listIndividuals().toList(); + ArrayList<Individual> individualsTo = new ArrayList<Individual>(); + int nbIndividuals = individuals.size(); + int toBeRemoved = (int)( percentage * nbIndividuals ); //number of classes to be removed + + int [] n = this.randNumbers(nbIndividuals, toBeRemoved); + for ( int i=0; i<toBeRemoved; i++ ) { + Individual indiv = individuals.get(n[i]); + individualsTo.add( indiv ); + } + for ( Individual indiv : individuals ) { + for (Iterator it2 = indiv.listComments(null); it2.hasNext(); ) //get all comments + comments.add( ((Literal) it2.next()) ); + for (Literal lit : comments ) //remove comments + indiv.removeComment( lit ); + comments.clear(); + } + } + + //remove Ontologies comments + @SuppressWarnings("unchecked") + public void removeOntologiesComments ( float percentage ) { + ArrayList<Literal> comments = new ArrayList<Literal>(); // an array list to hold all the comments + List<Ontology> ontologies = this.modifiedModel.listOntologies().toList(); + ArrayList<Ontology> ontologiesTo = new ArrayList<Ontology>(); + int nbOntologies = ontologies.size(); + int toBeRemoved = (int)( percentage * nbOntologies ); //the number of Ontologies comments to be removed + + int [] n = this.randNumbers(nbOntologies, toBeRemoved); + for ( int i=0; i<toBeRemoved; i++ ) { + Ontology onto = ontologies.get(n[i]); + ontologiesTo.add( onto ); + } + + for ( Ontology onto : ontologies ) { + for (Iterator it2 = onto.listComments(null); it2.hasNext(); ) // get all comments + comments.add(((Literal) it2.next())); + for ( Literal lit : comments ) //remove all comments + onto.removeComment( lit ); + comments.clear(); + } + } + + //remove percentage comments + public void removeComments ( float percentage ) { + removeClassesComments ( percentage ); + removeIndividualsComments ( percentage ); + removePropertiesComments ( percentage ); + removeOntologiesComments ( percentage ); + } + + //flatten level + public void levelFlattened ( int level ) { + int size; + boolean active = false; + ArrayList<OntClass> levelClasses = new ArrayList<OntClass>(); //the list of classes from that level + ArrayList<OntClass> parentLevelClasses = new ArrayList<OntClass>(); //the list of parent of the child classes from that level + ArrayList<OntClass> superLevelClasses = new ArrayList<OntClass>(); //the list of parent of the parent classes from that level + if ( level == 1 ) //no change + return; + checkClassHierarchy(); //check if the class hierarchy is built + active = this.classHierarchy.flattenClassHierarchy( this.modifiedModel, level, levelClasses, parentLevelClasses, superLevelClasses); + size = levelClasses.size(); + + for ( int i=0; i<size; i++ ) { + OntClass childClass = levelClasses.get( i ); //child class + OntClass parentClass = parentLevelClasses.get( i ); //parent class + //all the classes are subclasses of owl: Thing + if ( active ) { //if ( !parentClass.getURI().equals( "Thing" ) ) { + OntClass superClass = superLevelClasses.get( i ); //parent class of the child class parents + childClass.addSuperClass( superClass ); + parentClass.removeSubClass( childClass ); + } else { + parentClass.removeSubClass( childClass ); + } + } + } + + public void noHierarchy () { + int level = this.getMaxLevel(); //this.classHierarchy.printClassHierarchy(); + while ( this.getMaxLevel() != 1 ) { + levelFlattened ( level ); + level--; + } + } + + //remove percentage restrictions from the model + public void removeRestrictions( float percentage ) { + List<Restriction> restrictions = new ArrayList<Restriction>(); + List<Restriction> restrictionsTo = new ArrayList<Restriction>(); //the array list of restrictions to be removed + restrictions = this.modifiedModel.listRestrictions().toList(); + int nbRestrictions = restrictions.size(); //the number of restrictions + int toBeRemoved = (int)( percentage*nbRestrictions ); //the number of restrictions to be removed + + int [] n = this.randNumbers(nbRestrictions, toBeRemoved); //build the list of restrictions to be removed + for ( int i=0; i<toBeRemoved; i++ ) { + Restriction res = restrictions.get(n[i]); + restrictionsTo.add( res ); + } + + for ( Restriction res : restrictionsTo ) + res.remove(); + } + + //the initial reference alignment + public void initializeAlignment() { + List<OntClass> classes = this.modifiedModel.listNamedClasses().toList();//all classes + 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 + + 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 + + for ( OntProperty prop : properties ) //list all properties + if ( prop.getNameSpace().equals( this.namespace ) ) + this.params.put( prop.getURI() , prop.getURI() ); //add them to the initial alignment } - - //OntModel model, OntModel modifiedModel, Alignment alignment - public void modifyOntology( Parameter p ) { - this.parameter = p; //set the parameter - float value = 0.0f; - String name, aux = ""; - - if ( this.parameter == null ) { - System.out.println( "No parameter" ); //no parameter as input - } - else { - name = this.parameter.getName(); //the name of the parameter - if ( this.parameter.getValue() != null ) //the value of the parameter - value = Float.valueOf( this.parameter.getValue() ).floatValue(); - else - aux = this.parameter.getValue(); - - if ( !this.isAlign ) { //if the initial hashtable for the alignment is not computed - initializeProperties(); //determine the elements from the initial reference alignment - } - - //add percentage classes - if ( name.equals( ParametersIds.ADD_SUBCLASS ) ) { - System.out.println( "Add Class" + "[" + value + "]"); - addSubClasses( value ); - } //remove percentage classes - if ( name.equals( ParametersIds.REMOVE_SUBCLASS ) ) { - System.out.println( "Remove Class" + "[" + value + "]"); - removeSubClasses( value ); - } //remove percentage comments - if ( name.equals( ParametersIds.REMOVE_COMMENT ) ) { - System.out.println( "Remove Comments" + "[" + value + "]"); - removeComments ( value ); - } //remove percentage properties - if ( name.equals( ParametersIds.REMOVE_PROPERTY ) ) { - System.out.println( "Remove Property" + "[" + value + "]"); - removeProperties ( value ); - } //add percentage properties - if ( name.equals( ParametersIds.ADD_PROPERTY ) ) { - System.out.println( "Add Property" + "[" + value + "]"); - addProperties ( value ); - } //recursive add nbClasses starting from level level - if ( name.equals( ParametersIds.ADD_CLASSES ) ) { - aux = ((Float)value).toString(); - int index = aux.indexOf("."); - int level = Integer.valueOf( aux.substring(0, index) ); - int nbClasses = Integer.valueOf( aux.substring(index+1, aux.length()) ); - System.out.println( "level " + level ); - 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 ) ) { - System.out.println("Remove all classes from level" + (int)value ); - removeClassesFromLevel ( (int)value ); - } //flatten a level - if ( name.equals( ParametersIds.LEVEL_FLATTENED ) ) { - //levelFlattened ( level ); - levelFlattened ( (int)value ); - System.out.println( "New class hierarchy level " + getMaxLevel() ) ; - } //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 - 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 ) ) { - System.out.println("Remove restrictions" + "[" + value + "]"); - removeRestrictions( value ); - } //remove percentage individuals - if ( name.equals( ParametersIds.REMOVE_INDIVIDUALS ) ) { - System.out.println("Remove individuals" + "[" + value + "]"); - removeIndividuals( value ); - } //no hierarchy - if ( name.equals( ParametersIds.NO_HIERARCHY ) ) { - System.out.println( "NoHierarchy" ); - noHierarchy(); + + //compute the alignment after the modifications + @SuppressWarnings("unchecked") + public void computeAlignment( String fileName ) { + URI onto1 = null; + URI onto2 = null; + + if ( !this.isChanged ) //if the namespace of the ontology is not changed + this.namespaceNew = "http://oaei.ontologymatching.org/2010/benchmarks/101/onto1.rdf#"; + + try { + onto1 = new URI ( this.namespace ); + onto2 = new URI ( this.namespaceNew ); + + this.alignment.init(onto1, onto2); + + long time = System.currentTimeMillis(); + + //Cell addAlignCell(Object ob1, Object ob2, String relation, double measure) throws AlignmentException { + Set<Object> e1 = params.keySet(); + for ( Iterator it = e1.iterator(); it.hasNext(); ) { + String key = (String)it.next(); + String value = (String)params.get( 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 ); + } + 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); + writer.flush(); + writer.close(); + } catch (AlignmentException aex) { System.out.println( "Exception " + aex.getMessage() ); + } catch (Exception ex) { System.err.println("Exception " + ex.getMessage()); + } + } + + //change the namespace of the modified ontology + public OntModel changeNamespace () { + List<Statement> statements = this.modifiedModel.listStatements().toList(); //the statements of the model + String newNamespace; + + newNamespace = "http://oaei.ontologymatching.org/2010/benchmarks/101/onto1.rdf#"; + this.namespaceNew = newNamespace; + this.isChanged = true; + boolean isSubj, isPred, isObj; + + OntModel newModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );//create new Model + + //iterate through all the statements and change the namespace + 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 ( subject.getLocalName() != null ) + if ( !subject.isLiteral() ) + if ( subject.getNameSpace().equals( this.namespace ) ) { + subj = newModel.createResource( newNamespace + subject.getLocalName() ); + isSubj = true; + } + + if ( !object.isLiteral() ) + if ( object.canAs( Resource.class ) ) + if ( object.isURIResource() ) + if ( object.asResource().getNameSpace().equals( this.namespace ) ) { + obj = newModel.createResource( newNamespace + object.asResource().getLocalName() ); + isObj = true; } - } - - } - -} + if ( !predicate.isLiteral() ) + if ( predicate.getNameSpace().equals( this.namespace ) ) { + pred = newModel.createProperty( newNamespace + predicate.getLocalName() ); + isPred = true; + } + + if ( isSubj ) { + if ( isPred ) { + 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 ); + } + } else { + if ( isPred ) { + 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 ); + } + } + } + + //rename the namespace of owl:Ontology + List<Ontology> ontos = newModel.listOntologies().toList(); + for ( Ontology o : ontos ) { + ResourceUtils.renameResource( o, newNamespace ); + } + + //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 + //System.out.println( "->namespace changed" ); + return this.modifiedModel; + } + + //returns the alignment + public Alignment getAlignment() { + return this.alignment; + } + + //must have the max level of the class hierarchy + public int getMaxLevel() { + checkClassHierarchy(); //check if the class hierarchy is built + return this.classHierarchy.getMaxLevel(); + } + + //OntModel model, OntModel modifiedModel, Alignment alignment + public void modifyOntology( Parameter p ) { + this.parameter = p; //set the parameter + float value = 0.0f; + String name, aux = ""; + + if ( this.parameter == null ) { + System.out.println( "No parameter" ); //no parameter as input + } + else { + name = this.parameter.getName(); //the name of the parameter + if ( this.parameter.getValue().equals( ParametersIds.NO_HIERARCHY ) ) + ; + else if(this.parameter.getValue() != null) //the value of the parameter + 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 ) ) { + System.out.println( "Add Class" + "[" + value + "]"); + addSubClasses( value ); + } //remove percentage classes + if ( name.equals( ParametersIds.REMOVE_SUBCLASS ) ) { + System.out.println( "Remove Class" + "[" + value + "]"); + removeSubClasses( value ); + } //remove percentage comments + if ( name.equals( ParametersIds.REMOVE_COMMENT ) ) { + System.out.println( "Remove Comments" + "[" + value + "]"); + removeComments ( value ); + } //remove percentage properties + if ( name.equals( ParametersIds.REMOVE_PROPERTY ) ) { + System.out.println( "Remove Property" + "[" + value + "]"); + removeProperties ( value ); + } //add percentage properties + if ( name.equals( ParametersIds.ADD_PROPERTY ) ) { + System.out.println( "Add Property" + "[" + value + "]"); + addProperties ( value ); + } //recursive add nbClasses starting from level level + if ( name.equals( ParametersIds.ADD_CLASSES ) ) { + aux = ((Float)value).toString(); + int index = aux.indexOf("."); + int level = Integer.valueOf( aux.substring(0, index) ); + int nbClasses = Integer.valueOf( aux.substring(index+1, aux.length()) ); + System.out.println( "level " + level ); + 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 ) ) { + System.out.println("Remove all classes from level" + (int)value ); + removeClassesFromLevel ( (int)value ); + } //flatten a level + if ( name.equals( ParametersIds.LEVEL_FLATTENED ) ) { + //levelFlattened ( level ); + levelFlattened ( (int)value ); + System.out.println( "New class hierarchy level " + getMaxLevel() ) ; + } //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 + 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 ) ) { + System.out.println("Remove restrictions" + "[" + value + "]"); + removeRestrictions( value ); + } //remove percentage individuals + if ( name.equals( ParametersIds.REMOVE_INDIVIDUALS ) ) { + System.out.println("Remove individuals" + "[" + value + "]"); + removeIndividuals( value ); + } //no hierarchy + if ( name.equals( ParametersIds.NO_HIERARCHY ) ) { + System.out.println( "NoHierarchy" ); + noHierarchy(); + } + } + } + + +} \ 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 66635b065806ae4cd24dcb275f839ef44c7c7ef3..f2553bf0d5e208d75841c9bd66d61654444a2478 100644 --- a/src/fr/inrialpes/exmo/align/gen/ParametersIds.java +++ b/src/fr/inrialpes/exmo/align/gen/ParametersIds.java @@ -1,28 +1,44 @@ -package fr.inrialpes.exmo.align.gen; - /* - * The list of all the modifications that can be applied to the test generator + * $Id: ParametersIds.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. */ -public class ParametersIds { - public static String ADD_SUBCLASS = "addSubClass"; - public static String REMOVE_SUBCLASS = "removeSubClass"; - public static String REMOVE_PROPERTY = "removeProperty"; - public static String REMOVE_COMMENT = "removeComment"; - public static String LEVEL_FLATTENED = "levelFlattened"; - public static String ADD_PROPERTY = "addProperty"; - public static String REMOVE_CLASSES = "removeClasses"; //remove classes from level - //add c classes beginning from level l -> the value of this parameters should be: - //beginning_level.number_of_classes_to_add - public static String ADD_CLASSES = "addClasses"; - public static String RENAME_PROPERTIES="renameProperties"; //rename properties - public static String RENAME_CLASSES ="renameClasses"; //rename classes - public static String RENAME_RESOURCES = "renameResources"; //value is null for this parameter - - - public static String REMOVE_RESTRICTION= "removeRestriction"; //remove restrictions +/* This program represents the list of all the modifications + that can be applied to the test generator + */ - public static String REMOVE_INDIVIDUALS= "removeIndividuals"; //remove individuals - public static String NO_HIERARCHY = "noHierarchy"; //no hierarchy +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 } diff --git a/src/fr/inrialpes/exmo/align/gen/TestGenerator.java b/src/fr/inrialpes/exmo/align/gen/TestGenerator.java index ad5be9394a329af958154348e1d6715cb4faec8b..0446193ff3d49391c5a6a937798cbc7183ba3ca9 100644 --- a/src/fr/inrialpes/exmo/align/gen/TestGenerator.java +++ b/src/fr/inrialpes/exmo/align/gen/TestGenerator.java @@ -1,169 +1,101 @@ -package fr.inrialpes.exmo.align.gen; +/* + * $Id: TestGenerator.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. + */ + +/* This program modifies the input ontology o according to the set of parameters p + and returns the alignment between the initial ontology and the modified one + */ -//Java classes -import java.io.FileOutputStream; -import java.io.InputStream; +package fr.inrialpes.exmo.align.gen; -//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 classes -import org.semanticweb.owl.align.Alignment; - -// Alignment API implementation classes +import fr.inrialpes.exmo.align.gen.inter.AlignedOntologyGenerator; import fr.inrialpes.exmo.align.impl.URIAlignment; import fr.inrialpes.exmo.align.service.jade.messageontology.Parameter; -import java.io.File; -import java.io.OutputStreamWriter; -import java.nio.charset.Charset; - -public class TestGenerator { - - //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 void writeOntology(OntModel model, String dest) { - try { - File f = new File(dest); - FileOutputStream fout = new FileOutputStream(f); - Charset defaultCharset = Charset.forName("UTF8"); - RDFWriter writer = model.getWriter("RDF/XML-ABBREV"); - writer.setProperty("showXmlDeclaration","true"); - writer.write(model.getBaseModel(), new OutputStreamWriter(fout, defaultCharset), ""); - fout.close(); - } catch (Exception ex) { - System.out.println("Exception " + ex.getMessage()); - } - } - - public static void printUsage() { - System.out.println( "inputOntology outputOntology parameters" ); - System.out.println( "[--------------------------------------------------------------------------]" ); - System.out.println( "[------------- The list of all modification is the following --------------]" ); - - System.out.println( "[1. Remove percentage subclasses \"removeSubClass\" --------------]" ); - System.out.println( "[2. Remove percentage properties \"removeProperty\" --------------]" ); - System.out.println( "[3. Remove percentage comments \"removeComment\" --------------]" ); - System.out.println( "[4. Remove percentage restrictions \"removeRestriction\" --------------]" ); - System.out.println( "[5. Add percentage subclasses \"addSubClass\" --------------]" ); - System.out.println( "[6. Add percentage properties \"addProperty\" --------------]" ); - System.out.println( "[7. Rename percentage classes \"renameClasses\" --------------]" ); - System.out.println( "[8. Rename percentage properties \"renameProperties\" --------------]" ); - - System.out.println( "[9. Remove all the classes from a level\"removeClasses\" ---------------]" ); - System.out.println( "[10. Add nbClasses to a specific level \"addClasses\" ---------------]" ); - System.out.println( "[11. Level flattened \"levelFlattened\" ---------------]" ); - System.out.println( "[12. Remove individuals \"removeIndividuals\" ------------]" ); - //noHierarchy - System.out.println( "[--------------------------------------------------------------------------]" ); - System.exit(-1); - } - - /* - * FileName -> the name of the Ontology - * GeneratorParameters -> the list of parameters - */ - public static void main (String [] args) { - TestGenerator t = new TestGenerator(); - String fileName = "", destFile = ""; - //String fileName = "onto.rdf"; - //String destFile = "onto1.rdf"; - GeneratorParameters parameters = new GeneratorParameters(); - - if ( args.length < 2 ) { - System.out.println("Usage"); - printUsage(); - } - else { - System.out.println( args[0] ); - System.out.println( args[1] ); - - fileName = args[0]; - destFile = args[1]; - parameters = new GeneratorParameters(); //initialize the parameters - - for ( int i=2; i<args.length; i+=2 ) { - if ( args[i].equals("addSubClass") ) /* add percentage classes */ - parameters.setParameter(ParametersIds.ADD_SUBCLASS, args[i+1]); - //add c classes beginning from level l -> the value of this parameters should be: - //beginning_level.number_of_classes_to_add - if ( args[i].equals("addClasses") ) /* add c classes beginning from level l */ - parameters.setParameter(ParametersIds.ADD_CLASSES, args[i+1]); - - if ( args[i].equals("removeSubClass") ) /* remove percentage subclasses */ - parameters.setParameter(ParametersIds.REMOVE_SUBCLASS, args[i+1]); - - if ( args[i].equals("removeClasses") ) /* remove classes from level */ - parameters.setParameter(ParametersIds.REMOVE_CLASSES, args[i+1]); - - if ( args[i].equals("addProperty") ) /* add percentage properties */ - parameters.setParameter(ParametersIds.ADD_PROPERTY, args[i+1]); - - if ( args[i].equals("removeProperty") ) /* remove percentage properties */ - parameters.setParameter(ParametersIds.REMOVE_PROPERTY, args[i+1]); - - if ( args[i].equals("renameProperties") )/* rename percentage properties */ - parameters.setParameter(ParametersIds.RENAME_PROPERTIES, args[i+1]); - - if ( args[i].equals("removeComment") ) /* remove percentage comments */ - parameters.setParameter(ParametersIds.REMOVE_COMMENT, args[i+1]); - - if ( args[i].equals("levelFlattened") ) /* flattened level */ - parameters.setParameter(ParametersIds.LEVEL_FLATTENED, args[i+1]); - - if ( args[i].equals("renameClasses") ) /* rename percentage classes */ - parameters.setParameter(ParametersIds.RENAME_CLASSES, args[i+1]); - - if ( args[i].equals("renameResources") )/* rename percentage resources */ - parameters.setParameter(ParametersIds.RENAME_RESOURCES, args[i+1]); - - if ( args[i].equals("removeRestriction") ) /* remove percentage restrictions */ - parameters.setParameter(ParametersIds.REMOVE_RESTRICTION, args[i+1]); +import fr.inrialpes.exmo.ontowrap.Ontology; +import fr.inrialpes.exmo.ontowrap.jena25.JENAOntology; +import java.util.Properties; +import org.semanticweb.owl.align.Alignment; - if ( args[i].equals("removeIndividuals") ) /* remove percentage individuals */ - parameters.setParameter(ParametersIds.REMOVE_INDIVIDUALS, args[i+1]); - if ( args[i].equals( ("noHierarchy")) ) /* no hierarchy */ - parameters.setParameter( ParametersIds.NO_HIERARCHY , null); - } - //load the model - OntModel model = t.loadOntology( fileName ); //the initial Ontology - OntModel modifiedModel = t.loadOntology( fileName ); //the modified Ontology - Alignment alignment = new URIAlignment(); //the initial Alignment - //build the ontology modifier for the first time - OntologyModifier modifier = new OntologyModifier( model, modifiedModel, alignment); - modifier.initializeProperties(); //initialize the reference alignment - //get the max level of the class hierarchy of the ontology - int level = modifier.getMaxLevel(); - System.out.println( "[-------------------------------------------------]" ); - for ( int i=0; i<parameters.size(); i++ ) { - Parameter p = parameters.getParameter(i); //the parameter at position index - modifier.modifyOntology( p ); //modify the ontology according to parameter p - System.out.println( "[We-modified-the-ontology-for-parameter " + p.getName() + "]"); - } - System.out.println( "[-------------------------------------------------]" ); +public class TestGenerator implements AlignedOntologyGenerator { + private Properties parameters; //the set of parameters + private OntModel model; //initial ontology + private OntModel modifiedModel; //modified ontology + private Alignment alignment; //the reference alignment + + public TestGenerator() {} + + //returns the modified ontology + public Ontology getModifiedOntology() { + JENAOntology onto = new JENAOntology(); //cast the model into Ontology + onto.setOntology( this.modifiedModel ); + return onto; //return the ontology + } + + //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() ); + } + } - modifier.computeAlignment( "referenceAlignment.rdf" ); - alignment = modifier.getAlignment(); //get the reference alignment - modifiedModel = modifier.getModifiedOntology(); //get the modified ontology - t.writeOntology( modifiedModel, destFile ); //write the model - System.out.println( "***" ); - System.out.println( "END" ); - System.out.println( "***" ); - } + 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(); + } + else { + System.err.println("Error : The object given is not an OntModel"); + System.exit(-1); } + 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.initializeAlignment(); //initialize the reference alignment + int level = modifier.getMaxLevel(); //get the max level of the class hierarchy of the ontology + + System.out.println( "[-------------------------------------------------]" ); + for(String key : this.parameters.stringPropertyNames()) { + String value = this.parameters.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( "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 + 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 bf5b73738cc6e8e83feb05c98ac8d647c7b7d703..c97e2b24efd81eb30bd754aac9a3529d2ad6ec11 100644 --- a/src/fr/inrialpes/exmo/align/gen/URITree.java +++ b/src/fr/inrialpes/exmo/align/gen/URITree.java @@ -1,359 +1,372 @@ -package fr.inrialpes.exmo.align.gen; +/* + * $Id: URITree.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. + */ + +/* This program represents the class hierarchy. + It retains only the URI of the classes . +*/ +package fr.inrialpes.exmo.align.gen; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Properties; -/* - * This class represents the class hierarchy. - * It retains only the URI of the classes . - */ - public class URITree { - private String URI; //the URI of the node - private ArrayList<URITree> children; //the list of children - private URITree parent; //the parent of the node - int depth; //the depth of the node - int maxDepth; //the max depth of the node + private String URI; //the URI of the node + private ArrayList<URITree> children;//the list of children + 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) { //the constructor - this.URI = URI; - this.children = new ArrayList<URITree>(); - this.parent = null; - this.depth = 0; - this.maxDepth = 0; - } + public URITree(String URI) { + this.URI = URI; + this.children = new ArrayList<URITree>(); + this.parent = null; + this.depth = 0; + this.maxDepth = 0; + } - //get the URI of the node - public String getURI () { - return this.URI; - } + //get the URI of the node + public String getURI () { + return this.URI; + } - //set the URI of the node - public void setURI(String URI) { - this.URI = 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; - } + //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; - } + //get the depth of the node + public int getDepth() { + return this.depth; + } - //return the max depth - public int getMaxDepth() { - return this.maxDepth; - } + //return the max depth + public int getMaxDepth() { + return this.maxDepth; + } - //set the parent of the node - public void setParent ( URITree parent ) { - this.parent = parent; - } + //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; - } + //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 ); - } + //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; - } + //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(); - } - - //add the node with the childURI to the parent with the URI parentURI - public void add(URITree root, String childURI, String parentURI) { - URITree parent; - parent = searchURITree(root, parentURI); //we search for the parent URITree - addChild(root, parent, childURI); //we add the new URITree - } - - //add a child - 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; - index++; - } + //returns the size of the children + public int getChildrenSize() { + return this.children.size(); + } + + //add the node with the childURI to the parent with the URI parentURI + public void add(URITree root, String childURI, String parentURI) { + URITree parent; + parent = searchURITree(root, parentURI); //we search for the parent URITree + addChild(root, parent, childURI); //we add the new URITree + } + + //add a child + public void addChild (URITree root, URITree node, String URI) { + int index = 0; + URITree n = null; - index = 0; - while ( index < root.getChildrenSize() ) { //we search among root children to see if the node is already there - n = root.getChildrenList().get( index ); - if ( n.getURI().equals( URI ) ) { //the node is already there - root.getChildrenList().remove( n ); //we remove the node - break; - } - index++; - } - addChildToNode( node, URI ); - } + while ( index < node.getChildrenSize() ) { //if the child is already in the list + if ( node.getChildAt( index ).getURI().equals( URI ) ) + return; + index++; + } + + index = 0; + while ( index < root.getChildrenSize() ) { //we search among root children to see if the node is already there + n = root.getChildrenList().get( index ); + if ( n.getURI().equals( URI ) ) { //the node is already there + root.getChildrenList().remove( n ); //we remove the node + break; + } + index++; + } + 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 ) //keep track of the max depth of the hierarchy - this.maxDepth = node.getDepth() + 1; + //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 - child.setParent( node ); //set the parent of the node - node.getChildrenList().add( child ); //add the node to the parent children list - } - - + if ( this.maxDepth < node.getDepth()+1 ) //keep track of the max depth of the hierarchy + this.maxDepth = node.getDepth() + 1; + + child.setParent( node ); //set the parent of the node + node.getChildrenList().add( child ); //add the node to the parent children list + } + //renames the class from the tree after we have renamed the classes + @SuppressWarnings("unchecked") + public void renameTree(URITree root, Properties params) { + int index = 0; + URITree node = root; + URITree ans = null; + String uri; + //the uri has change + while ( index < root.getChildrenSize() ) { //we start to search recursively + uri = root.getChildAt( index ).getURI(); + // System.out.println( "uri = " + uri ); + if ( params.containsKey( uri ) ) { + if ( !params.get( uri ).equals( uri ) ) + root.getChildAt( index ).setURI( (String)params.get( uri ) ); + } + rename(root.getChildAt( index ), new ArrayList(), 0, params); + index++; + } + } - //renames the class from the tree after we have renamed the classes - @SuppressWarnings("unchecked") - public void renameTree(URITree root, Properties params) { - int index = 0; - URITree node = root; - URITree ans = null; - String uri; + @SuppressWarnings("unchecked") + public void rename(URITree node, List occurs, int depth, Properties params) { + int index = 0; + URITree ans = null; + //verify if the label of the URITree is the one with the label of the URITree we search for + String uri = node.getURI(); + if ( params.containsKey( uri ) ) { - //the uri has change + if ( !params.get( uri ).equals( uri ) ) { + node.setURI( (String)params.get( uri ) ); + } + } - while ( index < root.getChildrenSize() ) { //we start to search recursively - uri = root.getChildAt( index ).getURI(); - // System.out.println( "uri = " + uri ); - if ( params.containsKey( uri ) ) { - if ( !params.get( uri ).equals( uri ) ) - root.getChildAt( index ).setURI( (String)params.get( uri ) ); - } - rename(root.getChildAt( index ), new ArrayList(), 0, params); - index++; - } - } + while( index < node.getChildrenSize()) { + URITree n = node.getChildAt( index ); + occurs.add( node ); + rename( n, occurs, depth+1, params ); + occurs.remove( node ); + index++; + } + } - @SuppressWarnings("unchecked") - public void rename(URITree node, List occurs, int depth, Properties params) { - int index = 0; - URITree ans = null; - //verify if the label of the URITree is the one with the label of the URITree we search for - String uri = node.getURI(); - if ( params.containsKey( uri ) ) { + //returns the URITree with the given URI + @SuppressWarnings("unchecked") + public URITree searchURITree(URITree root, String URI) { + 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 + ans = search(root.getChildAt( index ), new ArrayList(), 0, URI); + if ( ans != null ) + return ans; + index++; + } + return node; + } - if ( !params.get( uri ).equals( uri ) ) { - node.setURI( (String)params.get( uri ) ); - } - } + @SuppressWarnings("unchecked") + public URITree search(URITree node, List occurs, int depth, String URI) { + int index = 0; + URITree ans = null; + //verify if the label of the URITree is the one with the label of the URITree we search for + if ( node.getURI().equals( URI ) ) { + ans = node; + return ans; //has found the parent + } - while( index < node.getChildrenSize()) { - URITree n = node.getChildAt( index ); - occurs.add( node ); - rename( n, occurs, depth+1, params ); - occurs.remove( node ); - index++; - } + while( index < node.getChildrenSize()) { + URITree n = node.getChildAt( index ); + occurs.add( node ); + ans = search( n, occurs, depth+1, URI ); + if ( ans != null ) + return ans; + occurs.remove( node ); + index++; } + return null; //has not found the parent + } - //returns the URITree with the given URI - @SuppressWarnings("unchecked") - public URITree searchURITree(URITree root, String URI) { - 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 - ans = search(root.getChildAt( index ), new ArrayList(), 0, URI); - if ( ans != null ) - return ans; - index++; - } - return node; - } - - @SuppressWarnings("unchecked") - public URITree search(URITree node, List occurs, int depth, String URI) { - int index = 0; - URITree ans = null; - //verify if the label of the URITree is the one with the label of the URITree we search for - if ( node.getURI().equals( URI ) ) { - ans = node; - return ans; //has found the parent - } - - while( index < node.getChildrenSize()) { - URITree n = node.getChildAt( index ); - occurs.add( node ); - ans = search( n, occurs, depth+1, URI ); - if ( ans != null ) - return ans; - occurs.remove( node ); - index++; - } - return null; //has not found the parent - } - - //remove a child from the tree - @SuppressWarnings("unchecked") - public void removeFromURITree(URITree root, String URI) { - int index = 0; - int found = 0; - - while ( index < root.getChildrenSize() ) { - if ( root.getChildAt( index ).getURI().equals( URI ) ) { - root.getChildrenList().remove( index ); //found the node to delete - return; - } + //remove a child from the tree + @SuppressWarnings("unchecked") + public void removeFromURITree(URITree root, String URI) { + int index = 0; + int found = 0; + + while ( index < root.getChildrenSize() ) { + if ( root.getChildAt( index ).getURI().equals( URI ) ) { + root.getChildrenList().remove( index ); //found the node to delete + return; + } - found = remove(root.getChildAt( index ), new ArrayList(), 0, URI); - if ( found == 1 ) - return; - index++; - } - } + found = remove(root.getChildAt( index ), new ArrayList(), 0, URI); + if ( found == 1 ) + return; + index++; + } + } - @SuppressWarnings("unchecked") - public int remove(URITree node, List occurs, int depth, String URI) { - int index = 0; - int found = 0; - - if ( node.getURI().equals( URI ) ) { - URITree parent = node.getParent(); - //add the node children to the parent of the node - int cnt = 0; //reestablish the connection between nodes - while ( cnt < node.getChildrenSize() ) { - URITree child = node.getChildrenList().get( cnt ); - child.setDepth( node.getDepth() ); //modify the depth - child.setParent( parent ); //modify the parent - parent.getChildrenList().add( child ); //add the child to the parent of node - cnt++; - } + @SuppressWarnings("unchecked") + public int remove(URITree node, List occurs, int depth, String URI) { + int index = 0; + int found = 0; + + if ( node.getURI().equals( URI ) ) { + URITree parent = node.getParent(); + //add the node children to the parent of the node + int cnt = 0; //reestablish the connection between nodes + while ( cnt < node.getChildrenSize() ) { + URITree child = node.getChildrenList().get( cnt ); + child.setDepth( node.getDepth() ); //modify the depth + 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 - found = 1; - return found; //has found the parent - } - - while( index < node.getChildrenSize()) { - URITree n = node.getChildAt( index ); - occurs.add( node ); - found = remove( n, occurs, depth+1, URI ); - if ( found == 1 ) - return found; - occurs.remove( node ); - index++; - } - - return found; - } - - //get all the node from a specific level - @SuppressWarnings("unchecked") - public List<URITree> getNodesFromLevel (URITree root, int level) { - List<URITree> nodes = new ArrayList<URITree>(); //to store the nodes from a specific level - int index = 0; - if ( root.getDepth() == level ) - nodes.add( root ); - while ( index < root.getChildrenList().size() ) { - getNodes ( root.getChildAt(index), new ArrayList(), 0, nodes, level ); //recursively print all the children URITrees - index++; - } - return nodes; //return the list of nodes - } - - @SuppressWarnings("unchecked") - public void getNodes (URITree node, List occurs, int depth, List<URITree> nodes, int level) { - int index = 0; - if ( node.getDepth() == level ) //if it's on the level that we want, we add it to the hierarchy - nodes.add( node ); + parent.getChildrenList().remove( node ); //remove the node from the children list + found = 1; + return found; //has found the parent + } - while( index < node.getChildrenList().size() ) { - URITree n = node.getChildrenList().get(index); - occurs.add( node ); - getNodes( n, occurs, depth+1, nodes, level ); - occurs.remove( node ); - index++; - } - } - - //change the depth if the nodes lower the level to node.getDepth()-1 - @SuppressWarnings("unchecked") - public void changeDepth (URITree root, int level) { - int index = 0; - this.maxDepth = this.maxDepth-1; - while ( index < root.getChildrenList().size() ) { - change ( root.getChildAt(index), new ArrayList(), 0, level ); - index++; - } - } + while( index < node.getChildrenSize()) { + URITree n = node.getChildAt( index ); + occurs.add( node ); + found = remove( n, occurs, depth+1, URI ); + if ( found == 1 ) + return found; + occurs.remove( node ); + index++; + } + + return found; + } + + //get all the node from a specific level + @SuppressWarnings("unchecked") + public List<URITree> getNodesFromLevel (URITree root, int level) { + List<URITree> nodes = new ArrayList<URITree>(); //to store the nodes from a specific level + int index = 0; + if ( root.getDepth() == level ) + nodes.add( root ); + while ( index < root.getChildrenList().size() ) { + getNodes ( root.getChildAt(index), new ArrayList(), 0, nodes, level );//recursively print all the children URITrees + index++; + } + return nodes; //return the list of nodes + } + + @SuppressWarnings("unchecked") + public void getNodes (URITree node, List occurs, int depth, List<URITree> nodes, int level) { + int index = 0; + if ( node.getDepth() == level ) //if it's on the level that we want, we add it to the hierarchy + nodes.add( node ); + + while( index < node.getChildrenList().size() ) { + URITree n = node.getChildrenList().get(index); + occurs.add( node ); + getNodes( n, occurs, depth+1, nodes, level ); + occurs.remove( node ); + index++; + } + } - @SuppressWarnings("unchecked") - public void change (URITree node, List occurs, int depth, int level) { - int index = 0; + //change the depth if the nodes lower the level to node.getDepth()-1 + @SuppressWarnings("unchecked") + public void changeDepth (URITree root, int level) { + int index = 0; + this.maxDepth = this.maxDepth-1; + while ( index < root.getChildrenList().size() ) { + change ( root.getChildAt(index), new ArrayList(), 0, level ); + index++; + } + } + + @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 ); - } + 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 ); - change( n, occurs, depth+1, level ); - occurs.remove( node ); - index++; - } - } + while( index < node.getChildrenList().size() ) { + URITree n = node.getChildrenList().get(index); + occurs.add( node ); + change( n, occurs, depth+1, level ); + occurs.remove( node ); + index++; + } + } - //print the tree - @SuppressWarnings("unchecked") - public void printURITree( URITree root ) { - int index = 0; - //System.out.println( "[" + root.getURI() + "]" + "->" + root.getDepth() ); + //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); - index++; - } - } - - @SuppressWarnings("unchecked") - public void print (URITree node, List occurs, int depth) { - int index = 0; - indent( node.getDepth() ); - System.out.println( "[" + node.getURI() + "]" + "->" + node.getDepth() ); + while ( index < root.getChildrenList().size() ) { + //recursively print all the children URITrees + print(root.getChildAt(index), new ArrayList(), 0); + index++; + } + } + + @SuppressWarnings("unchecked") + public void print (URITree node, List occurs, int depth) { + 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 ); - print( n, occurs, depth+1 ); - occurs.remove( node ); - index++; - } - } + while( index < node.getChildrenList().size() ) { + URITree n = node.getChildrenList().get( index ); + occurs.add( node ); + print( n, occurs, depth+1 ); + occurs.remove( node ); + index++; + } + } - protected void indent( int depth ) { - for (int i = 0; i < depth; i++) { - System.out.print( " " ); - } - } + protected void indent( int depth ) { + for (int i = 0; i < depth; i++) { + System.out.print( " " ); + } + } } diff --git a/src/fr/inrialpes/exmo/align/gen/inter/AlignedOntologyGenerator.java b/src/fr/inrialpes/exmo/align/gen/inter/AlignedOntologyGenerator.java new file mode 100644 index 0000000000000000000000000000000000000000..c53769677b14f0e10423efa440a827f842da287f --- /dev/null +++ b/src/fr/inrialpes/exmo/align/gen/inter/AlignedOntologyGenerator.java @@ -0,0 +1,34 @@ +/* + * $Id: AlignedOntologyGenerator.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. + */ + + +package fr.inrialpes.exmo.align.gen.inter; + +import fr.inrialpes.exmo.ontowrap.Ontology; +import java.util.Properties; +import org.semanticweb.owl.align.Alignment; + +public interface AlignedOntologyGenerator { + + //generate an Alignment refering to the generated Ontology as onto2 (to be saved) + public Alignment generate(Ontology o, Properties p);; + +}