1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.mapping.rule.instructions;
\r
14 import gnu.trove.map.hash.TIntIntHashMap;
\r
15 import gnu.trove.set.hash.TIntHashSet;
\r
17 import java.util.Arrays;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.layer0.utils.triggers.IModification;
\r
23 import org.simantics.mapping.IContextualModification;
\r
24 import org.simantics.mapping.constraint.instructions.IInstruction;
\r
26 public class ClaimRuleInstruction implements IRuleInstruction {
\r
28 IInstruction instruction;
\r
29 IRuleInstruction continuation;
\r
31 public ClaimRuleInstruction(IInstruction instruction,
\r
32 IRuleInstruction continuation) {
\r
33 this.instruction = instruction;
\r
34 this.continuation = continuation;
\r
37 public ClaimRuleInstruction(IInstruction instruction) {
\r
38 this(instruction, null);
\r
42 public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
43 final IContextualModification modi = instruction.claim(g, bindings);
\r
45 final Object[] curBindings = Arrays.copyOf(bindings, bindings.length);
\r
46 return new IModification() {
\r
49 public void perform(WriteGraph g) throws DatabaseException {
\r
50 modi.perform(g, curBindings);
\r
51 if(continuation != null) {
\r
52 IModification modi2 = continuation.execute(g, curBindings);
\r
60 if(continuation != null)
\r
61 return continuation.execute(g, bindings);
\r
66 public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
67 instruction.doClaim(g, bindings);
\r
71 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
\r
72 instruction.collectVariables(reads, writes);
\r
73 if(continuation != null)
\r
74 continuation.collectVariables(reads, writes);
\r
78 public void mapVariables(TIntIntHashMap map) {
\r
79 instruction.mapVariables(map);
\r
80 if(continuation != null)
\r
81 continuation.mapVariables(map);
\r
84 public void toString(StringBuilder b, int indent) {
\r
86 instruction.toString(b, indent+1);
\r
87 if(continuation != null) {
\r
89 for(int i=0;i<indent;++i)
\r
90 b.append(INDENTATION);
\r
92 continuation.toString(b, indent+1);
\r