1 package org.simantics.db.layer0.variable;
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;
16 public class StandardVariableModifier implements VariableModifier {
18 final private static Binding datatype_binging = Bindings.getBindingUnchecked(Datatype.class);
19 final public static StandardVariableModifier INSTANCE = new StandardVariableModifier();
22 public String isValid(Object newValue) {
27 public String isValid(Object newValue, Binding binding) {
32 public void modify(WriteGraph graph, Variable variable, Object value) throws DatabaseException {
33 modify(graph, variable, value, null);
36 boolean isEnumeration(ReadGraph graph, Resource literal) throws DatabaseException {
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);
46 public void modify(WriteGraph graph, Variable variable, Object value, Binding binding) throws DatabaseException {
48 Variable parent = variable.getParent(graph);
49 Resource container = parent.getPossibleRepresents(graph);
50 if(container == null) return;
52 Resource property = variable.getPossiblePredicateResource(graph);
53 if(property == null) return;
55 // Variable parent = variable.getPossiblePropertyValue(graph, Variables.PARENT);
56 // Variable predicate = variable.getPossiblePropertyValue(graph, Variables.PREDICATE);
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;
63 Statement object = graph.getPossibleStatement(container, property);
64 if(object == null) return;
66 Resource objectResource = object.getObject();
67 if(isEnumeration(graph, objectResource)) {
69 Layer0 L0 = Layer0.getInstance(graph);
70 Resource type = graph.getSingleObject(objectResource, L0.PartOf);
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);
80 if(object.isAsserted(container)) {
82 Layer0 L0 = Layer0.getInstance(graph);
83 Resource type = graph.getPossibleType(objectResource, L0.Literal);
85 Statement dt = graph.getPossibleStatement(objectResource, L0.HasDataType);
86 Datatype custom = dt.isAsserted(objectResource) ? null : (Datatype)graph.getValue(dt.getObject(), datatype_binging);
88 objectResource = graph.newResource();
89 graph.claim(objectResource, L0.InstanceOf, null, type);
90 graph.claim(container, property, objectResource);
92 graph.addLiteral(objectResource, L0.HasDataType, L0.HasDataType_Inverse, L0.DataType, custom, datatype_binging);
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);
100 // graph.claimLiteral(objectResource, usesUnit, unit, Bindings.STRING);
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);
109 graph.claimValue(objectResource, value, binding);