From c50567bfc6e40ddcb24d7d126d6bddb0b2039341 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Tue, 6 Jan 2015 15:51:38 +0000
Subject: [PATCH] - made the components of Propert/RelationConstruction ordered

---
 .../impl/edoal/PropertyConstruction.java      | 56 +++++++++++--------
 .../impl/edoal/RelationConstruction.java      | 20 +++----
 .../exmo/align/parser/RDFParser.java          | 12 ++--
 test/src/EDOALExportTest.java                 | 35 +++++++-----
 test/src/SPARQLConstructVisitorTest.java      |  2 +-
 5 files changed, 69 insertions(+), 56 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/impl/edoal/PropertyConstruction.java b/src/fr/inrialpes/exmo/align/impl/edoal/PropertyConstruction.java
index f3bf15c6..0999db64 100644
--- a/src/fr/inrialpes/exmo/align/impl/edoal/PropertyConstruction.java
+++ b/src/fr/inrialpes/exmo/align/impl/edoal/PropertyConstruction.java
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2006 Digital Enterprise Research Insitute (DERI) Innsbruck
  * Sourceforge version 1.5 - 2006 - was PropertyExpr
- * Copyright (C) INRIA, 2009-2010, 2012
+ * Copyright (C) INRIA, 2009-2010, 2012, 2015
  *
  * 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
@@ -22,8 +22,8 @@
 
 package fr.inrialpes.exmo.align.impl.edoal;
 
-import java.util.Collection;
-import java.util.HashSet;
+import java.util.List;
+import java.util.Vector;
 
 import fr.inrialpes.exmo.align.parser.SyntaxElement.Constructor;
 import fr.inrialpes.exmo.align.parser.SyntaxElement;
