]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/ConstraintConjunction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / ConstraintConjunction.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.AndInstruction;
18 import org.simantics.mapping.constraint.instructions.IInstruction;
19 import org.simantics.utils.datastructures.persistent.ImmutableSet;
20
21 public class ConstraintConjunction implements IConstraint {
22
23         IConstraint[] constraints;
24         
25         public ConstraintConjunction(IConstraint... constraints) {
26                 this.constraints = constraints;
27         }
28
29         @Override
30         public ImmutableSet<Resource> binds() {
31                 ImmutableSet<Resource> ret = ImmutableSet.empty();
32                 for(IConstraint c : constraints)
33                         ret = ret.addAll(c.binds());
34                 return ret;
35         }
36
37         @Override
38         public IInstruction createInstruction(
39                         TObjectIntHashMap<Resource> variableIds,
40                         ImmutableSet<Resource> bound) throws TooManyUnboundVariablesException {
41                 IInstruction[] instructions = new IInstruction[constraints.length];
42                 for(int i=0;i<constraints.length;++i) {
43                         instructions[i] = constraints[i].createInstruction(variableIds, bound);
44                         bound = bound.addAll(constraints[i].binds());
45                 }
46                 return new AndInstruction(instructions);
47         }
48
49         @Override
50         public int isApplicable(ImmutableSet<Resource> bound) {
51                 int ret = Integer.MAX_VALUE;
52                 for(int i=0;i<constraints.length;++i) {
53                         int temp = constraints[i].isApplicable(bound);
54                         if(temp < ret) {
55                                 if(temp == 0)
56                                         return 0;
57                                 ret = temp;                             
58                         }
59                         bound = bound.addAll(constraints[i].binds());
60                 }
61                 return ret;
62         }
63
64         
65         
66 }