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.Collection;
\r
17 import java.util.Iterator;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
\r
24 public class BinaryPredicateSubjectQuery extends BinaryPredicateInstruction {
\r
26 public BinaryPredicateSubjectQuery(int variable0, int variable1,
\r
27 IBinaryPredicate predicate) {
\r
28 super(variable0, variable1, predicate);
\r
32 public Object query(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
33 Resource r1 = (Resource)bindings[variable1];
\r
34 Collection<Resource> result = predicate.getSubjects(g, r1);
\r
35 if(result.isEmpty())
\r
36 return IInstruction.FAILURE;
\r
37 Iterator<Resource> it = result.iterator();
\r
38 bindings[variable0] = it.next();
\r
45 @SuppressWarnings("unchecked")
\r
47 public Object next(ReadGraph g, Object[] bindings, Object continuation) {
\r
48 Iterator<Resource> it = (Iterator<Resource>)continuation;
\r
50 bindings[variable0] = it.next();
\r
57 return IInstruction.FAILURE;
\r
61 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
\r
62 super.collectVariables(reads, writes);
\r
63 writes.add(variable0);
\r