]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
OrderedSet support (graph mappings)
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 4 Dec 2013 06:18:08 +0000 (06:18 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 4 Dec 2013 06:18:08 +0000 (06:18 +0000)
Code cleanup.

fixes #4578

git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@28462 ac1ea38d-2e2b-0410-8846-a27921b304fc

20 files changed:
org.simantics.objmap2/src/org/simantics/objmap/graph/Mappings.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/CompoundRelatedGetValue.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/GetType.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OptionalRelatedElements.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsAdd.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsGet.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsRem.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElement.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElements.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElementsGet.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedOrderedSetElements.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedValue.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/SetType.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/UpdateMethod.java
org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/OrderedElementsRuleFactory.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/graph/schema/AdaptedLinkType.java
org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DynamicSimpleLinkType.java
org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java

index c9aef6bfba5f263a88b7aa7f991355fa8f2b33ff..ef609bc7426b7a47f0732d782c97d09f7d6a826c 100644 (file)
@@ -11,7 +11,6 @@
  *******************************************************************************/\r
 package org.simantics.objmap.graph;\r
 \r
-import org.simantics.db.Resource;\r
 import org.simantics.objmap.graph.impl.Mapping;\r
 import org.simantics.objmap.graph.schema.IMappingSchema;\r
 /**\r
index 039fe7e5fabd400edd22ce5846517641a0b450f9..f20199efed8c382e68400b44e6b8b3913877f607 100644 (file)
@@ -17,9 +17,6 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;\r
 \r
 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;\r
-import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;\r
-import org.simantics.objmap.graph.rules.adapters.ValueAdapter;\r
-\r
 \r
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
@@ -30,5 +27,4 @@ public @interface CompoundRelatedGetValue {
        String objRelation();\r
        String objType();\r
        String valRelation();\r
-//     Class<? extends ValueAdapter> adapter() default IdentityAdapter.class;\r
 }\r
index bd18ca19cc256c90056e202b3b8e3cf42ae377cd..97542a33934f34199d1014855801a461d39be6e4 100644 (file)
@@ -20,7 +20,6 @@ import java.lang.annotation.Target;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.METHOD)\r
-//@HasGetSetRuleFactory(RelatedGetSetValueRuleFactory.class)\r
 public @interface GetType {\r
        String value();\r
 }\r
index bc6988d0e30f5b12d70df8a9ccc862feddc44d3e..297adaa6239a14d682ff84163ffa4eb253eaf8ed 100644 (file)
@@ -22,7 +22,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(OptionalRelatedElementsRuleFactory.class)\r
 @IsFieldRule\r
 public @interface OptionalRelatedElements {\r
        String value();\r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsAdd.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsAdd.java
new file mode 100644 (file)
index 0000000..08aa7fb
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+public @interface OrderedElementsAdd {\r
+}\r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsGet.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsGet.java
new file mode 100644 (file)
index 0000000..88d3d8e
--- /dev/null
@@ -0,0 +1,31 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;\r
+\r
+\r
+\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+@IsCollectionRule\r
+@HasCollectionAdder(OrderedElementsAdd.class)\r
+@HasCollectionRemover(OrderedElementsRem.class)\r
+public @interface OrderedElementsGet {\r
+       boolean composition() default false;\r
+}\r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsRem.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsRem.java
new file mode 100644 (file)
index 0000000..c922f79
--- /dev/null
@@ -0,0 +1,23 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+public @interface OrderedElementsRem {\r
+}\r
index 9d99f4d72fe7e1f7bfd576437bd3680c5b9a72a7..36c74aa3dff6baab900b8bfce1228479143a5b0c 100644 (file)
@@ -20,7 +20,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.FIELD)\r
-//HasFieldRuleFactory(RelatedElementRuleFactory.class)\r
 @IsFieldRule\r
 public @interface RelatedElement {\r
        String value();\r
index f7de2b36144ce1f6f2201c22e740b973dc4099ce..6cbf6e11c66e0791668ab96c6c194200cf9ac09f 100644 (file)
@@ -21,7 +21,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(RelatedElementsRuleFactory.class)\r
 @IsFieldRule\r
 public @interface RelatedElements {\r
        String value();\r
index 95d9e40ee961f091a9ad48b7acad83bc04b10859..46b1f0355ecb2da91a2b7e114b960bb0a161a421 100644 (file)
@@ -23,7 +23,6 @@ import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.METHOD)\r
-//@HasCollectionRuleFactory(RelatedElementsRuleFactory2.class)\r
 @IsCollectionRule\r
 @HasCollectionAdder(RelatedElementsAdd.class)\r
 @HasCollectionRemover(RelatedElementsRem.class)\r
index 183f1c44b7e775c1f52aba0c8f49e3cf2a3534c2..d437288a5be4cc5329d16481cfd966b57c819180 100644 (file)
@@ -21,7 +21,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(RelatedOrderedSetElementsRuleFactory.class)\r
 @IsFieldRule\r
 public @interface RelatedOrderedSetElements {\r
        boolean composition() default false;\r
index b28516892bcfcf0ff7474086d25f238e6f2d0e51..8f61c2f15c8ded79f1add40b2a8d19523277a0a3 100644 (file)
@@ -29,7 +29,6 @@ import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
  */\r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(RelatedValueRuleFactory.class)\r
 @IsFieldRule\r
 public @interface RelatedValue {\r
     String value();\r
index 2c6f4d3865e51a4ee528c55dbe03fbaa6d63af18..c3f8b4d850a87f80b619e9495c21994acc6538e7 100644 (file)
@@ -20,7 +20,6 @@ import java.lang.annotation.Target;
 \r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.METHOD)\r
-//@HasGetSetRuleFactory(RelatedGetSetValueRuleFactory.class)\r
 public @interface SetType {\r
        String value();\r
 }\r
index 1c8802e8757ace3e083e17b304b2c07e021d3e21..841340a65a814cb974982c66add0f401c38dd864 100644 (file)
@@ -27,7 +27,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
  */\r
 @Retention(RetentionPolicy.RUNTIME)\r
 @Target(ElementType.METHOD)\r
-//@HasMethodRuleFactory(UpdateMethodFactory.class)\r
 @IsFieldRule\r
 public @interface UpdateMethod {\r
 }\r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/OrderedElementsRuleFactory.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/OrderedElementsRuleFactory.java
new file mode 100644 (file)
index 0000000..aeaea5f
--- /dev/null
@@ -0,0 +1,55 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.graph.annotations.factories;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Method;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsAdd;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsGet;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsRem;\r
+import org.simantics.objmap.graph.rules.MappedElementsRule;\r
+import org.simantics.objmap.graph.rules.domain.RelatedOrderedSetElementsAccessor;\r
+import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;\r
+import org.simantics.objmap.graph.rules.range.ListAccessor;\r
+\r
+\r
+public class OrderedElementsRuleFactory<Range> implements ICollectionRuleFactory<Resource,Range> {\r
+       \r
+       @Override\r
+       public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,\r
+                       Method getter, Method adder, Method remover)\r
+                       throws DatabaseException {\r
+               OrderedElementsGet getterAnn = (OrderedElementsGet)annotation;\r
+               return new MappedElementsRule<Resource,Range>(new RelatedOrderedSetElementsAccessor(getterAnn.composition()),\r
+                                       new ListAccessor<Range,Range>(getter, adder, remover));\r
+       }\r
+       \r
+       @Override\r
+       public boolean isAdder(Annotation getterAnnotation, Annotation annotation) {\r
+               OrderedElementsGet getterAnn = (OrderedElementsGet)getterAnnotation;\r
+               OrderedElementsAdd adderAnn = (OrderedElementsAdd)annotation;\r
+               return true;\r
+       }\r
+       \r
+       @Override\r
+       public boolean isRemover(Annotation getterAnnotation, Annotation annotation) {\r
+               OrderedElementsGet getterAnn = (OrderedElementsGet)getterAnnotation;\r
+               OrderedElementsRem adderAnn = (OrderedElementsRem)annotation;\r
+               return true;\r
+       }\r
+\r
+}\r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java
new file mode 100644 (file)
index 0000000..5f79c58
--- /dev/null
@@ -0,0 +1,94 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.graph.rules.range;\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.objmap.exceptions.MappingException;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+\r
+/**\r
+ * Accessor for mapped collections. \r
+ * Uses three methods:\r
+ * - Getter: returns the collection.\r
+ * - Adder: adds one item into the collection.\r
+ * - Remover: removes one item from the collection. \r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ * @param <T>\r
+ */\r
+public class ListAccessor<Range,T>  implements IRangeAccessor<Range,Collection<T>> {\r
+       \r
+       private Method getter;\r
+       private Method adder;\r
+       private Method remover;\r
+       \r
+       public ListAccessor(Method getter, Method adder, Method remover) {\r
+               this.getter = getter;\r
+               this.adder = adder;\r
+               this.remover = remover;\r
+       }\r
+       \r
+       @SuppressWarnings("unchecked")\r
+       public java.util.Collection<T> get(Object element) throws MappingException {\r
+               try {\r
+                       return (Collection<T>) getter.invoke(element);\r
+               } catch (IllegalArgumentException e) {\r
+                       throw new MappingException(e);\r
+               } catch (IllegalAccessException e) {\r
+                       throw new MappingException(e);\r
+               } catch (InvocationTargetException e) {\r
+                       throw new MappingException(e);\r
+               }\r
+       };\r
+\r
+       @Override\r
+       public boolean set(Range element, Collection<T> value)\r
+                       throws MappingException {\r
+               java.util.Collection<T> current = get(element);\r
+               Collection<Pair<Integer, T>> adding = new ArrayList<Pair<Integer, T>>();\r
+               Collection<T> removing = new ArrayList<T>();\r
+               for (T e : current) {\r
+                       if (!value.contains(e))\r
+                               removing.add(e);\r
+               }\r
+               int i = 0;\r
+               for (T e : value) {\r
+                       if (!current.contains(e))\r
+                               adding.add(new Pair<Integer, T>(i, e));\r
+                       i++;\r
+               }\r
+               \r
+               try {\r
+                       for (T e : removing) {\r
+                               remover.invoke(element, e);\r
+                       }\r
+                       \r
+                       for (Pair<Integer,T> e : adding) {\r
+                               adder.invoke(element, e.first,e.second);\r
+                       }\r
+               } catch (IllegalArgumentException e) {\r
+                       throw new MappingException(e);\r
+               } catch (IllegalAccessException e) {\r
+                       throw new MappingException(e);\r
+               } catch (InvocationTargetException e) {\r
+                       throw new MappingException(e);\r
+               }\r
+               return removing.size() > 0 || adding.size() > 0;\r
+               \r
+       }\r
+}\r
index 2d8494d8f12f6f68939a96a70eebf2cdf24c18c8..d66e4f1bbed63812c82b5f617164d26d8f3eb528 100644 (file)
@@ -58,7 +58,8 @@ public class AdaptedLinkType<Range> implements ILinkType<Resource,Range> {
        \r
     }\r
     \r
