]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/ExistsInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / ExistsInstruction.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.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.util.RemoverUtil;
23 import org.simantics.mapping.IContextualModification;
24
25 public class ExistsInstruction implements IInstruction {
26
27         IInstruction instruction;
28         int[] variables;        
29         
30         public ExistsInstruction(IInstruction instruction, int ... variables) {
31                 this.instruction = instruction;
32                 this.variables = variables;
33         }       
34
35         @Override
36         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
37                 instruction.collectVariables(reads, writes);
38         }
39         
40         class Claim implements IContextualModification {
41                         
42                 public Claim() {
43                 }
44
45                 @Override
46                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
47 //                      ITask task = ThreadLogger.getInstance().begin("g");
48                         for(int var : variables) {
49                                 bindings[var] = DISABLE_MODI ? null : g.newResource();
50                                 if(DEBUG)
51                                     System.out.println("@ newResource " + NameUtils.getSafeName(g, (Resource)bindings[var]));
52                 if(DEBUG_MODI)
53                     System.out.println("claim exists " + NameUtils.getSafeName(g, (Resource)bindings[var]));
54                         }
55 //                      task.finish();
56                         instruction.doClaim(g, bindings);       
57                 }
58         }
59         
60         @Override
61         public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {
62                 if(instruction.query(g, bindings)==IInstruction.FAILURE) {
63 //                      System.out.println("Creates Claim");
64                         return new Claim();
65                 }
66                 return null;
67         }
68         
69         class Deny implements IContextualModification {
70                 Object continuation;
71                 
72                 public Deny(Object continuation) {
73                         this.continuation = continuation;
74                 }
75
76                 @Override
77                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
78                         Object continuation = this.continuation;
79                         do {
80                             for(int var : variables) {
81                                 if(DEBUG_MODI)
82                                     System.out.println("deny exists " + 
83                                             NameUtils.getSafeName(g, (Resource)bindings[var]));
84                                 if(!DISABLE_MODI)
85                                         RemoverUtil.remove(g, (Resource)bindings[var]);
86                                 }
87                                 continuation = instruction.next(g, bindings, continuation);
88                         } while(continuation != null && continuation != IInstruction.FAILURE);  
89                 }
90         }
91         
92         @Override
93         public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
94                 Object continuation = instruction.query(g, bindings);
95                 if(continuation != IInstruction.FAILURE)
96                         return new Deny(continuation);
97                 return null;
98         }
99
100         @Override
101         public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
102                 if(instruction.query(g, bindings)==IInstruction.FAILURE) {
103 //                      ITask task = ThreadLogger.getInstance().begin("g");
104                         for(int var : variables) {
105                                 bindings[var] = DISABLE_MODI ? null : g.newResource();
106                                 if(DEBUG)
107                                         System.out.println("@ newResource " + NameUtils.getSafeName(g, (Resource)bindings[var]));
108                                 if(DEBUG_MODI)
109                                         System.out.println("claim exists " + NameUtils.getSafeName(g, (Resource)bindings[var]));
110                         }                       
111 //                      task.finish();
112                         instruction.doClaim(g, bindings);
113                 }               
114         }
115
116         @Override
117         public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
118                 Object continuation = instruction.query(g, bindings);
119                 if(continuation != IInstruction.FAILURE)
120                         do {
121                                 for(int var : variables) {
122                             if(DEBUG_MODI)
123                                 System.out.println("deny exists " + 
124                                         NameUtils.getSafeName(g, (Resource)bindings[var]));
125                             if(!DISABLE_MODI)
126                                 RemoverUtil.remove(g, (Resource)bindings[var]);
127                                 }
128                                 continuation = instruction.next(g, bindings, continuation);
129                         } while(continuation != null && continuation != IInstruction.FAILURE);          
130         }
131
132         @Override
133         public void mapVariables(TIntIntHashMap map) {
134                 instruction.mapVariables(map);
135                 for(int i=0;i<variables.length;++i)
136                         variables[i] = map.get(variables[i]);
137         }
138
139         @Override
140         public Object next(ReadGraph g, Object[] bindings, Object continuation) throws DatabaseException {
141                 return instruction.next(g, bindings, continuation);
142         }
143
144         @Override
145         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
146                 return instruction.query(g, bindings);
147         }
148         
149         @Override
150         public void toString(StringBuilder b, int indent) {
151                 b.append('[');
152                 boolean first = true;
153                 for(int v : variables) {
154                         if(first)
155                                 first = false;
156                         else 
157                                 b.append(',');
158                         b.append(v);            
159                 }
160                 b.append("] ");
161                 instruction.toString(b, indent+1);
162         }
163
164 }