]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/SuperRelation.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / predicates / SuperRelation.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.predicates;
13
14 import java.util.Collection;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.Layer0;
21
22 public class SuperRelation extends UnaryPredicate {
23
24         Resource superrelation;
25
26         public SuperRelation(Resource superrelation) {
27                 this.superrelation = superrelation;
28         }
29
30         @Override
31         public Collection<Resource> getResources(ReadGraph g) {
32                 throw new UnsupportedOperationException();
33         }
34
35         @Override
36         public boolean has(ReadGraph g, Resource resource) throws DatabaseException {
37                 return g.isSubrelationOf(resource, superrelation);
38         }
39
40         @Override
41         public boolean supportsUnboundedQuery() {
42                 return false;
43         }
44
45         @Override
46         public void add(WriteGraph g, Resource r) throws DatabaseException {
47                 if(!g.isSubrelationOf(r, superrelation)) {
48                 Layer0 b = Layer0.getInstance(g);
49                         g.claim(r, b.SubrelationOf, superrelation);
50                         if(!g.hasStatement(r, b.InverseOf)) {
51                                 Resource invR = g.newResource();
52                                 g.claim(r, b.InverseOf, invR);
53                                 g.claim(invR, b.SubrelationOf, g.getInverse(superrelation));
54                         }
55                 }
56         }
57
58         @Override
59         public void remove(WriteGraph g, Resource r) throws DatabaseException {
60                 // FIXME not implemented correctly
61         Layer0 b = Layer0.getInstance(g);
62                 if(g.isSubrelationOf(r, superrelation))
63                         g.denyStatement(r, b.SubrelationOf, superrelation);
64         }
65
66         @Override
67         public boolean supportsAddition() {
68                 return true;
69         }
70
71         @Override
72         public boolean supportsRemoval() {
73                 return false;
74         }
75
76         @Override
77         public int hashCode() {
78                 final int prime = 31;
79                 int result = 1;
80                 result = prime * result + ((superrelation == null) ? 0 : superrelation.hashCode());
81                 return result;
82         }
83
84         @Override
85         public boolean equals(Object obj) {
86                 if (this == obj)
87                         return true;
88                 if (obj == null)
89                         return false;
90                 if (getClass() != obj.getClass())
91                         return false;
92                 SuperRelation other = (SuperRelation) obj;
93                 if (superrelation == null) {
94                         if (other.superrelation != null)
95                                 return false;
96                 } else if (!superrelation.equals(other.superrelation))
97                         return false;
98                 return true;
99         }
100         
101 }