X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fviewpoint%2FVariablePropertyRule.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fviewpoint%2FVariablePropertyRule.java;h=76ec9b919b73a82bf4d7aad785a2bf6a711b6ee9;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java new file mode 100644 index 000000000..76ec9b919 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java @@ -0,0 +1,145 @@ +package org.simantics.modeling.ui.viewpoint; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import org.simantics.browsing.ui.model.children.ChildRule; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.db.layer0.variable.Variables.Role; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.selectionview.SelectionViewResources; + +public class VariablePropertyRule implements ChildRule { + + final private ArrayList requiredProperties = new ArrayList(); + final private ArrayList filteredProperties = new ArrayList(); + + public VariablePropertyRule(ReadGraph graph, Resource rule) throws DatabaseException { + + ModelingResources MOD = ModelingResources.getInstance(graph); + for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_RequireProperty)) { + String name = graph.getValue(r, Bindings.STRING); + requiredProperties.add(name); + } + for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_FilterProperty)) { + String name = graph.getValue(r, Bindings.STRING); + filteredProperties.add(name); + } + + } + + @Override + public boolean isCompatible(Class contentType) { + return contentType.equals(Variable.class); + } + + private boolean validate(ReadGraph graph, Variable parent, Variable child) throws DatabaseException { + + Resource predicate = child.getPossiblePredicateResource(graph); + if(predicate == null) return true; + + Layer0 L0 = Layer0.getInstance(graph); + if(L0.HasName.equals(predicate) || L0.HasLabel.equals(predicate)) { + Role role = parent.getRole(graph); + if(role == Role.PROPERTY) return false; + } + return true; + } + + @Override + public Collection getChildren(ReadGraph graph, Object parent_) + throws DatabaseException { + + Variable parent = (Variable)parent_; + + ArrayList result = new ArrayList(); + + Collection properties = parent.getProperties(graph); + ArrayList propertiesPredicates = new ArrayList(); + for (Variable property : properties) { + Resource r = property.getPossiblePredicateResource(graph); + if (r != null) + propertiesPredicates.add(r); + } + + Layer0 L0 = Layer0.getInstance(graph); + SelectionViewResources SEL = SelectionViewResources.getInstance(graph); + + props: for(Variable property : properties) { + + if (isUnder(graph, L0, SEL, property, propertiesPredicates)) continue props; + + for(String req : requiredProperties) if(property.getPossibleProperty(graph, req) == null) continue props; + + for(String req : filteredProperties) if(property.getName(graph).equals(req)) continue props; + + if(!validate(graph, parent, property)) continue; + + //System.err.println("add " + property.getURI(graph)); + + result.add(property); + + } + + Resource predicateResource = parent.getPossiblePredicateResource(graph); + if (predicateResource != null) { + Collection underOfs = graph.getObjects(predicateResource, SEL.UnderOf); + if (!underOfs.isEmpty()) { + Collection siblings = parent.getParent(graph).getProperties(graph); + for (Variable sibling : siblings) { + Resource r = sibling.getPossiblePredicateResource(graph); + if (r != null) { + if (underOfs.contains(r)) + result.add(sibling); + } + } + } + } + + if(isUnderProperty(graph, parent)) { + Collection children = parent.getChildren(graph); + for(Variable child : children) { + result.add(child); + } + } + + return result; + } + + private boolean isUnderProperty(ReadGraph graph, Variable variable) throws DatabaseException { + Role role = variable.getRole(graph); + if(Role.PROPERTY.equals(role)) { + return true; + } + Variable parent = variable.getParent(graph); + if(parent == null) return false; + else return isUnderProperty(graph, parent); + + } + + private boolean isUnder(ReadGraph graph, Layer0 L0, SelectionViewResources SEL, Variable property, ArrayList propertiesPredicates) throws DatabaseException { + Resource predicate = property.getPossiblePredicateResource(graph); + if (predicate == null) + return false; + Collection shownUnders = graph.getObjects(predicate, SEL.IsShownUnder); + return !Collections.disjoint(propertiesPredicates, shownUnders); + } + + @Override + public Collection getParents(ReadGraph graph, Object child) + throws DatabaseException { + Variable parent = ((Variable)child).getParent(graph); + if(parent == null) + return Collections.emptyList(); + else + return Collections.singleton(parent); + } + +}