1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.layer0.utils.binaryPredicates;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.utils.OrderedSetUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.utils.datastructures.Pair;
27 public class OrderedSetElementsPredicate extends BinaryPredicate {
29 public static final OrderedSetElementsPredicate INSTANCE =
30 new OrderedSetElementsPredicate();
33 public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
34 Layer0 l0 = Layer0.getInstance(g);
35 if(g.isInstanceOf(subject, l0.OrderedSet))
36 return OrderedSetUtils.toList(g, subject);
38 return Collections.emptyList();
42 public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) {
43 throw new UnsupportedOperationException();
47 public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
48 Collection<Resource> result = new ArrayList<Resource>(1);
49 Layer0 l0 = Layer0.getInstance(g);
50 for(Statement stat : g.getStatements(object, l0.IsWeaklyRelatedTo)) {
51 Resource pred = stat.getPredicate();
52 if(g.isInstanceOf(pred, l0.OrderedSet) && !pred.equals(object))
59 public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
60 return OrderedSetUtils.contains(g, subject, object) &&
61 g.isInstanceOf(subject, Layer0.getInstance(g).OrderedSet);
65 public boolean supportsGetObjects() {
70 public boolean supportsGetStatements() {
75 public boolean supportsGetSubjects() {
80 public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
81 OrderedSetUtils.add(g, subject, object);
85 public void remove(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
86 OrderedSetUtils.remove(g, subject, object);
90 public boolean supportsAdditions() {
95 public boolean supportsRemovals() {