1 package org.simantics.db.common;
3 import java.util.HashSet;
6 import org.simantics.databoard.adapter.AdaptException;
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.graph.representation.TransferableGraph1;
12 import org.simantics.graph.representation.TransferableGraphUtils;
13 import org.simantics.graph.representation.Value;
14 import org.simantics.layer0.Layer0;
16 public class TransferableGraphRequests {
18 private TransferableGraph1 tg;
19 private ReadGraph graph;
21 public TransferableGraphRequests(TransferableGraph1 tg, ReadGraph graph) {
26 public Resource getExternal(int id) throws DatabaseException {
27 String uri = TransferableGraphUtils.getURI(tg, id);
28 return graph.getPossibleResource(uri);
31 public Set<Integer> getObjects(int subject, Resource relation) throws DatabaseException {
33 Set<Integer> result = new HashSet<Integer>();
34 for(int i=0;i<tg.statements.length;i+=4) {
35 if(tg.statements[i] == subject) {
36 Resource predicate = getExternal(tg.statements[i+1]);
38 if(graph.isSubrelationOf(predicate, relation)) result.add(tg.statements[i+3]);
45 public Set<Integer> getSubjects(int object, Resource inverseRelation) throws DatabaseException {
47 Set<Integer> result = new HashSet<Integer>();
48 for(int i=0;i<tg.statements.length;i+=4) {
49 if(tg.statements[i+3] == object) {
50 Resource predicate = getExternal(tg.statements[i+2]);
52 if(graph.isSubrelationOf(predicate, inverseRelation)) result.add(tg.statements[i]);
59 public int getPossibleObject(int subject, Resource relation) throws DatabaseException {
60 Set<Integer> objects = getObjects(subject, relation);
61 if(objects.size() == 1) return objects.iterator().next();
65 public int getSingleObject(int subject, Resource relation) throws DatabaseException {
67 Set<Integer> objects = getObjects(subject, relation);
68 if(objects.size() == 1) return objects.iterator().next();
69 else if (objects.size() == 0) throw new DatabaseException("No objects for subject " + subject + " and relation " + graph.getURI(relation));
70 else throw new DatabaseException("Multiple (" + objects.size() + ") objects for subject " + subject + " and relation " + graph.getURI(relation));
74 public int getPossibleSubject(int object, Resource inverseRelation) throws DatabaseException {
75 Set<Integer> subjects = getSubjects(object, inverseRelation);
76 if(subjects.size() == 1) return subjects.iterator().next();
80 public int getSingleSubject(int object, Resource inverseRelation) throws DatabaseException {
82 Set<Integer> subjects = getObjects(object, inverseRelation);
83 if(subjects.size() == 1) return subjects.iterator().next();
84 else if (subjects.size() == 0) throw new DatabaseException("No subjects for object " + object + " and relation " + graph.getURI(inverseRelation));
85 else throw new DatabaseException("Multiple (" + subjects.size() + ") subjects for object " + object + " and relation " + graph.getURI(inverseRelation));
89 public <T> T getRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException {
91 int object = getSingleObject(subject, relation);
92 return getValue(object, binding);
96 public <T> T getPossibleRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException {
97 int object = getPossibleObject(subject, relation);
100 return getPossibleValue(object, binding);
103 @SuppressWarnings("unchecked")
104 public <T> T getValue(int subject, Binding binding) throws DatabaseException {
106 Value value = TransferableGraphUtils.findValue(tg, subject);
107 if(value == null) throw new DatabaseException("No value for subject " + subject);
109 return (T)value.value.getValue(binding);
110 } catch (AdaptException e) {
111 throw new DatabaseException(e);
116 @SuppressWarnings("unchecked")
117 public <T> T getPossibleValue(int subject, Binding binding) {
118 Value value = TransferableGraphUtils.findValue(tg, subject);
119 if(value == null) return null;
121 return (T)value.value.getValue(binding);
122 } catch (AdaptException e) {
127 public boolean isInstanceOf(int subject, Resource type) throws DatabaseException {
129 Layer0 L0 = Layer0.getInstance(graph);
130 for(int t : getObjects(subject, L0.InstanceOf)) {
131 Resource tr = getExternal(t);
133 if(graph.isInheritedFrom(tr, type)) return true;
141 public String formatResource(int r) throws DatabaseException {
143 String result = "" + r;
144 String uri = TransferableGraphUtils.getURI(tg, r);
145 if(!uri.isEmpty()) result = uri;
147 Value value = TransferableGraphUtils.findValue(tg, r);
148 if(value != null) result += " (value " + value.value + ")";
154 public String listStatements() throws DatabaseException {
156 StringBuilder b = new StringBuilder();
157 for(int i=0;i<tg.statements.length;i+=4) {
158 int subject = tg.statements[i];
159 int predicate = tg.statements[i+1];
160 int object = tg.statements[i+3];
161 b.append(formatResource(subject) + " " + formatResource(predicate) + " " + formatResource(object) + "\n");
167 public ReadGraph getGraph() {