1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.mapping.constraint.instructions;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.exception.DatabaseException;
17 public class AndInstruction extends CombinedInstruction {
19 public AndInstruction(IInstruction... instructions) {
23 static class Continuation {
28 public Continuation(Object continuation, int instructionId,
30 this.continuation = continuation;
31 this.instructionId = instructionId;
38 public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
39 Continuation ret = null;
40 int instructionId = 0;
41 while(instructionId < instructions.length) {
43 System.out.println("AndInstruction.query " + instructionId + "/" + instructions.length);
44 Object continuation = instructions[instructionId].query(g, bindings);
45 if(continuation == IInstruction.FAILURE) {
48 return IInstruction.FAILURE;
49 instructionId = ret.instructionId;
50 continuation = instructions[instructionId].next(g, bindings, ret.continuation);
51 if(continuation == IInstruction.FAILURE) {
55 if(continuation == null)
58 ret.continuation = continuation;
62 else if(continuation != null)
63 ret = new Continuation(continuation, instructionId, ret);
70 public Object next(ReadGraph g, Object[] bindings, Object ret_) throws DatabaseException {
71 Continuation ret = (Continuation)ret_;
75 return IInstruction.FAILURE;
76 instructionId = ret.instructionId;
78 instructions[instructionId].next(g, bindings, ret.continuation);
79 if(continuation == IInstruction.FAILURE) {
83 if(continuation == null)
86 ret.continuation = continuation;
90 while(instructionId < instructions.length) {
91 Object continuation = instructions[instructionId].query(g, bindings);
92 if(continuation == IInstruction.FAILURE) {
95 return IInstruction.FAILURE;
96 instructionId = ret.instructionId;
97 continuation = instructions[instructionId].next(g, bindings, ret.continuation);
98 if(continuation == IInstruction.FAILURE) {
102 if(continuation == null)
105 ret.continuation = continuation;
109 else if(continuation != null)
110 ret = new Continuation(continuation, instructionId, ret);
117 public void toString(StringBuilder b, int indent) {
119 boolean first = true;
120 for(IInstruction inst : instructions) {
125 for(int i=0;i<indent+1;++i)
126 b.append(INDENTATION);
128 inst.toString(b, indent + 1);
131 for(int i=0;i<indent;++i)
132 b.append(INDENTATION);