X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FTransferableGraphRequests.java;h=3d7404f151e6005141c7d3e0446783a9677a4abd;hb=c744918d55c304854e4fd316ccf04ce38d6071ff;hp=8f776b7e613deca0bb234e76ea7975bfb4aedcc3;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java index 8f776b7e6..3d7404f15 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java @@ -1,171 +1,171 @@ -package org.simantics.db.common; - -import java.util.HashSet; -import java.util.Set; - -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.Binding; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.graph.representation.TransferableGraphUtils; -import org.simantics.graph.representation.Value; -import org.simantics.layer0.Layer0; - -public class TransferableGraphRequests { - - private TransferableGraph1 tg; - private ReadGraph graph; - - public TransferableGraphRequests(TransferableGraph1 tg, ReadGraph graph) { - this.tg = tg; - this.graph = graph; - } - - public Resource getExternal(int id) throws DatabaseException { - String uri = TransferableGraphUtils.getURI(tg, id); - return graph.getPossibleResource(uri); - } - - public Set getObjects(int subject, Resource relation) throws DatabaseException { - - Set result = new HashSet(); - for(int i=0;i getSubjects(int object, Resource inverseRelation) throws DatabaseException { - - Set result = new HashSet(); - for(int i=0;i objects = getObjects(subject, relation); - if(objects.size() == 1) return objects.iterator().next(); - return -1; - } - - public int getSingleObject(int subject, Resource relation) throws DatabaseException { - - Set objects = getObjects(subject, relation); - if(objects.size() == 1) return objects.iterator().next(); - else if (objects.size() == 0) throw new DatabaseException("No objects for subject " + subject + " and relation " + graph.getURI(relation)); - else throw new DatabaseException("Multiple (" + objects.size() + ") objects for subject " + subject + " and relation " + graph.getURI(relation)); - - } - - public int getPossibleSubject(int object, Resource inverseRelation) throws DatabaseException { - Set subjects = getSubjects(object, inverseRelation); - if(subjects.size() == 1) return subjects.iterator().next(); - return -1; - } - - public int getSingleSubject(int object, Resource inverseRelation) throws DatabaseException { - - Set subjects = getObjects(object, inverseRelation); - if(subjects.size() == 1) return subjects.iterator().next(); - else if (subjects.size() == 0) throw new DatabaseException("No subjects for object " + object + " and relation " + graph.getURI(inverseRelation)); - else throw new DatabaseException("Multiple (" + subjects.size() + ") subjects for object " + object + " and relation " + graph.getURI(inverseRelation)); - - } - - public T getRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException { - - int object = getSingleObject(subject, relation); - return getValue(object, binding); - - } - - public T getPossibleRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException { - int object = getPossibleObject(subject, relation); - if (object == -1) - return null; - return getPossibleValue(object, binding); - } - - @SuppressWarnings("unchecked") - public T getValue(int subject, Binding binding) throws DatabaseException { - - Value value = TransferableGraphUtils.findValue(tg, subject); - if(value == null) throw new DatabaseException("No value for subject " + subject); - try { - return (T)value.value.getValue(binding); - } catch (AdaptException e) { - throw new DatabaseException(e); - } - - } - - @SuppressWarnings("unchecked") - public T getPossibleValue(int subject, Binding binding) { - Value value = TransferableGraphUtils.findValue(tg, subject); - if(value == null) return null; - try { - return (T)value.value.getValue(binding); - } catch (AdaptException e) { - return null; - } - } - - public boolean isInstanceOf(int subject, Resource type) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - for(int t : getObjects(subject, L0.InstanceOf)) { - Resource tr = getExternal(t); - if(tr != null) - if(graph.isInheritedFrom(tr, type)) return true; - } - - return false; - - } - - - public String formatResource(int r) throws DatabaseException { - - String result = "" + r; - String uri = TransferableGraphUtils.getURI(tg, r); - if(!uri.isEmpty()) result = uri; - - Value value = TransferableGraphUtils.findValue(tg, r); - if(value != null) result += " (value " + value.value + ")"; - - return result; - - } - - public String listStatements() throws DatabaseException { - - StringBuilder b = new StringBuilder(); - for(int i=0;i getObjects(int subject, Resource relation) throws DatabaseException { + + Set result = new HashSet(); + for(int i=0;i getSubjects(int object, Resource inverseRelation) throws DatabaseException { + + Set result = new HashSet(); + for(int i=0;i objects = getObjects(subject, relation); + if(objects.size() == 1) return objects.iterator().next(); + return -1; + } + + public int getSingleObject(int subject, Resource relation) throws DatabaseException { + + Set objects = getObjects(subject, relation); + if(objects.size() == 1) return objects.iterator().next(); + else if (objects.size() == 0) throw new DatabaseException("No objects for subject " + subject + " and relation " + graph.getURI(relation)); + else throw new DatabaseException("Multiple (" + objects.size() + ") objects for subject " + subject + " and relation " + graph.getURI(relation)); + + } + + public int getPossibleSubject(int object, Resource inverseRelation) throws DatabaseException { + Set subjects = getSubjects(object, inverseRelation); + if(subjects.size() == 1) return subjects.iterator().next(); + return -1; + } + + public int getSingleSubject(int object, Resource inverseRelation) throws DatabaseException { + + Set subjects = getObjects(object, inverseRelation); + if(subjects.size() == 1) return subjects.iterator().next(); + else if (subjects.size() == 0) throw new DatabaseException("No subjects for object " + object + " and relation " + graph.getURI(inverseRelation)); + else throw new DatabaseException("Multiple (" + subjects.size() + ") subjects for object " + object + " and relation " + graph.getURI(inverseRelation)); + + } + + public T getRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException { + + int object = getSingleObject(subject, relation); + return getValue(object, binding); + + } + + public T getPossibleRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException { + int object = getPossibleObject(subject, relation); + if (object == -1) + return null; + return getPossibleValue(object, binding); + } + + @SuppressWarnings("unchecked") + public T getValue(int subject, Binding binding) throws DatabaseException { + + Value value = TransferableGraphUtils.findValue(tg, subject); + if(value == null) throw new DatabaseException("No value for subject " + subject); + try { + return (T)value.value.getValue(binding); + } catch (AdaptException e) { + throw new DatabaseException(e); + } + + } + + @SuppressWarnings("unchecked") + public T getPossibleValue(int subject, Binding binding) { + Value value = TransferableGraphUtils.findValue(tg, subject); + if(value == null) return null; + try { + return (T)value.value.getValue(binding); + } catch (AdaptException e) { + return null; + } + } + + public boolean isInstanceOf(int subject, Resource type) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + for(int t : getObjects(subject, L0.InstanceOf)) { + Resource tr = getExternal(t); + if(tr != null) + if(graph.isInheritedFrom(tr, type)) return true; + } + + return false; + + } + + + public String formatResource(int r) throws DatabaseException { + + String result = "" + r; + String uri = TransferableGraphUtils.getURI(tg, r); + if(!uri.isEmpty()) result = uri; + + Value value = TransferableGraphUtils.findValue(tg, r); + if(value != null) result += " (value " + value.value + ")"; + + return result; + + } + + public String listStatements() throws DatabaseException { + + StringBuilder b = new StringBuilder(); + for(int i=0;i