From 4c30b8007d3e3b640384e7e2258682e35560a776 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Thu, 22 May 2014 19:09:14 +0000
Subject: [PATCH] - implemented semi-deep cloning in BasicOntologyNetwork -
 moved convertExtensions to Extensions

---
 .../exmo/align/impl/BasicAlignment.java       | 25 +++----------------
 .../exmo/align/impl/BasicOntologyNetwork.java | 19 +++++++++++---
 .../inrialpes/exmo/align/impl/Extensions.java | 19 ++++++++++++++
 .../exmo/align/impl/ObjectAlignment.java      |  6 ++---
 .../exmo/align/impl/edoal/EDOALAlignment.java |  8 +++---
 5 files changed, 46 insertions(+), 31 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/impl/BasicAlignment.java b/src/fr/inrialpes/exmo/align/impl/BasicAlignment.java
index 8657fc7c..c1d65f9d 100644
--- a/src/fr/inrialpes/exmo/align/impl/BasicAlignment.java
+++ b/src/fr/inrialpes/exmo/align/impl/BasicAlignment.java
@@ -218,6 +218,8 @@ public class BasicAlignment implements Alignment {
 
     public Collection<String[]> getExtensions(){ return extensions.getValues(); }
 
+    public Extensions getExtensionsObject(){ return extensions; }
+
     public void setExtensions( Extensions ext ){ extensions = ext; }
 
     public void setExtension( String uri, String label, String value ) {
@@ -667,25 +669,6 @@ public class BasicAlignment implements Alignment {
 	return result;
     }
 
-    public Extensions convertExtension( String label, String method ) {
-	Extensions newext = (Extensions)extensions.clone();
-	String oldid = extensions.getExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
-	if ( oldid != null && !oldid.equals("") ) {
-	    newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.DERIVEDFROM, oldid );
-	    newext.unsetExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
-	}
-	String pretty = getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY );
-	if ( pretty != null ){
-	    newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY, pretty+"/"+label );
-	};
-	if ( extensions.getExtension( Namespace.ALIGNMENT.uri, Annotations.PROVENANCE ) != null ) {
-	    newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.PROVENANCE,
-				 extensions.getExtension( Namespace.ALIGNMENT.uri, Annotations.PROVENANCE ) );
-	}
-	newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.METHOD, method );
-	return newext;
-    }
-
     /**
      * A new alignment is created such that for
      * any pair (o, o', n, r) in O the resulting alignment will contain:
@@ -699,7 +682,7 @@ public class BasicAlignment implements Alignment {
 	// We must inverse getType
 	result.setType( getType() );
 	result.setLevel( getLevel() );
-	result.setExtensions( convertExtension( "inverted", "http://exmo.inrialpes.fr/align/impl/BasicAlignment#inverse" ) );
+	result.setExtensions( extensions.convertExtension( "inverted", "http://exmo.inrialpes.fr/align/impl/BasicAlignment#inverse" ) );
 	//for ( Enumeration e = namespaces.getNames() ; e.hasMoreElements(); ){
 	//    String label = (String)e.nextElement();
 	for ( String label : namespaces.stringPropertyNames() ) {
@@ -743,7 +726,7 @@ public class BasicAlignment implements Alignment {
 	align.setLevel( getLevel() );
 	align.setFile1( getFile1() );
 	align.setFile2( getFile2() );
-	align.setExtensions( convertExtension( "cloned", this.getClass().getName()+"#clone" ) );
+	align.setExtensions( extensions.convertExtension( "cloned", this.getClass().getName()+"#clone" ) );
 	//for ( Enumeration e = namespaces.getNames() ; e.hasMoreElements(); ){
 	//    String label = (String)e.nextElement();
 	for ( String label : namespaces.stringPropertyNames() ) {
diff --git a/src/fr/inrialpes/exmo/align/impl/BasicOntologyNetwork.java b/src/fr/inrialpes/exmo/align/impl/BasicOntologyNetwork.java
index 43982ad9..7dcd086f 100644
--- a/src/fr/inrialpes/exmo/align/impl/BasicOntologyNetwork.java
+++ b/src/fr/inrialpes/exmo/align/impl/BasicOntologyNetwork.java
@@ -20,7 +20,6 @@
 
 package fr.inrialpes.exmo.align.impl; 
 
-//import java.lang.Cloneable; // JE: what happens when I do a clone() ??
 import java.lang.Iterable;
 import java.util.Collections;
 import java.util.Collection;
@@ -168,9 +167,23 @@ public class BasicOntologyNetwork implements OntologyNetwork {
 
 
     /**
-     * Clone?
+     * Clone does some deeper cloning
+     * It has the same content but a different id (no id indeed)
      */
