1 package org.simantics.db.layer0.variable;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.type.Datatype;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.function.All;
16 import org.simantics.utils.ObjectUtils;
18 public class ConstantPropertyVariable extends AbstractPropertyVariable {
20 final protected Variable parent;
23 final Binding binding;
24 final Map<String, Variable> properties;
25 final private Resource predicate;
27 public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding, Resource predicate, Collection<ConstantPropertyVariableBuilder> propertyBuilders, Set<String> classifications) {
28 assert parent != null;
33 this.binding = binding;
34 this.predicate = predicate;
35 this.properties = new HashMap<String, Variable>(propertyBuilders.size());
36 for(ConstantPropertyVariableBuilder builder : propertyBuilders) {
37 properties.put(builder.getName(), builder.build(this));
39 if(!classifications.isEmpty())
40 properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null));
43 public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding, Collection<ConstantPropertyVariableBuilder> propertyBuilders, Set<String> classifications) {
44 this(parent, name, value, binding, null, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet());
47 public ConstantPropertyVariable(Variable parent, String name, Object value, Binding binding) {
48 this(parent, name, value, binding, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet());
52 public <T> T getValue(ReadGraph graph) throws DatabaseException {
57 public <T> T getValue(ReadGraph graph, Binding binding)
58 throws DatabaseException {
63 public void setValue(WriteGraph graph, Object value, Binding binding)
64 throws DatabaseException {
65 throw new DatabaseException("Value is constant.");
69 public String getName(ReadGraph graph) throws DatabaseException {
74 public Resource getType(ReadGraph graph) throws DatabaseException {
79 public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
84 public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
89 public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
94 // public Object getSerialized(ReadGraph graph) throws DatabaseException {
99 public Variable getParent(ReadGraph graph) throws DatabaseException {
104 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
109 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
110 return binding != null ? binding.type() : null;
114 public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {
119 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
124 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
129 protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
130 throws DatabaseException {
131 Variable result = properties.get(name);
132 if(result != null) return result;
133 if(predicate != null) {
134 HashMap<String,Variable> ps = new HashMap<String,Variable>();
135 All.collectPropertiesFromContext(graph, this, predicate, ps);
142 public Map<String, Variable> collectDomainProperties(ReadGraph graph,
143 Map<String, Variable> properties) throws DatabaseException {
144 if(!this.properties.isEmpty()) {
145 if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());
146 properties.putAll(this.properties);
148 if(predicate != null) All.collectPropertiesFromContext(graph, this, predicate, properties);
153 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
154 Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS);
155 if(property != null) return property.getValue(graph);
156 else return Collections.emptySet();
160 public int hashCode() {
161 final int prime = 31;
163 result = prime * result + name.hashCode();
164 result = prime * result + parent.hashCode();
165 result = prime * result + ((value == null) ? 0 : value.hashCode());
166 result = prime * result + ((binding == null) ? 0 : binding.hashCode());
171 public boolean equals(Object obj) {
176 if (getClass() != obj.getClass())
178 ConstantPropertyVariable other = (ConstantPropertyVariable) obj;
179 if (!name.equals(other.name))
181 if (!parent.equals(other.parent))
183 if (!ObjectUtils.objectEquals(value, other.value))
185 return ObjectUtils.objectEquals(binding, other.binding);