X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fvariable%2FConstantPropertyVariable.java;h=14db2cbfed491e3209e14aceaa33d75717c44469;hb=15af8a20abe8b2ba24b52c9da8bce6c92351dc43;hp=33601b846cc81a0c9b0c86853db68833f4163065;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantPropertyVariable.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantPropertyVariable.java index 33601b846..14db2cbfe 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantPropertyVariable.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantPropertyVariable.java @@ -1,173 +1,188 @@ -package org.simantics.db.layer0.variable; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.type.Datatype; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.utils.ObjectUtils; - -public class ConstantPropertyVariable extends AbstractPropertyVariable { - - final protected Variable parent; - final String name; - final Object value; - final Binding binding; - final Map properties; - - public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding, Collection propertyBuilders, Set classifications) { - assert parent != null; - assert name != null; - this.parent = parent; - this.name = name; - this.value = value; - this.binding = binding; - this.properties = new HashMap(propertyBuilders.size()); - for(ConstantPropertyVariableBuilder builder : propertyBuilders) { - properties.put(builder.getName(), builder.build(this)); - } - if(!classifications.isEmpty()) - properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null)); - } - - public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding) { - this(parent, name, value, binding, Collections.emptyList(), Collections.emptySet()); - } - - @Override - public T getValue(ReadGraph graph) throws DatabaseException { - return (T)value; - } - - @Override - public T getValue(ReadGraph graph, Binding binding) - throws DatabaseException { - return (T)value; - } - - @Override - public void setValue(WriteGraph graph, Object value, Binding binding) - throws DatabaseException { - throw new DatabaseException("Value is constant."); - } - - @Override - public String getName(ReadGraph graph) throws DatabaseException { - return name; - } - - @Override - public Resource getType(ReadGraph graph) throws DatabaseException { - return null; - } - - @Override - public Resource getPossibleType(ReadGraph graph) throws DatabaseException { - return null; - } - - @Override - public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException { - return null; - } - - @Override - public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException { - return null; - } - -// @Override -// public Object getSerialized(ReadGraph graph) throws DatabaseException { -// return name; -// } - - @Override - public Variable getParent(ReadGraph graph) throws DatabaseException { - return parent; - } - - @Override - public Resource getRepresents(ReadGraph graph) throws DatabaseException { - return null; - } - - @Override - public Datatype getDatatype(ReadGraph graph) throws DatabaseException { - return binding != null ? binding.type() : null; - } - - @Override - public Resource getPropertyResource(ReadGraph graph) throws DatabaseException { - return null; - } - - @Override - public Resource getContainerResource(ReadGraph graph) throws DatabaseException { - return null; - } - - @Override - public Variable getPredicate(ReadGraph graph) throws DatabaseException { - return null; - } - - @Override - protected Variable getPossibleDomainProperty(ReadGraph graph, String name) - throws DatabaseException { - return properties.get(name); - } - - @Override - public Map collectDomainProperties(ReadGraph graph, - Map properties) throws DatabaseException { - if(!this.properties.isEmpty()) { - if(properties == null) properties = new HashMap(this.properties.size()); - properties.putAll(this.properties); - } - return properties; - } - - @Override - public Set getClassifications(ReadGraph graph) throws DatabaseException { - Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS); - if(property != null) return property.getValue(graph); - else return Collections.emptySet(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + name.hashCode(); - result = prime * result + parent.hashCode(); - result = prime * result + ((value == null) ? 0 : value.hashCode()); - result = prime * result + ((binding == null) ? 0 : binding.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - ConstantPropertyVariable other = (ConstantPropertyVariable) obj; - if (!name.equals(other.name)) - return false; - if (!parent.equals(other.parent)) - return false; - if (!ObjectUtils.objectEquals(value, other.value)) - return false; - return ObjectUtils.objectEquals(binding, other.binding); - } - -} +package org.simantics.db.layer0.variable; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.type.Datatype; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.function.All; +import org.simantics.utils.ObjectUtils; + +public class ConstantPropertyVariable extends AbstractPropertyVariable { + + final protected Variable parent; + final String name; + final Object value; + final Binding binding; + final Map properties; + final private Resource predicate; + + public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding, Resource predicate, Collection propertyBuilders, Set classifications) { + assert parent != null; + assert name != null; + this.parent = parent; + this.name = name; + this.value = value; + this.binding = binding; + this.predicate = predicate; + this.properties = new HashMap(propertyBuilders.size()); + for(ConstantPropertyVariableBuilder builder : propertyBuilders) { + properties.put(builder.getName(), builder.build(this)); + } + if(!classifications.isEmpty()) + properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null)); + } + + public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding, Collection propertyBuilders, Set classifications) { + this(parent, name, value, binding, null, Collections.emptyList(), Collections.emptySet()); + } + + public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding) { + this(parent, name, value, binding, Collections.emptyList(), Collections.emptySet()); + } + + @Override + public T getValue(ReadGraph graph) throws DatabaseException { + return (T)value; + } + + @Override + public T getValue(ReadGraph graph, Binding binding) + throws DatabaseException { + return (T)value; + } + + @Override + public void setValue(WriteGraph graph, Object value, Binding binding) + throws DatabaseException { + throw new DatabaseException("Value is constant."); + } + + @Override + public String getName(ReadGraph graph) throws DatabaseException { + return name; + } + + @Override + public Resource getType(ReadGraph graph) throws DatabaseException { + return null; + } + + @Override + public Resource getPossibleType(ReadGraph graph) throws DatabaseException { + return null; + } + + @Override + public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException { + return null; + } + + @Override + public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException { + return null; + } + +// @Override +// public Object getSerialized(ReadGraph graph) throws DatabaseException { +// return name; +// } + + @Override + public Variable getParent(ReadGraph graph) throws DatabaseException { + return parent; + } + + @Override + public Resource getRepresents(ReadGraph graph) throws DatabaseException { + return null; + } + + @Override + public Datatype getDatatype(ReadGraph graph) throws DatabaseException { + return binding != null ? binding.type() : null; + } + + @Override + public Resource getPropertyResource(ReadGraph graph) throws DatabaseException { + return predicate; + } + + @Override + public Resource getContainerResource(ReadGraph graph) throws DatabaseException { + return null; + } + + @Override + public Variable getPredicate(ReadGraph graph) throws DatabaseException { + return null; + } + + @Override + protected Variable getPossibleDomainProperty(ReadGraph graph, String name) + throws DatabaseException { + Variable result = properties.get(name); + if(result != null) return result; + if(predicate != null) { + HashMap ps = new HashMap(); + All.collectPropertiesFromContext(graph, this, predicate, ps); + return ps.get(name); + } + return null; + } + + @Override + public Map collectDomainProperties(ReadGraph graph, + Map properties) throws DatabaseException { + if(!this.properties.isEmpty()) { + if(properties == null) properties = new HashMap(this.properties.size()); + properties.putAll(this.properties); + } + if(predicate != null) All.collectPropertiesFromContext(graph, this, predicate, properties); + return properties; + } + + @Override + public Set getClassifications(ReadGraph graph) throws DatabaseException { + Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS); + if(property != null) return property.getValue(graph); + else return Collections.emptySet(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + name.hashCode(); + result = prime * result + parent.hashCode(); + result = prime * result + ((value == null) ? 0 : value.hashCode()); + result = prime * result + ((binding == null) ? 0 : binding.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ConstantPropertyVariable other = (ConstantPropertyVariable) obj; + if (!name.equals(other.name)) + return false; + if (!parent.equals(other.parent)) + return false; + if (!ObjectUtils.objectEquals(value, other.value)) + return false; + return ObjectUtils.objectEquals(binding, other.binding); + } + +}