1 package org.simantics.db.layer0.variable;
\r
3 import java.util.Collection;
\r
4 import java.util.Collections;
\r
5 import java.util.HashMap;
\r
6 import java.util.Map;
\r
7 import java.util.Set;
\r
9 import org.simantics.databoard.binding.Binding;
\r
10 import org.simantics.databoard.type.Datatype;
\r
11 import org.simantics.db.ReadGraph;
\r
12 import org.simantics.db.Resource;
\r
13 import org.simantics.db.WriteGraph;
\r
14 import org.simantics.db.exception.DatabaseException;
\r
15 import org.simantics.utils.ObjectUtils;
\r
17 abstract public class AbstractConstantPropertyVariable extends AbstractPropertyVariable {
\r
19 final protected Variable parent;
\r
21 final Binding binding;
\r
22 final Map<String, Variable> properties;
\r
24 public AbstractConstantPropertyVariable(Variable parent, String name, Binding binding, Collection<ConstantPropertyVariableBuilder> propertyBuilders, Set<String> classifications) {
\r
25 assert parent != null;
\r
26 assert name != null;
\r
27 this.parent = parent;
\r
29 this.binding = binding;
\r
30 this.properties = new HashMap<String, Variable>(propertyBuilders.size());
\r
31 for(ConstantPropertyVariableBuilder builder : propertyBuilders) {
\r
32 properties.put(builder.getName(), builder.build(this));
\r
34 if(!classifications.isEmpty())
\r
35 properties.put(Variables.CLASSIFICATIONS, new ConstantPropertyVariable(this, Variables.CLASSIFICATIONS, classifications, null));
\r
38 public AbstractConstantPropertyVariable(Variable parent, String name, Binding binding) {
\r
39 this(parent, name, binding, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet());
\r
43 public void setValue(WriteGraph graph, Object value, Binding binding)
\r
44 throws DatabaseException {
\r
45 throw new DatabaseException("Value is constant.");
\r
49 public String getName(ReadGraph graph) throws DatabaseException {
\r
54 public Resource getType(ReadGraph graph) throws DatabaseException {
\r
59 public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
\r
64 public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
\r
69 public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
\r
74 // public Object getSerialized(ReadGraph graph) throws DatabaseException {
\r
79 public Variable getParent(ReadGraph graph) throws DatabaseException {
\r
84 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
\r
89 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
\r
90 return binding != null ? binding.type() : null;
\r
94 public Resource getPropertyResource(ReadGraph graph) throws DatabaseException {
\r
99 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
\r
104 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
\r
109 protected Variable getPossibleDomainProperty(ReadGraph graph, String name)
\r
110 throws DatabaseException {
\r
111 return properties.get(name);
\r
115 public Map<String, Variable> collectDomainProperties(ReadGraph graph,
\r
116 Map<String, Variable> properties) throws DatabaseException {
\r
117 if(!this.properties.isEmpty()) {
\r
118 if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());
\r
119 properties.putAll(this.properties);
\r
125 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
\r
126 Variable property = getPossibleDomainProperty(graph, Variables.CLASSIFICATIONS);
\r
127 if(property != null) return property.getValue(graph);
\r
128 else return Collections.emptySet();
\r
132 public int hashCode() {
\r
133 final int prime = 31;
\r
135 result = prime * result + name.hashCode();
\r
136 result = prime * result + parent.hashCode();
\r
137 result = prime * result + ((binding == null) ? 0 : binding.hashCode());
\r
142 public boolean equals(Object obj) {
\r
147 if (getClass() != obj.getClass())
\r
149 AbstractConstantPropertyVariable other = (AbstractConstantPropertyVariable) obj;
\r
150 if (!name.equals(other.name))
\r
152 if (!parent.equals(other.parent))
\r
154 return ObjectUtils.objectEquals(binding, other.binding);
\r