From: luukkainen Date: Wed, 4 Dec 2013 06:18:08 +0000 (+0000) Subject: OrderedSet support (graph mappings) X-Git-Tag: simantics-1.19.0~2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d08b28566d477295d9f14023f36de9f7406cdb5d;p=simantics%2F3d.git OrderedSet support (graph mappings) Code cleanup. fixes #4578 git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@28462 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/Mappings.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/Mappings.java index c9aef6bf..ef609bc7 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/Mappings.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/Mappings.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.simantics.objmap.graph; -import org.simantics.db.Resource; import org.simantics.objmap.graph.impl.Mapping; import org.simantics.objmap.graph.schema.IMappingSchema; /** diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/CompoundRelatedGetValue.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/CompoundRelatedGetValue.java index 039fe7e5..f20199ef 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/CompoundRelatedGetValue.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/CompoundRelatedGetValue.java @@ -17,9 +17,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.simantics.objmap.graph.annotations.meta.IsGetSetRule; -import org.simantics.objmap.graph.rules.adapters.IdentityAdapter; -import org.simantics.objmap.graph.rules.adapters.ValueAdapter; - @Retention(RetentionPolicy.RUNTIME) @@ -30,5 +27,4 @@ public @interface CompoundRelatedGetValue { String objRelation(); String objType(); String valRelation(); -// Class adapter() default IdentityAdapter.class; } diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/GetType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/GetType.java index bd18ca19..97542a33 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/GetType.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/GetType.java @@ -20,7 +20,6 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) -//@HasGetSetRuleFactory(RelatedGetSetValueRuleFactory.class) public @interface GetType { String value(); } diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OptionalRelatedElements.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OptionalRelatedElements.java index bc6988d0..297adaa6 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OptionalRelatedElements.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OptionalRelatedElements.java @@ -22,7 +22,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) -//@HasFieldRuleFactory(OptionalRelatedElementsRuleFactory.class) @IsFieldRule public @interface OptionalRelatedElements { String value(); 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 index 00000000..08aa7fb4 --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsAdd.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface OrderedElementsAdd { +} 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 index 00000000..88d3d8e1 --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsGet.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.simantics.objmap.graph.annotations.meta.IsCollectionRule; + + + + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@IsCollectionRule +@HasCollectionAdder(OrderedElementsAdd.class) +@HasCollectionRemover(OrderedElementsRem.class) +public @interface OrderedElementsGet { + boolean composition() default false; +} 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 index 00000000..c922f79f --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedElementsRem.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface OrderedElementsRem { +} diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElement.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElement.java index 9d99f4d7..36c74aa3 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElement.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElement.java @@ -20,7 +20,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) -//HasFieldRuleFactory(RelatedElementRuleFactory.class) @IsFieldRule public @interface RelatedElement { String value(); diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElements.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElements.java index f7de2b36..6cbf6e11 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElements.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElements.java @@ -21,7 +21,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) -//@HasFieldRuleFactory(RelatedElementsRuleFactory.class) @IsFieldRule public @interface RelatedElements { String value(); diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElementsGet.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElementsGet.java index 95d9e40e..46b1f035 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElementsGet.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedElementsGet.java @@ -23,7 +23,6 @@ import org.simantics.objmap.graph.annotations.meta.IsCollectionRule; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) -//@HasCollectionRuleFactory(RelatedElementsRuleFactory2.class) @IsCollectionRule @HasCollectionAdder(RelatedElementsAdd.class) @HasCollectionRemover(RelatedElementsRem.class) diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedOrderedSetElements.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedOrderedSetElements.java index 183f1c44..d437288a 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedOrderedSetElements.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedOrderedSetElements.java @@ -21,7 +21,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) -//@HasFieldRuleFactory(RelatedOrderedSetElementsRuleFactory.class) @IsFieldRule public @interface RelatedOrderedSetElements { boolean composition() default false; diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedValue.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedValue.java index b2851689..8f61c2f1 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedValue.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/RelatedValue.java @@ -29,7 +29,6 @@ import org.simantics.objmap.graph.rules.adapters.ValueAdapter; */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) -//@HasFieldRuleFactory(RelatedValueRuleFactory.class) @IsFieldRule public @interface RelatedValue { String value(); diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/SetType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/SetType.java index 2c6f4d38..c3f8b4d8 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/SetType.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/SetType.java @@ -20,7 +20,6 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) -//@HasGetSetRuleFactory(RelatedGetSetValueRuleFactory.class) public @interface SetType { String value(); } diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/UpdateMethod.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/UpdateMethod.java index 1c8802e8..841340a6 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/UpdateMethod.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/UpdateMethod.java @@ -27,7 +27,6 @@ import org.simantics.objmap.graph.annotations.meta.IsFieldRule; */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) -//@HasMethodRuleFactory(UpdateMethodFactory.class) @IsFieldRule public @interface UpdateMethod { } 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 index 00000000..aeaea5fc --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/OrderedElementsRuleFactory.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.annotations.factories; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.bidirectional.IBidirectionalMappingRule; +import org.simantics.objmap.graph.annotations.OrderedElementsAdd; +import org.simantics.objmap.graph.annotations.OrderedElementsGet; +import org.simantics.objmap.graph.annotations.OrderedElementsRem; +import org.simantics.objmap.graph.rules.MappedElementsRule; +import org.simantics.objmap.graph.rules.domain.RelatedOrderedSetElementsAccessor; +import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory; +import org.simantics.objmap.graph.rules.range.ListAccessor; + + +public class OrderedElementsRuleFactory implements ICollectionRuleFactory { + + @Override + public IBidirectionalMappingRule create(ReadGraph g, Annotation annotation, + Method getter, Method adder, Method remover) + throws DatabaseException { + OrderedElementsGet getterAnn = (OrderedElementsGet)annotation; + return new MappedElementsRule(new RelatedOrderedSetElementsAccessor(getterAnn.composition()), + new ListAccessor(getter, adder, remover)); + } + + @Override + public boolean isAdder(Annotation getterAnnotation, Annotation annotation) { + OrderedElementsGet getterAnn = (OrderedElementsGet)getterAnnotation; + OrderedElementsAdd adderAnn = (OrderedElementsAdd)annotation; + return true; + } + + @Override + public boolean isRemover(Annotation getterAnnotation, Annotation annotation) { + OrderedElementsGet getterAnn = (OrderedElementsGet)getterAnnotation; + OrderedElementsRem adderAnn = (OrderedElementsRem)annotation; + return true; + } + +} 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 index 00000000..5f79c585 --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/range/ListAccessor.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.rules.range; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.objmap.exceptions.MappingException; +import org.simantics.utils.datastructures.Pair; + + +/** + * Accessor for mapped collections. + * Uses three methods: + * - Getter: returns the collection. + * - Adder: adds one item into the collection. + * - Remover: removes one item from the collection. + * + * @author Marko Luukkainen + * + * @param + */ +public class ListAccessor implements IRangeAccessor> { + + private Method getter; + private Method adder; + private Method remover; + + public ListAccessor(Method getter, Method adder, Method remover) { + this.getter = getter; + this.adder = adder; + this.remover = remover; + } + + @SuppressWarnings("unchecked") + public java.util.Collection get(Object element) throws MappingException { + try { + return (Collection) getter.invoke(element); + } catch (IllegalArgumentException e) { + throw new MappingException(e); + } catch (IllegalAccessException e) { + throw new MappingException(e); + } catch (InvocationTargetException e) { + throw new MappingException(e); + } + }; + + @Override + public boolean set(Range element, Collection value) + throws MappingException { + java.util.Collection current = get(element); + Collection> adding = new ArrayList>(); + Collection removing = new ArrayList(); + for (T e : current) { + if (!value.contains(e)) + removing.add(e); + } + int i = 0; + for (T e : value) { + if (!current.contains(e)) + adding.add(new Pair(i, e)); + i++; + } + + try { + for (T e : removing) { + remover.invoke(element, e); + } + + for (Pair e : adding) { + adder.invoke(element, e.first,e.second); + } + } catch (IllegalArgumentException e) { + throw new MappingException(e); + } catch (IllegalAccessException e) { + throw new MappingException(e); + } catch (InvocationTargetException e) { + throw new MappingException(e); + } + return removing.size() > 0 || adding.size() > 0; + + } +} diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/AdaptedLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/AdaptedLinkType.java index 2d8494d8..d66e4f1b 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/AdaptedLinkType.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/AdaptedLinkType.java @@ -58,7 +58,8 @@ public class AdaptedLinkType implements ILinkType { } - @Override + @SuppressWarnings("unchecked") + @Override public Range createRangeElement(ReadGraph g, Resource domainElement) throws MappingException { try { diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DynamicSimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DynamicSimpleLinkType.java index ff8622b8..4205f2c4 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DynamicSimpleLinkType.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DynamicSimpleLinkType.java @@ -90,7 +90,8 @@ public class DynamicSimpleLinkType extends SimpleLinkType{ } } - @Override + @SuppressWarnings("unchecked") + @Override public Range createRangeElement(ReadGraph g, Resource domainElement) throws MappingException { try { diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java index b9192849..b863fbad 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java @@ -28,6 +28,7 @@ import org.simantics.objmap.graph.annotations.HasCollectionAdder; import org.simantics.objmap.graph.annotations.HasCollectionRemover; import org.simantics.objmap.graph.annotations.HasSetter; import org.simantics.objmap.graph.annotations.OptionalRelatedElements; +import org.simantics.objmap.graph.annotations.OrderedElementsGet; import org.simantics.objmap.graph.annotations.OrderedSetType; import org.simantics.objmap.graph.annotations.RelatedElements; import org.simantics.objmap.graph.annotations.RelatedElementsGet; @@ -38,6 +39,7 @@ import org.simantics.objmap.graph.annotations.RelatedValue; import org.simantics.objmap.graph.annotations.UpdateMethod; import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory; import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory; +import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory; import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory; import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2; import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory; @@ -236,6 +238,8 @@ public class MappingSchemas { public static ICollectionRuleFactory createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) { if (annotation.annotationType().equals(RelatedElementsGet.class)) return new RelatedElementsRuleFactory2(); + if (annotation.annotationType().equals(OrderedElementsGet.class)) + return new OrderedElementsRuleFactory(); return null; } diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java index 15abf842..880d117a 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java @@ -74,7 +74,8 @@ public class SimpleLinkType implements ILinkType { throw new MappingException(e); } } - @Override + @SuppressWarnings("unchecked") + @Override public Range createRangeElement(ReadGraph g, Resource domainElement) throws MappingException { try {