]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/BinaryPredicateInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / BinaryPredicateInstruction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.mapping.constraint.instructions;
13
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;
21
22 public class BinaryPredicateInstruction extends Instruction2 {
23         
24         IBinaryPredicate predicate;
25         
26         public BinaryPredicateInstruction(int variable0, int variable1,
27                         IBinaryPredicate predicate) {
28                 super(variable0, variable1);
29                 this.predicate = predicate;
30         }
31
32         @Override
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])) {
36 //                      task.finish();
37                         return null;
38                 }
39                 else {
40 //                      task.finish();
41                         return IInstruction.FAILURE;
42                 }
43         }
44         
45         @Override
46         public Object next(ReadGraph g, Object[] bindings, Object continuation) {
47                 return IInstruction.FAILURE;
48         }
49         
50         @Override
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)) {
56 //                      task.finish();          
57                         return new IContextualModification() {
58
59                                 @Override
60                                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
61                                         Resource r0 = (Resource)bindings[variable0];
62                                         Resource r1 = (Resource)bindings[variable1];
63                                         if(DEBUG_MODI)
64                                             System.out.println("claim(" +
65                                                     NameUtils.getSafeName(g, r0) + ", " +
66                                                     predicate.toString(g) + ", " +
67                                                     NameUtils.getSafeName(g, r1) + ")"
68                                                     );
69                                         if(!DISABLE_MODI)
70                                                 predicate.add(g, r0, r1);
71                                 }
72                         
73                         };
74                 }
75 //              task.finish();
76                 return null;
77         }
78
79         @Override
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");
84                 if(DEBUG)
85                 System.out.println("@ claim(" + NameUtils.getSafeName(g, r0) + 
86                         ", " + predicate.toString(g) + ", " +  NameUtils.getSafeName(g, r1) + ")");
87                 if(DEBUG_MODI)
88             System.out.println("claim(" +
89                     NameUtils.getSafeName(g, r0) + ", " +
90                     predicate.toString(g) + ", " +
91                     NameUtils.getSafeName(g, r1) + ")"
92                     );
93                 if(!DISABLE_MODI)
94                         predicate.add(g, r0, r1);
95 //              task.finish();
96         }
97         
98         @Override
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() {
104
105                                 @Override
106                                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
107                                         Resource r0 = (Resource)bindings[variable0];
108                                         Resource r1 = (Resource)bindings[variable1];
109                                         if(DEBUG_MODI)
110                         System.out.println("deny(" +
111                                 NameUtils.getSafeName(g, r0) + ", " +
112                                 predicate.toString(g) + ", " +
113                                 NameUtils.getSafeName(g, r1) + ")"
114                                 );
115                                         if(!DISABLE_MODI)
116                                                 predicate.remove(g, r0, r1);
117                                 }
118                         
119                         };
120                 return null;
121         }
122
123         @Override
124         public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {      
125                 Resource r0 = (Resource)bindings[variable0];
126                 Resource r1 = (Resource)bindings[variable1];
127                 if(DEBUG_MODI)
128             System.out.println("deny(" +
129                     NameUtils.getSafeName(g, r0) + ", " +
130                     predicate.toString(g) + ", " +
131                     NameUtils.getSafeName(g, r1) + ")"
132                     );
133                 if(!DISABLE_MODI)
134                         predicate.remove(g, r0, r1);
135         }
136         
137         @Override
138         public void toString(StringBuilder b, int indent) {
139                 b.append('(');
140                 b.append(variable0);
141                 b.append(',');
142                 b.append(variable1);
143                 b.append(')');
144         }
145 }