Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 291361
Collapse All | Expand All

(-)META-INF/MANIFEST.MF (+2 lines)
Lines 14-19 Link Here
14
Bundle-ActivationPolicy: lazy
14
Bundle-ActivationPolicy: lazy
15
Export-Package: org.eclipse.ocl.ecore,
15
Export-Package: org.eclipse.ocl.ecore,
16
 org.eclipse.ocl.ecore.impl,
16
 org.eclipse.ocl.ecore.impl,
17
 org.eclipse.ocl.ecore.dynamic,
17
 org.eclipse.ocl.ecore.internal;x-friends:="org.eclipse.ocl.ecore.tests",
18
 org.eclipse.ocl.ecore.internal;x-friends:="org.eclipse.ocl.ecore.tests",
19
 org.eclipse.ocl.ecore.internal.l10n,
18
 org.eclipse.ocl.ecore.util
20
 org.eclipse.ocl.ecore.util
19
Bundle-RequiredExecutionEnvironment: J2SE-1.5
21
Bundle-RequiredExecutionEnvironment: J2SE-1.5
(-)src/org/eclipse/ocl/ecore/internal/l10n/OCLEcoreMessages.properties (+22 lines)
Added Link Here
1
###############################################################################
2
# Copyright (c) 2008 Zeligsoft Inc. and others.
3
# All rights reserved. This program and the accompanying materials
4
# are made available under the terms of the Eclipse Public License v1.0
5
# which accompanies this distribution, and is available at
6
# http://www.eclipse.org/legal/epl-v10.html
7
#
8
# Contributors:
9
#     Zeligsoft - initial API and implementation
10
#
11
# $Id$
12
#
13
###############################################################################
14
15
#
16
# Indicates an invalid derivation expression for an EStructuralFeature
17
# in the OCL setting delegate annotation
18
#
19
#   0 - the EClass name that owns the feature
20
#   1 - the EStructuralFeature name
21
#   2 - the OCL parser's error message
22
bad_attribute_derivation = Invalid OCL attribute derivation for {0}.{1}: {2}
(-)src/org/eclipse/ocl/ecore/internal/l10n/IMessages.java (+65 lines)
Added Link Here
1
/**
2
 * <copyright>
3
 *
4
 * Copyright (c) 2006, 2008 IBM Corporation, Zeligsoft Inc., and others.
5
 * All rights reserved.   This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 *
10
 * Contributors:
11
 *   IBM - Initial API and implementation
12
 *   Zeligsoft - Bug 216701
13
 *
14
 * </copyright>
15
 *
16
 * $Id: IMessages.java,v 1.2 2007/02/14 14:46:07 cdamus Exp $
17
 */
18
package org.eclipse.ocl.ecore.internal.l10n;
19
20
import java.text.MessageFormat;
21
22
/**
23
 * Abstraction of the OSGi runtime's NLS class, to abstract the dependency on
24
 * Eclipse platform.
25
 *
26
 * @author Christian W. Damus (cdamus)
27
 */
28
interface IMessages {
29
	String bind(String message, Object arg);
30
	String bind(String message, Object arg1, Object arg2);
31
	String bind(String message, Object[] args);
32
	
33
	class Default implements IMessages {
34
		public String bind(String message, Object arg) {
35
			return MessageFormat.format(message, new Object[] {arg});
36
		}
37
38
		public String bind(String message, Object arg1, Object arg2) {
39
			return MessageFormat.format(message, new Object[] {arg1, arg2});
40
		}
41
42
		public String bind(String message, Object[] args) {
43
			return MessageFormat.format(message, args);
44
		}
45
	}
46
	
47
	class NLS implements IMessages {
48
		public NLS() {
49
			org.eclipse.osgi.util.NLS.initializeMessages(
50
					OCLEcoreMessages.BUNDLE_NAME, OCLEcoreMessages.class);
51
		}
52
53
		public String bind(String message, Object arg) {
54
			return org.eclipse.osgi.util.NLS.bind(message, arg);
55
		}
56
57
		public String bind(String message, Object arg1, Object arg2) {
58
			return org.eclipse.osgi.util.NLS.bind(message, arg1, arg2);
59
		}
60
61
		public String bind(String message, Object[] args) {
62
			return org.eclipse.osgi.util.NLS.bind(message, args);
63
		}
64
	}
65
}
(-)src/org/eclipse/ocl/ecore/dynamic/OCLSettingDelegate.java (+127 lines)
Added Link Here
1
/**
2
 * <copyright>
3
 * 
4
 * Copyright (c) 2008 Zeligsoft Inc. and others.
5
 * All rights reserved.   This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 * 
10
 * Contributors:
11
 *   Zeligsoft - Initial API and implementation
12
 * 
13
 * </copyright>
14
 *
15
 * $Id$
16
 */
