]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/SuperRelation.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / predicates / SuperRelation.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.layer0.utils.predicates;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 \r
22 public class SuperRelation extends UnaryPredicate {\r
23 \r
24         Resource superrelation;\r
25 \r
26         public SuperRelation(Resource superrelation) {\r
27                 this.superrelation = superrelation;\r
28         }\r
29 \r
30         @Override\r
31         public Collection<Resource> getResources(ReadGraph g) {\r
32                 throw new UnsupportedOperationException();\r
33         }\r
34 \r
35         @Override\r
36         public boolean has(ReadGraph g, Resource resource) throws DatabaseException {\r
37                 return g.isSubrelationOf(resource, superrelation);\r
38         }\r
39 \r
40         @Override\r
41         public boolean supportsUnboundedQuery() {\r
42                 return false;\r
43         }\r
44 \r
45         @Override\r
46         public void add(WriteGraph g, Resource r) throws DatabaseException {\r
47                 if(!g.isSubrelationOf(r, superrelation)) {\r
48                 Layer0 b = Layer0.getInstance(g);\r
49                         g.claim(r, b.SubrelationOf, superrelation);\r
50                         if(!g.hasStatement(r, b.InverseOf)) {\r
51                                 Resource invR = g.newResource();\r
52                                 g.claim(r, b.InverseOf, invR);\r
53                                 g.claim(invR, b.SubrelationOf, g.getInverse(superrelation));\r
54                         }\r
55                 }\r
56         }\r
57 \r
58         @Override\r
59         public void remove(WriteGraph g, Resource r) throws DatabaseException {\r
60                 // FIXME not implemented correctly\r
61         Layer0 b = Layer0.getInstance(g);\r
62                 if(g.isSubrelationOf(r, superrelation))\r
63                         g.denyStatement(r, b.SubrelationOf, superrelation);\r
64         }\r
65 \r
66         @Override\r
67         public boolean supportsAddition() {\r
68                 return true;\r
69         }\r
70 \r
71         @Override\r
72         public boolean supportsRemoval() {\r
73                 return false;\r
74         }\r
75 \r
76         @Override\r
77         public int hashCode() {\r
78                 final int prime = 31;\r
79                 int result = 1;\r
80                 result = prime * result + ((superrelation == null) ? 0 : superrelation.hashCode());\r
81                 return result;\r
82         }\r
83 \r
84         @Override\r
85         public boolean equals(Object obj) {\r
86                 if (this == obj)\r
87                         return true;\r
88                 if (obj == null)\r
89                         return false;\r
90                 if (getClass() != obj.getClass())\r
91                         return false;\r
92                 SuperRelation other = (SuperRelation) obj;\r
93                 if (superrelation == null) {\r
94                         if (other.superrelation != null)\r
95                                 return false;\r
96                 } else if (!superrelation.equals(other.superrelation))\r
97                         return false;\r
98                 return true;\r
99         }\r
100         \r
101 }\r