]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/BinaryPredicateConstraint.java
(refs #7250) Error messages for undefined existentials
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / BinaryPredicateConstraint.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;
13
14 import gnu.trove.map.hash.TObjectIntHashMap;
15
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;
23
24 public class BinaryPredicateConstraint implements IConstraint {
25
26         Resource variable0;
27         Resource variable1;
28         IBinaryPredicate predicate;             
29
30         public BinaryPredicateConstraint(IBinaryPredicate predicate,
31                         Resource variable0, Resource variable1) {
32                 this.predicate = predicate;
33                 this.variable0 = variable0;
34                 this.variable1 = variable1;
35         }
36
37         @Override
38         public ImmutableSet<Resource> binds() {
39                 return ImmutableSet.of(variable0, variable1);
40         }
41
42         @Override
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);
51                         else
52                                 return new BinaryPredicateObjectQuery(id0, id1, predicate);
53                 }
54                 else {
55                         if(bound.contains(variable1))
56                                 return new BinaryPredicateSubjectQuery(id0, id1, predicate);
57                         else
58                                 throw new TooManyUnboundVariablesException();                   
59                 }
60         }
61
62         @Override
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;
69                         else
70                                 return 0;
71                 }
72                 else {
73                         if(bound.contains(variable1) && predicate.supportsGetSubjects())
74                                 return Integer.MAX_VALUE;
75                         else 
76                                 return 0;
77                 }
78         }
79
80 }