1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.variable;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.LinkedList;
18 import java.util.List;
21 import org.simantics.databoard.Databoard;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.mutable.Variant;
24 import org.simantics.databoard.type.Datatype;
25 import org.simantics.databoard.type.NumberType;
26 import org.simantics.databoard.util.URIStringUtils;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.RequestProcessor;
29 import org.simantics.db.Resource;
30 import org.simantics.db.WriteGraph;
31 import org.simantics.db.common.request.PossibleIndexRoot;
32 import org.simantics.db.common.request.TernaryRead;
33 import org.simantics.db.common.utils.Logger;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.exception.InvalidVariableException;
36 import org.simantics.db.layer0.exception.MissingVariableException;
37 import org.simantics.db.layer0.exception.MissingVariableValueException;
38 import org.simantics.db.layer0.request.Model;
39 import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable;
40 import org.simantics.db.layer0.request.PossibleVariableIndexRoot;
41 import org.simantics.db.layer0.request.PossibleVariableModel;
42 import org.simantics.db.layer0.request.PropertyInfo;
43 import org.simantics.db.layer0.request.PropertyInfoRequest;
44 import org.simantics.db.layer0.request.ResourceURIToVariable;
45 import org.simantics.db.layer0.request.VariableIndexRoot;
46 import org.simantics.db.layer0.request.VariableURI;
47 import org.simantics.layer0.Layer0;
48 import org.simantics.operation.Layer0X;
49 import org.simantics.project.ontology.ProjectResource;
50 import org.simantics.scl.runtime.function.Function1;
51 import org.simantics.scl.runtime.function.Function2;
52 import org.simantics.scl.runtime.function.Function3;
53 import org.simantics.simulation.ontology.SimulationResource;
54 import org.simantics.simulator.variable.exceptions.NodeManagerException;
55 import org.simantics.utils.datastructures.Pair;
57 import gnu.trove.map.hash.TObjectIntHashMap;
59 final public class Variables {
61 public static final Variant PENDING_NODE_VALUE = new Variant();
63 public static final NodeStructure PENDING_NODE_STRUCTURE = new NodeStructure(Collections.emptyMap(), Collections.emptyMap()) {
64 public boolean equals(Object object) {
65 return this == object;
69 public static enum Role {
71 CHILD("/"), PROPERTY("#");
73 transient final String identifier;
75 private Role(String identifier) {
76 this.identifier = identifier;
79 final public String getIdentifier() {
83 public static Role getRole( String identifier ) {
84 for (Role role : Role.values()) if (role.identifier.equals( identifier )) return role;
91 //final public static String PREDICATE = "PREDICATE";
95 final public static String NAME = "HasName";
96 final public static String CLASSIFICATIONS = "classifications";
97 final public static String EXPRESSION = "HasExpression";
98 final public static String INPUT_VALIDATOR = "HasInputValidator";
99 final public static String INPUT_MODIFIER = "HasInputModifier";
100 final public static String FORMATTER = "HasFormatter";
102 final public static String STANDARD_RESOURCE = "hasStandardResource";
105 //final public static String REPRESENTS = "Represents";
106 final public static String TYPE = "Type";
107 final public static String URI = "URI";
109 * @deprecated use {@link Variable#getRVI(ReadGraph)} and {@link RVI} instead.
112 // final public static String SERIALISED = "Serialised";
115 //final public static String PARENT = "Parent";
116 //final public static String ROLE = "Role";
117 //final public static String DATATYPE = "DATATYPE";
118 //final public static String UNIT = "UNIT";
120 final public static String VALID = "valid";
121 final public static String REQUIRED = "required";
122 final public static String DEFAULT = "default";
123 final public static String READONLY = "readOnly";
124 final public static String VALIDATOR = "validator";
126 final public static String LABEL = "HasLabel";
128 final public static String ENUMERATION_VALUES = "HasEnumerationValues";
129 final public static String CUSTOM_MODIFIER = "HasCustomModifier";
131 final public static String DISPLAY_COLUMN = "HasDisplayColumn";
133 final public static String DISPLAY_PROPERTY = "HasDisplayProperty";
134 final public static String DISPLAY_VALUE = "HasDisplayValue";
135 final public static String DISPLAY_UNIT = "HasDisplayUnit";
137 final public static String CONVERTED_VALUE = "convertedValue";
140 * This property should exist for array valued property variables.
142 final public static String ARRAY_SIZE = "ARRAY_SIZE";
146 // use etc. variable.adapt(graph, Interface.class).getResource()
147 final public static String RESOURCE = "Resource";
149 final public static String CONTAINER_RESOURCE = "ContainerResource";
151 final public static String PROPERTY_RESOURCE = "PROPERTY_RESOURCE";
154 public final static String[] builtins = {
159 public static Variable getPossibleVariable(ReadGraph graph, Resource resource) throws DatabaseException {
160 String uri = graph.getPossibleURI(resource);
161 return uri != null ? getPossibleVariable(graph, uri) : null;
164 public static Variable getPossibleVariable(ReadGraph graph, String uri) throws DatabaseException {
166 return getVariable(graph, uri);
167 } catch (DatabaseException e) {
172 public static Variable getVariable(ReadGraph graph, Resource resource) throws DatabaseException {
173 return getVariable(graph, graph.getURI(resource));
176 public static Variable getVariable(ReadGraph graph, String uri) throws DatabaseException {
178 return graph.sync(new ResourceURIToVariable(uri));
179 } catch (MissingVariableException e) {
180 return VariableRepository.get(graph, uri);
184 private static int commonPrefixLength(String a, String b) {
185 int maxC = Math.min(a.length(), b.length());
186 for(int c=0;c<maxC;++c) {
187 if(a.charAt(c) != b.charAt(c))
193 private static boolean isSplitPos(String str, int p) {
196 char c = str.charAt(p);
197 return c=='/' || c=='#';
200 private static int pathLength(String path) {
202 for(int i=0;i<path.length();++i) {
203 char c = path.charAt(i);
210 private static String prefixByParentPath(int parentPathLength, String suffix) {
211 StringBuilder b = new StringBuilder();
212 for(int i=0;i<parentPathLength;++i)
218 public static String getRVI(ReadGraph graph, Variable base, Variable other) throws DatabaseException {
219 String baseURI = graph.syncRequest(new VariableURI(base));
220 String otherURI = graph.syncRequest(new VariableURI(other));
221 return getRelativeRVI(baseURI, otherURI);
224 public static String getRelativeRVI(String baseURI, String otherURI) {
225 int prefixLength = commonPrefixLength(baseURI, otherURI);
226 if(!isSplitPos(baseURI, prefixLength) || !isSplitPos(otherURI, prefixLength)) {
227 for(--prefixLength;prefixLength > 0 && !isSplitPos(baseURI, prefixLength);--prefixLength);
229 if(prefixLength == baseURI.length())
230 return otherURI.substring(prefixLength);
232 return prefixByParentPath(
233 pathLength(baseURI.substring(prefixLength)),
234 otherURI.substring(prefixLength));
237 public static String getRVI2(ReadGraph graph, Variable base, Variable other) throws DatabaseException {
238 TObjectIntHashMap<Variable> baseLength = new TObjectIntHashMap<Variable>();
239 for(int depth=0;base != null;base = base.getParent(graph),++depth)
240 baseLength.put(base, depth);
242 for(cur=other;!baseLength.containsKey(cur);cur=cur.getParent(graph));
245 String curURI = cur.getURI(graph);
246 String otherURI = other.getURI(graph);
248 return prefixByParentPath(baseLength.get(cur), otherURI.substring(curURI.length()));
251 public static String getProjectRVI(ReadGraph graph, Variable variable) throws DatabaseException {
252 Resource project = getProject(graph, variable);
253 String projectURI = graph.getURI(project);
254 return variable.getURI(graph).substring(projectURI.length());
257 private static int getSegmentEnd(String suffix) {
259 for(pos=1;pos<suffix.length();++pos) {
260 char c = suffix.charAt(pos);
261 if(c == '/' || c == '#')
267 public static String getRVI(String rvi, String suffix) throws DatabaseException {
268 if(suffix.isEmpty()) return rvi;
269 switch(suffix.charAt(0)) {
271 return getRVI(URIStringUtils.getRVIParent(rvi), suffix.substring(1));
274 int segmentEnd = getSegmentEnd(suffix);
275 return getRVI(rvi + "#" + suffix.substring(1, segmentEnd), suffix.substring(segmentEnd));
278 int segmentEnd = getSegmentEnd(suffix);
279 return getRVI(rvi + "/" + suffix.substring(1, segmentEnd), suffix.substring(segmentEnd));
287 public static Variable getRootVariable(ReadGraph graph) throws DatabaseException {
288 return graph.adapt(graph.getRootLibrary(), Variable.class);
291 public static Resource getPossibleIndexRoot(ReadGraph graph, Variable variable) throws DatabaseException {
292 return graph.syncRequest(new PossibleVariableIndexRoot(variable));
295 public static Resource getIndexRoot(ReadGraph graph, Variable variable) throws DatabaseException {
296 return graph.syncRequest(new VariableIndexRoot(variable));
299 public static Resource getModel(ReadGraph graph, Variable variable) throws DatabaseException {
300 String URI = variable.getURI(graph);
301 return VariablesImpl.getFirst(graph, SimulationResource.getInstance(graph).Model, URI, 8);
304 public static Resource getPossibleModel(ReadGraph graph, Variable variable) throws DatabaseException {
305 return graph.syncRequest(new PossibleVariableModel(variable));
308 public static Resource getProject(ReadGraph graph, Variable variable) throws DatabaseException {
309 String URI = variable.getURI(graph);
310 return VariablesImpl.getFirst(graph, ProjectResource.getInstance(graph).Project, URI, 8);
313 public static Variable getConfigurationContext(ReadGraph graph, Resource resource) throws DatabaseException {
314 SimulationResource SIMU = SimulationResource.getInstance(graph);
315 if(!graph.isInstanceOf(resource, SIMU.Model)) resource = graph.sync(new Model(resource));
316 Resource configurationResource = graph.getSingleObject(resource, SIMU.HasConfiguration);
317 return Variables.getVariable(graph, configurationResource);
320 public static Resource getConfigurationContextResource(ReadGraph graph, Resource resource) throws DatabaseException {
321 Variable config = getConfigurationContext(graph, resource);
322 return config.getRepresents(graph);
325 public static Variable getConfigurationContext(ReadGraph graph, Variable variable) throws DatabaseException {
326 SimulationResource SIMU = SimulationResource.getInstance(graph);
327 Resource model = Variables.getModel(graph, variable);
328 Resource configurationResource = graph.getSingleObject(model, SIMU.HasConfiguration);
329 return Variables.getVariable(graph, configurationResource);
332 public static Resource getPossibleConfigurationContextResource(ReadGraph graph, Resource resource) throws DatabaseException {
333 Variable config = getPossibleConfigurationContext(graph, resource);
334 return config != null ? config.getPossibleRepresents(graph) : null;
337 public static Variable getPossibleConfigurationContext(ReadGraph graph, Resource resource) throws DatabaseException {
338 SimulationResource SIMU = SimulationResource.getInstance(graph);
339 if (!graph.isInstanceOf(resource, SIMU.Model)) resource = graph.sync(new PossibleIndexRoot(resource));
340 if (resource == null)
342 Resource configurationResource = graph.getPossibleObject(resource, SIMU.HasConfiguration);
343 if (configurationResource == null)
345 return Variables.getPossibleVariable(graph, configurationResource);
348 public static Variable getPossibleConfigurationContext(ReadGraph graph, Variable variable) throws DatabaseException {
349 SimulationResource SIMU = SimulationResource.getInstance(graph);
350 Resource model = getPossibleIndexRoot(graph, variable);
353 Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);
354 if (configurationResource == null)
356 return Variables.getPossibleVariable(graph, configurationResource);
359 public static Variable getConfigurationVariable(ReadGraph graph, Resource resource, String RVI) throws DatabaseException {
360 Variable context = getConfigurationContext(graph, resource);
361 return context.browse(graph, RVI);
364 public static Variable getConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
365 Variable context = getConfigurationContext(graph, variable);
366 RVI rvi = variable.getRVI(graph);
367 return rvi.resolve(graph, context);
370 public static Variable getPossibleConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
371 Variable context = getPossibleConfigurationContext(graph, variable);
375 RVI rvi = variable.getRVI(graph);
376 return rvi.resolvePossible(graph, context);
377 } catch (MissingVariableException e) {
383 public static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException {
384 Variable var = rvi.resolve(graph, getConfigurationContext(graph, resource));
385 return var.getDatatype(graph);
389 public static Resource getRealization(ReadGraph graph, Variable variable) throws DatabaseException {
390 String URI = variable.getURI(graph);
391 return VariablesImpl.getFirst(graph, Layer0X.getInstance(graph).Realization, URI, 8);
394 public static boolean isContext(ReadGraph graph, Variable variable) throws DatabaseException {
395 Resource type = variable.getPossibleType(graph);
397 if(graph.isInheritedFrom(type, Layer0.getInstance(graph).RVIContext)) return true;
402 public static Variable getPossibleContext(ReadGraph graph, Variable variable) throws DatabaseException {
403 if(isContext(graph, variable)) return variable;
404 Variable parent = variable.getParent(graph);
405 if(parent == null) return null;
406 return getPossibleContext(graph, parent);
409 public static Variable getContext(ReadGraph graph, Variable variable) throws DatabaseException {
410 Variable context = getPossibleContext(graph, variable);
412 throw new MissingVariableException("No context found for " + variable.getURI(graph), variable.getPossibleRepresents(graph));
416 public static RVI getRVI2(ReadGraph graph, Variable variable) throws DatabaseException {
417 return variable.getRVI(graph);
420 public static RVI getPossibleRVI2(ReadGraph graph, Variable variable) throws DatabaseException {
422 return variable.getRVI(graph);
423 } catch (DatabaseException e) {
428 public static String getRVI(ReadGraph graph, Variable variable) throws DatabaseException {
429 Resource realizationResource = getRealization(graph, variable);
430 if (realizationResource == null)
431 throw new InvalidVariableException("No realization found for " + variable.getURI(graph));
432 return variable.getURI(graph).substring(graph.getURI(realizationResource).length());
435 public static String getRVI(ReadGraph graph, Resource config) throws DatabaseException {
436 Variable var = getVariable(graph, config);
437 return getRVI(graph, var);
440 public static String getPossibleRVI(ReadGraph graph, Resource config) throws DatabaseException {
441 Variable var = getPossibleVariable(graph, config);
442 return var != null ? getPossibleRVI(graph, var) : null;
445 public static String getPossibleRVI(ReadGraph graph, Variable variable) throws DatabaseException {
447 return getRVI(graph, variable);
448 } catch (DatabaseException e) {
453 public static List<Variable> getPath(ReadGraph graph, Variable base, Variable var) throws DatabaseException {
454 if(!isChild(graph, base, var)) return null;
455 LinkedList<Variable> result = new LinkedList<Variable>();
456 var = var.getParent(graph);
457 while(!var.equals(base)) {
458 result.addFirst(var);
459 var = var.getParent(graph);
464 public static Variable getChild(ReadGraph graph, Variable base, Variable var) throws DatabaseException {
465 List<Variable> path = getPath(graph, base, var);
466 if(path == null || path.size() == 0) return null;
470 public static boolean isChild(ReadGraph graph, Variable base, Variable var) throws DatabaseException {
471 if(base.equals(var)) return false;
472 return var.getURI(graph).startsWith(base.getURI(graph));
475 public static Variable switchRealization(ReadGraph graph, Variable variable, Resource realization) throws DatabaseException {
476 Resource current = getRealization(graph, variable);
478 throw new InvalidVariableException("No current realization found for variable");
479 return switchRealization(graph, variable, current, realization);
482 public static Variable switchPossibleContext(ReadGraph graph, Variable variable, Resource realization) throws DatabaseException {
483 Variable current = getPossibleContext(graph, variable);
486 Resource currentContext = current.getPossibleRepresents(graph);
487 if (currentContext == null)
489 return switchPossibleRealization(graph, variable, currentContext, realization);
492 public static Variable switchRealization(ReadGraph graph, Variable variable, Variable realization) throws DatabaseException {
493 Resource current = getRealization(graph, variable);
494 return switchRealization(graph, variable, current, realization);
497 public static Variable switchRealization(ReadGraph graph, Variable variable, Resource currentRealization, Resource targetRealization) throws DatabaseException {
498 String currentURI = graph.getURI(currentRealization);
499 String targetURI = graph.getURI(targetRealization);
500 String variableURI = variable.getURI(graph);
501 String targetVariableURI = targetURI + variableURI.substring(currentURI.length());
502 return getVariable(graph, targetVariableURI);
505 public static Variable switchRealization(ReadGraph graph, Variable variable, Resource currentRealization, Variable targetRealization) throws DatabaseException {
506 String currentURI = graph.getURI(currentRealization);
507 String targetURI = targetRealization.getURI(graph);
508 String variableURI = variable.getURI(graph);
509 String targetVariableURI = targetURI + variableURI.substring(currentURI.length());
510 return getVariable(graph, targetVariableURI);
513 public static Variable switchPossibleRealization(ReadGraph graph, Variable variable, Resource currentRealization, Resource targetRealization) throws DatabaseException {
514 String currentURI = graph.getURI(currentRealization);
515 String targetURI = graph.getURI(targetRealization);
516 String variableURI = variable.getURI(graph);
517 String targetVariableURI = targetURI + variableURI.substring(currentURI.length());
518 return getPossibleVariable(graph, targetVariableURI);
521 public static Variable toConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
522 Variable config = getConfigurationContext(graph, variable);
523 return switchRealization(graph, variable, config);
526 public static Variable toPossibleConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
528 Resource represents = variable.getPossibleRepresents(graph);
529 if(represents == null) return null;
530 Resource config = getPossibleConfigurationContextResource(graph, represents);
531 if(config == null) return null;
532 return switchPossibleContext(graph, variable, config);
536 public static String toRVI(ReadGraph graph, List<Resource> compositePath) throws DatabaseException {
537 Layer0 L0 = Layer0.getInstance(graph);
538 StringBuilder rvi = new StringBuilder();
539 for (Resource composite : compositePath) {
540 String name = graph.getPossibleRelatedValue(composite, L0.HasName);
545 String escapedName = URIStringUtils.escape(name);
546 rvi.append(escapedName);
548 return rvi.toString();
551 public static String appendRVI(ReadGraph graph, String modelURI, String rvi, Resource configuration) throws DatabaseException {
552 Layer0 L0 = Layer0.getInstance(graph);
553 String partName = graph.getPossibleRelatedValue(configuration, L0.HasName);
554 if (partName == null)
555 throw new MissingVariableException("Can not append a child corresponding to " + configuration + " to rvi '"
556 + rvi + "' since there is no name.", configuration);
557 String escaped = URIStringUtils.escape(partName);
558 return rvi + "/" + escaped;
561 public static boolean isValid(ReadGraph graph, Variable variable) {
562 if(variable == null) return false;
564 variable.getURI(graph);
565 } catch (DatabaseException e) {
571 public static Variable possibleChildWithType(ReadGraph graph, Variable variable, Resource targetType) throws DatabaseException {
572 Variable found = null;
573 for(Variable child : variable.getChildren(graph)) {
574 Resource type = child.getPossiblePropertyValue(graph, Variables.TYPE);
575 if(type != null && graph.isInheritedFrom(type, targetType)) {
576 if(found != null) return null;
583 public static Collection<Variable> childrenWithType(ReadGraph graph, Variable variable, Resource targetType) throws DatabaseException {
584 ArrayList<Variable> result = new ArrayList<Variable>();
585 for(Variable child : variable.getChildren(graph)) {
586 Resource type = child.getPossiblePropertyValue(graph, Variables.TYPE);
587 if(graph.isInheritedFrom(type, targetType)) result.add(child);
592 public static <T> T adapt(ReadGraph graph, Variable variable, String property, Class<T> clazz) throws DatabaseException {
593 Resource resource = variable.getPropertyValue(graph, property);
594 return graph.adapt(resource, clazz);
597 public static <T> T getPossiblePropertyValue(RequestProcessor processor, Variable variable, Resource property, Binding binding) {
599 return processor.sync(new TernaryRead<Variable,Resource,Binding,T>(variable, property, binding) {
602 public T perform(ReadGraph graph) throws DatabaseException {
603 return parameter.getPossiblePropertyValue(graph, parameter2, parameter3);
607 } catch (DatabaseException e) {
612 public static Variable possibleActiveVariable(ReadGraph graph, Variable variable) throws DatabaseException {
613 Variable activeVariable = graph.sync(new PossibleActiveVariableFromVariable(variable));
614 return activeVariable;
617 public static Variant requestNodeValue(ReadGraph graph, VariableNode<?> node) throws DatabaseException {
618 return requestNodeValue(graph, node, null);
621 public static Variant requestNodeValue(ReadGraph graph, VariableNode<?> node, final Binding binding) throws DatabaseException {
622 Variant value = graph.syncRequest(new NodeValueRequest(node, binding));
623 if(PENDING_NODE_VALUE == value && graph.getSynchronous()) {
624 // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
626 ValueGetter getter = new ValueGetter(node, binding);
628 node.support.manager.getRealm().syncExec(getter);
629 } catch (InterruptedException e) {
630 Logger.defaultLogError(e);
633 if (getter.exception != null)
634 throw new MissingVariableValueException("No value for node " + node, getter.exception);
636 return getter.result;
641 public static class NodeStructure {
642 // Immutable but wrapped with Collections.unmodifiableMap as an optimization
643 public final Map<String,Object> children;
644 // Immutable but not wrapped with Collections.unmodifiableMap as an optimization
645 public final Map<String,Object> properties;
646 private final int hash;
648 public NodeStructure(Map<String, Object> children, Map<String, Object> properties) {
649 this.children = children;
650 this.properties = properties;
651 this.hash = calcHash();
654 private int calcHash() {
655 return 31*children.hashCode() + 41*properties.hashCode();
659 public int hashCode() {
664 public boolean equals(Object object) {
667 else if (object == null || object == Variables.PENDING_NODE_STRUCTURE)
669 else if (!(object instanceof NodeStructure))
671 NodeStructure r = (NodeStructure)object;
672 return r.children.equals(children) && r.properties.equals(properties);
676 public static NodeStructure requestNodeStructure(ReadGraph graph, VariableNode<?> node) throws DatabaseException {
677 NodeStructure value = graph.syncRequest(new NodeStructureRequest(node));
679 throw new InvalidVariableException("External data access error " + String.valueOf(node));
680 if(PENDING_NODE_STRUCTURE == value && graph.getSynchronous()) {
681 // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
683 StructureGetter getter = new StructureGetter(node);
685 node.support.manager.getRealm().syncExec(getter);
686 } catch (InterruptedException e) {
687 Logger.defaultLogError(e);
688 throw new InvalidVariableException("External data access error " + String.valueOf(node), e);
691 if (getter.exception != null)
692 throw new InvalidVariableException("External data access error " + String.valueOf(node), getter.exception);
693 if (getter.result == null)
694 throw new InvalidVariableException("External data access error " + String.valueOf(node));
696 return getter.result;
703 public static String getPossibleUnit(ReadGraph graph, Variable variable) throws DatabaseException {
707 Resource predicate = variable.getPossiblePredicateResource(graph);
708 if(predicate != null) {
709 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
710 if(info.definedUnit != null) return info.definedUnit;
713 Variant variant = variable.getVariantValue(graph);
714 Binding binding = variant.getBinding();
715 if(binding == null) return null;
716 Datatype dt = binding.type();
717 if(!(dt instanceof NumberType)) return null;
718 NumberType nt = (NumberType)dt;
721 } catch (DatabaseException e) {
730 * @param context1 primary context to use for resolving the specified RVI, must not be null
731 * @param context2 secondary context to use for resolving the specified RVI, may be <code>null</code>
732 * @return pair of variables where first is the resolved variable and second
733 * is the context it was resolved with
734 * @throws DatabaseException
737 public static Pair<Variable, Variable> resolvePossible(ReadGraph graph, RVI rvi, Variable context1, Variable context2) throws DatabaseException {
738 Variable v = rvi.resolvePossible(graph, context1);
740 return new Pair<>(v, context1);
741 if (context2 != null) {
742 v = rvi.resolvePossible(graph, context2);
744 return new Pair<>(v, context2);
750 @SuppressWarnings("rawtypes")
751 private static class ValueGetter implements VariableNodeReadRunnable {
753 final VariableNode n;
754 final Binding binding;
758 public ValueGetter(VariableNode n, Binding binding) {
760 this.binding = binding;
763 @SuppressWarnings("unchecked")
768 result = new Variant(binding, n.support.manager.getValue(n.node, binding));
770 result = n.support.manager.getValue(n.node);
771 } catch (Exception e) {
772 Logger.defaultLogError(e);
779 @SuppressWarnings("rawtypes")
780 private static class StructureGetter implements VariableNodeReadRunnable {
782 final VariableNode n;
783 NodeStructure result;
786 public StructureGetter(VariableNode n) {
793 result = NodeStructureRequest.get(n);
794 } catch (NodeManagerException e) {
795 Logger.defaultLogError(e);
802 public static Variable tryGetProperty(ReadGraph graph, Resource entity, Resource property) throws DatabaseException {
803 Variable v = Variables.getPossibleVariable(graph, entity);
804 return v != null ? v.getPossibleProperty(graph, property) : null;
807 public static ValueAccessor createValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
808 Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
809 Function1<Variable, Datatype> getDatatype) {
810 return new SCLValueAccessor(getValue1, getValue2, setValue2, setValue3, getDatatype);
813 public static void setRVIProperty(WriteGraph graph, Variable variable, RVI rvi) throws DatabaseException {
814 Layer0 L0 = Layer0.getInstance(graph);
815 Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
816 Resource predicate = variable.getPredicateResource(graph);
817 Resource subject = variable.getParent(graph).getRepresents(graph);
818 graph.deny(subject, predicate);
819 graph.claimLiteral(subject, predicate, L0.RVI, rvi, rviBinding);