-    //public BasicOntologyNetwork clone() {}
+    public BasicOntologyNetwork clone() {
+	//public Object clone() {
+	BasicOntologyNetwork network = new BasicOntologyNetwork();
+	network.setExtensions( extensions.convertExtension( "cloned", this.getClass().getName()+"#clone" ) );
+	for ( URI onto : ontologies.keySet() ) network.addOntology( onto );
+	for ( Alignment al : alignments ) {
+	    try { 
+		network.addAlignment( al );
+	    } catch (AlignmentException alex) {
+		logger.debug( "IGNORED Exception : should not happen {}", al );
+	    }
+	}
+	return network;
+    }
 
     /**
      * Normalizes an ontology network for it to have exactly one alignment between each pair of ontologies.
diff --git a/src/fr/inrialpes/exmo/align/impl/Extensions.java b/src/fr/inrialpes/exmo/align/impl/Extensions.java
index aad1e69b..1331b309 100644
--- a/src/fr/inrialpes/exmo/align/impl/Extensions.java
+++ b/src/fr/inrialpes/exmo/align/impl/Extensions.java
@@ -72,6 +72,25 @@ public class Extensions {
 	return table.values();
     }
 
+    public Extensions convertExtension( String label, String method ) {
+	Extensions newext = (Extensions)clone();
+	String oldid = getExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
+	if ( oldid != null && !oldid.equals("") ) {
+	    newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.DERIVEDFROM, oldid );
+	    newext.unsetExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
+	}
+	String pretty = getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY );
+	if ( pretty != null ){
+	    newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY, pretty+"/"+label );
+	};
+	if ( getExtension( Namespace.ALIGNMENT.uri, Annotations.PROVENANCE ) != null ) {
+	    newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.PROVENANCE,
+				 getExtension( Namespace.ALIGNMENT.uri, Annotations.PROVENANCE ) );
+	}
+	newext.setExtension( Namespace.ALIGNMENT.uri, Annotations.METHOD, method );
+	return newext;
+    }
+
     @SuppressWarnings( "unchecked" )
     public Object clone() {
 	return new Extensions( (Hashtable<String,String[]>)table.clone() ); //[W:unchecked]
diff --git a/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java b/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java
index ab3334af..ada4592c 100644
--- a/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java
+++ b/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * Copyright (C) INRIA, 2003-2011, 2013
+ * Copyright (C) INRIA, 2003-2011, 2013-2014
  *
  * 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
@@ -119,7 +119,7 @@ public class ObjectAlignment extends BasicAlignment {
 	align.setLevel( getLevel() );
 	align.setFile1( getFile1() );
 	align.setFile2( getFile2() );
-	align.setExtensions( convertExtension( "EDOALURIConverted", this.getClass().getName()+"#toURI" ) );
+	align.setExtensions( extensions.convertExtension( "EDOALURIConverted", this.getClass().getName()+"#toURI" ) );
 	for (Enumeration e = getElements(); e.hasMoreElements();) {
 	    Cell c = (Cell)e.nextElement();
 	    try {
@@ -147,7 +147,7 @@ public class ObjectAlignment extends BasicAlignment {
 	}
 	alignment.setType( al.getType() );
 	alignment.setLevel( al.getLevel() );
-	alignment.setExtensions( al.convertExtension( "ObjectURIConverted", "fr.inrialpes.exmo.align.ObjectAlignment#toObject" ) );
+	alignment.setExtensions( al.extensions.convertExtension( "ObjectURIConverted", "fr.inrialpes.exmo.align.ObjectAlignment#toObject" ) );
 	LoadedOntology<Object> o1 = (LoadedOntology<Object>)alignment.getOntologyObject1(); // [W:unchecked]
 	LoadedOntology<Object> o2 = (LoadedOntology<Object>)alignment.getOntologyObject2(); // [W:unchecked]
 	Object obj1 = null;
diff --git a/src/fr/inrialpes/exmo/align/impl/edoal/EDOALAlignment.java b/src/fr/inrialpes/exmo/align/impl/edoal/EDOALAlignment.java
index 108771a9..6db026de 100644
--- a/src/fr/inrialpes/exmo/align/impl/edoal/EDOALAlignment.java
+++ b/src/fr/inrialpes/exmo/align/impl/edoal/EDOALAlignment.java
@@ -2,7 +2,7 @@
  * $Id$
  *
  * Sourceforge version 1.6 - 2008 - was OMWGAlignment
- * Copyright (C) INRIA, 2007-2013
+ * Copyright (C) INRIA, 2007-2014
  *
  * 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
@@ -233,7 +233,7 @@ public class EDOALAlignment extends BasicAlignment {
 	align.setLevel( getLevel() );
 	align.setFile1( getFile1() );
 	align.setFile2( getFile2() );
-	align.setExtensions( convertExtension( "EDOALURIConverted", "http://exmo.inrialpes.fr/align/impl/edoal/EDOALAlignment#toURI" ) );
+	align.setExtensions( extensions.convertExtension( "EDOALURIConverted", "http://exmo.inrialpes.fr/align/impl/edoal/EDOALAlignment#toURI" ) );
 	for (Enumeration e = getElements(); e.hasMoreElements();) {
 	    Cell c = (Cell)e.nextElement();
 	    try {
@@ -306,7 +306,7 @@ public class EDOALAlignment extends BasicAlignment {
      */
     public void convertToEDOAL( BasicAlignment al ) throws AlignmentException {
 	setType( al.getType() );
-	setExtensions( al.convertExtension( "toEDOAL", "fr.inrialpes.exmo.align.edoal.EDOALAlignment#toEDOAL" ) );
+	setExtensions( al.getExtensionsObject().convertExtension( "toEDOAL", "fr.inrialpes.exmo.align.edoal.EDOALAlignment#toEDOAL" ) );
 	LoadedOntology<Object> o1 = (LoadedOntology<Object>)getOntologyObject1(); // [W:unchecked]
 	LoadedOntology<Object> o2 = (LoadedOntology<Object>)getOntologyObject2(); // [W:unchecked]
 	for ( Cell c : al ) {
@@ -360,7 +360,7 @@ public class EDOALAlignment extends BasicAlignment {
 	align.setLevel( getLevel() );
 	align.setFile1( getFile1() );
 	align.setFile2( getFile2() );
-	align.setExtensions( convertExtension( "cloned", this.getClass().getName()+"#clone" ) );
+	align.setExtensions( extensions.convertExtension( "cloned", this.getClass().getName()+"#clone" ) );
 	try {
 	    align.ingest( this );
 	} catch (AlignmentException ex) { 
-- 
GitLab