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.rule.instructions;
14 import gnu.trove.map.hash.TIntIntHashMap;
15 import gnu.trove.set.hash.TIntHashSet;
17 import java.util.Arrays;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.utils.triggers.IModification;
23 import org.simantics.mapping.IContextualModification;
24 import org.simantics.mapping.constraint.instructions.IInstruction;
26 public class ClaimRuleInstruction implements IRuleInstruction {
28 IInstruction instruction;
29 IRuleInstruction continuation;
31 public ClaimRuleInstruction(IInstruction instruction,
32 IRuleInstruction continuation) {
33 this.instruction = instruction;
34 this.continuation = continuation;
37 public ClaimRuleInstruction(IInstruction instruction) {
38 this(instruction, null);
42 public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {
43 final IContextualModification modi = instruction.claim(g, bindings);
45 final Object[] curBindings = Arrays.copyOf(bindings, bindings.length);
46 return new IModification() {
49 public void perform(WriteGraph g) throws DatabaseException {
50 modi.perform(g, curBindings);
51 if(continuation != null) {
52 IModification modi2 = continuation.execute(g, curBindings);
60 if(continuation != null)
61 return continuation.execute(g, bindings);
66 public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {
67 instruction.doClaim(g, bindings);
71 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
72 instruction.collectVariables(reads, writes);
73 if(continuation != null)
74 continuation.collectVariables(reads, writes);
78 public void mapVariables(TIntIntHashMap map) {
79 instruction.mapVariables(map);
80 if(continuation != null)
81 continuation.mapVariables(map);
84 public void toString(StringBuilder b, int indent) {
86 instruction.toString(b, indent+1);
87 if(continuation != null) {
89 for(int i=0;i<indent;++i)
90 b.append(INDENTATION);
92 continuation.toString(b, indent+1);