]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java
Add logging by default to adapters exception-methods
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / TransferableGraphRequests.java
1 package org.simantics.db.common;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
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;
15
16 public class TransferableGraphRequests {
17         
18         private TransferableGraph1 tg;
19         private ReadGraph graph;
20         
21         public TransferableGraphRequests(TransferableGraph1 tg, ReadGraph graph) {
22                 this.tg = tg;
23                 this.graph = graph;
24         }
25
26         public Resource getExternal(int id) throws DatabaseException {
27                 String uri = TransferableGraphUtils.getURI(tg, id);
28                 return graph.getPossibleResource(uri);
29         }
30         
31         public Set<Integer> getObjects(int subject, Resource relation) throws DatabaseException {
32
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]);
37                                 if(predicate != null)
38                                         if(graph.isSubrelationOf(predicate, relation)) result.add(tg.statements[i+3]); 
39                         }
40                 }
41                 return result;
42                 
43         }
44
45         public Set<Integer> getSubjects(int object, Resource inverseRelation) throws DatabaseException {
46
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]);
51                                 if(predicate != null)
52                                         if(graph.isSubrelationOf(predicate, inverseRelation)) result.add(tg.statements[i]); 
53                         }
54                 }
55                 return result;
56                 
57         }
58
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();
62                 return -1;
63         }
64
65         public int getSingleObject(int subject, Resource relation) throws DatabaseException {
66
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));
71                 
72         }
73
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();
77                 return -1;
78         }
79
80         public int getSingleSubject(int object, Resource inverseRelation) throws DatabaseException {
81
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));
86                 
87         }
88
89         public <T> T getRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException {
90                 
91                 int object = getSingleObject(subject, relation);
92                 return getValue(object, binding);
93                 
94         }
95
96         public <T> T getPossibleRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException {
97                 int object = getPossibleObject(subject, relation);
98                 if (object == -1)
99                         return null;
100                 return getPossibleValue(object, binding);
101         }
102
103         @SuppressWarnings("unchecked")
104         public <T> T getValue(int subject, Binding binding) throws DatabaseException {
105
106                 Value value = TransferableGraphUtils.findValue(tg, subject);
107                 if(value == null) throw new DatabaseException("No value for subject " + subject);
108                 try {
109                         return (T)value.value.getValue(binding);
110                 } catch (AdaptException e) {
111                         throw new DatabaseException(e);
112                 }
113                 
114         }
115
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;
120                 try {
121                         return (T)value.value.getValue(binding);
122                 } catch (AdaptException e) {
123                         return null;
124                 }
125         }
126
127         public boolean isInstanceOf(int subject, Resource type) throws DatabaseException {
128
129                 Layer0 L0 = Layer0.getInstance(graph);
130                 for(int t : getObjects(subject, L0.InstanceOf)) {
131                         Resource tr = getExternal(t);
132                         if(tr != null)
133                                 if(graph.isInheritedFrom(tr, type)) return true;
134                 }
135                 
136                 return false;
137                 
138         }
139         
140
141         public String formatResource(int r) throws DatabaseException {
142                 
143                 String result = "" + r;
144                 String uri = TransferableGraphUtils.getURI(tg, r);
145                 if(!uri.isEmpty()) result = uri;
146                 
147                 Value value = TransferableGraphUtils.findValue(tg, r);
148                 if(value != null) result += " (value " + value.value + ")";
149                 
150                 return result;
151                 
152         }
153         
154         public String listStatements() throws DatabaseException {
155                 
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");
162         }
163         return b.toString();
164                 
165         }
166         
167         public ReadGraph getGraph() {
168                 return graph;
169         }
170         
171 }