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.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;
24 public class TripletConstraint implements IConstraint {
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;
39 public TripletConstraint(Resource variable0, Resource variable1,
41 this(variable0, variable1, variable2, null);
45 public ImmutableSet<Resource> binds() {
46 return ImmutableSet.of(variable0, variable1, variable2);
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);
61 return new TripletObjectQuery(id0, id1, id2);
64 if(bound.contains(variable2))
65 return new TripletPredicateQuery(id0, id1, id2, predicate);
67 return new TripletPredicateObjectQuery(id0, id1, id2, predicate);
71 throw new TooManyUnboundVariablesException();
76 public int isApplicable(ImmutableSet<Resource> bound) {
77 if(bound.contains(variable0))
78 return Integer.MAX_VALUE;