]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/TripletObjectQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / TripletObjectQuery.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
23 public class TripletObjectQuery extends TripletInstruction {
24
25         public TripletObjectQuery(int variable0, int variable1, int variable2) {
26                 super(variable0, variable1, variable2); 
27         }
28         
29         @Override
30         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
31                 Resource r0 = (Resource)bindings[variable0];
32                 Resource r1 = (Resource)bindings[variable1];
33                 Collection<Resource> result = g.getObjects(r0, r1);
34                 if(result.isEmpty())
35                         return IInstruction.FAILURE;
36                 Iterator<Resource> it = result.iterator();
37                 bindings[variable2] = it.next();
38                 if(it.hasNext())
39                         return it;
40                 else
41                         return null;
42         }
43         
44         @SuppressWarnings("unchecked")
45         @Override
46         public Object next(ReadGraph g, Object[] bindings, Object continuation) {
47                 Iterator<Resource> it = (Iterator<Resource>)continuation;
48                 if(it.hasNext()) {
49                         bindings[variable2] = it.next();
50                         if(it.hasNext())
51                                 return it;
52                         else
53                                 return null;
54                 }
55                 else
56                         return IInstruction.FAILURE;
57         }       
58         
59         @Override
60         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {           
61                 super.collectVariables(reads, writes);
62                 writes.add(variable2);
63         }
64
65 }