]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/AndInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / AndInstruction.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 org.simantics.db.ReadGraph;
15 import org.simantics.db.exception.DatabaseException;
16
17 public class AndInstruction extends CombinedInstruction {
18         
19         public AndInstruction(IInstruction... instructions) {
20                 super(instructions);
21         }
22
23         static class Continuation {
24                 Object continuation;
25                 int instructionId;      
26                 Continuation next;              
27                 
28                 public Continuation(Object continuation, int instructionId,
29                                 Continuation next) {
30                         this.continuation = continuation;
31                         this.instructionId = instructionId;
32                         this.next = next;
33                 }
34
35         }
36         
37         @Override
38         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
39                 Continuation ret = null;
40                 int instructionId = 0;
41                 while(instructionId < instructions.length) {
42                         if(DEBUG)
43                                 System.out.println("AndInstruction.query " + instructionId + "/" + instructions.length);
44                         Object continuation = instructions[instructionId].query(g, bindings);
45                         if(continuation == IInstruction.FAILURE) {
46                                 while(true) {
47                                         if(ret == null)
48                                                 return IInstruction.FAILURE;
49                                         instructionId = ret.instructionId;
50                                         continuation = instructions[instructionId].next(g, bindings, ret.continuation);
51                                         if(continuation == IInstruction.FAILURE) {
52                                                 ret = ret.next;                                         
53                                                 continue;
54                                         }
55                                         if(continuation == null) 
56                                                 ret = ret.next;
57                                         else
58                                                 ret.continuation = continuation;
59                                         break;
60                                 }                                                                       
61                         }
62                         else if(continuation != null)
63                                 ret = new Continuation(continuation, instructionId, ret);
64                         ++instructionId;
65                 }
66                 return ret;
67         }
68         
69         @Override
70         public Object next(ReadGraph g, Object[] bindings, Object ret_) throws DatabaseException {
71                 Continuation ret = (Continuation)ret_;
72                 int instructionId;
73                 while(true) {
74                         if(ret == null)
75                                 return IInstruction.FAILURE;
76                         instructionId = ret.instructionId;
77                         Object continuation = 
78                                 instructions[instructionId].next(g, bindings, ret.continuation);
79                         if(continuation == IInstruction.FAILURE) {
80                                 ret = ret.next;                                         
81                                 continue;
82                         }
83                         if(continuation == null) 
84                                 ret = ret.next;
85                         else
86                                 ret.continuation = continuation;
87                         break;
88                 }
89                 ++instructionId;
90                 while(instructionId < instructions.length) {
91                         Object continuation = instructions[instructionId].query(g, bindings);
92                         if(continuation == IInstruction.FAILURE) {
93                                 while(true) {
94                                         if(ret == null)
95                                                 return IInstruction.FAILURE;
96                                         instructionId = ret.instructionId;
97                                         continuation = instructions[instructionId].next(g, bindings, ret.continuation);
98                                         if(continuation == IInstruction.FAILURE) {
99                                                 ret = ret.next;                                         
100                                                 continue;
101                                         }
102                                         if(continuation == null) 
103                                                 ret = ret.next;
104                                         else
105                                                 ret.continuation = continuation;
106                                         break;
107                                 }                                                                       
108                         }
109                         else if(continuation != null)
110                                 ret = new Continuation(continuation, instructionId, ret);
111                         ++instructionId;
112                 }
113                 return ret;
114         }
115
116         @Override
117         public void toString(StringBuilder b, int indent) {             
118                 b.append("(    ");
119                 boolean first = true;
120                 for(IInstruction inst : instructions) {
121                         if(first)
122                                 first = false;
123                         else {
124                                 b.append(",\n");
125                                 for(int i=0;i<indent+1;++i)
126                                         b.append(INDENTATION);
127                         }
128                         inst.toString(b, indent + 1);                   
129                 }
130                 b.append('\n');
131                 for(int i=0;i<indent;++i)
132                         b.append(INDENTATION);
133                 b.append(")");          
134         }
135         
136 }