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