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.predicates.IUnaryPredicate;
\r
20 import org.simantics.layer0.utils.predicates.Type;
\r
21 import org.simantics.mapping.IContextualModification;
\r
23 public class UnaryPredicateInstruction extends Instruction1 {
\r
25 IUnaryPredicate predicate;
\r
27 public UnaryPredicateInstruction(int variable0, IUnaryPredicate predicate) {
\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])) {
\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 r = (Resource)bindings[variable0];
\r
53 // ITask task = ThreadLogger.getInstance().begin("g");
\r
54 if(!predicate.has(g, r)) {
\r
56 return new IContextualModification() {
\r
59 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
60 // ITask task = ThreadLogger.getInstance().begin("g");
\r
62 System.out.println("claim(" + predicate + ", " +
\r
63 NameUtils.getSafeName(g, (Resource)bindings[variable0]));
\r
65 predicate.add(g, (Resource)bindings[variable0]);
\r
76 public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
77 Resource r0 = (Resource)bindings[variable0];
\r
78 // ITask task = ThreadLogger.getInstance().begin("g");
\r
80 String predicateName = predicate.toString();
\r
81 if(predicate instanceof Type)
\r
82 predicateName = ":: " + NameUtils.getSafeName(g, ((Type)predicate).getType());
\r
83 System.out.println("@ claimUnary(" + NameUtils.getSafeName(g, r0) + ", " + predicateName + ")");
\r
86 System.out.println("claim(" + predicate + ", " +
\r
87 NameUtils.getSafeName(g, (Resource)bindings[variable0]));
\r
89 predicate.add(g, r0);
\r
94 public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
95 Resource r = (Resource)bindings[variable0];
\r
96 if(predicate.has(g, r))
\r
97 return new IContextualModification() {
\r
100 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
102 System.out.println("deny(" + predicate + ", " +
\r
103 NameUtils.getSafeName(g, (Resource)bindings[variable0]));
\r
105 predicate.remove(g, (Resource)bindings[variable0]);
\r
113 public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
114 Resource r0 = (Resource)bindings[variable0];
\r
115 if(predicate.has(g, r0)) {
\r
117 System.out.println("deny(" + predicate + ", " +
\r
118 NameUtils.getSafeName(g, (Resource)bindings[variable0]));
\r
120 predicate.remove(g, r0);
\r
125 public void toString(StringBuilder b, int indent) {
\r
127 b.append(variable0);
\r