17
18
package org.eclipse.ocl.ecore.dynamic;
19
20
import java.util.Collection;
21
22
import org.eclipse.emf.common.util.BasicEList;
23
import org.eclipse.emf.common.util.ECollections;
24
import org.eclipse.emf.ecore.EClass;
25
import org.eclipse.emf.ecore.EClassifier;
26
import org.eclipse.emf.ecore.EPackage;
27
import org.eclipse.emf.ecore.EStructuralFeature;
28
import org.eclipse.emf.ecore.EcorePackage;
29
import org.eclipse.emf.ecore.InternalEObject;
30
import org.eclipse.emf.ecore.util.BasicSettingDelegate;
31
import org.eclipse.emf.ecore.util.EcoreUtil;
32
import org.eclipse.ocl.ParserException;
33
import org.eclipse.ocl.ecore.OCL;
34
import org.eclipse.ocl.expressions.OCLExpression;
35
36
/**
37
 * An implementation of a setting delegate that computes OCL derived features.
38
 * 
39
 * @author Christian W. Damus (cdamus)
40
 * @since 1.3
41
 */
42
public class OCLSettingDelegate
43
		extends BasicSettingDelegate.Stateless {
44
45
	private OCL ocl;
46
47
	private OCLExpression<EClassifier> query;
48
49
	private ValueConverter converter;
50
51
	/**
52
	 * Initializes me with my structural feature.
53
	 * 
54
	 * @param structuralFeature
55
	 *            the structural feature that I handle
56
	 * 
57
	 * @throws ParserException
58
	 *             if the structural feature's OCL derivation is invalid
59
	 */
60
	protected OCLSettingDelegate(EStructuralFeature structuralFeature,
61
			OCLSettingDelegateFactory.OCLCache oclCache)
62
			throws ParserException {
63
64
		super(structuralFeature);
65
66
		EClass context = structuralFeature.getEContainingClass();
67
		EPackage ePackage = context.getEPackage();
68
69
		this.ocl = oclCache.getOCL(ePackage);
70
71
		OCL.Helper helper = ocl.createOCLHelper();
72
		helper.setAttributeContext(context, structuralFeature);
73
74
		String expr = EcoreUtil.getAnnotation(structuralFeature,
75
			EcorePackage.eNS_URI, "OCL"); //$NON-NLS-1$
76
77
		this.query = helper.createDerivedValueExpression(expr)
78
			.getSpecification().getBodyExpression();
79
80
		this.converter = structuralFeature.isMany()
81
			? ValueConverter.LIST
82
			: ValueConverter.VERBATIM;
83
	}
84
85
	@Override
86
	protected Object get(InternalEObject owner, boolean resolve,
87
			boolean coreType) {
88
89
		return converter.convert(ocl, ocl.evaluate(owner, query));
90
	}
91
92
	@Override
93
	protected boolean isSet(InternalEObject owner) {
94
		return false; // derived features are, implicitly, never set
95
	}
96
97
	//
98
	// Nested classes
99
	//
100
101
	private static interface ValueConverter {
102
103
		ValueConverter VERBATIM = new ValueConverter() {
104
105
			public Object convert(OCL ocl, Object value) {
106
				// no way to report problem to the user in invalid case
107
				return ocl.isInvalid(value)? null : value;
108
			}
109
		};
110
111
		ValueConverter LIST = new ValueConverter() {
112
113
			public Object convert(OCL ocl, Object value) {
114
				if (ocl.isInvalid(value)) {
115
					// no way to report specific problem to the user
116
					return ECollections.EMPTY_ELIST;
117
				}
118
				
119
				Collection<?> collection = (Collection<?>) value;
120
				return new BasicEList.UnmodifiableEList<Object>(collection
121
					.size(), collection.toArray());
122
			}
123
		};
124
125
		Object convert(OCL ocl, Object value);
126
	}
127
}
(-)src/org/eclipse/ocl/ecore/internal/l10n/OCLEcoreMessages.java (+101 lines)
Added Link Here
1
/**
2
 * <copyright>
3
 *
4
 * Copyright (c) 2005, 2008 IBM Corporation, Zeligsoft Inc., and others.
5
 * All rights reserved.   This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 *
10
 * Contributors:
11
 *   IBM - Initial API and implementation
12
 *   Zeligsoft - Bug 216701
13
 *
14
 * </copyright>
15
 *
16
 * $Id: OCLMessages.java,v 1.6 2007/12/12 22:08:04 cdamus Exp $
17
 */
