1 package org.simantics.document.linking.ge;
\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
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
22 * Rule for browsing properties and their document links.
\r
24 * The rule has two modes, one list all properties, and another lists only properties that have document links.
\r
26 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
29 public class PropertyChildRule implements org.simantics.browsing.ui.model.children.ChildRule{
\r
31 private boolean includeAllProps = true;
\r
32 private boolean showOnlyCheckable = false;
\r
34 public PropertyChildRule() {
\r
38 public PropertyChildRule(boolean includeAllProps) {
\r
39 this.includeAllProps = includeAllProps;
\r
42 public void setShowOnlyCheckable(boolean showOnlyCheckable) {
\r
43 this.showOnlyCheckable = showOnlyCheckable;
\r
47 public boolean isCompatible(Class<?> contentType) {
\r
48 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
\r
51 public void setIncludeAllProps(boolean includeAllProps) {
\r
52 this.includeAllProps = includeAllProps;
\r
56 public Collection<?> getChildren(ReadGraph graph, Object obj)
\r
57 throws DatabaseException {
\r
59 ArrayList<Variable> children = new ArrayList<Variable>();
\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
68 variable = graph.adapt(resource, Variable.class);
\r
69 } catch (Throwable t) {
\r
73 variable = (Variable)obj;
\r
74 resource = variable.getPossibleRepresents(graph);
\r
77 if (resource == null)
\r
80 DocumentLink sl = DocumentLink.getInstance(graph);
\r
81 if (graph.isInstanceOf(resource, sl.Source))
\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
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
100 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
\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
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
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
130 children.add(new StandardGraphChildVariable(variable, null, source));
\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
139 if (showOnlyCheckable &&
\r
140 (SourceLinkUtil.isValidSource(graph, source) &&
\r
141 SourceLinkUtil.isUpToDateSource(graph, source)))
\r
143 children.add(new StandardGraphChildVariable(variable, null, source));
\r
146 while (children.remove(variable)) {
\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
162 if (selectedProps.containsKey(s.getPredicate())) {
\r
163 if (!s.isAsserted(resource))
\r
164 selectedProps.put(s.getPredicate(), s);
\r
166 selectedProps.put(s.getPredicate(), s);
\r
169 return selectedProps;
\r
173 public Collection<?> getParents(ReadGraph graph, Object child)
\r
174 throws DatabaseException {
\r
175 return new ArrayList<Resource>();
\r