1 package org.simantics.modeling.ui.viewpoint;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
5 import java.util.Collections;
\r
7 import org.simantics.browsing.ui.model.children.ChildRule;
\r
8 import org.simantics.databoard.Bindings;
\r
9 import org.simantics.db.ReadGraph;
\r
10 import org.simantics.db.Resource;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.db.layer0.variable.Variable;
\r
13 import org.simantics.db.layer0.variable.Variables;
\r
14 import org.simantics.db.layer0.variable.Variables.Role;
\r
15 import org.simantics.layer0.Layer0;
\r
16 import org.simantics.modeling.ModelingResources;
\r
17 import org.simantics.selectionview.SelectionViewResources;
\r
19 public class VariablePropertyRule implements ChildRule {
\r
21 final private ArrayList<String> requiredProperties = new ArrayList<String>();
\r
22 final private ArrayList<String> filteredProperties = new ArrayList<String>();
\r
24 public VariablePropertyRule(ReadGraph graph, Resource rule) throws DatabaseException {
\r
26 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
27 for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_RequireProperty)) {
\r
28 String name = graph.getValue(r, Bindings.STRING);
\r
29 requiredProperties.add(name);
\r
31 for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_FilterProperty)) {
\r
32 String name = graph.getValue(r, Bindings.STRING);
\r
33 filteredProperties.add(name);
\r
39 public boolean isCompatible(Class<?> contentType) {
\r
40 return contentType.equals(Variable.class);
\r
43 private boolean validate(ReadGraph graph, Variable parent, Variable child) throws DatabaseException {
\r
45 Resource predicate = child.getPossiblePredicateResource(graph);
\r
46 if(predicate == null) return true;
\r
48 Layer0 L0 = Layer0.getInstance(graph);
\r
49 if(L0.HasName.equals(predicate) || L0.HasLabel.equals(predicate)) {
\r
50 Role role = parent.getRole(graph);
\r
51 if(role == Role.PROPERTY) return false;
\r
57 public Collection<?> getChildren(ReadGraph graph, Object parent_)
\r
58 throws DatabaseException {
\r
60 Variable parent = (Variable)parent_;
\r
62 ArrayList<Variable> result = new ArrayList<Variable>();
\r
64 Collection<Variable> properties = parent.getProperties(graph);
\r
65 ArrayList<Resource> propertiesPredicates = new ArrayList<Resource>();
\r
66 for (Variable property : properties) {
\r
67 Resource r = property.getPossiblePredicateResource(graph);
\r
69 propertiesPredicates.add(r);
\r
72 Layer0 L0 = Layer0.getInstance(graph);
\r
73 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
\r
75 props: for(Variable property : properties) {
\r
77 if (isUnder(graph, L0, SEL, property, propertiesPredicates)) continue props;
\r
79 Boolean hidden = property.getPossiblePropertyValue(graph, SEL.hidden, Bindings.BOOLEAN);
\r
80 if(hidden != null && hidden) continue props;
\r
82 for(String req : requiredProperties) if(property.getPossibleProperty(graph, req) == null) continue props;
\r
84 for(String req : filteredProperties) if(property.getName(graph).equals(req)) continue props;
\r
86 if(!validate(graph, parent, property)) continue;
\r
88 //System.err.println("add " + property.getURI(graph));
\r
90 result.add(property);
\r
94 Resource predicateResource = parent.getPossiblePredicateResource(graph);
\r
95 if (predicateResource != null) {
\r
96 Collection<Resource> underOfs = graph.getObjects(predicateResource, SEL.UnderOf);
\r
97 if (!underOfs.isEmpty()) {
\r
98 Collection<Variable> siblings = parent.getParent(graph).getProperties(graph);
\r
99 for (Variable sibling : siblings) {
\r
100 Resource r = sibling.getPossiblePredicateResource(graph);
\r
102 if (underOfs.contains(r))
\r
103 result.add(sibling);
\r
109 if(isUnderProperty(graph, parent)) {
\r
110 Collection<Variable> children = parent.getChildren(graph);
\r
111 for(Variable child : children) {
\r
119 private boolean isUnderProperty(ReadGraph graph, Variable variable) throws DatabaseException {
\r
120 Role role = variable.getRole(graph);
\r
121 if(Role.PROPERTY.equals(role)) {
\r
124 Variable parent = variable.getParent(graph);
\r
125 if(parent == null) return false;
\r
126 else return isUnderProperty(graph, parent);
\r
130 private boolean isUnder(ReadGraph graph, Layer0 L0, SelectionViewResources SEL, Variable property, ArrayList<Resource> propertiesPredicates) throws DatabaseException {
\r
131 Resource predicate = property.getPossiblePredicateResource(graph);
\r
132 if (predicate == null)
\r
134 Collection<Resource> shownUnders = graph.getObjects(predicate, SEL.IsShownUnder);
\r
135 return !Collections.disjoint(propertiesPredicates, shownUnders);
\r
139 public Collection<?> getParents(ReadGraph graph, Object child)
\r
140 throws DatabaseException {
\r
141 Variable parent = ((Variable)child).getParent(graph);
\r
143 return Collections.emptyList();
\r
145 return Collections.singleton(parent);
\r