]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/ClaimRuleInstruction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / rule / instructions / ClaimRuleInstruction.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.mapping.rule.instructions;\r
13 \r
14 import gnu.trove.map.hash.TIntIntHashMap;\r
15 import gnu.trove.set.hash.TIntHashSet;\r
16 \r
17 import java.util.Arrays;\r
18 \r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.layer0.utils.triggers.IModification;\r
23 import org.simantics.mapping.IContextualModification;\r
24 import org.simantics.mapping.constraint.instructions.IInstruction;\r
25 \r
26 public class ClaimRuleInstruction implements IRuleInstruction {\r
27 \r
28         IInstruction instruction;       \r
29         IRuleInstruction continuation;\r
30         \r
31         public ClaimRuleInstruction(IInstruction instruction,\r
32                 IRuleInstruction continuation) {\r
33                 this.instruction = instruction;\r
34                 this.continuation = continuation;\r
35         }\r
36 \r
37         public ClaimRuleInstruction(IInstruction instruction) {\r
38                 this(instruction, null);\r
39         }\r
40         \r
41         @Override\r
42         public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {\r
43                 final IContextualModification modi = instruction.claim(g, bindings);\r
44                 if(modi != null) {\r
45                         final Object[] curBindings = Arrays.copyOf(bindings, bindings.length);\r
46                         return new IModification() {\r
47 \r
48                                 @Override\r
49                                 public void perform(WriteGraph g) throws DatabaseException {\r
50                                         modi.perform(g, curBindings);\r
51                                         if(continuation != null) {\r
52                                                 IModification modi2 = continuation.execute(g, curBindings);\r
53                                                 if(modi2 != null)\r
54                                                         modi2.perform(g);\r
55                                         }\r
56                                 }\r
57                                 \r
58                         };\r
59                 }\r
60                 if(continuation != null)\r
61                         return continuation.execute(g, bindings);\r
62                 return null;\r
63         }\r
64         \r
65         @Override\r
66         public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {\r
67                 instruction.doClaim(g, bindings);\r
68         }\r
69         \r
70         @Override\r
71         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {\r
72                 instruction.collectVariables(reads, writes);\r
73                 if(continuation != null)\r
74                         continuation.collectVariables(reads, writes);\r
75         }\r
76 \r
77         @Override\r
78         public void mapVariables(TIntIntHashMap map) {\r
79                 instruction.mapVariables(map);\r
80                 if(continuation != null)\r
81                         continuation.mapVariables(map);\r
82         }\r
83         @Override\r
84         public void toString(StringBuilder b, int indent) {\r
85                 b.append("claim ");\r
86                 instruction.toString(b, indent+1);\r
87                 if(continuation != null) {\r
88                         b.append('\n');\r
89                         for(int i=0;i<indent;++i)\r
90                                 b.append(INDENTATION);\r
91                         b.append("and ");\r
92                         continuation.toString(b, indent+1);\r
93                 }\r
94         }\r
95 }\r