diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/DseIdPoolHelper.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/DseIdPoolHelper.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/DseIdPoolHelper.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/DseIdPoolHelper.java new file mode 100644 index 00000000..f6fee7be --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/DseIdPoolHelper.java | |||
@@ -0,0 +1,68 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package org.eclipse.viatra.dse.base; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.HashMap; | ||
13 | import java.util.concurrent.ConcurrentHashMap; | ||
14 | |||
15 | import org.eclipse.viatra.dse.api.DSEException; | ||
16 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule; | ||
17 | |||
18 | public enum DseIdPoolHelper { | ||
19 | |||
20 | INSTANCE; | ||
21 | |||
22 | public static interface IGetRuleExecutions { | ||
23 | int getRuleExecutions(BatchTransformationRule<?, ?> rule); | ||
24 | } | ||
25 | |||
26 | public static class IdProvider { | ||
27 | |||
28 | private final BatchTransformationRule<?, ?> rule; | ||
29 | private IGetRuleExecutions getRuleExecutions; | ||
30 | |||
31 | public IdProvider(IGetRuleExecutions getRuleExecutions, BatchTransformationRule<?, ?> rule) { | ||
32 | this.getRuleExecutions = getRuleExecutions; | ||
33 | this.rule = rule; | ||
34 | } | ||
35 | |||
36 | public int getId() { | ||
37 | return getRuleExecutions.getRuleExecutions(rule); | ||
38 | } | ||
39 | |||
40 | } | ||
41 | |||
42 | private ConcurrentHashMap<Thread, HashMap<BatchTransformationRule<?, ?>, IdProvider>> idProviders = new ConcurrentHashMap<>(); | ||
43 | |||
44 | public int getId(BatchTransformationRule<?, ?> rule) { | ||
45 | Thread currentThread = Thread.currentThread(); | ||
46 | HashMap<BatchTransformationRule<?, ?>, IdProvider> ruleMap = idProviders.get(currentThread); | ||
47 | if (ruleMap == null) { | ||
48 | throw new DSEException("There is no registered id provider"); | ||
49 | } | ||
50 | IdProvider idProvider = ruleMap.get(rule); | ||
51 | return idProvider.getId(); | ||
52 | } | ||
53 | |||
54 | public void registerRules(IGetRuleExecutions getRuleExecutions, Collection<BatchTransformationRule<?, ?>> rules) { | ||
55 | Thread currentThread = Thread.currentThread(); | ||
56 | HashMap<BatchTransformationRule<?, ?>, IdProvider> ruleMap = new HashMap<>(); | ||
57 | for (BatchTransformationRule<?, ?> rule : rules) { | ||
58 | IdProvider idProvider = new IdProvider(getRuleExecutions, rule); | ||
59 | ruleMap.put(rule, idProvider); | ||
60 | } | ||
61 | idProviders.put(currentThread, ruleMap); | ||
62 | } | ||
63 | |||
64 | public void disposeByThread() { | ||
65 | Thread currentThread = Thread.currentThread(); | ||
66 | idProviders.remove(currentThread); | ||
67 | } | ||
68 | } | ||