/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.mapping.constraint; import gnu.trove.map.hash.TObjectIntHashMap; import org.simantics.db.Resource; import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate; import org.simantics.mapping.constraint.instructions.BinaryPredicateInstruction; import org.simantics.mapping.constraint.instructions.BinaryPredicateObjectQuery; import org.simantics.mapping.constraint.instructions.BinaryPredicateSubjectQuery; import org.simantics.mapping.constraint.instructions.IInstruction; import org.simantics.utils.datastructures.persistent.ImmutableSet; public class BinaryPredicateConstraint implements IConstraint { Resource variable0; Resource variable1; IBinaryPredicate predicate; public BinaryPredicateConstraint(IBinaryPredicate predicate, Resource variable0, Resource variable1) { this.predicate = predicate; this.variable0 = variable0; this.variable1 = variable1; } @Override public ImmutableSet binds() { return ImmutableSet.of(variable0, variable1); } @Override public IInstruction createInstruction( TObjectIntHashMap variableIds, ImmutableSet bound) throws TooManyUnboundVariablesException { int id0 = variableIds.get(variable0); int id1 = variableIds.get(variable1); if(bound.contains(variable0)) { if(bound.contains(variable1)) return new BinaryPredicateInstruction(id0, id1, predicate); else return new BinaryPredicateObjectQuery(id0, id1, predicate); } else { if(bound.contains(variable1)) return new BinaryPredicateSubjectQuery(id0, id1, predicate); else throw new TooManyUnboundVariablesException(); } } @Override public int isApplicable(ImmutableSet bound) { if(bound.contains(variable0)) { if(bound.contains(variable1)) return Integer.MAX_VALUE; else if(predicate.supportsGetObjects()) return Integer.MAX_VALUE; else return 0; } else { if(bound.contains(variable1) && predicate.supportsGetSubjects()) return Integer.MAX_VALUE; else return 0; } } }