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