--- /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;\r
+\r
+import gnu.trove.map.hash.TObjectIntHashMap;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;\r
+import org.simantics.mapping.constraint.instructions.BinaryPredicateInstruction;\r
+import org.simantics.mapping.constraint.instructions.BinaryPredicateObjectQuery;\r
+import org.simantics.mapping.constraint.instructions.BinaryPredicateSubjectQuery;\r
+import org.simantics.mapping.constraint.instructions.IInstruction;\r
+import org.simantics.utils.datastructures.persistent.ImmutableSet;\r
+\r
+public class BinaryPredicateConstraint implements IConstraint {\r
+\r
+ Resource variable0;\r
+ Resource variable1;\r
+ IBinaryPredicate predicate; \r
+\r
+ public BinaryPredicateConstraint(IBinaryPredicate predicate,\r
+ Resource variable0, Resource variable1) {\r
+ this.predicate = predicate;\r
+ this.variable0 = variable0;\r
+ this.variable1 = variable1;\r
+ }\r
+\r
+ @Override\r
+ public ImmutableSet<Resource> binds() {\r
+ return ImmutableSet.of(variable0, variable1);\r
+ }\r
+\r
+ @Override\r
+ public IInstruction createInstruction(\r
+ TObjectIntHashMap<Resource> variableIds,\r
+ ImmutableSet<Resource> bound) throws TooManyUnboundVariablesException {\r
+ int id0 = variableIds.get(variable0);\r
+ int id1 = variableIds.get(variable1);\r
+ if(bound.contains(variable0)) {\r
+ if(bound.contains(variable1))\r
+ return new BinaryPredicateInstruction(id0, id1, predicate);\r
+ else\r
+ return new BinaryPredicateObjectQuery(id0, id1, predicate);\r
+ }\r
+ else {\r
+ if(bound.contains(variable1))\r
+ return new BinaryPredicateSubjectQuery(id0, id1, predicate);\r
+ else\r
+ throw new TooManyUnboundVariablesException(); \r
+ }\r
+ }\r
+\r
+ @Override\r
+ public int isApplicable(ImmutableSet<Resource> bound) {\r
+ if(bound.contains(variable0)) {\r
+ if(bound.contains(variable1))\r
+ return Integer.MAX_VALUE;\r
+ else if(predicate.supportsGetObjects())\r
+ return Integer.MAX_VALUE;\r
+ else\r
+ return 0;\r
+ }\r
+ else {\r
+ if(bound.contains(variable1) && predicate.supportsGetSubjects())\r
+ return Integer.MAX_VALUE;\r
+ else \r
+ return 0;\r
+ }\r
+ }\r
+\r
+}\r