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