]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/OrInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / OrInstruction.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 OrInstruction extends CombinedInstruction {
18         
19         public OrInstruction(IInstruction... instructions) {
20                 super(instructions);
21         }
22
23         static class Continuation {
24                 Object continuation;
25                 int instructionId;      
26                 
27                 public Continuation(Object continuation, int instructionId) {
28                         this.continuation = continuation;
29                         this.instructionId = instructionId;
30                 }
31         }
32         
33         @Override
34         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
35                 int instructionId;
36                 for(instructionId=0;instructionId<instructions.length-1;++instructionId) {
37                         if(DEBUG)
38                                 System.out.println("OrInstruction.query " + instructionId + "/" + instructions.length);
39                         Object continuation = instructions[instructionId].query(g, bindings);
40                         if(continuation != IInstruction.FAILURE)
41                                 return new Continuation(continuation, instructionId);
42                 }
43                 return instructions[instructionId].query(g, bindings);
44         }
45         
46         @Override
47         public Object next(ReadGraph g, Object[] bindings, Object ret_) throws DatabaseException {
48                 Continuation ret = (Continuation)ret_;
49                 int instructionId = ret.instructionId;
50                 Object continuation = ret.continuation;
51                 
52                 if(continuation != null) {
53                         continuation = instructions[instructionId].next(g, bindings, continuation);
54                         if(continuation != IInstruction.FAILURE) {
55                                 ret.continuation = continuation;
56                                 return ret;
57                         }
58                 }
59                 
60                 for(++instructionId;instructionId<instructions.length-1;++instructionId) {
61                         continuation = instructions[instructionId].query(g, bindings);
62                         if(continuation != IInstruction.FAILURE) {
63                                 ret.continuation = continuation;
64                                 ret.instructionId = instructionId;                              
65                                 return ret;
66                         }
67                 }
68                 return instructions[instructionId].query(g, bindings);
69         }
70
71         @Override
72         public void toString(StringBuilder b, int indent) {             
73                 b.append("(    ");
74                 boolean first = true;
75                 for(IInstruction inst : instructions) {
76                         if(first)
77                                 first = false;
78                         else {
79                                 b.append(";\n");
80                                 for(int i=0;i<indent+1;++i)
81                                         b.append(INDENTATION);
82                         }
83                         inst.toString(b, indent + 1);                   
84                 }
85                 b.append('\n');
86                 for(int i=0;i<indent;++i)
87                         b.append(INDENTATION);
88                 b.append(")");          
89         }
90 }