]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/constraint/instructions/TripletPredicateQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / constraint / instructions / TripletPredicateQuery.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.ArrayList;
17 import java.util.Collection;
18 import java.util.Iterator;
19
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Statement;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25
26 public class TripletPredicateQuery extends TripletInstruction {
27
28         Resource predicate;
29         
30         public TripletPredicateQuery(int variable0, int variable1, int variable2,
31                         Resource predicate) {
32                 super(variable0, variable1, variable2);
33                 this.predicate = predicate;
34         }
35         
36         public TripletPredicateQuery(int variable0, int variable1, int variable2) {
37                 super(variable0, variable1, variable2); 
38         }
39         
40         @Override
41         public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
42                 Resource r0 = (Resource)bindings[variable0];
43                 Resource r2 = (Resource)bindings[variable2];
44                 Collection<Resource> result = new ArrayList<Resource>();
45                 for(Statement stat : 
46                         predicate == null ? 
47                                 g.getStatements(r0, Layer0.getInstance(g).IsWeaklyRelatedTo) : 
48                                 g.getStatements(r0, predicate))
49                         if(stat.getObject().equals(r2))
50                                 result.add(stat.getPredicate());
51                 if(result.isEmpty())
52                         return IInstruction.FAILURE;
53                 Iterator<Resource> it = result.iterator();
54                 bindings[variable1] = it.next();
55                 if(it.hasNext())
56                         return it;
57                 else
58                         return null;
59         }
60         
61         @SuppressWarnings("unchecked")
62         @Override
63         public Object next(ReadGraph g, Object[] bindings, Object continuation) {
64                 Iterator<Resource> it = (Iterator<Resource>)continuation;
65                 if(it.hasNext()) {
66                         bindings[variable1] = it.next();
67                         if(it.hasNext())
68                                 return it;
69                         else
70                                 return null;
71                 }
72                 else
73                         return IInstruction.FAILURE;
74         }       
75         
76         @Override
77         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {           
78                 super.collectVariables(reads, writes);
79                 writes.add(variable1);
80         }
81
82 }