X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fvariable%2FConstantChildVariable.java;fp=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fvariable%2FConstantChildVariable.java;h=2bfa3862cc74b9e40d1222c218c33073f9627b41;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java new file mode 100644 index 000000000..2bfa3862c --- /dev/null +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java @@ -0,0 +1,161 @@ +package org.simantics.db.layer0.variable; + +import gnu.trove.map.hash.THashMap; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.simantics.databoard.binding.Binding; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.NoSingleResultException; + +public class ConstantChildVariable extends AbstractChildVariable { + + final Variable parent; + final String name; + final Map properties; + + public ConstantChildVariable(Variable parent, String name) { + this.parent = parent; + this.name = name; + this.properties = new THashMap(); + } + + public ConstantChildVariable(Variable parent, String name, Collection propertyBuilders) { + this(parent, name); + for(ConstantPropertyVariableBuilder b : propertyBuilders) { + ConstantPropertyVariable property = b.build(this); + properties.put(b.getName(), property); + } + } + + public ConstantChildVariable(Variable parent, String name, ConstantPropertyVariableBuilder ... propertyBuilders) { + this(parent, name); + for(ConstantPropertyVariableBuilder b : propertyBuilders) { + ConstantPropertyVariable property = b.build(this); + properties.put(b.getName(), property); + } + } + + public ConstantChildVariable(Variable parent, String name, String[] propertyNames, Binding[] bindings, Object ... values) { + + this(parent, name); + + buildProperties(this, propertyNames, bindings, values, properties); + + } + + private Map buildProperties(Variable parent, String[] propertyNames, Binding[] bindings, Object ... values) { + + assert parent != null; + assert propertyNames.length == bindings.length; + assert propertyNames.length == values.length; + + Map result = new HashMap(); + + for(int i=0;i collectDomainProperties(ReadGraph graph, Map properties) throws DatabaseException { + if(properties == null) properties = new HashMap(this.properties.size()); + properties.putAll(this.properties); + return properties; + } + + @Override + public Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException { + return properties.get(name); + } + + @Override + public Variable getParent(ReadGraph graph) throws DatabaseException { + return parent; + } + +// @Override +// public Object getSerialized(ReadGraph graph) throws DatabaseException { +// return getName(graph); +// } + + @Override + public String getName(ReadGraph graph) throws DatabaseException { + return name; + } + + @Override + public String getLabel(ReadGraph graph) throws DatabaseException { + Variable variable = getPossibleDomainProperty(graph, Variables.LABEL); + if(variable != null) return variable.getValue(graph); + else return name; + } + + @Override + public Resource getRepresents(ReadGraph graph) throws DatabaseException { + return null; +// Variable variable = getPossibleDomainProperty(graph, Variables.REPRESENTS); +// if(variable != null) return variable.getValue(graph); +// else return null; + } + + @Override + public Resource getType(ReadGraph graph) throws DatabaseException { + return getDomainProperty(graph, Variables.TYPE).getValue(graph); + } + + @Override + public Resource getPossibleType(ReadGraph graph) throws DatabaseException { + Variable v = getPossibleDomainProperty(graph, Variables.TYPE); + return v != null ? v.getPossibleValue(graph) : null; + } + + @Override + public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException { + Resource type = getPossibleType(graph, baseType); + if (type != null) + return type; + throw new NoSingleResultException("variable " + getPossibleURI(graph) + " has no type"); + } + + @Override + public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException { + Variable typev = getPossibleDomainProperty(graph, Variables.TYPE); + if (typev == null) + return null; + Resource type = typev.getValue(graph); + return type != null && graph.isInheritedFrom(type, baseType) ? type : null; + } + + @Override + public Collection getProperties(ReadGraph graph, String classification) throws DatabaseException { + Collection result = null; + Variable singleResult = null; + for(ConstantPropertyVariable property : properties.values()) { + if(property.getClassifications(graph).contains(classification)) { + if(result == null && singleResult == null) singleResult = property; + else if(result == null && singleResult != null) { + result = new ArrayList(); + result.add(singleResult); + result.add(property); + } else { + result.add(property); + } + } + } + if(result != null) return result; + if(singleResult != null) return Collections.singletonList(singleResult); + else return Collections.emptyList(); + } + +}