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 gnu.trove.map.hash.TIntIntHashMap;
15 import gnu.trove.set.hash.TIntHashSet;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.mapping.IContextualModification;
22 public abstract class CombinedInstruction implements IInstruction {
23 IInstruction[] instructions;
25 public CombinedInstruction(IInstruction... instructions) {
26 this.instructions = instructions;
29 class CompositeClaim implements IContextualModification {
31 IContextualModification modification;
34 public CompositeClaim(IContextualModification modification, int instructionId) {
35 this.modification = modification;
36 this.instructionId = instructionId;
40 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
41 modification.perform(g, bindings);
42 for(int i=instructionId+1;i<instructions.length;++i)
43 instructions[i].doClaim(g, bindings);
49 public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {
50 for(int instructionId=0;instructionId<instructions.length;++instructionId) {
51 IContextualModification modification = instructions[instructionId].claim(g, bindings);
52 if(modification != null) {
53 if(instructionId==instructions.length-1)
56 return new CompositeClaim(modification, instructionId);
62 class CompositeDeny implements IContextualModification {
64 IContextualModification modification;
67 public CompositeDeny(IContextualModification modification, int instructionId) {
68 this.modification = modification;
69 this.instructionId = instructionId;
73 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
74 modification.perform(g, bindings);
75 for(int i=instructionId+1;i<instructions.length;++i)
76 instructions[i].doDeny(g, bindings);
82 public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
83 for(int instructionId=0;instructionId<instructions.length;++instructionId) {
84 IContextualModification modification = instructions[instructionId].deny(g, bindings);
85 if(modification != null) {
86 if(instructionId==instructions.length-1)
89 return new CompositeDeny(modification, instructionId);
96 public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
97 for(int i=0;i<instructions.length;++i)
98 instructions[i].doClaim(g, bindings);
102 public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
103 for(int i=0;i<instructions.length;++i)
104 instructions[i].doDeny(g, bindings);
108 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
109 for(IInstruction inst : instructions)
110 inst.collectVariables(reads, writes);
114 public void mapVariables(TIntIntHashMap map) {
115 for(IInstruction inst : instructions)
116 inst.mapVariables(map);