]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/ConstantChildVariable.java
Initial support for concurrency in databoard, bindings and serializers
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / ConstantChildVariable.java
1 package org.simantics.db.layer0.variable;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10
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;
16
17 public class ConstantChildVariable extends AbstractChildVariable {
18
19     final Variable parent;
20     final String name;
21     final Map<String, ConstantPropertyVariable> properties;
22
23     public ConstantChildVariable(Variable parent, String name) {
24         this.parent = parent;
25         this.name = name;
26         this.properties = new THashMap<String, ConstantPropertyVariable>();
27     }
28
29     public ConstantChildVariable(Variable parent, String name, Collection<ConstantPropertyVariableBuilder> propertyBuilders) {
30         this(parent, name);
31         for(ConstantPropertyVariableBuilder b : propertyBuilders) {
32                 ConstantPropertyVariable property = b.build(this);
33                 properties.put(b.getName(), property);
34         }
35     }
36     
37     public ConstantChildVariable(Variable parent, String name, ConstantPropertyVariableBuilder ... propertyBuilders) {
38         this(parent, name);
39         for(ConstantPropertyVariableBuilder b : propertyBuilders) {
40                 ConstantPropertyVariable property = b.build(this);
41                 properties.put(b.getName(), property);
42         }
43     }
44
45     public ConstantChildVariable(Variable parent, String name, String[] propertyNames, Binding[] bindings, Object ... values) {
46
47         this(parent, name);
48
49         buildProperties(this, propertyNames, bindings, values, properties);
50
51     }
52
53     private Map<String, Variable> buildProperties(Variable parent, String[] propertyNames, Binding[] bindings, Object ... values) {
54
55         assert parent != null;
56         assert propertyNames.length == bindings.length;
57         assert propertyNames.length == values.length;
58
59         Map<String, Variable> result = new HashMap<String, Variable>();
60         
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]));
64         }
65         
66         return result;
67
68     }
69
70     @Override
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);
74         return properties;
75     }
76     
77     @Override
78     public Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
79         return properties.get(name);
80     }
81     
82     @Override
83     public Variable getParent(ReadGraph graph) throws DatabaseException {
84         return parent;
85     }
86     
87 //    @Override
88 //    public Object getSerialized(ReadGraph graph) throws DatabaseException {
89 //      return getName(graph);
90 //    }
91     
92     @Override
93     public String getName(ReadGraph graph) throws DatabaseException {
94         return name;
95     }
96
97     @Override
98     public String getLabel(ReadGraph graph) throws DatabaseException {
99         Variable variable = getPossibleDomainProperty(graph, Variables.LABEL);
100         if(variable != null) return variable.getValue(graph);
101         else return name;
102     }
103
104     @Override
105     public Resource getRepresents(ReadGraph graph) throws DatabaseException {
106         return null;
107 //      Variable variable = getPossibleDomainProperty(graph, Variables.REPRESENTS);
108 //      if(variable != null) return variable.getValue(graph);
109 //      else return null;
110     }
111
112     @Override
113     public Resource getType(ReadGraph graph) throws DatabaseException {
114         return getDomainProperty(graph, Variables.TYPE).getValue(graph);
115     }
116
117     @Override
118     public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
119         Variable v = getPossibleDomainProperty(graph, Variables.TYPE);
120         return v != null ? v.<Resource>getPossibleValue(graph) : null;
121     }
122
123     @Override
124     public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
125         Resource type = getPossibleType(graph, baseType);
126         if (type != null)
127                 return type;
128         throw new NoSingleResultException("variable " + getPossibleURI(graph) + " has no type", -1);
129     }
130
131     @Override
132     public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
133         Variable typev = getPossibleDomainProperty(graph, Variables.TYPE);
134         if (typev == null)
135                 return null;
136         Resource type = typev.getValue(graph);
137         return type != null && graph.isInheritedFrom(type, baseType) ? type : null;
138     }
139
140     @Override
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);
151                         } else {
152                                 result.add(property);
153                         }
154                 }
155         }
156         if(result != null) return result;
157         if(singleResult != null) return Collections.singletonList(singleResult);
158         else return Collections.emptyList();
159     } 
160
161 }