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.ArrayDeque;
15 import java.util.Collection;
16 import java.util.Deque;
17 import java.util.HashSet;
20 import org.simantics.db.Resource;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.utils.datastructures.Pair;
26 public class TransitiveClosure extends BinaryPredicate {
27 IBinaryPredicate predicate;
30 public TransitiveClosure(IBinaryPredicate predicate) {
31 this.predicate = predicate;
35 public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
36 if(!has(g, subject, object))
37 predicate.add(g, subject, object);
41 public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
42 Deque<Resource> unprocessed = new ArrayDeque<Resource>();
43 unprocessed.add(subject);
44 Set<Resource> result = new HashSet<Resource>();
45 while(!unprocessed.isEmpty())
46 for(Resource r : predicate.getObjects(g, unprocessed.pop()))
53 public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) {
54 // TODO Auto-generated method stub
59 public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
60 Deque<Resource> unprocessed = new ArrayDeque<Resource>();
61 unprocessed.add(object);
62 Set<Resource> result = new HashSet<Resource>();
63 while(!unprocessed.isEmpty())
64 for(Resource r : predicate.getSubjects(g, unprocessed.pop()))
71 public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
72 Deque<Resource> unprocessed = new ArrayDeque<Resource>();
73 unprocessed.add(subject);
74 Set<Resource> objects = new HashSet<Resource>();
75 while(!unprocessed.isEmpty())
76 for(Resource r : predicate.getObjects(g, unprocessed.pop()))
79 else if(objects.add(r))
85 public void remove(WriteGraph g, Resource subject, Resource object) {
86 throw new UnsupportedOperationException();
90 public boolean supportsAdditions() {
91 return predicate.supportsAdditions();
95 public boolean supportsGetObjects() {
96 return predicate.supportsGetObjects();
100 public boolean supportsGetStatements() {
101 return false; // FIXME: just unimplemented
105 public boolean supportsGetSubjects() {
106 return predicate.supportsGetSubjects();
110 public boolean supportsRemovals() {
115 public int hashCode() {
116 final int prime = 31;
118 result = prime * result
119 + ((predicate == null) ? 0 : predicate.hashCode());
124 public boolean equals(Object obj) {
129 if (getClass() != obj.getClass())
131 TransitiveClosure other = (TransitiveClosure) obj;
132 if (predicate == null) {
133 if (other.predicate != null)
135 } else if (!predicate.equals(other.predicate))