]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/TransitiveClosure.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / binaryPredicates / TransitiveClosure.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.layer0.utils.binaryPredicates;
13
14 import java.util.ArrayDeque;
15 import java.util.Collection;
16 import java.util.Deque;
17 import java.util.HashSet;
18 import java.util.Set;
19
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;
25
26 public class TransitiveClosure extends BinaryPredicate {
27         IBinaryPredicate predicate;
28         
29
30         public TransitiveClosure(IBinaryPredicate predicate) {
31                 this.predicate = predicate;
32         }
33
34         @Override
35         public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
36                 if(!has(g, subject, object))
37                         predicate.add(g, subject, object);
38         }
39
40         @Override
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()))
47                                 if(result.add(r))
48                                         unprocessed.push(r);
49                 return result;
50         }
51
52         @Override
53         public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) {
54                 // TODO Auto-generated method stub
55                 return null;
56         }
57
58         @Override
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()))
65                                 if(result.add(r))
66                                         unprocessed.push(r);
67                 return result;
68         }
69
70         @Override
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()))
77                                 if(r.equals(object))
78                                         return true;
79                                 else if(objects.add(r))
80                                         unprocessed.push(r);
81                 return false;
82         }
83
84         @Override
85         public void remove(WriteGraph g, Resource subject, Resource object) {
86                 throw new UnsupportedOperationException();
87         }
88
89         @Override
90         public boolean supportsAdditions() {
91                 return predicate.supportsAdditions();
92         }
93
94         @Override
95         public boolean supportsGetObjects() {
96                 return predicate.supportsGetObjects();
97         }
98
99         @Override
100         public boolean supportsGetStatements() {
101                 return false; // FIXME: just unimplemented
102         }
103
104         @Override
105         public boolean supportsGetSubjects() {
106                 return predicate.supportsGetSubjects();
107         }
108
109         @Override
110         public boolean supportsRemovals() {
111                 return false;
112         }
113
114         @Override
115         public int hashCode() {
116                 final int prime = 31;
117                 int result = 1;
118                 result = prime * result
119                                 + ((predicate == null) ? 0 : predicate.hashCode());
120                 return result;
121         }
122
123         @Override
124         public boolean equals(Object obj) {
125                 if (this == obj)
126                         return true;
127                 if (obj == null)
128                         return false;
129                 if (getClass() != obj.getClass())
130                         return false;
131                 TransitiveClosure other = (TransitiveClosure) obj;
132                 if (predicate == null) {
133                         if (other.predicate != null)
134                                 return false;
135                 } else if (!predicate.equals(other.predicate))
136                         return false;
137                 return true;
138         }
139         
140 }