1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.mapping.constraint.instructions;
\r
14 import gnu.trove.set.hash.TIntHashSet;
\r
16 import java.util.ArrayList;
\r
17 import java.util.Collection;
\r
18 import java.util.Iterator;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.Statement;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.layer0.Layer0;
\r
26 public class TripletPredicateQuery extends TripletInstruction {
\r
30 public TripletPredicateQuery(int variable0, int variable1, int variable2,
\r
31 Resource predicate) {
\r
32 super(variable0, variable1, variable2);
\r
33 this.predicate = predicate;
\r
36 public TripletPredicateQuery(int variable0, int variable1, int variable2) {
\r
37 super(variable0, variable1, variable2);
\r
41 public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
42 Resource r0 = (Resource)bindings[variable0];
\r
43 Resource r2 = (Resource)bindings[variable2];
\r
44 Collection<Resource> result = new ArrayList<Resource>();
\r
45 for(Statement stat :
\r
46 predicate == null ?
\r
47 g.getStatements(r0, Layer0.getInstance(g).IsWeaklyRelatedTo) :
\r
48 g.getStatements(r0, predicate))
\r
49 if(stat.getObject().equals(r2))
\r
50 result.add(stat.getPredicate());
\r
51 if(result.isEmpty())
\r
52 return IInstruction.FAILURE;
\r
53 Iterator<Resource> it = result.iterator();
\r
54 bindings[variable1] = it.next();
\r
61 @SuppressWarnings("unchecked")
\r
63 public Object next(ReadGraph g, Object[] bindings, Object continuation) {
\r
64 Iterator<Resource> it = (Iterator<Resource>)continuation;
\r
66 bindings[variable1] = it.next();
\r
73 return IInstruction.FAILURE;
\r
77 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
\r
78 super.collectVariables(reads, writes);
\r
79 writes.add(variable1);
\r