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.constraint.instructions;
\r
14 import org.simantics.db.ReadGraph;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.WriteGraph;
\r
17 import org.simantics.db.common.utils.NameUtils;
\r
18 import org.simantics.db.exception.DatabaseException;
\r
19 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
\r
20 import org.simantics.mapping.IContextualModification;
\r
22 public class BinaryPredicateInstruction extends Instruction2 {
\r
24 IBinaryPredicate predicate;
\r
26 public BinaryPredicateInstruction(int variable0, int variable1,
\r
27 IBinaryPredicate predicate) {
\r
28 super(variable0, variable1);
\r
29 this.predicate = predicate;
\r
33 public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
34 // ITask task = ThreadLogger.getInstance().begin("g");
\r
35 if(predicate.has(g, (Resource)bindings[variable0], (Resource)bindings[variable1])) {
\r
41 return IInstruction.FAILURE;
\r
46 public Object next(ReadGraph g, Object[] bindings, Object continuation) {
\r
47 return IInstruction.FAILURE;
\r
51 public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
52 Resource r0 = (Resource)bindings[variable0];
\r
53 Resource r1 = (Resource)bindings[variable1];
\r
54 // ITask task = ThreadLogger.getInstance().begin("g");
\r
55 if(!predicate.has(g, r0, r1)) {
\r
57 return new IContextualModification() {
\r
60 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
61 Resource r0 = (Resource)bindings[variable0];
\r
62 Resource r1 = (Resource)bindings[variable1];
\r
64 System.out.println("claim(" +
\r
65 NameUtils.getSafeName(g, r0) + ", " +
\r
66 predicate.toString(g) + ", " +
\r
67 NameUtils.getSafeName(g, r1) + ")"
\r
70 predicate.add(g, r0, r1);
\r
80 public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
81 Resource r0 = (Resource)bindings[variable0];
\r
82 Resource r1 = (Resource)bindings[variable1];
\r
83 // ITask task = ThreadLogger.getInstance().begin("g");
\r
85 System.out.println("@ claim(" + NameUtils.getSafeName(g, r0) +
\r
86 ", " + predicate.toString(g) + ", " + NameUtils.getSafeName(g, r1) + ")");
\r
88 System.out.println("claim(" +
\r
89 NameUtils.getSafeName(g, r0) + ", " +
\r
90 predicate.toString(g) + ", " +
\r
91 NameUtils.getSafeName(g, r1) + ")"
\r
94 predicate.add(g, r0, r1);
\r
99 public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
100 Resource r0 = (Resource)bindings[variable0];
\r
101 Resource r1 = (Resource)bindings[variable1];
\r
102 if(predicate.has(g, r0, r1))
\r
103 return new IContextualModification() {
\r
106 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
107 Resource r0 = (Resource)bindings[variable0];
\r
108 Resource r1 = (Resource)bindings[variable1];
\r
110 System.out.println("deny(" +
\r
111 NameUtils.getSafeName(g, r0) + ", " +
\r
112 predicate.toString(g) + ", " +
\r
113 NameUtils.getSafeName(g, r1) + ")"
\r
116 predicate.remove(g, r0, r1);
\r
124 public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
125 Resource r0 = (Resource)bindings[variable0];
\r
126 Resource r1 = (Resource)bindings[variable1];
\r
128 System.out.println("deny(" +
\r
129 NameUtils.getSafeName(g, r0) + ", " +
\r
130 predicate.toString(g) + ", " +
\r
131 NameUtils.getSafeName(g, r1) + ")"
\r
134 predicate.remove(g, r0, r1);
\r
138 public void toString(StringBuilder b, int indent) {
\r
140 b.append(variable0);
\r
142 b.append(variable1);
\r