]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/PropertyChildRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / PropertyChildRule.java
1 package org.simantics.document.linking.ge;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.HashMap;\r
6 import java.util.HashSet;\r
7 import java.util.Map;\r
8 import java.util.Set;\r
9 \r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.Statement;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
15 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;\r
16 import org.simantics.db.layer0.variable.Variable;\r
17 import org.simantics.document.linking.ontology.DocumentLink;\r
18 import org.simantics.document.linking.utils.SourceLinkUtil;\r
19 import org.simantics.layer0.Layer0;\r
20 \r
21 /**\r
22  * Rule for browsing properties and their document links.\r
23  * \r
24  * The rule has two modes, one list all properties, and another lists only properties that have document links.\r
25  * \r
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
27  *\r
28  */\r
29 public class PropertyChildRule implements org.simantics.browsing.ui.model.children.ChildRule{\r
30         \r
31         private boolean includeAllProps = true;\r
32         private boolean showOnlyCheckable = false;\r
33         \r
34         public PropertyChildRule() {\r
35                 \r
36         }\r
37         \r
38         public PropertyChildRule(boolean includeAllProps) {\r
39                 this.includeAllProps = includeAllProps;\r
40         }\r
41         \r
42         public void setShowOnlyCheckable(boolean showOnlyCheckable) {\r
43                 this.showOnlyCheckable = showOnlyCheckable;\r
44         }\r
45         \r
46         @Override\r
47         public boolean isCompatible(Class<?> contentType) {\r
48                 return contentType.equals(Resource.class) || contentType.equals(Variable.class);\r
49         }\r
50         \r
51         public void setIncludeAllProps(boolean includeAllProps) {\r
52                 this.includeAllProps = includeAllProps;\r
53         }\r
54         \r
55         @Override\r
56         public Collection<?> getChildren(ReadGraph graph, Object obj)\r
57                         throws DatabaseException {\r
58 \r
59                 ArrayList<Variable> children = new ArrayList<Variable>();\r
60                 \r
61                 boolean isResource = true;\r
62                 Resource resource = null;\r
63                 Variable variable = null;\r
64                 if (obj instanceof Resource) {\r
65                         resource = (Resource)obj;\r
66                         isResource = true;\r
67                         try {\r
68                                 variable = graph.adapt(resource, Variable.class);\r
69                         } catch (Throwable t) {\r
70                                 return children;\r
71                         }\r
72                 } else {\r
73                         variable = (Variable)obj;\r
74                         resource = variable.getPossibleRepresents(graph);\r
75                         isResource = false;\r
76                 }\r
77                 if (resource == null)\r
78                         return children;\r
79         \r
80                 DocumentLink sl = DocumentLink.getInstance(graph);\r
81                 if (graph.isInstanceOf(resource, sl.Source))\r
82                         return children;\r
83                 if (!graph.hasValue(resource)) {\r
84                         if (includeAllProps) {\r
85                                 // reading all objects returns both asserted and instance specific properties.\r
86                                 // we must filter out asserted properties if there is a equivalent instance property. \r
87                                 Map<Resource,Statement> selectedProps = getProps(graph, resource);\r
88                                 for (Statement s : selectedProps.values()) {\r
89                                         children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));\r
90                                 }\r
91                         } else {\r
92                                 \r
93                                 Set<Resource> linked = new HashSet<Resource>();\r
94                                 Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);\r
95                                 for (Resource source : sources) {\r
96                                         if (showOnlyCheckable && \r
97                                            (SourceLinkUtil.isValidSource(graph, source) &&\r
98                                             SourceLinkUtil.isUpToDateSource(graph, source)))\r
99                                                 continue;\r
100                                         Resource rel = graph.getPossibleObject(source, sl.consernsRelation);\r
101                                         linked.add(rel);\r
102                                 }\r
103                                 Map<Resource,Statement> selectedProps = getProps(graph, resource);\r
104                                 for (Statement s : selectedProps.values()) {\r
105                                         if (linked.contains(s.getPredicate()))\r
106                                                 children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));\r
107                                 }\r
108                                         \r
109                         }\r
110                 }\r
111                 if (!isResource) {\r
112                         //find sources declared on parentResource using consernsRelation\r
113                         Variable parentVariable = variable.getParent(graph);\r
114                         Resource parentRes = null;\r
115                         Resource relation = null;\r
116                         if (parentVariable != null) {\r
117                                 parentRes = parentVariable.getPossibleRepresents(graph);\r
118                                 relation = variable.getPossiblePredicateResource(graph);\r
119                         }\r
120                         \r
121                         if (parentRes != null && relation != null) {\r
122                                 Collection<Resource> sources = graph.getObjects(parentRes, sl.hasSource);\r
123                                 for (Resource source : sources) {\r
124                                         Resource rel = graph.getPossibleObject(source, sl.consernsRelation);\r
125                                         if (rel != null && rel.equals(relation)) {\r
126                                                 if (showOnlyCheckable && \r
127                                                    (SourceLinkUtil.isValidSource(graph, source) &&\r
128                                                         SourceLinkUtil.isUpToDateSource(graph, source)))\r
129                                                         continue;\r
130                                                 children.add(new StandardGraphChildVariable(variable, null, source));\r
131                                         }\r
132                                 }\r
133                         }\r
134                         //find sources declared on this resource, ie. sources that do not have consernsRelation.\r
135                         Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);\r
136                         for (Resource source : sources) {\r
137                                 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);\r
138                                 if (rel == null) {\r
139                                         if (showOnlyCheckable && \r
140                                            (SourceLinkUtil.isValidSource(graph, source) &&\r
141                                                 SourceLinkUtil.isUpToDateSource(graph, source)))\r
142                                                 continue;\r
143                                         children.add(new StandardGraphChildVariable(variable, null, source));\r
144                                 }\r
145                         }\r
146                         while (children.remove(variable)) {\r
147                                 \r
148                         }\r
149                 }\r
150                 \r
151 \r
152                 return children;\r
153         }\r
154         \r
155         private Map<Resource,Statement> getProps(ReadGraph graph, Resource resource) throws DatabaseException {\r
156                 Layer0 l0 = Layer0.getInstance(graph);\r
157                 Map<Resource, Statement> selectedProps = new HashMap<Resource, Statement>();\r
158                 Collection<Statement> propertyStatements = graph.getStatements(resource, l0.HasProperty);\r
159                 for (Statement s : propertyStatements) {\r
160                         if (!graph.isInstanceOf(s.getObject(), l0.Literal) && !graph.isInstanceOf(s.getObject(), l0.SCLValue))\r
161                                 continue;\r
162                         if (selectedProps.containsKey(s.getPredicate())) {\r
163                                 if (!s.isAsserted(resource))\r
164                                         selectedProps.put(s.getPredicate(), s);\r
165                         } else {\r
166                                 selectedProps.put(s.getPredicate(), s);\r
167                         }\r
168                 }\r
169                 return selectedProps;\r
170         }\r
171         \r
172         @Override\r
173         public Collection<?> getParents(ReadGraph graph, Object child)\r
174                         throws DatabaseException {\r
175                 return new ArrayList<Resource>();\r
176         }\r
177 \r
178 }\r