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 OrInstruction extends CombinedInstruction {
19 public OrInstruction(IInstruction... instructions) {
23 static class Continuation {
27 public Continuation(Object continuation, int instructionId) {
28 this.continuation = continuation;
29 this.instructionId = instructionId;
34 public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
36 for(instructionId=0;instructionId<instructions.length-1;++instructionId) {
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);
43 return instructions[instructionId].query(g, bindings);
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;
52 if(continuation != null) {
53 continuation = instructions[instructionId].next(g, bindings, continuation);
54 if(continuation != IInstruction.FAILURE) {
55 ret.continuation = continuation;
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;
68 return instructions[instructionId].query(g, bindings);
72 public void toString(StringBuilder b, int indent) {
75 for(IInstruction inst : instructions) {
80 for(int i=0;i<indent+1;++i)
81 b.append(INDENTATION);
83 inst.toString(b, indent + 1);
86 for(int i=0;i<indent;++i)
87 b.append(INDENTATION);