1 package org.simantics.document.linking.ge;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.HashSet;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.Statement;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
15 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.document.linking.ontology.DocumentLink;
18 import org.simantics.document.linking.utils.SourceLinkUtil;
19 import org.simantics.layer0.Layer0;
22 * Rule for browsing properties and their document links.
24 * The rule has two modes, one list all properties, and another lists only properties that have document links.
26 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29 public class PropertyChildRule implements org.simantics.browsing.ui.model.children.ChildRule{
31 private boolean includeAllProps = true;
32 private boolean showOnlyCheckable = false;
34 public PropertyChildRule() {
38 public PropertyChildRule(boolean includeAllProps) {
39 this.includeAllProps = includeAllProps;
42 public void setShowOnlyCheckable(boolean showOnlyCheckable) {
43 this.showOnlyCheckable = showOnlyCheckable;
47 public boolean isCompatible(Class<?> contentType) {
48 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
51 public void setIncludeAllProps(boolean includeAllProps) {
52 this.includeAllProps = includeAllProps;
56 public Collection<?> getChildren(ReadGraph graph, Object obj)
57 throws DatabaseException {
59 ArrayList<Variable> children = new ArrayList<Variable>();
61 boolean isResource = true;
62 Resource resource = null;
63 Variable variable = null;
64 if (obj instanceof Resource) {
65 resource = (Resource)obj;
68 variable = graph.adapt(resource, Variable.class);
69 } catch (Throwable t) {
73 variable = (Variable)obj;
74 resource = variable.getPossibleRepresents(graph);
80 DocumentLink sl = DocumentLink.getInstance(graph);
81 if (graph.isInstanceOf(resource, sl.Source))
83 if (!graph.hasValue(resource)) {
84 if (includeAllProps) {
85 // reading all objects returns both asserted and instance specific properties.
86 // we must filter out asserted properties if there is a equivalent instance property.
87 Map<Resource,Statement> selectedProps = getProps(graph, resource);
88 for (Statement s : selectedProps.values()) {
89 children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));
93 Set<Resource> linked = new HashSet<Resource>();
94 Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);
95 for (Resource source : sources) {
96 if (showOnlyCheckable &&
97 (SourceLinkUtil.isValidSource(graph, source) &&
98 SourceLinkUtil.isUpToDateSource(graph, source)))
100 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
103 Map<Resource,Statement> selectedProps = getProps(graph, resource);
104 for (Statement s : selectedProps.values()) {
105 if (linked.contains(s.getPredicate()))
106 children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));
112 //find sources declared on parentResource using consernsRelation
113 Variable parentVariable = variable.getParent(graph);
114 Resource parentRes = null;
115 Resource relation = null;
116 if (parentVariable != null) {
117 parentRes = parentVariable.getPossibleRepresents(graph);
118 relation = variable.getPossiblePredicateResource(graph);
121 if (parentRes != null && relation != null) {
122 Collection<Resource> sources = graph.getObjects(parentRes, sl.hasSource);
123 for (Resource source : sources) {
124 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
125 if (rel != null && rel.equals(relation)) {
126 if (showOnlyCheckable &&
127 (SourceLinkUtil.isValidSource(graph, source) &&
128 SourceLinkUtil.isUpToDateSource(graph, source)))
130 children.add(new StandardGraphChildVariable(variable, null, source));
134 //find sources declared on this resource, ie. sources that do not have consernsRelation.
135 Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);
136 for (Resource source : sources) {
137 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
139 if (showOnlyCheckable &&
140 (SourceLinkUtil.isValidSource(graph, source) &&
141 SourceLinkUtil.isUpToDateSource(graph, source)))
143 children.add(new StandardGraphChildVariable(variable, null, source));
146 while (children.remove(variable)) {
155 private Map<Resource,Statement> getProps(ReadGraph graph, Resource resource) throws DatabaseException {
156 Layer0 l0 = Layer0.getInstance(graph);
157 Map<Resource, Statement> selectedProps = new HashMap<Resource, Statement>();
158 Collection<Statement> propertyStatements = graph.getStatements(resource, l0.HasProperty);
159 for (Statement s : propertyStatements) {
160 if (!graph.isInstanceOf(s.getObject(), l0.Literal) && !graph.isInstanceOf(s.getObject(), l0.SCLValue))
162 if (selectedProps.containsKey(s.getPredicate())) {
163 if (!s.isAsserted(resource))
164 selectedProps.put(s.getPredicate(), s);
166 selectedProps.put(s.getPredicate(), s);
169 return selectedProps;
173 public Collection<?> getParents(ReadGraph graph, Object child)
174 throws DatabaseException {
175 return new ArrayList<Resource>();