]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/BinaryPredicateConstraint.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / BinaryPredicateConstraint.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.mapping.constraint;\r
13 \r
14 import gnu.trove.map.hash.TObjectIntHashMap;\r
15 \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
23 \r
24 public class BinaryPredicateConstraint implements IConstraint {\r
25 \r
26         Resource variable0;\r
27         Resource variable1;\r
28         IBinaryPredicate predicate;             \r
29 \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
35         }\r
36 \r
37         @Override\r
38         public ImmutableSet<Resource> binds() {\r
39                 return ImmutableSet.of(variable0, variable1);\r
40         }\r
41 \r
42         @Override\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
51                         else\r
52                                 return new BinaryPredicateObjectQuery(id0, id1, predicate);\r
53                 }\r
54                 else {\r
55                         if(bound.contains(variable1))\r
56                                 return new BinaryPredicateSubjectQuery(id0, id1, predicate);\r
57                         else\r
58                                 throw new TooManyUnboundVariablesException();                   \r
59                 }\r
60         }\r
61 \r
62         @Override\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
69                         else\r
70                                 return 0;\r
71                 }\r
72                 else {\r
73                         if(bound.contains(variable1) && predicate.supportsGetSubjects())\r
74                                 return Integer.MAX_VALUE;\r
75                         else \r
76                                 return 0;\r
77                 }\r
78         }\r
79 \r
80 }\r