18
19
package org.eclipse.ocl.ecore.internal.l10n;
20
21
import java.lang.reflect.Field;
22
import java.lang.reflect.Modifier;
23
import java.util.MissingResourceException;
24
import java.util.ResourceBundle;
25
26
/**
27
 * An accessor class for externalized strings.
28
 * 
29
 * @author Christian Vogt (cvogt)
30
 * @since 1.3
31
 */
32
public class OCLEcoreMessages {
33
34
	static final String BUNDLE_NAME = "org.eclipse.ocl.ecore.internal.l10n.OCLMessages"; //$NON-NLS-1$
35
36
	public static String bad_attribute_derivation;
37
	
38
	private static IMessages messagesImpl;
39
	
40
	public static String bind(String message, Object arg) {
41
		return messagesImpl.bind(message, arg);
42
	}
43
	
44
	public static String bind(String message, Object arg1, Object arg2) {
45
		return messagesImpl.bind(message, arg1, arg2);
46
	}
47
	
48
	public static String bind(String message, Object[] args) {
49
		return messagesImpl.bind(message, args);
50
	}
51
52
	static {
53
		try {
54
			@SuppressWarnings("unchecked")
55
			Class<IMessages> nlsClass = (Class<IMessages>) Class.forName("org.eclipse.ocl.ecore.internal.l10n.IMessages$NLS"); //$NON-NLS-1$
56
			messagesImpl = nlsClass.newInstance();
57
		} catch (NoClassDefFoundError e) {
58
			// expected in non-Eclipse environment
59
		} catch (Exception e) {
60
			// expected in non-Eclipse environment
61
		}
62
		
63
		if (messagesImpl == null) {
64
			// could not find the NLS class.  Try initializing the messages,
65
			//    ourselves
66
			initializeMessages();
67
			messagesImpl = new IMessages.Default();
68
		}
69
	}
70
	
71
	private static void initializeMessages() {
72
		ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME);
73
		int publicStatic = Modifier.PUBLIC | Modifier.STATIC;
74
		
75
		Class<OCLEcoreMessages> clazz = OCLEcoreMessages.class;
76
		
77
		if (bundle != null) {
78
			Field[] fields = clazz.getDeclaredFields();
79
			
80
			for (int i = 0; i < fields.length; i++) {
81
				Field next = fields[i];
82
				
83
				if (((next.getModifiers() & publicStatic) == publicStatic)
84
						&& (next.getType() == String.class)) {
85
					String name = next.getName();
86
					
87
					try {
88
						try {
89
							next.set(null, bundle.getString(name));
90
						} catch (MissingResourceException e) {
91
							// just use its own name, then
92
							next.set(null, "Missing message for key: " + name); //$NON-NLS-1$
93
						}
94
					} catch (Exception e) {
95
						// oh, well.  Can't set a value for this one
96
					}
97
				}
98
			}
99
		}
100
	}
