]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardVariableModifier.java
possibleObjectForType utility
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardVariableModifier.java
1 package org.simantics.db.layer0.variable;
2
3 import java.util.Map;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.binding.Binding;
7 import org.simantics.databoard.type.Datatype;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.request.EnumerationMap;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.layer0.Layer0;
15
16 public class StandardVariableModifier implements VariableModifier {
17
18         final private static Binding datatype_binging = Bindings.getBindingUnchecked(Datatype.class);
19         final public static StandardVariableModifier INSTANCE = new StandardVariableModifier();
20
21         @Override
22         public String isValid(Object newValue) {
23                 return null;
24         }
25
26         @Override
27         public String isValid(Object newValue, Binding binding) {
28                 return null;
29         }
30
31         @Override
32         public void modify(WriteGraph graph, Variable variable, Object value) throws DatabaseException {
33                 modify(graph, variable, value, null);
34         }
35
36         boolean isEnumeration(ReadGraph graph, Resource literal) throws DatabaseException {
37                 
38                 Layer0 L0 = Layer0.getInstance(graph);
39                 Resource parent = graph.getPossibleObject(literal, L0.PartOf);
40                 if(parent == null) return false;
41                 return graph.hasStatement(parent, L0.Enumeration);
42                 
43         }
44         
45         @Override
46         public void modify(WriteGraph graph, Variable variable, Object value, Binding binding) throws DatabaseException {
47
48                 Variable parent = variable.getParent(graph);
49                 Resource container = parent.getPossibleRepresents(graph);
50                 if(container == null) return;
51                 
52                 Resource property = variable.getPossiblePredicateResource(graph);
53                 if(property == null) return;
54                 
55 //              Variable parent = variable.getPossiblePropertyValue(graph, Variables.PARENT);
56 //              Variable predicate = variable.getPossiblePropertyValue(graph, Variables.PREDICATE);
57                 
58 //              Resource container = parent.getPossiblePropertyValue(graph, Variables.REPRESENTS);
59 //              if(container == null) return;
60 //              Resource property = predicate.getPossiblePropertyValue(graph, Variables.REPRESENTS);
61 //              if(property == null) return;
62         
63                 Statement object = graph.getPossibleStatement(container, property);
64                 if(object == null) return;
65                 
66                 Resource objectResource = object.getObject();
67                 if(isEnumeration(graph, objectResource)) {
68
69                         Layer0 L0 = Layer0.getInstance(graph);
70                         Resource type = graph.getSingleObject(objectResource, L0.PartOf);
71                         
72                         Map<String, Resource> enumMap = graph.syncRequest(new EnumerationMap(type));
73                         Resource newLiteral = enumMap.get(value);
74                         graph.deny(container, property, objectResource);
75                         graph.claim(container, property, newLiteral);
76                         
77                         return;
78                 }
79                 
80                 if(object.isAsserted(container)) {
81
82                         Layer0 L0 = Layer0.getInstance(graph);
83                         Resource type = graph.getPossibleType(objectResource, L0.Literal);
84
85                         Statement dt = graph.getPossibleStatement(objectResource, L0.HasDataType);
86                         Datatype custom = dt.isAsserted(objectResource) ? null : (Datatype)graph.getValue(dt.getObject(), datatype_binging);
87
88                         objectResource = graph.newResource();
89                         graph.claim(objectResource, L0.InstanceOf, null, type);
90                         graph.claim(container, property, objectResource);
91                         if(custom != null) {
92                                 graph.addLiteral(objectResource, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, custom, datatype_binging);
93                         }
94                         
95                         
96 //                      Resource usesUnit = graph.getPossibleResource("http://flowbat.fi/Flowbat-0.1/UsesDatatype");
97 //                      if(usesUnit != null) {
98 //                              String unit = graph.getPossibleRelatedValue(oldObject, usesUnit, Bindings.STRING);
99 //                              if(unit != null) {
100 //                                      graph.claimLiteral(objectResource, usesUnit, unit, Bindings.STRING);
101 //                              }
102 //                              Resource isDefault = graph.getResource("http://flowbat.fi/Flowbat-0.1/IsDefault");
103 //                              Resource isUndefined = graph.getResource("http://flowbat.fi/Flowbat-0.1/IsUndefined");
104 //                              graph.claimLiteral(objectResource, isDefault, false, Bindings.BOOLEAN);
105 //                              graph.claimLiteral(objectResource, isUndefined, false, Bindings.BOOLEAN);
106 //                      }
107                         
108                 }
109                 graph.claimValue(objectResource, value, binding);
110                 
111         }
112         
113
114 }