/******************************************************************************* * 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.layer0.utils.binaryPredicates; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.utils.datastructures.Pair; public class OrderedSetElementsPredicate extends BinaryPredicate { public static final OrderedSetElementsPredicate INSTANCE = new OrderedSetElementsPredicate(); @Override public Collection getObjects(ReadGraph g, Resource subject) throws DatabaseException { Layer0 l0 = Layer0.getInstance(g); if(g.isInstanceOf(subject, l0.OrderedSet)) return OrderedSetUtils.toList(g, subject); else return Collections.emptyList(); } @Override public Collection> getStatements(ReadGraph g) { throw new UnsupportedOperationException(); } @Override public Collection getSubjects(ReadGraph g, Resource object) throws DatabaseException { Collection result = new ArrayList(1); Layer0 l0 = Layer0.getInstance(g); for(Statement stat : g.getStatements(object, l0.IsWeaklyRelatedTo)) { Resource pred = stat.getPredicate(); if(g.isInstanceOf(pred, l0.OrderedSet) && !pred.equals(object)) result.add(pred); } return result; } @Override public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException { return OrderedSetUtils.contains(g, subject, object) && g.isInstanceOf(subject, Layer0.getInstance(g).OrderedSet); } @Override public boolean supportsGetObjects() { return true; } @Override public boolean supportsGetStatements() { return false; } @Override public boolean supportsGetSubjects() { return true; } @Override public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException { OrderedSetUtils.add(g, subject, object); } @Override public void remove(WriteGraph g, Resource subject, Resource object) throws DatabaseException { OrderedSetUtils.remove(g, subject, object); } @Override public boolean supportsAdditions() { return true; } @Override public boolean supportsRemovals() { return true; } }