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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.mapping.constraint;
\r
14 import gnu.trove.map.hash.TObjectIntHashMap;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
\r
18 import org.simantics.mapping.constraint.instructions.BinaryPredicateInstruction;
\r
19 import org.simantics.mapping.constraint.instructions.BinaryPredicateObjectQuery;
\r
20 import org.simantics.mapping.constraint.instructions.BinaryPredicateSubjectQuery;
\r
21 import org.simantics.mapping.constraint.instructions.IInstruction;
\r
22 import org.simantics.utils.datastructures.persistent.ImmutableSet;
\r
24 public class BinaryPredicateConstraint implements IConstraint {
\r
28 IBinaryPredicate predicate;
\r
30 public BinaryPredicateConstraint(IBinaryPredicate predicate,
\r
31 Resource variable0, Resource variable1) {
\r
32 this.predicate = predicate;
\r
33 this.variable0 = variable0;
\r
34 this.variable1 = variable1;
\r
38 public ImmutableSet<Resource> binds() {
\r
39 return ImmutableSet.of(variable0, variable1);
\r
43 public IInstruction createInstruction(
\r
44 TObjectIntHashMap<Resource> variableIds,
\r
45 ImmutableSet<Resource> bound) throws TooManyUnboundVariablesException {
\r
46 int id0 = variableIds.get(variable0);
\r
47 int id1 = variableIds.get(variable1);
\r
48 if(bound.contains(variable0)) {
\r
49 if(bound.contains(variable1))
\r
50 return new BinaryPredicateInstruction(id0, id1, predicate);
\r
52 return new BinaryPredicateObjectQuery(id0, id1, predicate);
\r
55 if(bound.contains(variable1))
\r
56 return new BinaryPredicateSubjectQuery(id0, id1, predicate);
\r
58 throw new TooManyUnboundVariablesException();
\r
63 public int isApplicable(ImmutableSet<Resource> bound) {
\r
64 if(bound.contains(variable0)) {
\r
65 if(bound.contains(variable1))
\r
66 return Integer.MAX_VALUE;
\r
67 else if(predicate.supportsGetObjects())
\r
68 return Integer.MAX_VALUE;
\r
73 if(bound.contains(variable1) && predicate.supportsGetSubjects())
\r
74 return Integer.MAX_VALUE;
\r