101
}
(-)src/org/eclipse/ocl/ecore/dynamic/OCLSettingDelegateFactory.java (+159 lines)
Added Link Here
1
/**
2
 * <copyright>
3
 * 
4
 * Copyright (c) 2008 Zeligsoft Inc. and others.
5
 * All rights reserved.   This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 * 
10
 * Contributors:
11
 *   Zeligsoft - Initial API and implementation
12
 * 
13
 * </copyright>
14
 *
15
 * $Id$
16
 */
17
18
package org.eclipse.ocl.ecore.dynamic;
19
20
import java.lang.ref.Reference;
21
22
import org.eclipse.emf.common.notify.Notification;
23
import org.eclipse.emf.common.notify.impl.AdapterImpl;
24
import org.eclipse.emf.common.util.ECollections;
25
import org.eclipse.emf.ecore.EPackage;
26
import org.eclipse.emf.ecore.EStructuralFeature;
27
import org.eclipse.emf.ecore.InternalEObject;
28
import org.eclipse.emf.ecore.resource.Resource;
29
import org.eclipse.emf.ecore.util.BasicSettingDelegate;
30
import org.eclipse.ocl.ParserException;
31
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
32
import org.eclipse.ocl.ecore.OCL;
33
import org.eclipse.ocl.ecore.internal.OCLEcorePlugin;
34
import org.eclipse.ocl.ecore.internal.l10n.OCLEcoreMessages;
35
36
/**
37
 * Factory for OCL derived-attribute setting delegates.
38
 * 
39
 * @author Christian W. Damus (cdamus)
40
 * 
41
 * @since 1.3
42
 */
43
public class OCLSettingDelegateFactory
44
		implements EStructuralFeature.Internal.SettingDelegate.Factory {
45
46
	private OCLCache oclCache = new OCLCache();
47
48
	public EStructuralFeature.Internal.SettingDelegate createSettingDelegate(
49
			EStructuralFeature structuralFeature) {
50
51
		EStructuralFeature.Internal.SettingDelegate result;
52
53
		try {
54
			result = new OCLSettingDelegate(structuralFeature, oclCache);
55
		} catch (ParserException e) {
56
			// I will not be able to return any values
57
			OCLEcorePlugin.error(1, OCLEcoreMessages.bind(
58
				OCLEcoreMessages.bad_attribute_derivation, new Object[]{
59
					structuralFeature.getEContainingClass().getName(),
60
					structuralFeature.getName(), e.getLocalizedMessage()}));
61
62
			// so, return a setting delegate that has no value, ever
63
			return new BasicSettingDelegate.Stateless(structuralFeature) {
64
65
				@Override
66
				protected Object get(InternalEObject owner, boolean resolve,
67
						boolean coreType) {
68
69
					return eStructuralFeature.isMany()
70
						? ECollections.EMPTY_ELIST
71
						: null;
72
				}
73
74
				@Override
75
				protected boolean isSet(InternalEObject owner) {
76
					return false;
77
				}
78
79
			};
80
		}
81
82
		return result;
83
	}
84
85
	/**
86
	 * Creates the OCL environment in which to parse setting delegates for the
87
	 * specified environment. Subclasses may override to create custom
88
	 * environments, but should note that the result is cached.
89
	 * 
90
	 * @param ePackage
91
	 *            an Ecore package for which to create a setting delegate
92
	 *            environment
93
	 * @return the environment
94
	 */
95
	protected OCL createOCL(final EPackage ePackage) {
96
		Resource res = ePackage.eResource();
97
		EcoreEnvironmentFactory envFactory;
98
99
		if ((res != null) && (res.getResourceSet() != null)) {
100
			// it's a dynamic package. Use the local package registry
101
			envFactory = new EcoreEnvironmentFactory(res.getResourceSet()
102
				.getPackageRegistry());
103
			
104
			// and expunge when it's unloaded
105
			res.eAdapters().add(new AdapterImpl() {
106
				@Override
107
				public void notifyChanged(Notification msg) {
108
					if ((msg.getFeatureID(Resource.class) == Resource.RESOURCE__IS_LOADED) && !msg.getNewBooleanValue()) {
109
						oclCache.expunge(ePackage);
110
					}
111
				}
112
			});
113
		} else {
114
			// the shared instance uses the static package registry
115
			envFactory = EcoreEnvironmentFactory.INSTANCE;
116
		}
117
118
		return OCL.newInstance(envFactory);
119
	}
120
121
	//
122
	// Nested classes
123
	//
124
125
	/**
126
	 * A doubly-weak-referenced cache of OCL environments created to handle
127
	 * Ecore packages.
128
	 */
129
	protected class OCLCache
130
			extends java.util.WeakHashMap<EPackage, Reference<OCL>> {
131
132
		protected synchronized OCL getOCL(EPackage ePackage) {
133
			Reference<OCL> result = get(ePackage);
134
135
			if ((result == null) || (result.get() == null)) {
136
				result = new java.lang.ref.WeakReference<OCL>(
137
					createOCL(ePackage));
138
			}
139
140
			put(ePackage, result);
141
142
			return result.get();
143
		}
144
		
145
		protected synchronized void expunge(EPackage ePackage) {
146
			Reference<OCL> oclRef = get(ePackage);
147
			if (oclRef != null) {
148
				OCL ocl = oclRef.get();
149
				
150
				if (ocl != null) {
151
					ocl.dispose();
152
				}
153
				
154
				remove(ePackage);
155
			}
156
		}
157
	}
158
159
}
(-)src/org/eclipse/ocl/ecore/tests/AbstractTestSuite.java (-1 / +2 lines)
Lines 9-15 Link Here
9
 *
