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.MissingVariableException;
36 import org.simantics.db.layer0.request.Model;
37 import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable;
38 import org.simantics.db.layer0.request.PossibleVariableIndexRoot;
39 import org.simantics.db.layer0.request.PossibleVariableModel;
40 import org.simantics.db.layer0.request.PropertyInfo;
41 import org.simantics.db.layer0.request.PropertyInfoRequest;
42 import org.simantics.db.layer0.request.ResourceURIToVariable;
43 import org.simantics.db.layer0.request.VariableIndexRoot;
44 import org.simantics.db.layer0.request.VariableURI;
45 import org.simantics.layer0.Layer0;
46 import org.simantics.operation.Layer0X;
47 import org.simantics.project.ontology.ProjectResource;
48 import org.simantics.scl.runtime.function.Function1;
49 import org.simantics.scl.runtime.function.Function2;
50 import org.simantics.scl.runtime.function.Function3;
51 import org.simantics.simulation.ontology.SimulationResource;
52 import org.simantics.simulator.variable.exceptions.NodeManagerException;
53 import org.simantics.utils.datastructures.Pair;
55 import gnu.trove.map.hash.TObjectIntHashMap;
57 final public class Variables {
59 public static final Variant PENDING_NODE_VALUE = new Variant();
61 public static final NodeStructure PENDING_NODE_STRUCTURE = new NodeStructure(Collections.emptyMap(), Collections.emptyMap()) {
62 public boolean equals(Object object) {
63 return this == object;
67 public static enum Role {
69 CHILD("/"), PROPERTY("#");
71 transient final String identifier;
73 private Role(String identifier) {
74 this.identifier = identifier;
77 final public String getIdentifier() {
81 public static Role getRole( String identifier ) {
82 for (Role role : Role.values()) if (role.identifier.equals( identifier )) return role;
89 //final public static String PREDICATE = "PREDICATE";
93 final public static String NAME = "HasName";
94 final public static String CLASSIFICATIONS = "classifications";
95 final public static String EXPRESSION = "HasExpression";
96 final public static String INPUT_VALIDATOR = "HasInputValidator";
97 final public static String INPUT_MODIFIER = "HasInputModifier";
98 final public static String FORMATTER = "HasFormatter";
100 final public static String STANDARD_RESOURCE = "hasStandardResource";
103 //final public static String REPRESENTS = "Represents";
104 final public static String TYPE = "Type";
105 final public static String URI = "URI";
107 * @deprecated use {@link Variable#getRVI(ReadGraph)} and {@link RVI} instead.
110 // final public static String SERIALISED = "Serialised";
113 //final public static String PARENT = "Parent";
114 //final public static String ROLE = "Role";
115 //final public static String DATATYPE = "DATATYPE";
116 //final public static String UNIT = "UNIT";
118 final public static String VALID = "valid";
119 final public static String REQUIRED = "required";
120 final public static String DEFAULT = "default";
121 final public static String READONLY = "readOnly";
122 final public static String VALIDATOR = "validator";
124 final public static String LABEL = "HasLabel";
126 final public static String ENUMERATION_VALUES = "HasEnumerationValues";
127 final public static String CUSTOM_MODIFIER = "HasCustomModifier";
129 final public static String DISPLAY_COLUMN = "HasDisplayColumn";
131 final public static String DISPLAY_PROPERTY = "HasDisplayProperty";
132 final public static String DISPLAY_VALUE = "HasDisplayValue";
133 final public static String DISPLAY_UNIT = "HasDisplayUnit";
135 final public static String CONVERTED_VALUE = "convertedValue";
138 * This property should exist for array valued property variables.
140 final public static String ARRAY_SIZE = "ARRAY_SIZE";
144 // use etc. variable.adapt(graph, Interface.class).getResource()
145 final public static String RESOURCE = "Resource";
147 final public static String CONTAINER_RESOURCE = "ContainerResource";
149 final public static String PROPERTY_RESOURCE = "PROPERTY_RESOURCE";
152 public final static String[] builtins = {
157 public static Variable getPossibleVariable(ReadGraph graph, Resource resource) throws DatabaseException {
158 String uri = graph.getPossibleURI(resource);
159 return uri != null ? getPossibleVariable(graph, uri) : null;
162 public static Variable getPossibleVariable(ReadGraph graph, String uri) throws DatabaseException {
164 return getVariable(graph, uri);
165 } catch (DatabaseException e) {
170 public static Variable getVariable(ReadGraph graph, Resource resource) throws DatabaseException {
171 return getVariable(graph, graph.getURI(resource));
174 public static Variable getVariable(ReadGraph graph, String uri) throws DatabaseException {
176 return graph.sync(new ResourceURIToVariable(uri));
177 } catch (MissingVariableException e) {
178 return VariableRepository.get(graph, uri);
182 private static int commonPrefixLength(String a, String b) {
183 int maxC = Math.min(a.length(), b.length());
184 for(int c=0;c<maxC;++c) {
185 if(a.charAt(c) != b.charAt(c))
191 private static boolean isSplitPos(String str, int p) {
194 char c = str.charAt(p);
195 return c=='/' || c=='#';
198 private static int pathLength(String path) {
200 for(int i=0;i<path.length();++i) {
201 char c = path.charAt(i);
208 private static String prefixByParentPath(int parentPathLength, String suffix) {
209 StringBuilder b = new StringBuilder();
210 for(int i=0;i<parentPathLength;++i)
216 public static String getRVI(ReadGraph graph, Variable base, Variable other) throws DatabaseException {
217 String baseURI = graph.syncRequest(new VariableURI(base));
218 String otherURI = graph.syncRequest(new VariableURI(other));
219 return getRelativeRVI(baseURI, otherURI);
222 public static String getRelativeRVI(String baseURI, String otherURI) {
223 int prefixLength = commonPrefixLength(baseURI, otherURI);
224 if(!isSplitPos(baseURI, prefixLength) || !isSplitPos(otherURI, prefixLength)) {
225 for(--prefixLength;prefixLength > 0 && !isSplitPos(baseURI, prefixLength);--prefixLength);
227 if(prefixLength == baseURI.length())
228 return otherURI.substring(prefixLength);
230 return prefixByParentPath(
231 pathLength(baseURI.substring(prefixLength)),
232 otherURI.substring(prefixLength));
235 public static String getRVI2(ReadGraph graph, Variable base, Variable other) throws DatabaseException {
236 TObjectIntHashMap<Variable> baseLength = new TObjectIntHashMap<Variable>();
237 for(int depth=0;base != null;base = base.getParent(graph),++depth)
238 baseLength.put(base, depth);
240 for(cur=other;!baseLength.containsKey(cur);cur=cur.getParent(graph));
243 String curURI = cur.getURI(graph);
244 String otherURI = other.getURI(graph);
246 return prefixByParentPath(baseLength.get(cur), otherURI.substring(curURI.length()));
249 public static String getProjectRVI(ReadGraph graph, Variable variable) throws DatabaseException {
250 Resource project = getProject(graph, variable);
251 String projectURI = graph.getURI(project);
252 return variable.getURI(graph).substring(projectURI.length());
255 private static int getSegmentEnd(String suffix) {
257 for(pos=1;pos<suffix.length();++pos) {
258 char c = suffix.charAt(pos);
259 if(c == '/' || c == '#')
265 public static String getRVI(String rvi, String suffix) throws DatabaseException {
266 if(suffix.isEmpty()) return rvi;
267 switch(suffix.charAt(0)) {
269 return getRVI(URIStringUtils.getRVIParent(rvi), suffix.substring(1));
272 int segmentEnd = getSegmentEnd(suffix);
273 return getRVI(rvi + "#" + suffix.substring(1, segmentEnd), suffix.substring(segmentEnd));
276 int segmentEnd = getSegmentEnd(suffix);
277 return getRVI(rvi + "/" + suffix.substring(1, segmentEnd), suffix.substring(segmentEnd));
285 public static Variable getRootVariable(ReadGraph graph) throws DatabaseException {
286 return graph.adapt(graph.getRootLibrary(), Variable.class);
289 public static Resource getPossibleIndexRoot(ReadGraph graph, Variable variable) throws DatabaseException {
290 return graph.syncRequest(new PossibleVariableIndexRoot(variable));
293 public static Resource getIndexRoot(ReadGraph graph, Variable variable) throws DatabaseException {
294 return graph.syncRequest(new VariableIndexRoot(variable));
297 public static Resource getModel(ReadGraph graph, Variable variable) throws DatabaseException {
298 String URI = variable.getURI(graph);
299 return VariablesImpl.getFirst(graph, SimulationResource.getInstance(graph).Model, URI, 8);
302 public static Resource getPossibleModel(ReadGraph graph, Variable variable) throws DatabaseException {
303 return graph.syncRequest(new PossibleVariableModel(variable));
306 public static Resource getProject(ReadGraph graph, Variable variable) throws DatabaseException {
307 String URI = variable.getURI(graph);
308 return VariablesImpl.getFirst(graph, ProjectResource.getInstance(graph).Project, URI, 8);
311 public static Variable getConfigurationContext(ReadGraph graph, Resource resource) throws DatabaseException {
312 SimulationResource SIMU = SimulationResource.getInstance(graph);
313 if(!graph.isInstanceOf(resource, SIMU.Model)) resource = graph.sync(new Model(resource));
314 Resource configurationResource = graph.getSingleObject(resource, SIMU.HasConfiguration);
315 return Variables.getVariable(graph, configurationResource);
318 public static Resource getConfigurationContextResource(ReadGraph graph, Resource resource) throws DatabaseException {
319 Variable config = getConfigurationContext(graph, resource);
320 return config.getRepresents(graph);
323 public static Variable getConfigurationContext(ReadGraph graph, Variable variable) throws DatabaseException {
324 SimulationResource SIMU = SimulationResource.getInstance(graph);
325 Resource model = Variables.getModel(graph, variable);
326 Resource configurationResource = graph.getSingleObject(model, SIMU.HasConfiguration);
327 return Variables.getVariable(graph, configurationResource);
330 public static Resource getPossibleConfigurationContextResource(ReadGraph graph, Resource resource) throws DatabaseException {
331 Variable config = getPossibleConfigurationContext(graph, resource);
332 return config != null ? config.getPossibleRepresents(graph) : null;
335 public static Variable getPossibleConfigurationContext(ReadGraph graph, Resource resource) throws DatabaseException {
336 SimulationResource SIMU = SimulationResource.getInstance(graph);
337 if (!graph.isInstanceOf(resource, SIMU.Model)) resource = graph.sync(new PossibleIndexRoot(resource));
338 if (resource == null)
340 Resource configurationResource = graph.getPossibleObject(resource, SIMU.HasConfiguration);
341 if (configurationResource == null)
343 return Variables.getPossibleVariable(graph, configurationResource);
346 public static Variable getPossibleConfigurationContext(ReadGraph graph, Variable variable) throws DatabaseException {
347 SimulationResource SIMU = SimulationResource.getInstance(graph);
348 Resource model = getPossibleIndexRoot(graph, variable);
351 Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);
352 if (configurationResource == null)
354 return Variables.getPossibleVariable(graph, configurationResource);
357 public static Variable getConfigurationVariable(ReadGraph graph, Resource resource, String RVI) throws DatabaseException {
358 Variable context = getConfigurationContext(graph, resource);
359 return context.browse(graph, RVI);
362 public static Variable getConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
363 Variable context = getConfigurationContext(graph, variable);
364 RVI rvi = variable.getRVI(graph);
365 return rvi.resolve(graph, context);
368 public static Variable getPossibleConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
369 Variable context = getPossibleConfigurationContext(graph, variable);
373 RVI rvi = variable.getRVI(graph);
374 return rvi.resolvePossible(graph, context);
375 } catch (MissingVariableException e) {
381 public static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException {
382 Variable var = rvi.resolve(graph, getConfigurationContext(graph, resource));
383 return var.getDatatype(graph);
387 public static Resource getRealization(ReadGraph graph, Variable variable) throws DatabaseException {
388 String URI = variable.getURI(graph);
389 return VariablesImpl.getFirst(graph, Layer0X.getInstance(graph).Realization, URI, 8);
392 public static boolean isContext(ReadGraph graph, Variable variable) throws DatabaseException {
393 Resource type = variable.getPossibleType(graph);
395 if(graph.isInheritedFrom(type, Layer0.getInstance(graph).RVIContext)) return true;
400 public static Variable getPossibleContext(ReadGraph graph, Variable variable) throws DatabaseException {
401 if(isContext(graph, variable)) return variable;
402 Variable parent = variable.getParent(graph);
403 if(parent == null) return null;
404 return getPossibleContext(graph, parent);
407 public static Variable getContext(ReadGraph graph, Variable variable) throws DatabaseException {
408 Variable context = getPossibleContext(graph, variable);
409 if(context == null) throw new DatabaseException("No context found for " + variable.getURI(graph));
413 public static RVI getRVI2(ReadGraph graph, Variable variable) throws DatabaseException {
414 return variable.getRVI(graph);
417 public static RVI getPossibleRVI2(ReadGraph graph, Variable variable) throws DatabaseException {
419 return variable.getRVI(graph);
420 } catch (DatabaseException e) {
425 public static String getRVI(ReadGraph graph, Variable variable) throws DatabaseException {
426 Resource realizationResource = getRealization(graph, variable);
427 if (realizationResource == null)
428 throw new DatabaseException("No realization found for " + variable.getURI(graph));
429 return variable.getURI(graph).substring(graph.getURI(realizationResource).length());
432 public static String getRVI(ReadGraph graph, Resource config) throws DatabaseException {
433 Variable var = getVariable(graph, config);
434 return getRVI(graph, var);
437 public static String getPossibleRVI(ReadGraph graph, Resource config) throws DatabaseException {
438 Variable var = getPossibleVariable(graph, config);
439 return var != null ? getPossibleRVI(graph, var) : null;
442 public static String getPossibleRVI(ReadGraph graph, Variable variable) throws DatabaseException {
444 return getRVI(graph, variable);
445 } catch (DatabaseException e) {
450 public static List<Variable> getPath(ReadGraph graph, Variable base, Variable var) throws DatabaseException {
451 if(!isChild(graph, base, var)) return null;
452 LinkedList<Variable> result = new LinkedList<Variable>();
453 var = var.getParent(graph);
454 while(!var.equals(base)) {
455 result.addFirst(var);
456 var = var.getParent(graph);
461 public static Variable getChild(ReadGraph graph, Variable base, Variable var) throws DatabaseException {
462 List<Variable> path = getPath(graph, base, var);
463 if(path == null || path.size() == 0) return null;
467 public static boolean isChild(ReadGraph graph, Variable base, Variable var) throws DatabaseException {
468 if(base.equals(var)) return false;
469 return var.getURI(graph).startsWith(base.getURI(graph));
472 public static Variable switchRealization(ReadGraph graph, Variable variable, Resource realization) throws DatabaseException {
473 Resource current = getRealization(graph, variable);
474 if(current == null) throw new DatabaseException("No current realization found for variable");
475 return switchRealization(graph, variable, current, realization);
478 public static Variable switchPossibleContext(ReadGraph graph, Variable variable, Resource realization) throws DatabaseException {
479 Variable current = getPossibleContext(graph, variable);
482 Resource currentContext = current.getPossibleRepresents(graph);
483 if (currentContext == null)
485 return switchPossibleRealization(graph, variable, currentContext, realization);
488 public static Variable switchRealization(ReadGraph graph, Variable variable, Variable realization) throws DatabaseException {
489 Resource current = getRealization(graph, variable);
490 return switchRealization(graph, variable, current, realization);
493 public static Variable switchRealization(ReadGraph graph, Variable variable, Resource currentRealization, Resource targetRealization) throws DatabaseException {
494 String currentURI = graph.getURI(currentRealization);
495 String targetURI = graph.getURI(targetRealization);
496 String variableURI = variable.getURI(graph);
497 String targetVariableURI = targetURI + variableURI.substring(currentURI.length());
498 return getVariable(graph, targetVariableURI);
501 public static Variable switchRealization(ReadGraph graph, Variable variable, Resource currentRealization, Variable targetRealization) throws DatabaseException {
502 String currentURI = graph.getURI(currentRealization);
503 String targetURI = targetRealization.getURI(graph);
504 String variableURI = variable.getURI(graph);
505 String targetVariableURI = targetURI + variableURI.substring(currentURI.length());
506 return getVariable(graph, targetVariableURI);
509 public static Variable switchPossibleRealization(ReadGraph graph, Variable variable, Resource currentRealization, Resource targetRealization) throws DatabaseException {
510 String currentURI = graph.getURI(currentRealization);
511 String targetURI = graph.getURI(targetRealization);
512 String variableURI = variable.getURI(graph);
513 String targetVariableURI = targetURI + variableURI.substring(currentURI.length());
514 return getPossibleVariable(graph, targetVariableURI);
517 public static Variable toConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
518 Variable config = getConfigurationContext(graph, variable);
519 return switchRealization(graph, variable, config);
522 public static Variable toPossibleConfigurationVariable(ReadGraph graph, Variable variable) throws DatabaseException {
524 Resource represents = variable.getPossibleRepresents(graph);
525 if(represents == null) return null;
526 Resource config = getPossibleConfigurationContextResource(graph, represents);
527 if(config == null) return null;
528 return switchPossibleContext(graph, variable, config);
532 public static String toRVI(ReadGraph graph, List<Resource> compositePath) throws DatabaseException {
533 Layer0 L0 = Layer0.getInstance(graph);
534 StringBuilder rvi = new StringBuilder();
535 for (Resource composite : compositePath) {
536 String name = graph.getPossibleRelatedValue(composite, L0.HasName);
541 String escapedName = URIStringUtils.escape(name);
542 rvi.append(escapedName);
544 return rvi.toString();
547 public static String appendRVI(ReadGraph graph, String modelURI, String rvi, Resource configuration) throws DatabaseException {
549 Layer0 L0 = Layer0.getInstance(graph);
550 String partName = graph.getPossibleRelatedValue(configuration, L0.HasName);
551 if(partName == null) throw new MissingVariableException("Can not append a child corresponding to " + configuration + " to rvi '" + rvi + "' since there is no name.");
552 String escaped = URIStringUtils.escape(partName);
553 return rvi + "/" + escaped;
557 public static boolean isValid(ReadGraph graph, Variable variable) {
558 if(variable == null) return false;
560 variable.getURI(graph);
561 } catch (DatabaseException e) {
567 public static Variable possibleChildWithType(ReadGraph graph, Variable variable, Resource targetType) throws DatabaseException {
568 Variable found = null;
569 for(Variable child : variable.getChildren(graph)) {
570 Resource type = child.getPossiblePropertyValue(graph, Variables.TYPE);
571 if(type != null && graph.isInheritedFrom(type, targetType)) {
572 if(found != null) return null;
579 public static Collection<Variable> childrenWithType(ReadGraph graph, Variable variable, Resource targetType) throws DatabaseException {
580 ArrayList<Variable> result = new ArrayList<Variable>();
581 for(Variable child : variable.getChildren(graph)) {
582 Resource type = child.getPossiblePropertyValue(graph, Variables.TYPE);
583 if(graph.isInheritedFrom(type, targetType)) result.add(child);
588 public static <T> T adapt(ReadGraph graph, Variable variable, String property, Class<T> clazz) throws DatabaseException {
589 Resource resource = variable.getPropertyValue(graph, property);
590 return graph.adapt(resource, clazz);
593 public static <T> T getPossiblePropertyValue(RequestProcessor processor, Variable variable, Resource property, Binding binding) {
595 return processor.sync(new TernaryRead<Variable,Resource,Binding,T>(variable, property, binding) {
598 public T perform(ReadGraph graph) throws DatabaseException {
599 return parameter.getPossiblePropertyValue(graph, parameter2, parameter3);
603 } catch (DatabaseException e) {
608 public static Variable possibleActiveVariable(ReadGraph graph, Variable variable) throws DatabaseException {
609 Variable activeVariable = graph.sync(new PossibleActiveVariableFromVariable(variable));
610 return activeVariable;
613 public static Variant requestNodeValue(ReadGraph graph, VariableNode<?> node) throws DatabaseException {
614 return requestNodeValue(graph, node, null);
617 public static Variant requestNodeValue(ReadGraph graph, VariableNode<?> node, final Binding binding) throws DatabaseException {
618 Variant value = graph.syncRequest(new NodeValueRequest(node, binding));
619 if(PENDING_NODE_VALUE == value && graph.getSynchronous()) {
620 // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
622 ValueGetter getter = new ValueGetter(node, binding);
624 node.support.manager.getRealm().syncExec(getter);
625 } catch (InterruptedException e) {
626 Logger.defaultLogError(e);
629 if (getter.exception != null)
630 throw new DatabaseException(getter.exception);
632 return getter.result;
637 public static class NodeStructure {
638 // Immutable but wrapped with Collections.unmodifiableMap as an optimization
639 public final Map<String,Object> children;
640 // Immutable but not wrapped with Collections.unmodifiableMap as an optimization
641 public final Map<String,Object> properties;
642 private final int hash;
644 public NodeStructure(Map<String, Object> children, Map<String, Object> properties) {
645 this.children = children;
646 this.properties = properties;
647 this.hash = calcHash();
650 private int calcHash() {
651 return 31*children.hashCode() + 41*properties.hashCode();
655 public int hashCode() {
660 public boolean equals(Object object) {
663 else if (object == null || object == Variables.PENDING_NODE_STRUCTURE)
665 else if (!(object instanceof NodeStructure))
667 NodeStructure r = (NodeStructure)object;
668 return r.children.equals(children) && r.properties.equals(properties);
672 public static NodeStructure requestNodeStructure(ReadGraph graph, VariableNode<?> node) throws DatabaseException {
673 NodeStructure value = graph.syncRequest(new NodeStructureRequest(node));
674 if (value == null) throw new DatabaseException("External data access error");
675 if(PENDING_NODE_STRUCTURE == value && graph.getSynchronous()) {
676 // In this case a PENDING value was previously cached but now the value needs to be obtained for real.
678 StructureGetter getter = new StructureGetter(node);
680 node.support.manager.getRealm().syncExec(getter);
681 } catch (InterruptedException e) {
682 Logger.defaultLogError(e);
683 throw new DatabaseException("External data access error", e);
686 if (getter.exception != null)
687 throw new DatabaseException("External data access error", getter.exception);
688 if (getter.result == null)
689 throw new DatabaseException("External data access error");
691 return getter.result;
698 public static String getPossibleUnit(ReadGraph graph, Variable variable) throws DatabaseException {
702 Resource predicate = variable.getPossiblePredicateResource(graph);
703 if(predicate != null) {
704 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
705 if(info.definedUnit != null) return info.definedUnit;
708 Variant variant = variable.getVariantValue(graph);
709 Binding binding = variant.getBinding();
710 if(binding == null) return null;
711 Datatype dt = binding.type();
712 if(!(dt instanceof NumberType)) return null;
713 NumberType nt = (NumberType)dt;
716 } catch (DatabaseException e) {
725 * @param context1 primary context to use for resolving the specified RVI, must not be null
726 * @param context2 secondary context to use for resolving the specified RVI, may be <code>null</code>
727 * @return pair of variables where first is the resolved variable and second
728 * is the context it was resolved with
729 * @throws DatabaseException
732 public static Pair<Variable, Variable> resolvePossible(ReadGraph graph, RVI rvi, Variable context1, Variable context2) throws DatabaseException {
733 Variable v = rvi.resolvePossible(graph, context1);
735 return new Pair<>(v, context1);
736 if (context2 != null) {
737 v = rvi.resolvePossible(graph, context2);
739 return new Pair<>(v, context2);
745 @SuppressWarnings("rawtypes")
746 private static class ValueGetter implements VariableNodeReadRunnable {
748 final VariableNode n;
749 final Binding binding;
753 public ValueGetter(VariableNode n, Binding binding) {
755 this.binding = binding;
758 @SuppressWarnings("unchecked")
763 result = new Variant(binding, n.support.manager.getValue(n.node, binding));
765 result = n.support.manager.getValue(n.node);
766 } catch (Exception e) {
767 Logger.defaultLogError(e);
774 @SuppressWarnings("rawtypes")
775 private static class StructureGetter implements VariableNodeReadRunnable {
777 final VariableNode n;
778 NodeStructure result;
781 public StructureGetter(VariableNode n) {
788 result = NodeStructureRequest.get(n);
789 } catch (NodeManagerException e) {
790 Logger.defaultLogError(e);
797 public static Variable tryGetProperty(ReadGraph graph, Resource entity, Resource property) throws DatabaseException {
798 Variable v = Variables.getPossibleVariable(graph, entity);
799 return v != null ? v.getPossibleProperty(graph, property) : null;
802 public static ValueAccessor createValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
803 Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
804 Function1<Variable, Datatype> getDatatype) {
805 return new SCLValueAccessor(getValue1, getValue2, setValue2, setValue3, getDatatype);
808 public static void setRVIProperty(WriteGraph graph, Variable variable, RVI rvi) throws DatabaseException {
809 Layer0 L0 = Layer0.getInstance(graph);
810 Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
811 Resource predicate = variable.getPredicateResource(graph);
812 Resource subject = variable.getParent(graph).getRepresents(graph);
813 graph.deny(subject, predicate);
814 graph.claimLiteral(subject, predicate, L0.RVI, rvi, rviBinding);