]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/TripletInstruction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / TripletInstruction.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.mapping.IContextualModification;
20
21 public class TripletInstruction extends Instruction3 {
22         
23         public TripletInstruction(int variable0, int variable1, int variable2) {
24                 super(variable0, variable1, variable2);
25         }
26
27         @Override
28         public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {
29                 Resource r0 = (Resource)bindings[variable0];
30                 Resource r1 = (Resource)bindings[variable1];
31                 Resource r2 = (Resource)bindings[variable2];
32 //              ITask task = ThreadLogger.getInstance().begin("g");
33                 if(!g.hasStatement(r0, r1, r2)) {
34 //                      task.finish();          
35                         return new IContextualModification() {
36
37                                 @Override
38                                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
39                                         Resource r0 = (Resource)bindings[variable0];
40                                         Resource r1 = (Resource)bindings[variable1];
41                                         Resource r2 = (Resource)bindings[variable2];
42                                         if(DEBUG_MODI)
43                         System.out.println("claim(" +
44                                 NameUtils.getSafeName(g, r0) + ", " +
45                                 NameUtils.getSafeName(g, r1) + ", " +
46                                 NameUtils.getSafeName(g, r2) + ")"
47                                 );
48                                         if(!DISABLE_MODI)
49                                                 g.claim(r0, r1, r2);
50                                 }
51                         
52                         };
53                 }
54 //              task.finish();
55                 return null;
56         }
57         
58         @Override
59         public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {
60                 Resource r0 = (Resource)bindings[variable0];
61                 Resource r1 = (Resource)bindings[variable1];
62                 Resource r2 = (Resource)bindings[variable2];
63                 if(DEBUG) {
64                 System.out.println("@ claim(" +
65                     NameUtils.getSafeName(g, r0) + ", " +
66                     NameUtils.getSafeName(g, r1) + ", " +
67                     NameUtils.getSafeName(g, r2) + ")");
68                 }
69                 if(DEBUG_MODI)
70             System.out.println("claim(" +
71                     NameUtils.getSafeName(g, r0) + ", " +
72                     NameUtils.getSafeName(g, r1) + ", " +
73                     NameUtils.getSafeName(g, r2) + ")"
74                     );
75 //              ITask task = ThreadLogger.getInstance().begin("g");     
76                 if(!DISABLE_MODI)
77                         g.claim(r0, r1, r2);
78 //              task.finish();
79         }
80
81         @Override
82         public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {
83                 Resource r0 = (Resource)bindings[variable0];
84                 Resource r1 = (Resource)bindings[variable1];
85                 Resource r2 = (Resource)bindings[variable2];
86                 if(g.hasStatement(r0, r1, r2))
87                         return new IContextualModification() {
88
89                                 @Override
90                                 public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {
91                                         Resource r0 = (Resource)bindings[variable0];
92                                         Resource r1 = (Resource)bindings[variable1];
93                                         Resource r2 = (Resource)bindings[variable2];
94                                         if(DEBUG_MODI)
95                         System.out.println("deny(" +
96                                 NameUtils.getSafeName(g, r0) + ", " +
97                                 NameUtils.getSafeName(g, r1) + ", " +
98                                 NameUtils.getSafeName(g, r2) + ")"
99                                 );
100                                         if(!DISABLE_MODI)
101                                                 g.denyStatement(r0, r1, r2);
102                                 }
103                         
104                         };
105                 return null;
106         }
107         
108         @Override
109         public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException {
110                 Resource r0 = (Resource)bindings[variable0];
111                 Resource r1 = (Resource)bindings[variable1];
112                 Resource r2 = (Resource)bindings[variable2];
113                 if(DEBUG) {
114                 System.out.println("deny(" +
115                         NameUtils.getSafeName(g, r0) + ", " +
116                         NameUtils.getSafeName(g, r1) + ", " +
117                         NameUtils.getSafeName(g, r2) + ")");
118                 }
119                 if(DEBUG_MODI)
120             System.out.println("deny(" +
121                     NameUtils.getSafeName(g, r0) + ", " +
122                     NameUtils.getSafeName(g, r1) + ", " +
123                     NameUtils.getSafeName(g, r2) + ")"
124                     );
125                 if(!DISABLE_MODI)
126                         g.denyStatement(r0, r1, r2);            
127         }
128
129         @Override
130         public Object next(ReadGraph g, Object[] bindings, Object continuation) throws DatabaseException {
131                 return IInstruction.FAILURE;
132         }
133
134         @Override
135         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
136                 Resource r0 = (Resource)bindings[variable0];
137                 Resource r1 = (Resource)bindings[variable1];
138                 Resource r2 = (Resource)bindings[variable2];
139                 if(DEBUG) {
140                 System.out.print("hasStatement(" +
141                     NameUtils.getSafeName(g, r0) + ", " +
142                     NameUtils.getSafeName(g, r1) + ", " +
143                     NameUtils.getSafeName(g, r2) + ")");
144                 }
145                 if(g.hasStatement(r0, r1, r2)) {
146                     if(DEBUG)
147                         System.out.println(" -> true");
148                         return null;
149                 }
150                 else {
151                     if(DEBUG)
152                         System.out.println(" -> false");
153                         return IInstruction.FAILURE;
154                 }
155         }
156
157         @Override
158         public void toString(StringBuilder b, int indent) {
159                 b.append('<');
160                 b.append(variable0);
161                 b.append(',');
162                 b.append(variable1);
163                 b.append(',');
164                 b.append(variable2);
165                 b.append('>');
166         }
167 }