--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.mapping.constraint.instructions;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;\r
+import org.simantics.mapping.IContextualModification;\r
+\r
+public class BinaryPredicateInstruction extends Instruction2 {\r
+ \r
+ IBinaryPredicate predicate;\r
+ \r
+ public BinaryPredicateInstruction(int variable0, int variable1,\r
+ IBinaryPredicate predicate) {\r
+ super(variable0, variable1);\r
+ this.predicate = predicate;\r
+ }\r
+\r
+ @Override\r
+ public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {\r
+// ITask task = ThreadLogger.getInstance().begin("g");\r
+ if(predicate.has(g, (Resource)bindings[variable0], (Resource)bindings[variable1])) {\r
+// task.finish();\r
+ return null;\r
+ }\r
+ else {\r
+// task.finish();\r
+ return IInstruction.FAILURE;\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public Object next(ReadGraph g, Object[] bindings, Object continuation) {\r
+ return IInstruction.FAILURE;\r
+ }\r
+ \r
+ @Override\r
+ public IContextualModification claim(ReadGraph g, Object[] bindings) throws DatabaseException {\r
+ Resource r0 = (Resource)bindings[variable0];\r
+ Resource r1 = (Resource)bindings[variable1];\r
+// ITask task = ThreadLogger.getInstance().begin("g");\r
+ if(!predicate.has(g, r0, r1)) {\r
+// task.finish(); \r
+ return new IContextualModification() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {\r
+ Resource r0 = (Resource)bindings[variable0];\r
+ Resource r1 = (Resource)bindings[variable1];\r
+ if(DEBUG_MODI)\r
+ System.out.println("claim(" +\r
+ NameUtils.getSafeName(g, r0) + ", " +\r
+ predicate.toString(g) + ", " +\r
+ NameUtils.getSafeName(g, r1) + ")"\r
+ );\r
+ if(!DISABLE_MODI)\r
+ predicate.add(g, r0, r1);\r
+ }\r
+ \r
+ };\r
+ }\r
+// task.finish();\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void doClaim(WriteGraph g, Object[] bindings) throws DatabaseException {\r
+ Resource r0 = (Resource)bindings[variable0];\r
+ Resource r1 = (Resource)bindings[variable1];\r
+// ITask task = ThreadLogger.getInstance().begin("g");\r
+ if(DEBUG)\r
+ System.out.println("@ claim(" + NameUtils.getSafeName(g, r0) + \r
+ ", " + predicate.toString(g) + ", " + NameUtils.getSafeName(g, r1) + ")");\r
+ if(DEBUG_MODI)\r
+ System.out.println("claim(" +\r
+ NameUtils.getSafeName(g, r0) + ", " +\r
+ predicate.toString(g) + ", " +\r
+ NameUtils.getSafeName(g, r1) + ")"\r
+ );\r
+ if(!DISABLE_MODI)\r
+ predicate.add(g, r0, r1);\r
+// task.finish();\r
+ }\r
+ \r
+ @Override\r
+ public IContextualModification deny(ReadGraph g, Object[] bindings) throws DatabaseException {\r
+ Resource r0 = (Resource)bindings[variable0];\r
+ Resource r1 = (Resource)bindings[variable1];\r
+ if(predicate.has(g, r0, r1))\r
+ return new IContextualModification() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph g, Object[] bindings) throws DatabaseException {\r
+ Resource r0 = (Resource)bindings[variable0];\r
+ Resource r1 = (Resource)bindings[variable1];\r
+ if(DEBUG_MODI)\r
+ System.out.println("deny(" +\r
+ NameUtils.getSafeName(g, r0) + ", " +\r
+ predicate.toString(g) + ", " +\r
+ NameUtils.getSafeName(g, r1) + ")"\r
+ );\r
+ if(!DISABLE_MODI)\r
+ predicate.remove(g, r0, r1);\r
+ }\r
+ \r
+ };\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void doDeny(WriteGraph g, Object[] bindings) throws DatabaseException { \r
+ Resource r0 = (Resource)bindings[variable0];\r
+ Resource r1 = (Resource)bindings[variable1];\r
+ if(DEBUG_MODI)\r
+ System.out.println("deny(" +\r
+ NameUtils.getSafeName(g, r0) + ", " +\r
+ predicate.toString(g) + ", " +\r
+ NameUtils.getSafeName(g, r1) + ")"\r
+ );\r
+ if(!DISABLE_MODI)\r
+ predicate.remove(g, r0, r1);\r
+ }\r
+ \r
+ @Override\r
+ public void toString(StringBuilder b, int indent) {\r
+ b.append('(');\r
+ b.append(variable0);\r
+ b.append(',');\r
+ b.append(variable1);\r
+ b.append(')');\r
+ }\r
+}\r