9
 *
10
 * Contributors:
10
 * Contributors:
11
 *   IBM - Initial API and implementation
11
 *   IBM - Initial API and implementation
12
 *   Zeligsoft - Bugs 243079, 244948, 244886, 245619
12
 *   Zeligsoft - Bugs 243079, 244948, 244886, 245619, 216701
13
 *
13
 *
14
 * </copyright>
14
 * </copyright>
15
 *
15
 *
Lines 197-202 Link Here
197
		result.addTest(RegressionTest.suite());
197
		result.addTest(RegressionTest.suite());
198
		result.addTest(EcoreEnvironmentTest.suite());
198
		result.addTest(EcoreEnvironmentTest.suite());
199
        result.addTest(ExtensibilityTest.suite());
199
        result.addTest(ExtensibilityTest.suite());
200
        result.addTest(OCLSettingDelegateTest.suite());
200
		result.addTest(ValidationTest.suite());
201
		result.addTest(ValidationTest.suite());
201
		result.addTest(ProblemOptionTest.suite());
202
		result.addTest(ProblemOptionTest.suite());
202
		result.addTest(ParsingOptionsTest.suite());
203
		result.addTest(ParsingOptionsTest.suite());
(-)src/org/eclipse/ocl/ecore/tests/OCLSettingDelegateTest.java (+293 lines)
Added Link Here
1
/**
2
 * <copyright>
3
 * 
4
 * Copyright (c) 2008 Zeligsoft Inc. and others.
5
 * All rights reserved.   This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 * 
10
 * Contributors:
11
 *   Zeligsoft - Initial API and implementation
12
 * 
13
 * </copyright>
14
 *
15
 * $Id$
16
 */
17
18
package org.eclipse.ocl.ecore.tests;
19
20
import java.util.Arrays;
21
import java.util.Map;
22
23
import junit.framework.Test;
24
import junit.framework.TestSuite;
25
26
import org.eclipse.emf.common.util.BasicEList;
27
import org.eclipse.emf.common.util.EList;
28
import org.eclipse.emf.common.util.Enumerator;
29
import org.eclipse.emf.common.util.URI;
30
import org.eclipse.emf.ecore.EAttribute;
31
import org.eclipse.emf.ecore.EClass;
32
import org.eclipse.emf.ecore.EEnum;
33
import org.eclipse.emf.ecore.EFactory;
34
import org.eclipse.emf.ecore.EObject;
35
import org.eclipse.emf.ecore.EPackage;
36
import org.eclipse.emf.ecore.EReference;
37
import org.eclipse.emf.ecore.EStructuralFeature;
38
import org.eclipse.emf.ecore.resource.Resource;
39
import org.eclipse.emf.ecore.resource.ResourceSet;
40
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
41
import org.eclipse.ocl.ecore.dynamic.OCLSettingDelegateFactory;
42
43
/**
44
 * Tests for the OCL setting delegate implementation.
45
 * 
46
 * @author Christian W. Damus (cdamus)
47
 */
