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