1 package org.simantics.db.layer0.variable;
3 import gnu.trove.map.hash.THashMap;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.exception.NoSingleResultException;
17 public class ConstantChildVariable extends AbstractChildVariable {
19 final Variable parent;
21 final Map<String, ConstantPropertyVariable> properties;
23 public ConstantChildVariable(Variable parent, String name) {
26 this.properties = new THashMap<String, ConstantPropertyVariable>();
29 public ConstantChildVariable(Variable parent, String name, Collection<ConstantPropertyVariableBuilder> propertyBuilders) {
31 for(ConstantPropertyVariableBuilder b : propertyBuilders) {
32 ConstantPropertyVariable property = b.build(this);
33 properties.put(b.getName(), property);
37 public ConstantChildVariable(Variable parent, String name, ConstantPropertyVariableBuilder ... propertyBuilders) {
39 for(ConstantPropertyVariableBuilder b : propertyBuilders) {
40 ConstantPropertyVariable property = b.build(this);
41 properties.put(b.getName(), property);
45 public ConstantChildVariable(Variable parent, String name, String[] propertyNames, Binding[] bindings, Object ... values) {
49 buildProperties(this, propertyNames, bindings, values, properties);
53 private Map<String, Variable> buildProperties(Variable parent, String[] propertyNames, Binding[] bindings, Object ... values) {
55 assert parent != null;
56 assert propertyNames.length == bindings.length;
57 assert propertyNames.length == values.length;
59 Map<String, Variable> result = new HashMap<String, Variable>();
61 for(int i=0;i<values.length;i++) {
62 String property = propertyNames[i];
63 result.put(property, new ConstantPropertyVariable(parent, property, values[i], bindings[i]));
71 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
72 if(properties == null) properties = new HashMap<String,Variable>(this.properties.size());
73 properties.putAll(this.properties);
78 public Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
79 return properties.get(name);
83 public Variable getParent(ReadGraph graph) throws DatabaseException {
88 // public Object getSerialized(ReadGraph graph) throws DatabaseException {
89 // return getName(graph);
93 public String getName(ReadGraph graph) throws DatabaseException {
98 public String getLabel(ReadGraph graph) throws DatabaseException {
99 Variable variable = getPossibleDomainProperty(graph, Variables.LABEL);
100 if(variable != null) return variable.getValue(graph);
105 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
107 // Variable variable = getPossibleDomainProperty(graph, Variables.REPRESENTS);
108 // if(variable != null) return variable.getValue(graph);
113 public Resource getType(ReadGraph graph) throws DatabaseException {
114 return getDomainProperty(graph, Variables.TYPE).getValue(graph);
118 public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
119 Variable v = getPossibleDomainProperty(graph, Variables.TYPE);
120 return v != null ? v.<Resource>getPossibleValue(graph) : null;
124 public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
125 Resource type = getPossibleType(graph, baseType);
128 throw new NoSingleResultException("variable " + getPossibleURI(graph) + " has no type", -1);
132 public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
133 Variable typev = getPossibleDomainProperty(graph, Variables.TYPE);
136 Resource type = typev.getValue(graph);
137 return type != null && graph.isInheritedFrom(type, baseType) ? type : null;
141 public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {
142 Collection<Variable> result = null;
143 Variable singleResult = null;
144 for(ConstantPropertyVariable property : properties.values()) {
145 if(property.getClassifications(graph).contains(classification)) {
146 if(result == null && singleResult == null) singleResult = property;
147 else if(result == null && singleResult != null) {
148 result = new ArrayList<Variable>();
149 result.add(singleResult);
150 result.add(property);
152 result.add(property);
156 if(result != null) return result;
157 if(singleResult != null) return Collections.singletonList(singleResult);
158 else return Collections.emptyList();