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.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.util.RemoverUtil;
23 import org.simantics.mapping.IContextualModification;
25 public class ExistsInstruction implements IInstruction {
27 IInstruction instruction;
30 public ExistsInstruction(IInstruction instruction, int ... variables) {
31 this.instruction = instruction;
32 this.variables = variables;
36 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
37 instruction.collectVariables(reads, writes);
40 class Claim implements IContextualModification {
46 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
47 // ITask task = ThreadLogger.getInstance().begin("g");
48 for(int var : variables) {
49 bindings[var] = DISABLE_MODI ? null : g.newResource();
51 System.out.println("@ newResource " + NameUtils.getSafeName(g, (Resource)bindings[var]));
53 System.out.println("claim exists " + NameUtils.getSafeName(g, (Resource)bindings[var]));
56 instruction.doClaim(g, bindings);
61 public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {
62 if(instruction.query(g, bindings)==IInstruction.FAILURE) {
63 // System.out.println("Creates Claim");
69 class Deny implements IContextualModification {
72 public Deny(Object continuation) {
73 this.continuation = continuation;
77 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
78 Object continuation = this.continuation;
80 for(int var : variables) {
82 System.out.println("deny exists " +
83 NameUtils.getSafeName(g, (Resource)bindings[var]));
85 RemoverUtil.remove(g, (Resource)bindings[var]);
87 continuation = instruction.next(g, bindings, continuation);
88 } while(continuation != null && continuation != IInstruction.FAILURE);
93 public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
94 Object continuation = instruction.query(g, bindings);
95 if(continuation != IInstruction.FAILURE)
96 return new Deny(continuation);
101 public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
102 if(instruction.query(g, bindings)==IInstruction.FAILURE) {
103 // ITask task = ThreadLogger.getInstance().begin("g");
104 for(int var : variables) {
105 bindings[var] = DISABLE_MODI ? null : g.newResource();
107 System.out.println("@ newResource " + NameUtils.getSafeName(g, (Resource)bindings[var]));
109 System.out.println("claim exists " + NameUtils.getSafeName(g, (Resource)bindings[var]));
112 instruction.doClaim(g, bindings);
117 public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
118 Object continuation = instruction.query(g, bindings);
119 if(continuation != IInstruction.FAILURE)
121 for(int var : variables) {
123 System.out.println("deny exists " +
124 NameUtils.getSafeName(g, (Resource)bindings[var]));
126 RemoverUtil.remove(g, (Resource)bindings[var]);
128 continuation = instruction.next(g, bindings, continuation);
129 } while(continuation != null && continuation != IInstruction.FAILURE);
133 public void mapVariables(TIntIntHashMap map) {
134 instruction.mapVariables(map);
135 for(int i=0;i<variables.length;++i)
136 variables[i] = map.get(variables[i]);
140 public Object next(ReadGraph g, Object[] bindings, Object continuation) throws DatabaseException {
141 return instruction.next(g, bindings, continuation);
145 public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
146 return instruction.query(g, bindings);
150 public void toString(StringBuilder b, int indent) {
152 boolean first = true;
153 for(int v : variables) {
161 instruction.toString(b, indent+1);