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