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 org.simantics.db.ReadGraph;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.utils.triggers.IModification;
21 import org.simantics.mapping.constraint.instructions.IInstruction;
23 public class IfRuleInstruction implements IRuleInstruction {
24 IInstruction condition;
25 IRuleInstruction rule;
26 IRuleInstruction elseRule;
28 public IfRuleInstruction(IInstruction condition, IRuleInstruction rule, IRuleInstruction elseRule) {
29 this.condition = condition;
31 this.elseRule = elseRule;
34 public IfRuleInstruction(IInstruction condition, IRuleInstruction rule) {
35 this(condition, rule, null);
39 public IModification execute(ReadGraph g, final Object[] bindings) throws DatabaseException {
41 System.out.println("IfRuleInstruction: condition");
42 Object cont = condition.query(g, bindings);
43 if(cont == IInstruction.FAILURE) {
45 System.out.println("IfRuleInstruction: else");
47 return elseRule.execute(g, bindings);
53 System.out.println("IfRuleInstruction: modification");
54 final IModification modi = rule.execute(g, bindings);
58 final Object curCont = condition.next(g, bindings, cont);
59 if(curCont==IInstruction.FAILURE)
61 return new IModification() {
63 public void perform(WriteGraph g) throws DatabaseException {
65 Object cont = curCont;
67 rule.doExecute(g, bindings);
70 cont = condition.next(g, bindings, cont);
71 } while(cont != IInstruction.FAILURE);
77 cont = condition.next(g, bindings, cont);
78 } while(cont != IInstruction.FAILURE);
83 public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {
84 Object cont = condition.query(g, bindings);
85 if(cont == IInstruction.FAILURE) {
87 elseRule.doExecute(g, bindings);
91 rule.doExecute(g, bindings);
94 cont = condition.next(g, bindings, cont);
95 } while(cont != IInstruction.FAILURE);
99 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
100 condition.collectVariables(reads, writes);
101 rule.collectVariables(reads, writes);
103 elseRule.collectVariables(reads, writes);
107 public void mapVariables(TIntIntHashMap map) {
108 condition.mapVariables(map);
109 rule.mapVariables(map);
111 elseRule.mapVariables(map);
115 public void toString(StringBuilder b, int indent) {
117 condition.toString(b, indent+1);
120 for(int i=0;i<indent;++i)
121 b.append(INDENTATION);
123 rule.toString(b, indent+1);
125 if(elseRule != null) {
127 for(int i=0;i<indent;++i)
128 b.append(INDENTATION);
130 elseRule.toString(b, indent+1);