]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/TripletConstraint.java
(refs #7250) Error messages for undefined existentials
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / TripletConstraint.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.mapping.constraint.instructions.IInstruction;
18 import org.simantics.mapping.constraint.instructions.TripletInstruction;
19 import org.simantics.mapping.constraint.instructions.TripletObjectQuery;
20 import org.simantics.mapping.constraint.instructions.TripletPredicateObjectQuery;
21 import org.simantics.mapping.constraint.instructions.TripletPredicateQuery;
22 import org.simantics.utils.datastructures.persistent.ImmutableSet;
23
24 public class TripletConstraint implements IConstraint {
25
26         Resource variable0;
27         Resource variable1;
28         Resource variable2;
29         Resource predicate;             
30
31         public TripletConstraint(Resource variable0, Resource variable1,
32                         Resource variable2, Resource predicate) {               
33                 this.variable0 = variable0;
34                 this.variable1 = variable1;
35                 this.variable2 = variable2;
36                 this.predicate = predicate;
37         }
38         
39         public TripletConstraint(Resource variable0, Resource variable1,
40                         Resource variable2) {
41                 this(variable0, variable1, variable2, null);
42         }
43
44         @Override
45         public ImmutableSet<Resource> binds() {
46                 return ImmutableSet.of(variable0, variable1, variable2);
47         }
48
49         @Override
50         public IInstruction createInstruction(
51                         TObjectIntHashMap<Resource> variableIds,
52                         ImmutableSet<Resource> bound) throws TooManyUnboundVariablesException {
53                 int id0 = variableIds.get(variable0);
54                 int id1 = variableIds.get(variable1);
55                 int id2 = variableIds.get(variable2);
56                 if(bound.contains(variable0)) {
57                         if(bound.contains(variable1)) {
58                                 if(bound.contains(variable2))
59                                         return new TripletInstruction(id0, id1, id2);
60                                 else
61                                         return new TripletObjectQuery(id0, id1, id2);
62                         }
63                         else {
64                                 if(bound.contains(variable2))
65                                         return new TripletPredicateQuery(id0, id1, id2, predicate);
66                                 else
67                                         return new TripletPredicateObjectQuery(id0, id1, id2, predicate);
68                         }               
69                 }
70                 else {
71                         throw new TooManyUnboundVariablesException();                   
72                 }
73         }
74
75         @Override
76         public int isApplicable(ImmutableSet<Resource> bound) {
77                 if(bound.contains(variable0))
78                         return Integer.MAX_VALUE;
79                 else
80                         return 0;
81         }
82
83 }