1 package org.simantics.db.layer0.variable;
3 import gnu.trove.map.hash.THashMap;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashMap;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.Databoard;
13 import org.simantics.databoard.binding.Binding;
14 import org.simantics.databoard.binding.StringBinding;
15 import org.simantics.databoard.binding.error.BindingConstructionException;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.binding.mutable.Variant;
18 import org.simantics.databoard.type.Datatype;
19 import org.simantics.databoard.util.URIStringUtils;
20 import org.simantics.datatypes.literal.GUID;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
25 import org.simantics.db.common.request.PossibleIndexRoot;
26 import org.simantics.db.common.request.PropertyMapOfResource;
27 import org.simantics.db.common.utils.NameUtils;
28 import org.simantics.db.exception.AdaptionException;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.exception.InvalidVariableException;
31 import org.simantics.db.layer0.exception.MissingVariableException;
32 import org.simantics.db.layer0.exception.MissingVariableValueException;
33 import org.simantics.db.layer0.request.PossibleResource;
34 import org.simantics.db.layer0.request.PropertyInfo;
35 import org.simantics.db.layer0.request.VariableRVIRequest;
36 import org.simantics.db.layer0.variable.RVI.GuidRVIPart;
37 import org.simantics.db.layer0.variable.RVI.RVIPart;
38 import org.simantics.db.layer0.variable.RVI.ResourceRVIPart;
39 import org.simantics.db.layer0.variable.RVI.StringRVIPart;
40 import org.simantics.db.layer0.variable.Variables.Role;
41 import org.simantics.layer0.Layer0;
44 * Abstract implementation of Variable -interface.
46 * @author Hannu Niemistö
48 public abstract class AbstractVariable implements Variable {
50 @SuppressWarnings("rawtypes")
51 final public VariableNode node;
53 public AbstractVariable(@SuppressWarnings("rawtypes") VariableNode node) {
58 * Returns a variable that is not one of the standard properties listed
59 * in <a href="https://www.simantics.org/wiki/index.php/Org.simantics.db.layer0.variable.Variable#Standard_required_properties">specification</a>.
61 protected abstract Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException;
62 public abstract Variable getPossibleExtraProperty(ReadGraph graph, String name) throws DatabaseException;
63 public abstract Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException;
65 public abstract void collectExtraProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException;
66 public abstract Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> map) throws DatabaseException;
67 public abstract Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException;
69 public abstract <T> T getValue(ReadGraph graph) throws DatabaseException;
70 public abstract <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException;
72 public abstract void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException;
73 public abstract String getName(ReadGraph graph) throws DatabaseException;
74 //public abstract Object getSerialized(ReadGraph graph) throws DatabaseException;
75 public abstract Variable getParent(ReadGraph graph) throws DatabaseException;
76 public abstract Role getRole(ReadGraph graph) throws DatabaseException;
77 public abstract Resource getRepresents(ReadGraph graph) throws DatabaseException;
79 public abstract Set<String> getClassifications(ReadGraph graph) throws DatabaseException;
82 public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {
83 throw new DatabaseException("PropertyInfo is not available");
87 public Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
88 Resource represents = getPossibleRepresents(graph);
89 if(represents != null) return graph.syncRequest(new PossibleIndexRoot(represents));
90 Variable parent = getParent(graph);
91 if(parent == null) return null;
92 return parent.getIndexRoot(graph);
95 public void validate(ReadGraph graph) throws DatabaseException {
98 public String getIdentifier() {
99 return getClass().getSimpleName();
104 public Role getPossibleRole(ReadGraph graph) throws DatabaseException {
106 return getRole(graph);
107 } catch (DatabaseException e) {
112 protected Variable resolveChild(ReadGraph graph, Resource resource) throws DatabaseException {
113 String rName = graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName, Bindings.STRING);
114 for(Variable child : browseChildren(graph)) {
115 String name = child.getPossiblePropertyValue(graph, Variables.NAME, Bindings.STRING);
116 if(rName.equals(name)) return child;
118 throw new DatabaseException("Could not resolve child " + resource);
121 protected Variable resolveChild(ReadGraph graph, GuidRVIPart part) throws DatabaseException {
122 return StandardRVIResolver.resolveChildDefault(graph, this, part);
125 protected Variable resolveProperty(ReadGraph graph, Resource resource) throws DatabaseException {
126 String rName = graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName, Bindings.STRING);
127 for(Variable child : browseProperties(graph)) {
128 String name = child.getPossiblePropertyValue(graph, Variables.NAME, Bindings.STRING);
129 if(rName.equals(name)) return child;
131 throw new DatabaseException("Could not resolve child " + resource);
134 protected Variable resolveProperty(ReadGraph graph, GuidRVIPart part) throws DatabaseException {
135 return StandardRVIResolver.resolvePropertyDefault(graph, this, part);
138 protected Variable resolvePossibleChild(ReadGraph graph, Resource resource) throws DatabaseException {
139 String rName = graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName, Bindings.STRING);
140 for(Variable child : browseChildren(graph)) {
141 String name = child.getPossiblePropertyValue(graph, Variables.NAME, Bindings.STRING);
142 if(rName.equals(name)) return child;
147 protected Variable resolvePossibleChild(ReadGraph graph, GuidRVIPart part) throws DatabaseException {
148 Layer0 L0 = Layer0.getInstance(graph);
149 for(Variable child : browseChildren(graph)) {
150 GUID id = child.getPossiblePropertyValue(graph, L0.identifier, GUID.BINDING);
152 if(id.mostSignificant == part.mostSignificant && id.leastSignificant == part.leastSignificant)
159 protected Variable resolvePossibleProperty(ReadGraph graph, Resource resource) throws DatabaseException {
160 String rName = graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName, Bindings.STRING);
161 for(Variable child : browseProperties(graph)) {
162 String name = child.getPossiblePropertyValue(graph, Variables.NAME, Bindings.STRING);
163 if(rName.equals(name)) return child;
168 protected Variable resolvePossibleProperty(ReadGraph graph, GuidRVIPart part) throws DatabaseException {
169 Layer0 L0 = Layer0.getInstance(graph);
170 for(Variable child : browseProperties(graph)) {
171 GUID id = child.getPossiblePropertyValue(graph, L0.identifier, GUID.BINDING);
173 if(id.mostSignificant == part.mostSignificant && id.leastSignificant == part.leastSignificant)
180 public String getLabel(ReadGraph graph) throws DatabaseException {
181 return getName(graph);
184 public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
185 Resource represents = getPossibleRepresents(graph);
186 if(represents == null) return null;
187 return graph.getPossibleRelatedValue2(represents, graph.getService(Layer0.class).HasLabel, getParent(graph), Bindings.STRING);
190 public Resource getType(ReadGraph graph) throws DatabaseException {
192 Resource resource = getPossibleRepresents(graph);
193 if(resource == null) {
194 String uri = getPossiblePropertyValue(graph, "typeURI");
195 if(uri != null) return graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri), TransientCacheAsyncListener.<Resource>instance());
196 throw new DatabaseException("No type for " + getURI(graph));
198 return graph.getSingleType(resource);
202 public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {
203 throw new UnsupportedOperationException();
207 public RVI getRVI(ReadGraph graph) throws DatabaseException {
208 Databoard databoard = graph.getService( Databoard.class );
209 Binding rviBinding = databoard.getBindingUnchecked( RVI.class );
210 if(Variables.isContext(graph, this)) {
211 return RVI.empty( rviBinding );
213 Variable parent = getParent(graph);
215 // TODO: consider using a more suitable exception here to better convey the situation.
216 throw new MissingVariableException("no parent for variable " + this + " (URI=" + getPossibleURI(graph) + ")");
217 RVI base = graph.syncRequest(new VariableRVIRequest(parent));
218 RVIPart part = getRVIPart(graph);
219 return new RVIBuilder(base).append(part).toRVI();
223 protected Variable getDomainProperty(ReadGraph graph, String name) throws DatabaseException {
224 Variable property = getPossibleDomainProperty(graph, name);
226 throw new MissingVariableException(getIdentifier() + ": Didn't find property " + name + ".");
230 protected void addProperty(Map<String, Variable> properties, String name, Object value, Binding binding) {
232 properties.put(name, new ConstantPropertyVariable(this, name, value, binding));
236 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
237 return new ConstantPropertyVariable(this, Variables.NAME, getName(graph), Bindings.STRING);
240 protected Variable getLabelVariable(ReadGraph graph) throws DatabaseException {
241 return new ConstantPropertyVariable(this, Variables.LABEL, getPossibleLabel(graph), Bindings.STRING);
244 final public Collection<Variable> browseProperties(ReadGraph graph) throws DatabaseException {
245 return getProperties(graph);
248 private Map<String, Variable> getPropertyMap(ReadGraph graph, String classification) throws DatabaseException {
249 return collectDomainProperties(graph, classification, null);
252 final static class PropertyMap extends THashMap<String,Variable> {
254 final private Variable variable;
256 PropertyMap(Variable variable) {
257 this.variable = variable;
260 private Variable getTypeVariable() {
262 return new AbstractConstantPropertyVariable(variable, Variables.TYPE, null) {
264 @SuppressWarnings("unchecked")
266 public <T> T getValue(ReadGraph graph) throws DatabaseException {
267 Resource represents = parent.getRepresents(graph);
268 if(represents == null) return null;
269 return (T)graph.getPossibleType(represents, Layer0.getInstance(graph).Entity);
273 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
274 return getValue(graph);
280 private Variable getURIVariable() {
282 return new AbstractConstantPropertyVariable(variable, Variables.URI, null) {
284 @SuppressWarnings("unchecked")
286 public <T> T getValue(ReadGraph graph) throws DatabaseException {
287 return (T)variable.getURI(graph);
291 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
292 return getValue(graph);
298 public Variable get(Object key) {
299 Variable result = super.get(key);
300 if(result != null) return result;
302 if(Variables.TYPE.equals(key)) return getTypeVariable();
303 else if(Variables.URI.equals(key)) return getURIVariable();
310 private Map<String, Variable> getPropertyMap(ReadGraph graph) throws DatabaseException {
311 PropertyMap properties = new PropertyMap(this);
312 // Map<String, Variable> properties = new HashMap<String, Variable>();
314 // properties.put(Variables.NAME, getNameVariable(graph));
315 // } catch (DatabaseException e) {
316 // // A variable that has no name doesn't exist by definition.
317 // // Therefore it can't have any properties.
318 // return Collections.emptyMap();
321 // Variable labelVariable = getLabelVariable(graph);
322 // if(labelVariable != null) properties.put(Variables.LABEL, getLabelVariable(graph));
323 // } catch (DatabaseException e) {
324 // // Label not absolutely mandatory.
326 // addProperty(properties, Variables.TYPE, getPossibleType(graph), null);
327 // addProperty(properties, Variables.URI, getPossibleURI(graph), Bindings.STRING);
328 //addProperty(properties, Variables.SERIALISED, getSerialized(graph), Bindings.STRING);
329 //addProperty(properties, Variables.PARENT, getParent(graph), null);
330 // addProperty(properties, Variables.ROLE, getRole(graph), Bindings.STRING);
331 // addProperty(properties, Variables.REPRESENTS, getPossibleRepresents(graph), null);
332 collectExtraProperties(graph, properties);
333 collectDomainProperties(graph, properties);
338 public Collection<Variable> getProperties(ReadGraph graph) throws DatabaseException {
339 return getPropertyMap(graph).values();
342 public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {
343 Map<String,Variable> propertyMap = getPropertyMap(graph, classification);
344 if(propertyMap == null) return Collections.emptyList();
345 else return propertyMap.values();
349 public Collection<Variable> getProperties(ReadGraph graph, Resource property) throws DatabaseException {
350 return getProperties(graph, uri(graph, property));
353 final public Collection<Variable> browseChildren(ReadGraph graph) throws DatabaseException {
354 return getChildren(graph);
358 public Variable getPossibleProperty(ReadGraph graph, String name)
359 throws DatabaseException {
360 if(Variables.NAME.equals(name)) {
361 return getNameVariable(graph);
363 if(Variables.LABEL.equals(name)) {
364 return getLabelVariable(graph);
366 if(Variables.TYPE.equals(name)) {
367 Object value = getPossibleType(graph);
369 return new ConstantPropertyVariable(this, name, value, null);
371 if(Variables.URI.equals(name)) {
372 // TODO: getPossibleURI or getURI?
373 Object value = getURI(graph);
375 return new ConstantPropertyVariable(this, name, value, Bindings.STRING);
377 // if(Variables.SERIALISED.equals(name)) {
378 // Object value = getSerialized(graph);
380 // return new ConstantPropertyVariable(this, name, value, Bindings.STRING);
382 /*if(Variables.PARENT.equals(name)) {
383 Object value = getParent(graph);
385 return new ConstantPropertyVariable(this, name, value, null);
387 // if(Variables.ROLE.equals(name)) {
388 // Object value = getRole(graph);
390 // return new ConstantPropertyVariable(this, name, value, null);
392 // if(Variables.REPRESENTS.equals(name)) {
393 // Object value = getRepresents(graph);
395 // return new ConstantPropertyVariable(this, name, value, null);
397 Variable extra = getPossibleExtraProperty(graph, name);
398 if(extra != null) return extra;
399 return getPossibleDomainProperty(graph, name);
403 public Variable getPossibleProperty(ReadGraph graph, Resource property) throws DatabaseException {
404 return getPossibleProperty(graph, name(graph, property));
407 @SuppressWarnings("unchecked")
408 protected <T> T checkNull(ReadGraph graph, Object value) throws DatabaseException {
410 throw new MissingVariableValueException(getClass().getSimpleName() + ": Didn't find value for " + getPossibleURI(graph));
414 private String name(ReadGraph graph, Resource property) throws DatabaseException {
415 return graph.getRelatedValue(property, Layer0.getInstance(graph).HasName, Bindings.STRING);
418 private String uri(ReadGraph graph, Resource property) throws DatabaseException {
419 return graph.getURI(property);
423 public <T> T getPropertyValue(ReadGraph graph, String name) throws DatabaseException {
424 if(Variables.LABEL.equals(name)) return checkNull(graph, getLabel(graph));
425 Variable result = getDomainProperty(graph, name);
426 if(result != null) return result.getValue(graph);
427 if(Variables.NAME.equals(name)) return checkNull(graph, getName(graph));
428 if(Variables.TYPE.equals(name)) return checkNull(graph, getPossibleType(graph));
429 if(Variables.URI.equals(name)) return checkNull(graph, getURI(graph));
430 // if(Variables.SERIALISED.equals(name)) return checkNull(graph, getSerialized(graph));
431 // if(Variables.ROLE.equals(name)) return checkNull(graph, getRole(graph));
432 // if(Variables.REPRESENTS.equals(name)) return checkNull(graph, getRepresents(graph));
433 Variable extra = getPossibleExtraProperty(graph, name);
434 if(extra != null) return extra.getValue(graph);
439 public <T> T getPropertyValue(ReadGraph graph, Resource property) throws DatabaseException {
440 return getPropertyValue(graph, name(graph, property));
443 @SuppressWarnings("unchecked")
445 public <T> T getPossiblePropertyValue(ReadGraph graph, String name)
446 throws DatabaseException {
448 Variable property = getPossibleDomainProperty(graph, name);
449 if(property != null) return property.getPossibleValue(graph);
451 if(Variables.NAME.equals(name)) return (T)getName(graph);
452 if(Variables.LABEL.equals(name)) return (T)getLabel(graph);
453 if(Variables.TYPE.equals(name)) return (T)getPossibleType(graph);
454 if(Variables.URI.equals(name)) return (T)getURI(graph);
455 // if(Variables.SERIALISED.equals(name)) return (T)getSerialized(graph);
456 // if(Variables.ROLE.equals(name)) return (T)getRole(graph);
457 // if(Variables.REPRESENTS.equals(name)) return (T)getRepresents(graph);
459 Variable extra = getPossibleExtraProperty(graph, name);
460 if(extra != null) return extra.getPossibleValue(graph);
467 public <T> T getPossiblePropertyValue(ReadGraph graph, Resource property) throws DatabaseException {
468 return getPossiblePropertyValue(graph, name(graph, property));
471 @SuppressWarnings("unchecked")
473 public <T> T getPropertyValue(ReadGraph graph, String name, Binding binding)
474 throws DatabaseException {
475 if(binding instanceof StringBinding) {
476 StringBinding sb = (StringBinding)binding;
478 if(Variables.NAME.equals(name)) return (T)sb.create((String)checkNull(graph, getName(graph)));
479 if(Variables.LABEL.equals(name)) return (T)sb.create((String)checkNull(graph, getLabel(graph)));
480 if(Variables.URI.equals(name)) return (T)sb.create((String)checkNull(graph, getURI(graph)));
481 // if(Variables.SERIALISED.equals(name)) return (T)sb.create((String)checkNull(graph, getSerialized(graph)));
482 } catch(BindingException e) {
483 throw new DatabaseException(e);
486 Variable property = getPossibleExtraProperty(graph, name);
488 return property.getValue(graph, binding);
489 property = getPossibleDomainProperty(graph, name);
491 throw new MissingVariableException("Didn't find property " + name + " for " + this + ".");
492 return property.getValue(graph, binding);
496 public <T> T getPropertyValue(ReadGraph graph, Resource property, Binding binding) throws DatabaseException {
497 return getPropertyValue(graph, name(graph, property), binding);
500 @SuppressWarnings("unchecked")
502 public <T> T getPossiblePropertyValue(ReadGraph graph, String name,
503 Binding binding) throws DatabaseException {
504 if(binding instanceof StringBinding) {
505 StringBinding sb = (StringBinding)binding;
507 if(Variables.NAME.equals(name)) return (T)sb.create((String)getName(graph));
508 if(Variables.LABEL.equals(name)) return (T)sb.create((String)getLabel(graph));
509 if(Variables.URI.equals(name)) return (T)sb.create((String)getURI(graph));
510 // if(Variables.SERIALISED.equals(name)) return (T)sb.create((String)getSerialized(graph));
511 } catch(BindingException e) {
512 throw new DatabaseException(e);
515 Variable property = getPossibleExtraProperty(graph, name);
517 return property.getPossibleValue(graph, binding);
518 property = getPossibleDomainProperty(graph, name);
521 return property.getPossibleValue(graph, binding);
525 public <T> T getPossiblePropertyValue(ReadGraph graph, Resource property, Binding binding) throws DatabaseException {
526 return getPossiblePropertyValue(graph, name(graph, property), binding);
530 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
532 setValue(graph, value, Bindings.getBinding(value.getClass()));
533 } catch (BindingConstructionException e) {
534 throw new DatabaseException(e);
539 public void setPropertyValue(WriteGraph graph, String name, Object value) throws DatabaseException {
540 getProperty(graph, name).setValue(graph, value);
544 public void setPropertyValue(WriteGraph graph, Resource property, Object value) throws DatabaseException {
545 setPropertyValue(graph, name(graph, property), value);
549 public void setPropertyValue(WriteGraph graph, String name, Object value,
550 Binding binding) throws DatabaseException {
551 getProperty(graph, name).setValue(graph, value, binding);
555 public void setPropertyValue(WriteGraph graph, Resource property, Object value, Binding binding) throws DatabaseException {
556 setPropertyValue(graph, name(graph, property), value, binding);
560 public Variable getChild(ReadGraph graph, String name)
561 throws DatabaseException {
562 Variable child = getPossibleChild(graph, name);
564 throw new MissingVariableException(getURI(graph) + ": didn't find child " + name + " for " + getIdentifier() + ".");
569 public Variable getProperty(ReadGraph graph, String name) throws DatabaseException {
570 Variable result = getPossibleProperty(graph, name);
572 throw new MissingVariableException(getClass().getSimpleName() + ": Didn't find property " + name + " for " + getPossibleURI(graph) + ".");
577 public Variable getProperty(ReadGraph graph, Resource property) throws DatabaseException {
578 return getProperty(graph, name(graph, property));
582 public Variable browse(ReadGraph graph, String suffix)
583 throws DatabaseException {
587 switch(suffix.charAt(0)) {
589 Variable parent = getParent(graph);
591 throw new MissingVariableException("Didn't find " + suffix + " for " + this + " (" + getPossibleURI(graph) + ").");
592 return parent.browse(graph, suffix.substring(1));
595 int segmentEnd = getSegmentEnd(suffix);
596 Variable property = getProperty(graph,
597 decodeString(suffix.substring(1, segmentEnd)));
599 throw new MissingVariableException("Didn't find " + suffix + " for " + this + " (" + getPossibleURI(graph) + ").");
600 return property.browse(graph, suffix.substring(segmentEnd));
603 int segmentEnd = getSegmentEnd(suffix);
604 Variable child = getChild(graph,
605 decodeString(suffix.substring(1, segmentEnd)));
607 throw new MissingVariableException("Didn't find " + suffix + " for " + this + " (" + getPossibleURI(graph) + ").");
608 return child.browse(graph, suffix.substring(segmentEnd));
611 throw new MissingVariableException("Didn't find " + suffix + " for " + this + " (" + getPossibleURI(graph) + ").");
616 private static int getSegmentEnd(String suffix) {
618 for(pos=1;pos<suffix.length();++pos) {
619 char c = suffix.charAt(pos);
620 if(c == '/' || c == '#')
627 public Variable browsePossible(ReadGraph graph, String suffix)
628 throws DatabaseException {
631 switch(suffix.charAt(0)) {
633 Variable parent = getParent(graph);
636 return parent.browsePossible(graph, suffix.substring(1));
639 int segmentEnd = getSegmentEnd(suffix);
640 Variable property = getPossibleProperty(graph,
641 decodeString(suffix.substring(1, segmentEnd)));
644 return property.browsePossible(graph, suffix.substring(segmentEnd));
647 int segmentEnd = getSegmentEnd(suffix);
648 Variable child = getPossibleChild(graph,
649 decodeString(suffix.substring(1, segmentEnd)));
652 return child.browsePossible(graph, suffix.substring(segmentEnd));
660 public Variable browse(ReadGraph graph, Resource config)
661 throws DatabaseException {
662 Variable variable = browsePossible(graph, config);
664 throw new MissingVariableException("Didn't find a variable related to " +
665 NameUtils.getSafeName(graph, config) + ".");
670 public Variable browsePossible(ReadGraph graph, Resource config)
671 throws DatabaseException {
672 Layer0 l0 = Layer0.getInstance(graph);
673 String name = (String)graph.getPossibleRelatedValue(config, l0.HasName, Bindings.STRING);
676 return getPossibleChild(graph, name);
680 public <T> T getInterface(ReadGraph graph, Class<T> clazz)
681 throws DatabaseException {
686 public String getURI(ReadGraph graph) throws DatabaseException {
688 Variable parent = getParent(graph);
690 throw new InvalidVariableException(this + " has no URI");
691 return parent.getURI(graph) + getRole(graph).getIdentifier() + encodeString(getName(graph));
695 * For debug messages.
699 * @throws DatabaseException
701 public String getPossibleURI(ReadGraph graph) throws DatabaseException {
702 Variable parent = getParent(graph);
705 if (parent instanceof AbstractVariable) {
706 String parentUri = ((AbstractVariable) parent).getPossibleURI(graph);
707 if (parentUri == null)
709 return parentUri + getRole(graph).getIdentifier() + encodeString(getName(graph));
714 public <T> T getPossibleValue(ReadGraph graph) throws DatabaseException {
716 return getValue(graph);
717 } catch(DatabaseException e) {
723 public Variant getVariantValue(ReadGraph graph) throws DatabaseException {
724 Binding binding = getPossibleDefaultBinding(graph);
725 if(binding != null) {
726 Object value = getValue(graph, binding);
727 return new Variant(binding, value);
729 // System.err.println("no data type for " + getURI(graph));
730 // TODO: hackish, consider doing something else here?
731 Object value = getValue(graph);
733 binding = Bindings.OBJECT.getContentBinding(value);
734 } catch (BindingException e) {
735 throw new DatabaseException(e);
737 return new Variant(binding, value);
741 public Variant getPossibleVariantValue(ReadGraph graph) throws DatabaseException {
742 Binding binding = getPossibleDefaultBinding(graph);
743 if(binding != null) {
744 Object value = getPossibleValue(graph, binding);
745 if(value == null) return null;
746 return new Variant(binding, value);
748 Object value = getPossibleValue(graph);
749 if(value == null) return null;
751 // TODO: hackish, consider doing something else here?
752 binding = value != null ? Bindings.getBinding(value.getClass()) : null;
753 return new Variant(binding, value);
754 } catch (BindingConstructionException e) {
760 public <T> T getPossibleValue(ReadGraph graph, Binding binding) throws DatabaseException {
762 return getValue(graph, binding);
763 } catch(MissingVariableValueException e) {
768 public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
769 throw new AdaptionException(this + " does not support adaption to " + clazz);
773 public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {
775 return adapt(graph, clazz);
776 } catch (AdaptionException e) {
782 public static String encodeString(String string) throws DatabaseException {
783 if (string == null || "".equals(string)) return string;
784 return URIStringUtils.escape(string);
787 public static String decodeString(String string) throws DatabaseException {
788 return URIStringUtils.unescape(string);
792 protected Variable getPossiblePropertyFromContext(ReadGraph graph, Resource context, String name) throws DatabaseException {
794 Map<String, Resource> predicates = graph.syncRequest(new PropertyMapOfResource(context));
795 Resource property = predicates.get(name);
796 if(property == null) return null;
797 Resource object = graph.getSingleObject(context, property);
798 Variable objectAdapter = graph.getPossibleContextualAdapter(object, new ModelledVariablePropertyDescriptorImpl(this, context, property),
799 ModelledVariablePropertyDescriptor.class, Variable.class);
800 if(objectAdapter != null) return objectAdapter;
801 return graph.getPossibleContextualAdapter(property, new ModelledVariablePropertyDescriptorImpl(this, context, property),
802 ModelledVariablePropertyDescriptor.class, Variable.class);
806 protected Map<String, Variable> collectPropertiesFromContext(ReadGraph graph, Resource context, Map<String, Variable> properties) throws DatabaseException {
808 for(Map.Entry<String, Resource> entry : graph.syncRequest(new PropertyMapOfResource(context)).entrySet()) {
809 String name = entry.getKey();
810 Resource property = entry.getValue();
811 Resource object = graph.getSingleObject(context, property);
812 Variable objectAdapter = graph.getPossibleContextualAdapter(object, new ModelledVariablePropertyDescriptorImpl(this, context, property),
813 ModelledVariablePropertyDescriptor.class, Variable.class);
814 if(objectAdapter != null) {
815 if(objectAdapter != null) {
816 if(properties == null) properties = new HashMap<String,Variable>();
817 properties.put(name, objectAdapter);
820 Variable predicateAdapter = graph.getPossibleContextualAdapter(property, new ModelledVariablePropertyDescriptorImpl(this, context, property),
821 ModelledVariablePropertyDescriptor.class, Variable.class);
822 if(predicateAdapter != null) {
823 if(properties == null) properties = new HashMap<String,Variable>();
824 properties.put(name, predicateAdapter);
835 public Variable resolve(ReadGraph graph, RVIPart part) throws DatabaseException {
836 if(part instanceof StringRVIPart) {
837 StringRVIPart srp = (StringRVIPart)part;
838 if(Role.CHILD.equals(srp.getRole())) return getChild(graph, srp.string);
839 else if(Role.PROPERTY.equals(srp.getRole())) return getProperty(graph, srp.string);
840 } else if(part instanceof ResourceRVIPart) {
841 ResourceRVIPart rrp = (ResourceRVIPart)part;
842 if(Role.CHILD.equals(rrp.getRole())) return resolveChild(graph, rrp.resource);
843 else if(Role.PROPERTY.equals(rrp.getRole())) return resolveProperty(graph, rrp.resource);
844 } else if(part instanceof GuidRVIPart) {
845 GuidRVIPart grp = (GuidRVIPart)part;
846 if(Role.CHILD.equals(grp.getRole())) return resolveChild(graph, grp);
847 else if(Role.PROPERTY.equals(grp.getRole())) return resolveProperty(graph, grp);
849 throw new DatabaseException("Unrecognized RVIPart: " + part);
853 public Variable resolvePossible(ReadGraph graph, RVIPart part) throws DatabaseException {
854 if(part instanceof StringRVIPart) {
855 StringRVIPart srp = (StringRVIPart)part;
856 if(Role.CHILD.equals(srp.getRole())) return getPossibleChild(graph, srp.string);
857 else if(Role.PROPERTY.equals(srp.getRole())) return getPossibleProperty(graph, srp.string);
858 } else if(part instanceof ResourceRVIPart) {
859 ResourceRVIPart rrp = (ResourceRVIPart)part;
860 if(Role.CHILD.equals(rrp.getRole())) return resolvePossibleChild(graph, rrp.resource);
861 else if(Role.PROPERTY.equals(rrp.getRole())) return resolvePossibleProperty(graph, rrp.resource);
862 } else if(part instanceof GuidRVIPart) {
863 GuidRVIPart grp = (GuidRVIPart)part;
864 if(Role.CHILD.equals(grp.getRole())) return resolvePossibleChild(graph, grp);
865 else if(Role.PROPERTY.equals(grp.getRole())) return resolvePossibleProperty(graph, grp);
867 throw new DatabaseException("Unrecognized RVIPart: " + part);
871 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
872 throw new DatabaseException("No data type.");
875 public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {
876 Datatype type = getDatatype(graph);
877 return Bindings.getBinding(type);
880 public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
882 return getDefaultBinding(graph);
883 } catch(DatabaseException e) {
889 public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {
891 return getDatatype(graph);
892 } catch(DatabaseException e) {
897 // public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
899 // Datatype type = getPossibleDatatype(graph);
900 // if(type == null) return null;
901 // return Bindings.getBinding(type);
906 // public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {
908 // Variant vt = getVariantValue(graph);
909 // if(vt == null) return null;
910 // Binding binding = vt.getBinding();
911 // if(binding == null) return null;
912 // return binding.type();
917 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
918 throw new DatabaseException(getClass().getSimpleName() + ": No predicate property for " + getPossibleURI(graph));
922 public Variable getPossiblePredicate(ReadGraph graph) throws DatabaseException {
924 return getPredicate(graph);
925 } catch(DatabaseException e) {
931 public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
932 Variable predicate = getPredicate(graph);
933 if(predicate == null) throw new DatabaseException(getClass().getSimpleName() + ": No predicate property for " + getPossibleURI(graph));
934 return predicate.getRepresents(graph);
938 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
939 Variable predicate = getPossiblePredicate(graph);
940 if(predicate == null) return null;
941 else return predicate.getPossibleRepresents(graph);
945 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
947 return getRepresents(graph);
948 } catch(DatabaseException e) {
954 public Resource getPossibleType(ReadGraph graph) throws DatabaseException {
955 Resource resource = getPossibleRepresents(graph);
956 if(resource == null) {
957 String uri = getPossiblePropertyValue(graph, "typeURI");
958 if(uri != null) return graph.syncRequest(new PossibleResource(uri), TransientCacheAsyncListener.<Resource>instance());
961 return graph.getPossibleObject(resource, Layer0.getInstance(graph).InstanceOf);
964 public Resource getType(ReadGraph graph, Resource baseType) throws DatabaseException {
965 Resource resource = getPossibleRepresents(graph);
966 if(resource == null) {
967 String uri = getPossiblePropertyValue(graph, "typeURI");
968 if(uri != null) return graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri), TransientCacheAsyncListener.<Resource>instance());
969 throw new DatabaseException("No type for " + getURI(graph));
971 return graph.getSingleType(resource, baseType);
975 public Resource getPossibleType(ReadGraph graph, Resource baseType) throws DatabaseException {
976 Resource resource = getPossibleRepresents(graph);
977 if(resource == null) {
978 String uri = getPossiblePropertyValue(graph, "typeURI");
980 Resource type = graph.syncRequest(new PossibleResource(uri), TransientCacheAsyncListener.<Resource>instance());
981 if(type == null) return null;
982 if(graph.isInheritedFrom(type, baseType)) return type;
987 return graph.getPossibleType(resource, baseType);
990 public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> map) throws DatabaseException {
991 Map<String,Variable> all = collectDomainProperties(graph, null);
992 for(Map.Entry<String, Variable> entry : all.entrySet()) {
993 Set<String> classifications = entry.getValue().getClassifications(graph);
994 if(classifications.contains(classification)) {
995 if(map == null) map = new HashMap<String,Variable>();
996 map.put(entry.getKey(), entry.getValue());
1003 public RVI getPossibleRVI(ReadGraph graph) throws DatabaseException {
1005 return getRVI(graph);
1006 } catch(DatabaseException e) {