]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/CombinedInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / CombinedInstruction.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.constraint.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.mapping.IContextualModification;
21
22 public abstract class CombinedInstruction implements IInstruction {
23         IInstruction[] instructions;
24         
25         public CombinedInstruction(IInstruction... instructions) {
26                 this.instructions = instructions;
27         }       
28         
29         class CompositeClaim implements IContextualModification {
30
31                 IContextualModification modification;
32                 int instructionId;
33                 
34                 public CompositeClaim(IContextualModification modification, int instructionId) {
35                         this.modification = modification;
36                         this.instructionId = instructionId;
37                 }
38
39                 @Override
40                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
41                         modification.perform(g, bindings);
42                         for(int i=instructionId+1;i<instructions.length;++i)
43                                 instructions[i].doClaim(g, bindings);
44                 }
45                 
46         }
47         
48         @Override
49         public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {
50                 for(int instructionId=0;instructionId<instructions.length;++instructionId) {
51                         IContextualModification modification = instructions[instructionId].claim(g, bindings);
52                         if(modification != null) {
53                                 if(instructionId==instructions.length-1)
54                                         return modification;
55                                 else 
56                                         return new CompositeClaim(modification, instructionId);
57                         }
58                 }
59                 return null;
60         }
61
62         class CompositeDeny implements IContextualModification {
63
64                 IContextualModification modification;
65                 int instructionId;
66                 
67                 public CompositeDeny(IContextualModification modification, int instructionId) {
68                         this.modification = modification;
69                         this.instructionId = instructionId;
70                 }
71
72                 @Override
73                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
74                         modification.perform(g, bindings);
75                         for(int i=instructionId+1;i<instructions.length;++i)
76                                 instructions[i].doDeny(g, bindings);
77                 }
78                 
79         }
80
81         @Override
82         public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
83                 for(int instructionId=0;instructionId<instructions.length;++instructionId) {
84                         IContextualModification modification = instructions[instructionId].deny(g, bindings);
85                         if(modification != null) {
86                                 if(instructionId==instructions.length-1)
87                                         return modification;
88                                 else 
89                                         return new CompositeDeny(modification, instructionId);
90                         }
91                 }
92                 return null;
93         }
94
95         @Override
96         public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
97                 for(int i=0;i<instructions.length;++i)
98                         instructions[i].doClaim(g, bindings);           
99         }
100
101         @Override
102         public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
103                 for(int i=0;i<instructions.length;++i)
104                         instructions[i].doDeny(g, bindings);                                    
105         }
106
107         @Override
108         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
109                 for(IInstruction inst : instructions)
110                         inst.collectVariables(reads, writes);
111         }       
112         
113         @Override
114         public void mapVariables(TIntIntHashMap map) {
115                 for(IInstruction inst : instructions)
116                         inst.mapVariables(map);
117         }
118
119 }