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