@@ -46,39 +46,46 @@ import fr.inrialpes.exmo.align.parser.TypeCheckingVisitor.TYPE;
 
 public class PropertyConstruction extends PropertyExpression {
 
-    /** Holds all expressions. */
-    private Collection<PathExpression> components;
+    /** Holds all expressions: ordered for comp */
+    private List<PathExpression> components;
     
     /** Operator of this complex expression. */
     private Constructor operator;
     
     public PropertyConstruction() {
 	super();
-	components = new HashSet<PathExpression>();
+	components = new Vector<PathExpression>();
     }
 
-    public PropertyConstruction( Constructor op, Collection<PathExpression> expressions ) {
-	if ((expressions == null) || (op == null)) {
+    public PropertyConstruction( Constructor op, List<PathExpression> expressions ) {
+	operator = op;
+	components = expressions;
+	if ( (expressions == null) || (op == null) ) {
 	    throw new NullPointerException("The subexpressions and the operator must not be null");
 	}
-	if (expressions.contains(null)) {
-	    throw new IllegalArgumentException("The subexpressions must not contain null");
-	}
-	// The collection should have only relations and end in a property
-	// It should be ordered with comp: implement List
-	this.components = expressions;
-	if ( op != SyntaxElement.AND.getOperator() &&
-	     op != SyntaxElement.OR.getOperator() &&
-	     op != SyntaxElement.NOT.getOperator() &&
-	     op != SyntaxElement.COMPOSE.getOperator() ) {
-	    throw new IllegalArgumentException( "Incorrect operator for property : "+op );
-	}
-	this.operator = op;
+	if ( op == SyntaxElement.COMPOSE.getOperator() ) {
+	    // In case of COMPOSE, the list should have only relations and end in a property
+	    for ( PathExpression pe : components.subList( 0, components.size()-1 ) ) {
+		if ( ! ( pe instanceof RelationExpression ) ) 
+		    throw new IllegalArgumentException( "Property composition must be based on relation expressions" );
+	    }
+	    if ( ! ( components.get( components.size()-1 ) instanceof PropertyExpression ) ) 
+		    throw new IllegalArgumentException( "Property composition must end by a property expressions" );
+	} else if ( op == SyntaxElement.AND.getOperator() ||
+		    op == SyntaxElement.OR.getOperator() ||
+		    op == SyntaxElement.NOT.getOperator() ) {
+	    // Otherwise it must only contain PropertyExpressions
+	    for ( PathExpression pe : components ) {
+		if ( ! ( pe instanceof PropertyExpression ) ) 
+		    throw new IllegalArgumentException( "Property construction with "+op+" must only contain property expressions" );
+	    }
+	} else throw new IllegalArgumentException( "Incorrect operator for property : "+op );
     }
 
     public void accept( EDOALVisitor visitor ) throws AlignmentException {
 	visitor.visit( this );
     }
+
     public TYPE accept(TypeCheckingVisitor visitor) throws AlignmentException {
 	return visitor.visit(this);
     }
@@ -87,15 +94,18 @@ public class PropertyConstruction extends PropertyExpression {
 	return operator;
     }
 
+    // Bypasses type checking!
     public void setOperator( Constructor op ) {
 	operator = op;
     }
 
-    public Collection<PathExpression> getComponents() {
+    public List<PathExpression> getComponents() {
 	return components;
     }
 
-    public void addComponents( PathExpression exp ) {
+    public void addComponent( PropertyExpression exp ) throws AlignmentException {
+	if ( operator == SyntaxElement.COMPOSE.getOperator() )
+	    throw new AlignmentException( "Cannot add component to a property composition path" );
 	components.add( exp );
     }
 
diff --git a/src/fr/inrialpes/exmo/align/impl/edoal/RelationConstruction.java b/src/fr/inrialpes/exmo/align/impl/edoal/RelationConstruction.java
index 679db57f..95c963b0 100644
--- a/src/fr/inrialpes/exmo/align/impl/edoal/RelationConstruction.java
+++ b/src/fr/inrialpes/exmo/align/impl/edoal/RelationConstruction.java
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2006 Digital Enterprise Research Insitute (DERI) Innsbruck
  * Sourceforge version 1.5 - 2006 - was RelationExpr
- * Copyright (C) INRIA, 2009-2010, 2012
+ * Copyright (C) INRIA, 2009-2010, 2012, 2015
  *
  * 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
@@ -22,8 +22,8 @@
 
 package fr.inrialpes.exmo.align.impl.edoal;
 
-import java.util.Collection;
-import java.util.HashSet;
+import java.util.List;
+import java.util.Vector;
 
 import fr.inrialpes.exmo.align.parser.SyntaxElement.Constructor;
 import fr.inrialpes.exmo.align.parser.SyntaxElement;
@@ -45,26 +45,24 @@ import fr.inrialpes.exmo.align.parser.TypeCheckingVisitor.TYPE;
 
 public class RelationConstruction extends RelationExpression {
 
-    /** Holds all expressions. */
-    private Collection<PathExpression> components;
+    /** Holds all expressions: ordered for composition */
+    private List<RelationExpression> components;
     
     /** Operator of this complex expression. */
     private Constructor operator;
     
     public RelationConstruction() {
 	super();
-	components = new HashSet<PathExpression>();
+	components = new Vector<RelationExpression>();
     }
 
-    public RelationConstruction( Constructor op, Collection<PathExpression> expressions ) {
+    public RelationConstruction( Constructor op, List<RelationExpression> expressions ) {
 	if ((expressions == null) || (op == null)) {
 	    throw new NullPointerException("The subexpressions and the operator must not be null");
 	}
 	if (expressions.contains(null)) {
 	    throw new IllegalArgumentException("The subexpressions must not contain null");
 	}
-	// The collection should have only relations
-	// It should be ordered: implement List
 	this.components = expressions;
 	if ( op != SyntaxElement.AND.getOperator() &&
 	     op != SyntaxElement.OR.getOperator() &&
@@ -94,11 +92,11 @@ public class RelationConstruction extends RelationExpression {
 	operator = op;
     }
 
-    public Collection<PathExpression> getComponents() {
+    public List<RelationExpression> getComponents() {
 	return components;
     }
 
-    public void addComponents( PathExpression exp ) {
+    public void addComponent( RelationExpression exp ) {
 	components.add( exp );
     }
 
diff --git a/src/fr/inrialpes/exmo/align/parser/RDFParser.java b/src/fr/inrialpes/exmo/align/parser/RDFParser.java
index 03a8cd60..be0e8b90 100644
--- a/src/fr/inrialpes/exmo/align/parser/RDFParser.java
+++ b/src/fr/inrialpes/exmo/align/parser/RDFParser.java
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2006 Digital Enterprise Research Insitute (DERI) Innsbruck
  * Sourceforge version 1.7 - 2008
- * Copyright (C) INRIA, 2008-2010, 2012-2014
+ * Copyright (C) INRIA, 2008-2010, 2012-2015
  *
  * 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
@@ -853,17 +853,17 @@ public class RDFParser {
         return new Datatype(uri);
     }
 
-    protected RelationExpression parseRelation(final Resource node) throws AlignmentException {
+    protected RelationExpression parseRelation( final Resource node ) throws AlignmentException {
         Resource rdfType = node.getProperty(RDF.type).getResource();
         Statement stmt = null;
-        if (rdfType.equals(SyntaxElement.RELATION_EXPR.resource)) {
-            URI id = getNodeId(node);
-            if (id != null) {
+        if ( rdfType.equals( SyntaxElement.RELATION_EXPR.resource ) ) {
+            URI id = getNodeId( node );
+            if ( id != null ) {
                 return new RelationId(id);
             } else {
                 Constructor op = null;
                 // Remains a PathExpression (that this is a relation is checked by typing)
-                List<PathExpression> clexpr = new LinkedList<PathExpression>();
+                List<RelationExpression> clexpr = new LinkedList<RelationExpression>();
                 if (node.hasProperty((Property) SyntaxElement.AND.resource)) {
                     op = SyntaxElement.AND.getOperator();
                     stmt = node.getProperty((Property) SyntaxElement.AND.resource);
diff --git a/test/src/EDOALExportTest.java b/test/src/EDOALExportTest.java
index c869d1b6..ca5aed04 100644
--- a/test/src/EDOALExportTest.java
+++ b/test/src/EDOALExportTest.java
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2006 Digital Enterprise Research Insitute (DERI) Innsbruck
  * Sourceforge version 1.3 -- 2007
- * Copyright (C) INRIA, 2009-2014
+ * Copyright (C) INRIA, 2009-2015
  *
  * 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
@@ -46,6 +46,7 @@ import java.io.OutputStreamWriter;
 
 import java.util.Collections;
 import java.util.LinkedHashSet;
+import java.util.Vector;
 import java.util.Set;
 import java.net.URI;
 
@@ -222,19 +223,23 @@ public class EDOALExportTest {
          "<edoal:Property rdf:about=\"http://my.beauty.url\"/>");
          assertEquals( render( Path.EMPTY ),"<edoal:Path rdf:resource=\"http://ns.inria.fr/edoal#emptyPath\"/>");
          */
-        final LinkedHashSet<PathExpression> expressions = new LinkedHashSet<PathExpression>(3);
+        final Vector<RelationExpression> expressions = new Vector<RelationExpression>();
         expressions.add(new RelationId(new URI("http://my.beauty.url")));
         expressions.add(new RelationId(new URI("http://my.nasty.url")));
         expressions.add(new RelationId(new URI("http://my.richi.url")));
-        assertEquals(render(new RelationConstruction(Constructor.COMP, expressions)),
+        assertEquals(render(new RelationConstruction(Constructor.COMP, (Vector<RelationExpression>)expressions)),
                 "<edoal:Relation><edoal:compose rdf:parseType=\"Collection\">"
                 + "<edoal:Relation rdf:about=\"http://my.beauty.url\"/>"
                 + "<edoal:Relation rdf:about=\"http://my.nasty.url\"/>"
                 + "<edoal:Relation rdf:about=\"http://my.richi.url\"/>"
                 + "</edoal:compose></edoal:Relation>");
 
-        expressions.add(new PropertyId(new URI("http://my.final.url")));
-        assertEquals(render(new PropertyConstruction(Constructor.COMP, expressions)),
+	final Vector<PathExpression> expr2 = new Vector<PathExpression>();
+	for ( PathExpression pe : expressions ) {
+	    expr2.add( (RelationExpression)pe );
+	}
+        expr2.add(new PropertyId(new URI("http://my.final.url")));
+        assertEquals(render(new PropertyConstruction(Constructor.COMP, expr2)),
                 "<edoal:Property><edoal:compose rdf:parseType=\"Collection\">"
                 + "<edoal:Relation rdf:about=\"http://my.beauty.url\"/>"
                 + "<edoal:Relation rdf:about=\"http://my.nasty.url\"/>"
@@ -367,7 +372,7 @@ public class EDOALExportTest {
 
     @Test(groups = {"full", "omwg", "raw"}, dependsOnMethods = {"setUp"})
     public void testExportPropertyExpr() throws Exception {
-        final Set<PathExpression> expressions = new LinkedHashSet<PathExpression>(2);
+        final Vector<PathExpression> expressions = new Vector<PathExpression>();
         expressions.add(new PropertyId(new URI("http://mein/super/property0")));
         expressions.add(new PropertyId(new URI("http://mein/super/property1")));
         final PropertyId single = new PropertyId(new URI("http://mein/super/property"));
@@ -380,7 +385,7 @@ public class EDOALExportTest {
                 + "<edoal:Property rdf:about=\"http://mein/super/property1\"/>"
                 + "</edoal:and></edoal:Property>");
 
-        final Set<PathExpression> expressions2 = new LinkedHashSet<PathExpression>(2);
+        final Vector<PathExpression> expressions2 = new Vector<PathExpression>();
         expressions2.add(new PropertyConstruction(Constructor.OR, expressions));
         expressions2.add(new PropertyValueRestriction(Comparator.EQUAL, new Value("5")));
         toExport = new PropertyConstruction(Constructor.AND, expressions2);
@@ -392,7 +397,7 @@ public class EDOALExportTest {
                 + "<edoal:comparator rdf:resource=\"http://ns.inria.org/edoal/1.0/#equals\"/>"
                 + "<edoal:value><edoal:Literal edoal:string=\"5\"/></edoal:value></edoal:PropertyValueRestriction>"
                 + "</edoal:and></edoal:Property>");
-        toExport = new PropertyConstruction(Constructor.NOT, Collections.singleton((PathExpression) new PropertyId(new URI("http://mein/super/property"))));
+        toExport = new PropertyConstruction(Constructor.NOT, new Vector<PathExpression>(Collections.singleton((PathExpression) new PropertyId(new URI("http://mein/super/property")))));
     }
 
     // ------
@@ -418,7 +423,7 @@ public class EDOALExportTest {
         // JE 2010: I could export it as well
         RelationExpression relexp = new RelationDomainRestriction(new ClassId("http://my/super/class"));
 
-        final Set<PathExpression> expressions = new LinkedHashSet<PathExpression>(2);
+        final Vector<RelationExpression> expressions = new Vector<RelationExpression>();
         expressions.add(new RelationId("http://my/super/relation0"));
         expressions.add(new RelationId("http://my/super/relation1"));
         expressions.add(relexp);
@@ -444,8 +449,8 @@ public class EDOALExportTest {
                 + "</edoal:class></edoal:RelationDomainRestriction>"
                 + "</edoal:or>" + "</edoal:Relation>");
 
-        final Set<PathExpression> expressions2 = new LinkedHashSet<PathExpression>();
-        expressions2.add(new RelationConstruction(Constructor.NOT, Collections.singleton((PathExpression) new RelationId("http://my/super/relation"))));
+        final Vector<RelationExpression> expressions2 = new Vector<RelationExpression>();
+        expressions2.add(new RelationConstruction(Constructor.NOT, new Vector<RelationExpression>(Collections.singleton((RelationExpression) new RelationId("http://my/super/relation")))));
         expressions2.add(new RelationCoDomainRestriction(new ClassId("http://my/super/class")));
 
         toExport = new RelationConstruction(Constructor.AND, expressions2);
@@ -459,25 +464,25 @@ public class EDOALExportTest {
                 + "<edoal:Class rdf:about=\"http://my/super/class\"/>"
                 + "</edoal:class></edoal:RelationCoDomainRestriction>"
                 + "</edoal:and>" + "</edoal:Relation>");
-        toExport = new RelationConstruction(Constructor.INVERSE, Collections.singleton((PathExpression) new RelationId("http://my/super/relation")));
+        toExport = new RelationConstruction(Constructor.INVERSE, new Vector<RelationExpression>(Collections.singleton((RelationExpression) new RelationId("http://my/super/relation"))));
         assertEquals(render(toExport),
                 "<edoal:Relation>"
                 + "<edoal:inverse>"
                 + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>"
                 + "</edoal:inverse>" + "</edoal:Relation>");
-        toExport = new RelationConstruction(Constructor.SYMMETRIC, Collections.singleton((PathExpression) new RelationId("http://my/super/relation")));
+        toExport = new RelationConstruction(Constructor.SYMMETRIC, new Vector<RelationExpression>(Collections.singleton((RelationExpression) new RelationId("http://my/super/relation"))));
         assertEquals(render(toExport),
                 "<edoal:Relation>"
                 + "<edoal:symmetric>"
                 + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>"
                 + "</edoal:symmetric>" + "</edoal:Relation>");
-        toExport = new RelationConstruction(Constructor.TRANSITIVE, Collections.singleton((PathExpression) new RelationId("http://my/super/relation")));
+        toExport = new RelationConstruction(Constructor.TRANSITIVE, new Vector<RelationExpression>(Collections.singleton((RelationExpression) new RelationId("http://my/super/relation"))));
         assertEquals(render(toExport),
                 "<edoal:Relation>"
                 + "<edoal:transitive>"
                 + "<edoal:Relation rdf:about=\"http://my/super/relation\"/>"
                 + "</edoal:transitive>" + "</edoal:Relation>");
-        toExport = new RelationConstruction(Constructor.REFLEXIVE, Collections.singleton((PathExpression) new RelationId("http://my/super/relation")));
+        toExport = new RelationConstruction(Constructor.REFLEXIVE, new Vector<RelationExpression>(Collections.singleton((RelationExpression) new RelationId("http://my/super/relation"))));
         assertEquals(render(toExport),
                 "<edoal:Relation>"
                 + "<edoal:reflexive>"
diff --git a/test/src/SPARQLConstructVisitorTest.java b/test/src/SPARQLConstructVisitorTest.java
index d30cd969..3231a355 100644
--- a/test/src/SPARQLConstructVisitorTest.java
+++ b/test/src/SPARQLConstructVisitorTest.java
@@ -1,7 +1,7 @@
 /*
  * $Id: READMETest.java 1985 2014-11-09 16:50:18Z euzenat $
  *
- * Copyright (C) INRIA, 2014
+ * Copyright (C) INRIA, 2014-2015
  *
  * 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
-- 
GitLab