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