]> gerrit.simantics Code Review - simantics/platform.git/blob
bfffd2c6e74a6a1e40a902f7645a528256a6d14b
[simantics/platform.git] /
1 package org.simantics.scl.compiler.internal.elaboration.transformations;
2
3 import gnu.trove.procedure.TObjectObjectProcedure;
4
5 import java.util.ArrayList;
6
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.elaboration.query.QMapping;
9 import org.simantics.scl.compiler.elaboration.query.Query;
10 import org.simantics.scl.compiler.elaboration.relations.LocalRelation;
11 import org.simantics.scl.compiler.elaboration.rules.SectionName;
12 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
13
14 public class DecomposedRule {
15     final TransformationRule rule;
16     final ArrayList<Query> sourceQueries = new ArrayList<Query>();
17     final ArrayList<QMapping> sourceMappings = new ArrayList<QMapping>();
18     final ArrayList<Query> targetQueries = new ArrayList<Query>();
19     final ArrayList<QMapping> targetMappings = new ArrayList<QMapping>();
20     
21     LocalRelation ruleMatchingRelation;
22     
23     private DecomposedRule(TransformationRule rule) {
24         this.rule = rule;
25     }
26     
27     public static DecomposedRule decompose(final TypingContext context, TransformationRule rule, final boolean forwardDirection) {
28         final DecomposedRule result = new DecomposedRule(rule);
29         rule.forEachSection(new TObjectObjectProcedure<SectionName, Query[]>() {
30             @Override
31             public boolean execute(SectionName sectionName, Query[] queries) {
32                 switch(forwardDirection ? sectionName.forwardRole : sectionName.backwardRole) {
33                 case MATCH:
34                     for(Query query : queries) {
35                         if(query instanceof QMapping) {
36                             QMapping mapping = (QMapping)query;
37                             result.sourceMappings.add(mapping);
38                         }
39                         else
40                             result.sourceQueries.add(query.copy(context));
41                     }
42                     break;
43                 case ENFORCE:
44                 case ENFORCE_FIRST_TIME: // Because this is one-shot, these two are equivalent
45                     for(Query query : queries) {
46                         if(query instanceof QMapping) {
47                             QMapping mapping = (QMapping)query;
48                             result.targetMappings.add(mapping);
49                         }
50                         else
51                             result.targetQueries.add(query.copy(context));
52                     }
53                     break;
54                 case IGNORE:
55                 }
56                 return true;
57             }
58         });
59         return result;
60     }
61 }