-    @Override\r
+    @SuppressWarnings("unchecked")\r
+       @Override\r
     public Range createRangeElement(ReadGraph g, Resource domainElement)\r
                throws MappingException {\r
        try {\r
index ff8622b8189eb25052d2ae67dd322e81690d3e60..4205f2c4535fd933f89ca55ebf2b842b1d5890cf 100644 (file)
@@ -90,7 +90,8 @@ public class DynamicSimpleLinkType<Range> extends SimpleLinkType<Range>{
                }\r
     }\r
        \r
-        @Override\r
+        @SuppressWarnings("unchecked")\r
+       @Override\r
            public Range createRangeElement(ReadGraph g, Resource domainElement)\r
                    throws MappingException {\r
                try {\r
index b91928492bf52a7609d7ebce30fe366b7fb48fc5..b863fbadca0d1b2924433edef63266abfa632b8c 100644 (file)
@@ -28,6 +28,7 @@ import org.simantics.objmap.graph.annotations.HasCollectionAdder;
 import org.simantics.objmap.graph.annotations.HasCollectionRemover;\r
 import org.simantics.objmap.graph.annotations.HasSetter;\r
 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsGet;\r
 import org.simantics.objmap.graph.annotations.OrderedSetType;\r
 import org.simantics.objmap.graph.annotations.RelatedElements;\r
 import org.simantics.objmap.graph.annotations.RelatedElementsGet;\r
@@ -38,6 +39,7 @@ import org.simantics.objmap.graph.annotations.RelatedValue;
 import org.simantics.objmap.graph.annotations.UpdateMethod;\r
 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;\r
 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;\r
 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;\r
 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;\r
 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;\r
@@ -236,6 +238,8 @@ public class MappingSchemas {
        public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {\r
                if (annotation.annotationType().equals(RelatedElementsGet.class))\r
                        return new RelatedElementsRuleFactory2<Object>();\r
+               if (annotation.annotationType().equals(OrderedElementsGet.class))\r
+                       return new OrderedElementsRuleFactory<Object>();\r
                return null;\r
        }\r
        \r
index 15abf842afed61f025778254e6f3ac60c501cccc..880d117aad2d6a30b91270d1cf2b995433064be5 100644 (file)
@@ -74,7 +74,8 @@ public class SimpleLinkType<Range> implements ILinkType<Resource,Range> {
             throw new MappingException(e);\r
         }\r
     }\r
-    @Override\r
+    @SuppressWarnings("unchecked")\r
+       @Override\r
     public Range createRangeElement(ReadGraph g, Resource domainElement)\r
             throws MappingException {\r
         try {\r