]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/IfRuleInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / rule / instructions / IfRuleInstruction.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.rule.instructions;
13
14 import gnu.trove.map.hash.TIntIntHashMap;
15 import gnu.trove.set.hash.TIntHashSet;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.utils.triggers.IModification;
21 import org.simantics.mapping.constraint.instructions.IInstruction;
22
23 public class IfRuleInstruction implements IRuleInstruction {
24         IInstruction condition;
25         IRuleInstruction rule;
26         IRuleInstruction elseRule;      
27         
28         public IfRuleInstruction(IInstruction condition, IRuleInstruction rule, IRuleInstruction elseRule) {
29                 this.condition = condition;
30                 this.rule = rule;
31                 this.elseRule = elseRule;
32         }
33         
34         public IfRuleInstruction(IInstruction condition, IRuleInstruction rule) {
35                 this(condition, rule, null);
36         }       
37         
38         @Override
39         public IModification execute(ReadGraph g, final Object[] bindings) throws DatabaseException {
40                 if(DEBUG)
41                         System.out.println("IfRuleInstruction: condition");
42                 Object cont = condition.query(g, bindings);
43                 if(cont == IInstruction.FAILURE) {
44                         if(DEBUG)
45                           System.out.println("IfRuleInstruction: else");
46                         if(elseRule!=null)
47                                 return elseRule.execute(g, bindings);
48                         else
49                                 return null;
50                 }
51                 do {
52                         if(DEBUG)
53                                 System.out.println("IfRuleInstruction: modification");
54                         final IModification modi = rule.execute(g, bindings);
55                         if(modi != null) {
56                                 if(cont==null)
57                                         return modi;
58                                 final Object curCont = condition.next(g, bindings, cont);
59                                 if(curCont==IInstruction.FAILURE)
60                                         return modi;
61                                 return new IModification() {
62                                         @Override
63                                         public void perform(WriteGraph g) throws DatabaseException {
64                                                 modi.perform(g);
65                                                 Object cont = curCont;
66                                                 do {
67                                                         rule.doExecute(g, bindings);
68                                                         if(cont==null)
69                                                                 break;
70                                                         cont = condition.next(g, bindings, cont);
71                                                 } while(cont != IInstruction.FAILURE);
72                                         }                               
73                                 };      
74                         }
75                         if(cont==null)
76                                 break;
77                         cont = condition.next(g, bindings, cont);
78                 } while(cont != IInstruction.FAILURE);
79                 return null;
80         }
81         
82         @Override
83         public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {
84                 Object cont = condition.query(g, bindings);
85                 if(cont == IInstruction.FAILURE) {
86                         if(elseRule!=null) 
87                                 elseRule.doExecute(g, bindings);
88                         return;
89                 }
90                 do {
91                         rule.doExecute(g, bindings);
92                         if(cont==null)
93                                 break;
94                         cont = condition.next(g, bindings, cont);
95                 } while(cont != IInstruction.FAILURE);
96         }
97         
98         @Override
99         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
100                 condition.collectVariables(reads, writes);
101                 rule.collectVariables(reads, writes);
102                 if(elseRule != null)
103                         elseRule.collectVariables(reads, writes);
104         }
105
106         @Override
107         public void mapVariables(TIntIntHashMap map) {
108                 condition.mapVariables(map);
109                 rule.mapVariables(map);
110                 if(elseRule != null)
111                         elseRule.mapVariables(map);
112         }
113
114         @Override
115         public void toString(StringBuilder b, int indent) {             
116                 b.append("if   ");
117                 condition.toString(b, indent+1);
118                 
119                 b.append('\n');         
120                 for(int i=0;i<indent;++i)
121                         b.append(INDENTATION);
122                 b.append("then ");
123                 rule.toString(b, indent+1);             
124                 
125                 if(elseRule != null) {
126                         b.append('\n');
127                         for(int i=0;i<indent;++i)
128                                 b.append(INDENTATION);
129                         b.append("else ");
130                         elseRule.toString(b, indent+1);
131                 }
132         }
133 }