]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/BinaryPredicateObjectQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / BinaryPredicateObjectQuery.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.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
24
25 public class BinaryPredicateObjectQuery extends BinaryPredicateInstruction {
26         
27         public BinaryPredicateObjectQuery(int variable0, int variable1,
28                         IBinaryPredicate predicate) {
29                 super(variable0, variable1, predicate);
30         }
31
32         @Override
33         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {          
34                 Resource r0 = (Resource)bindings[variable0];
35 //              ITask task = ThreadLogger.getInstance().begin("g");
36                 Collection<Resource> result = predicate.getObjects(g, r0);
37                 if(DEBUG) {
38                 System.out.println("getObjects(" +
39                         NameUtils.getSafeName(g, r0) + ", " +
40                         predicate.toString(g) + ") returned " + result.size() + " objects"                      
41                         );
42                 }
43 //              task.finish();
44                 if(result.isEmpty())
45                         return IInstruction.FAILURE;
46                 Iterator<Resource> it = result.iterator();
47                 bindings[variable1] = it.next();
48                 if(it.hasNext())
49                         return it;
50                 else
51                         return null;
52         }
53         
54         @SuppressWarnings("unchecked")
55         @Override
56         public Object next(ReadGraph g, Object[] bindings, Object continuation) {
57                 Iterator<Resource> it = (Iterator<Resource>)continuation;
58                 if(it.hasNext()) {
59                         bindings[variable1] = it.next();
60                         if(it.hasNext())
61                                 return it;
62                         else
63                                 return null;
64                 }
65                 else
66                         return IInstruction.FAILURE;
67         }       
68         
69         @Override
70         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
71                 super.collectVariables(reads, writes);
72                 writes.add(variable1);
73         }
74
75 }