]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/pattern/MappingRule.java
Support diagram profile activity store/restore in model export/import
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / pattern / MappingRule.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.mapping.pattern;
13
14
15 public class MappingRule {
16         /*
17         static final THashSet<Resource> EMPTY_SET = new THashSet<Resource>();
18         
19         THashMap<Resource, Resource> resourcesByDomains = new THashMap<Resource, Resource>();
20         Collection<IConstraint> constraints = new ArrayList<IConstraint>();
21         Collection<ConstraintGroup> constraintGroups = new ArrayList<ConstraintGroup>();
22         
23         static class ConstraintGroup {
24                 Collection<IConstraint> constraints;
25                 Collection<Resource> targets;
26                 Collection<Resource> dependencies;
27         }
28         
29         class DirectedRuleBuilder {
30                 THashSet<Resource> sourceResources = new THashSet<Resource>();
31                 THashSet<Resource> targetResources = new THashSet<Resource>();
32                 THashSet<IConstraint> sourceConstraints = new THashSet<IConstraint>();
33                 THashSet<IConstraint> targetConstraints = new THashSet<IConstraint>();
34                 TObjectIntHashMap<Resource> boundResources;
35                 int id;
36                 
37                 Instruction root = new DummyInstruction();
38                 Instruction cur = root;
39                 
40                 public DirectedRuleBuilder(final THashSet<Resource> sourceDomains,
41                                 TObjectIntHashMap<Resource> boundResources, int id) {
42                         this.boundResources = boundResources;
43                         this.id = id;
44                         
45                         // Classify resources
46                         
47                         resourcesByDomains.forEachEntry(new TObjectObjectProcedure<Resource, Resource>() {
48
49                                 @Override
50                                 public boolean execute(Resource domain, Resource resource) {
51                                         if(sourceDomains.contains(domain))
52                                                 sourceResources.add(resource);
53                                         else
54                                                 targetResources.add(resource);
55                                         return true;
56                                 }
57                                 
58                         });
59                         
60                         // Classify constraints                 
61                         
62                         for(IConstraint constraint : constraints) {
63                                 if(sourceResources.containsAll(constraint.binds()))
64                                         sourceConstraints.add(constraint);
65                                 else
66                                         targetConstraints.add(constraint);
67                         }
68                 }       
69                 
70                 int bestValue;
71                 IConstraint bestConstraint;
72                 
73                 private void findBestConstraint(THashSet<IConstraint> constraints) {
74                         bestValue = 0;
75                         bestConstraint = null;
76                         sourceConstraints.forEach(new TObjectProcedure<IConstraint>() {
77                                 
78                                 @Override
79                                 public boolean execute(IConstraint constraint) {
80                                         int temp = constraint.isApplicable(boundResources);
81                                         if(temp>bestValue) {
82                                                 bestValue = temp;
83                                                 bestConstraint = constraint;
84                                         }
85                                         return true;
86                                 }
87                                 
88                         });
89                 }
90                 
91                 private void applyConstraint(THashSet<Resource> resources, IConstraint constraint) {
92                         THashSet<Resource> unbounded = new THashSet<Resource>();
93                         for(Resource r : constraint.binds())
94                                 if(resources.remove(r)) {
95                                         unbounded.add(r);
96                                         boundResources.put(r, id++);
97                                 }                                       
98                         Instruction temp = constraint.createQueryInstruction(boundResources, unbounded);
99                         cur.next = temp;
100                         cur = temp;                     
101                 }
102                 
103                 private void applyConstraints(THashSet<Resource> resources, THashSet<IConstraint> constraints) {
104                         while(!constraints.isEmpty()) {
105                                 findBestConstraint(constraints);                                
106                                 if(bestConstraint != null) {
107                                         applyConstraint(resources, bestConstraint);
108                                         constraints.remove(bestConstraint);
109                                 }
110                                 else
111                                         throw new RuntimeException("Cannot form a directed mapping.");
112                         }
113                 }
114                 
115                 public void orderSourceConstraints() {
116                         applyConstraints(sourceResources, sourceConstraints);
117                         if(!sourceResources.isEmpty())
118                                 throw new RuntimeException("Couldn't bind all source resources");
119                 }
120                 
121                 public void orderTargetConstraints() {
122                         THashSet<ConstraintGroup> groups = new THashSet<ConstraintGroup>(constraintGroups);
123                         while(!groups.isEmpty()) {
124                                 
125                                 // Find a suitable group
126                                 
127                                 final Ref<ConstraintGroup> possibleGroup = new Ref<ConstraintGroup>();
128                                 final Collection<ConstraintGroup> removableGroups = new ArrayList<ConstraintGroup>();
129                                 groups.forEach(new TObjectProcedure<ConstraintGroup>() {
130
131                                         @Override
132                                         public boolean execute(ConstraintGroup group) {
133                                                 for(Resource target : group.targets)
134                                                         if(boundResources.containsKey(target)) {
135                                                                 removableGroups.add(group);
136                                                                 return true;
137                                                         }
138                                                 for(Resource dep : group.dependencies)
139                                                         if(!boundResources.containsKey(dep))
140                                                                 return true;
141                                                 possibleGroup.value = group;
142                                                 return false;
143                                         }
144                                         
145                                 });
146                                 groups.removeAll(removableGroups);
147                                 if(possibleGroup.value == null) {
148                                         if(!groups.isEmpty())
149                                                 throw new RuntimeException("Couldn't find a suitable group.");
150                                         break;
151                                 }
152                                 groups.remove(possibleGroup.value);
153                                 ConstraintGroup group = possibleGroup.value;
154                                         
155                                 // Order group constraints
156                                 
157                                 THashSet<IConstraint> groupConstraints = new THashSet<IConstraint>();
158                                 for(IConstraint constraint : group.constraints)
159                                         if(targetConstraints.remove(constraint))
160                                                 groupConstraints.add(constraint);
161                                 
162                                 applyConstraints(targetResources, groupConstraints);
163                                 
164                                 // Apply other constraints
165                                 
166                                 final Collection<IConstraint> removableConstraints = new ArrayList<IConstraint>();
167                                 targetConstraints.forEach(new TObjectProcedure<IConstraint>() {
168
169                                         @Override
170                                         public boolean execute(IConstraint constraint) {
171                                                 for(Resource r : constraint.binds())
172                                                         if(!boundResources.containsKey(r))
173                                                                 return true;
174                                                 
175                                                 Instruction temp = constraint.createQueryInstruction(boundResources, EMPTY_SET);
176                                                 cur.next = temp;
177                                                 cur = temp;
178                                                 
179                                                 removableConstraints.add(constraint);
180                                                 return true;
181                                         }
182                                         
183                                 });
184                                 targetConstraints.removeAll(removableConstraints);
185                         }
186                         if(!targetResources.isEmpty())
187                                 throw new RuntimeException("Couldn't bind all target resources");
188                 }
189                 
190         }
191         */
192 }