]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/BinaryPredicateSubjectQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / BinaryPredicateSubjectQuery.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.instructions;
13
14 import gnu.trove.set.hash.TIntHashSet;
15
16 import java.util.Collection;
17 import java.util.Iterator;
18
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
23
24 public class BinaryPredicateSubjectQuery extends BinaryPredicateInstruction {
25         
26         public BinaryPredicateSubjectQuery(int variable0, int variable1,
27                         IBinaryPredicate predicate) {
28                 super(variable0, variable1, predicate);
29         }
30
31         @Override
32         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
33                 Resource r1 = (Resource)bindings[variable1];
34                 Collection<Resource> result = predicate.getSubjects(g, r1);
35                 if(result.isEmpty())
36                         return IInstruction.FAILURE;
37                 Iterator<Resource> it = result.iterator();
38                 bindings[variable0] = it.next();
39                 if(it.hasNext())
40                         return it;
41                 else
42                         return null;
43         }
44         
45         @SuppressWarnings("unchecked")
46         @Override
47         public Object next(ReadGraph g, Object[] bindings, Object continuation) {
48                 Iterator<Resource> it = (Iterator<Resource>)continuation;
49                 if(it.hasNext()) {
50                         bindings[variable0] = it.next();
51                         if(it.hasNext())
52                                 return it;
53                         else
54                                 return null;
55                 }
56                 else
57                         return IInstruction.FAILURE;
58         }       
59         
60         @Override
61         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
62                 super.collectVariables(reads, writes);
63                 writes.add(variable0);
64         }
65
66 }