]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/DiagramToCompositeMapping3.java.old
Expose proxied subject in GroupProxySymbolItem
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / DiagramToCompositeMapping3.java.old
1 /*******************************************************************************
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.
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  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.modeling.mapping;
12
13 import org.simantics.db.Builtins;
14 import org.simantics.db.Resource;
15 import org.simantics.db.SyncReadGraph;
16 import org.simantics.db.exceptions.ResourceNotFoundException;
17 import org.simantics.db.procedure.SingleProcedure;
18 import org.simantics.db.requests.SingleRead;
19 import org.simantics.diagram.stubs.DiagramResource;
20 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
21 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
22 import org.simantics.layer0.utils.binaryPredicates.Relation;
23 import org.simantics.layer0.utils.binaryPredicates.UnionPredicate;
24 import org.simantics.layer0.utils.predicates.IUnaryPredicate;
25 import org.simantics.layer0.utils.predicates.Type;
26 import org.simantics.layer0.utils.triggers.IModification;
27 import org.simantics.layer0.utils.triggers.Trigger;
28 import org.simantics.mapping.constraint.instructions.AndInstruction;
29 import org.simantics.mapping.constraint.instructions.BinaryPredicateInstruction;
30 import org.simantics.mapping.constraint.instructions.BinaryPredicateObjectQuery;
31 import org.simantics.mapping.constraint.instructions.ExistsInstruction;
32 import org.simantics.mapping.constraint.instructions.IInstruction;
33 import org.simantics.mapping.constraint.instructions.TripletInstruction;
34 import org.simantics.mapping.constraint.instructions.TripletPredicateObjectQuery;
35 import org.simantics.mapping.constraint.instructions.TripletSubjectPredicateQuery;
36 import org.simantics.mapping.constraint.instructions.UnaryPredicateInstruction;
37 import org.simantics.mapping.rule.instructions.AndRuleInstruction;
38 import org.simantics.mapping.rule.instructions.ClaimRuleInstruction;
39 import org.simantics.mapping.rule.instructions.IRuleInstruction;
40 import org.simantics.mapping.rule.instructions.IfRuleInstruction;
41 import org.simantics.mapping.rule.instructions.QueryRuleInstruction;
42 import org.simantics.modeling.ModelingResources;
43 import org.simantics.structural.stubs.StructuralResource2;
44
45 public class DiagramToCompositeMapping3 extends Trigger {
46     static int VARIABLE_COUNT = 0;
47     
48     final static int Diagram = VARIABLE_COUNT++; // 0
49     final static int ParentType = VARIABLE_COUNT++; // 1
50     final static int Configuration = VARIABLE_COUNT++; // 2
51     final static int Element = VARIABLE_COUNT++; // 3
52     final static int ElementType = VARIABLE_COUNT++; // 4
53     final static int ComponentType = VARIABLE_COUNT++; // 5
54     final static int Component = VARIABLE_COUNT++; // 6
55     final static int Component2 = VARIABLE_COUNT++; // 6
56     
57     final static int DiagramConnectionRelation = VARIABLE_COUNT++; // 7
58     final static int DiagramConnectionRelation2 = VARIABLE_COUNT++; // 7
59     final static int CElement = VARIABLE_COUNT++; // 8
60     final static int CElement2 = VARIABLE_COUNT++; // 8
61     final static int ConnectionRelation = VARIABLE_COUNT++; // 9
62     final static int ConnectionRelation2 = VARIABLE_COUNT++; // 9
63     final static int Connector = VARIABLE_COUNT++; // 10   
64     final static int Connector2 = VARIABLE_COUNT++; // 10   
65     final static int Connection = VARIABLE_COUNT++; // 11
66
67     final static int Connection2 = VARIABLE_COUNT++; // 12
68     final static int Connection3 = VARIABLE_COUNT++; // 12
69     
70     
71     /* 
72      * 
73      */
74     static IRuleInstruction createMappingRule(SyncReadGraph g) throws ResourceNotFoundException {   
75         Builtins b = g.getBuiltins();
76         DiagramResource dr = DiagramResource.getInstance(g);
77         ModelingResources mr = ModelingResources.getInstance(g);
78         Resource stream = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Types#VLStream");
79         Resource streamFrom = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Relations#StreamFrom");
80         Resource streamTo = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Relations#StreamTo");
81         final StructuralResource2 sr = StructuralResource2.getInstance(g);
82         IBinaryPredicate instanceOf = new Relation(b.InstanceOf); 
83         IBinaryPredicate mapsToComponent = new Relation(mr.ElementToComponent);
84         IBinaryPredicate mapsToComponentType = new Relation(mr.SymbolToComponentType);
85
86         IBinaryPredicate mapsToConnection = new Relation(mr.DiagramConnectionToConnection);
87         IBinaryPredicate mapsToConnectionRelation = new Relation(mr.DiagramConnectionRelationToConnectionRelation);
88         IBinaryPredicate flagToParameter = new Relation(mr.FlagToParameter);
89         IUnaryPredicate mapped = new Tag(mr.Mapped);
90         IUnaryPredicate connection = new Type(dr.Connection);
91         IRuleInstruction rule = 
92             new AndRuleInstruction(     
93                 new IfRuleInstruction(
94                     // Condition
95                     new AndInstruction(
96                         new BinaryPredicateObjectQuery(Configuration, ParentType, 
97                             new UnionPredicate(
98                                 new Relation(sr.IsDefinedBy).inverse(g),
99                                 new Relation(b.HasConfiguration).inverse(g)
100                             )),
101                             new BinaryPredicateObjectQuery(Diagram, Element, OrderedSetElementsPredicate.INSTANCE)
102                     ),
103                     // Then rule    
104                     new QueryRuleInstruction(
105                         new IfRuleInstruction(
106                             new AndInstruction(
107                                 new BinaryPredicateObjectQuery(Element, ElementType, instanceOf),
108                                 new BinaryPredicateObjectQuery(ElementType, ComponentType, mapsToComponentType)
109                             ),
110                             new ClaimRuleInstruction(
111                                 new AndInstruction(                   
112                                         new ExistsInstruction(
113                                         new BinaryPredicateObjectQuery(Element, Component, mapsToComponent),
114                                         Component
115                                     ),
116                                     new BinaryPredicateInstruction(Component, ComponentType, instanceOf),
117                                     new BinaryPredicateInstruction(Component, Configuration, new Relation(b.PartOf)),
118                                     new UnaryPredicateInstruction(Component, mapped)
119                                 )
120                             ),
121                             new IfRuleInstruction(
122                                 new UnaryPredicateInstruction(Element, connection),
123                                 new ClaimRuleInstruction(
124                                     new AndInstruction(                   
125                                             new ExistsInstruction(
126                                             new BinaryPredicateObjectQuery(Element, Connection, mapsToConnection),
127                                             Connection
128                                         ),
129                                         new UnaryPredicateInstruction(Connection, new Type(stream)),
130                                         new BinaryPredicateInstruction(Connection, Configuration, new Relation(b.PartOf)),
131                                         new UnaryPredicateInstruction(Connection, mapped)                                           
132                                     ),
133                                     new AndRuleInstruction(                   
134                                             new IfRuleInstruction(
135                                                     new AndInstruction(
136                                                             new BinaryPredicateObjectQuery(Element, Connector, new Relation(dr.HasInput)),
137                                                             new TripletSubjectPredicateQuery(CElement, DiagramConnectionRelation, Connector, sr.IsConnected),
138                                                             new BinaryPredicateObjectQuery(DiagramConnectionRelation, ConnectionRelation, mapsToConnectionRelation),
139                                                             new BinaryPredicateObjectQuery(CElement, Component, mapsToComponent)
140                                                     ),
141                                                     new ClaimRuleInstruction(
142                                                             new AndInstruction(                   
143                                                                     new ExistsInstruction(
144                                                                             new TripletPredicateObjectQuery(Component, ConnectionRelation, Connection2, sr.IsConnected),
145                                                                             Connection2
146                                                                     ),
147                                                                     new UnaryPredicateInstruction(Connection2, new Type(sr.Connection)),
148                                                                     new BinaryPredicateInstruction(Connection, Connection2, new Relation(streamFrom)),
149                                                                     new UnaryPredicateInstruction(Connection2, mapped)                                          
150                                                             )
151
152                                                     )
153                                             ),
154                                             new IfRuleInstruction(
155                                                     new AndInstruction(
156                                                             new BinaryPredicateObjectQuery(Element, Connector2, new Relation(dr.HasOutput)),
157                                                             new TripletSubjectPredicateQuery(CElement2, DiagramConnectionRelation2, Connector2, sr.IsConnected),
158                                                             new BinaryPredicateObjectQuery(DiagramConnectionRelation2, ConnectionRelation2, mapsToConnectionRelation),
159                                                             new BinaryPredicateObjectQuery(CElement2, Component2, mapsToComponent)
160                                                     ),
161                                                     new ClaimRuleInstruction(
162                                                             new AndInstruction(                   
163                                                                     new ExistsInstruction(
164                                                                             new TripletPredicateObjectQuery(Component2, ConnectionRelation2, Connection3, sr.IsConnected),
165                                                                             Connection3
166                                                                     ),
167                                                                     new UnaryPredicateInstruction(Connection3, new Type(sr.Connection)),
168                                                                     new BinaryPredicateInstruction(Connection, Connection3, new Relation(streamTo)),
169                                                                     new UnaryPredicateInstruction(Connection3, mapped)                                          
170                                                             )
171
172                                                     )
173                                             )
174                                     )
175                                 )                               
176                             )
177                             
178                         )
179                     )
180                 )
181             );      
182         /*
183         StringBuilder sb = new StringBuilder();
184         rule.toString(sb, 0);
185         System.out.println(sb);
186         */
187         return rule;
188     }   
189
190     static IRuleInstruction profile(String name, IRuleInstruction rule) {
191         //return new ProfileRuleInstruction(name, rule);
192         return rule;
193     }
194
195     static IInstruction profile(String name, IInstruction inst) {
196         //return new ProfileInstruction(name, inst);
197         return inst;
198     }
199
200     static class RuleQuery implements SingleRead<IRuleInstruction> {
201
202         @Override
203         public boolean equals(Object other) {
204             return other != null && getClass().equals(other.getClass());
205         }
206
207         @Override
208         public int hashCode() {
209             return getClass().hashCode();
210         }
211
212         @Override
213         public void perform(SyncReadGraph g,
214                 SingleProcedure<IRuleInstruction> procedure) {
215             try {
216                 procedure.execute(g, createMappingRule(g));
217                 return;
218             } catch (ResourceNotFoundException e) {
219                 e.printStackTrace();                
220             }
221             procedure.execute(g, null);
222             
223         }
224         
225     }
226     
227     IRuleInstruction instruction;
228     Resource source;
229     Resource target;
230     
231     public DiagramToCompositeMapping3(SyncReadGraph g, Resource mapping) {
232         this.instruction = g.syncRequest(new RuleQuery());
233         this.source = g.getSingleObject(mapping, g.getInverse2(g.getBuiltins().HasTrigger));
234         this.target = g.getSingleObject(this.source, ModelingResources.getInstance(g).DiagramToComposite);
235     }
236     
237     @Override
238     public boolean equals(Object other) {
239         if(this==other)
240             return true;
241         if(!(other instanceof DiagramToCompositeMapping3))
242             return false;
243         DiagramToCompositeMapping3 map = (DiagramToCompositeMapping3)other;
244         return map.source.equals(source) && map.target.equals(target);
245     }
246
247     @Override
248     public int hashCode() {
249         return source.hashCode() + 31 * target.hashCode();
250     }
251
252     @Override
253     public void perform(SyncReadGraph g,
254             SingleProcedure<IModification> procedure) {
255         final Object[] bindings = new Object[VARIABLE_COUNT];
256         bindings[Diagram] = source;
257         bindings[Configuration] = target;
258         IModification modi = instruction.execute(g, bindings);
259 //      System.out.println("modi = " + modi);
260         procedure.execute(g, modi); 
261     }
262 }