48
@SuppressWarnings("nls")
49
public class OCLSettingDelegateTest
50
		extends AbstractTestSuite {
51
52
	private ResourceSet rset;
53
54
	private Resource testResource;
55
56
	private EPackage companyPackage;
57
58
	private EFactory companyFactory;
59
60
	private EClass companyClass;
61
62
	private EAttribute companyName;
63
64
	private EReference companyEmployees;
65
66
	private EAttribute companySize;
67
68
	private EClass employeeClass;
69
70
	private EAttribute employeeName;
71
72
	private EReference employeeManager;
73
74
	private EReference employeeDirectReports;
75
76
	private EReference employeeAllReports;
77
78
	private EEnum sizeKind;
79
80
	private Enumerator sizeSmall;
81
82
	private Enumerator sizeMedium;
83
84
	private Enumerator sizeLarge;
85
86
	private EObject acme;
87
88
	private Map<String, EObject> employees;
89
90
	public OCLSettingDelegateTest(String name) {
91
		super(name);
92
	}
93
94
	public static Test suite() {
95
		return new TestSuite(OCLSettingDelegateTest.class,
96
			"OCL Setting Delegate Tests");
97
	}
98
99
	public void test_eattributeDerivation() {
100
		assertSame(sizeSmall, size(acme));
101
102
		// add a load of employees
103
		EList<EObject> emps = employees(acme);
104
		for (int i = 0; i < 60; i++) {
105
			emps.add(companyFactory.create(employeeClass));
106
		}
107
108
		assertSame(sizeMedium, size(acme));
109
110
		// and another bunch
111
		for (int i = 0; i < 1000; i++) {
112
			emps.add(companyFactory.create(employeeClass));
113
		}
114
115
		assertSame(sizeLarge, size(acme));
116
	}
117
118
	public void test_ereferenceDerivation() {
119
		EList<EObject> amyReports = directReports(employee("Amy"));
120
		assertEquals(3, amyReports.size());
121
		assertTrue(amyReports.contains(employee("Bob")));
122
		assertTrue(amyReports.contains(employee("Jane")));
123
		assertTrue(amyReports.contains(employee("Fred")));
124
125
		EList<EObject> bobReports = directReports(employee("Bob"));
126
		assertEquals(2, bobReports.size());
127
		assertTrue(bobReports.contains(employee("Norbert")));
128
		assertTrue(bobReports.contains(employee("Sally")));
129
130
		EList<EObject> sallyReports = directReports(employee("Sally"));
131
		assertEquals(0, sallyReports.size());
132
	}
133
134
	public void test_allInstances() {
135
		EList<EObject> amyAllReports = allReports(employee("Amy"));
136
		assertEquals(5, amyAllReports.size());
137
		assertTrue(amyAllReports.contains(employee("Bob")));
138
		assertTrue(amyAllReports.contains(employee("Jane")));
139
		assertTrue(amyAllReports.contains(employee("Fred")));
140
		assertTrue(amyAllReports.contains(employee("Norbert")));
141
		assertTrue(amyAllReports.contains(employee("Sally")));
142
143
		// change the set of all instances of Employee
144
		set(create(acme, companyEmployees, employeeClass, "Manuel"),
145
			employeeManager, employee("Bob"));
146
147
		amyAllReports = allReports(employee("Amy"));
148
		assertEquals(6, amyAllReports.size());
149
		assertTrue(amyAllReports.contains(employee("Manuel")));
150
	}
151
152
	//
153
	// Test framework
154
	//
155
156
	@Override
157
	protected void setUp()
158
			throws Exception {
159
160
		super.setUp();
161
162
		((EStructuralFeature.Internal.SettingDelegate.Factory.Registry.Impl) EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE)
163
			.put("org.eclipse.ocl.ecore.OCL", new OCLSettingDelegateFactory());
164
165
		rset = new ResourceSetImpl();
166
		testResource = rset.getResource(URI.createPlatformPluginURI(
167
			"/org.eclipse.ocl.ecore.tests/model/TestCompany.xmi", true), true);
168
169
		acme = testResource.getContents().get(0);
170
171
		companyClass = acme.eClass();
172
		companyPackage = companyClass.getEPackage();
173
		companyFactory = companyPackage.getEFactoryInstance();
174
175
		companyName = (EAttribute) companyClass.getEStructuralFeature("name");
176
		companyEmployees = (EReference) companyClass
177
			.getEStructuralFeature("employees");
178
		companySize = (EAttribute) companyClass.getEStructuralFeature("size");
179
180
		employeeClass = companyEmployees.getEReferenceType();
181
		employeeName = (EAttribute) employeeClass.getEStructuralFeature("name");
182
		employeeManager = (EReference) employeeClass
183
			.getEStructuralFeature("manager");
184
		employeeDirectReports = (EReference) employeeClass
185
			.getEStructuralFeature("directReports");
186
		employeeAllReports = (EReference) employeeClass
187
			.getEStructuralFeature("allReports");
188
189
		sizeKind = (EEnum) companySize.getEAttributeType();
190
		sizeSmall = sizeKind.getEEnumLiteral("small");
191
		sizeMedium = sizeKind.getEEnumLiteral("medium");
192
		sizeLarge = sizeKind.getEEnumLiteral("large");
193
194
		employees = new java.util.HashMap<String, EObject>();
195
	}
196
197
	@Override
198
	protected void tearDown()
199
			throws Exception {
200
201
		for (Resource next : rset.getResources()) {
202
			next.unload();
203
		}
204
		rset.getResources().clear();
205
		rset = null;
206
		testResource = null;
207
		companyPackage = null;
208
		companyFactory = null;
209
		employees = null;
210
211
		((EStructuralFeature.Internal.SettingDelegate.Factory.Registry.Impl) EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE)
212
			.remove("org.eclipse.ocl.ecore.OCL");
213
214
		super.tearDown();
215
	}
216
217
	EObject employee(String name) {
218
		EObject result = employees.get(name);
219
		if (result == null) {
220
			EList<EObject> emps = get(acme, companyEmployees);
221
			for (EObject next : emps) {
222
				if (name.equals(name(next))) {
223
					result = next;
224
					employees.put(name, result);
225
					break;
226
				}
227
			}
228
		}
229
230
		return result;
231
	}
232
233
	String name(EObject employeeOrCompany) {
234
		EAttribute name = employeeClass.isInstance(employeeOrCompany)
235
			? employeeName
236
			: companyName;
237
		return get(employeeOrCompany, name);
238
	}
239
240
	EObject manager(EObject employee) {
241
		return get(employee, employeeManager);
242
	}
243
244
	EList<EObject> directReports(EObject employee) {
245
		return get(employee, employeeDirectReports);
246
	}
247
248
	EList<EObject> allReports(EObject employee) {
249
		return get(employee, employeeAllReports);
250
	}
251
252
	EList<EObject> employees(EObject company) {
253
		return get(company, companyEmployees);
254
	}
255
256
	Enumerator size(EObject company) {
257
		return get(company, companySize);
258
	}
259
260
	@SuppressWarnings("unchecked")
261
	<T> T get(EObject owner, EStructuralFeature feature) {
262
		return (T) owner.eGet(feature);
263
	}
264
265
	void set(EObject owner, EStructuralFeature feature, Object value) {
266
		owner.eSet(feature, value);
267
	}
268
269
	void add(EObject owner, EStructuralFeature feature, Object value) {
270
		this.<EList<Object>> get(owner, feature).add(value);
271
	}
272
273
	EObject create(EObject owner, EReference containment, EClass type,
274
			String name) {
275
		EObject result = companyFactory.create(type);
276
277
		if (containment.isMany()) {
278
			add(owner, containment, result);
279
		} else {
280
			set(owner, containment, result);
281
		}
282
283
		if (name != null) {
284
			set(result, type.getEStructuralFeature("name"), name);
285
		}
286
287
		return result;
288
	}
289
290
	<T> EList<T> list(T... element) {
291
		return new BasicEList<T>(Arrays.asList(element));
292
	}
293
}
(-)model/SettingDelegateTest.ecore (+44 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<ecore:EPackage xmi:version="2.0"
3
    xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="settingtest"
5
    nsURI="http://www.eclipse.org/ocl/test/2008/SettingDelegate" nsPrefix="st">
6
  <eClassifiers xsi:type="ecore:EClass" name="Company">
7
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
8
    <eStructuralFeatures xsi:type="ecore:EReference" name="employees" upperBound="-1"
9
        eType="#//Employee" containment="true" eOpposite="#//Employee/company"/>
10
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="size" lowerBound="1" eType="#//CompanySizeKind"
11
        changeable="false" volatile="true" transient="true" derived="true">
12
      <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
13
        <details key="settingDelegate" value="org.eclipse.ocl.ecore.OCL"/>
14
        <details key="OCL" value="let table : Set(Tuple(range : Sequence(Integer), size : CompanySizeKind)) =&#xD;    Set{Tuple{range=Sequence{0..49}, size=CompanySizeKind::small},&#xD;         Tuple{range=Sequence{50..999}, size=CompanySizeKind::medium},&#xD;         Tuple{range=Sequence{1000..1000000}, size=CompanySizeKind::large}} in&#xD;table->any(range->includes(employees->size())).size"/>
15
      </eAnnotations>
16
    </eStructuralFeatures>
17
  </eClassifiers>
18
  <eClassifiers xsi:type="ecore:EClass" name="Employee">
19
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
20
    <eStructuralFeatures xsi:type="ecore:EReference" name="manager" eType="#//Employee"/>
21
    <eStructuralFeatures xsi:type="ecore:EReference" name="company" lowerBound="1"
22
        eType="#//Company" eOpposite="#//Company/employees"/>
23
    <eStructuralFeatures xsi:type="ecore:EReference" name="directReports" upperBound="-1"
24
        eType="#//Employee" changeable="false" volatile="true" transient="true" derived="true">
25
      <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
26
        <details key="settingDelegate" value="org.eclipse.ocl.ecore.OCL"/>
27
        <details key="OCL" value="company.employees->select(manager = self)"/>
28
      </eAnnotations>
29
    </eStructuralFeatures>
30
    <eStructuralFeatures xsi:type="ecore:EReference" name="allReports" ordered="false"
31
        upperBound="-1" eType="#//Employee" changeable="false" volatile="true" transient="true"
32
        derived="true">
33
      <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
34
        <details key="settingDelegate" value="org.eclipse.ocl.ecore.OCL"/>
35
        <details key="OCL" value="Employee.allInstances()->select(e | e->closure(manager)->includes(self))"/>
36
      </eAnnotations>
37
    </eStructuralFeatures>
38
  </eClassifiers>
39
  <eClassifiers xsi:type="ecore:EEnum" name="CompanySizeKind">
40
    <eLiterals name="small"/>
41
    <eLiterals name="medium" value="1"/>
42
    <eLiterals name="large" value="2"/>
43
  </eClassifiers>
44
</ecore:EPackage>
(-)model/TestCompany.xmi (+9 lines)
Added Link Here
1
<?xml version="1.0" encoding="ASCII"?>
2
<st:Company xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:st="http://www.eclipse.org/ocl/test/2008/SettingDelegate" xsi:schemaLocation="http://www.eclipse.org/ocl/test/2008/SettingDelegate SettingDelegateTest.ecore" name="Acme">
3
  <employees name="Bob" manager="//@employees.3"/>
4
  <employees name="Jane" manager="//@employees.3"/>
5
  <employees name="Fred" manager="//@employees.3"/>
6
  <employees name="Amy"/>
7
  <employees name="Norbert" manager="//@employees.0"/>
8
  <employees name="Sally" manager="//@employees.0"/>
9
</st:Company>

Return to bug 291361