1 package org.simantics.db.layer0.util;
3 import java.util.Collection;
5 import org.simantics.databoard.util.URIStringUtils;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Statement;
9 import org.simantics.db.common.request.FunctionalStatementMapOfResource;
10 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.scl.runtime.tuple.Tuple2;
15 public class ExtendedUris {
17 public static Resource resolveAbsoluteUri(ReadGraph graph, String uri) throws DatabaseException {
18 if(!uri.startsWith("http:/"))
19 throw new IllegalArgumentException("Invalid absolute URI '" + uri + "'.");
20 return resolveRelativeUri(graph, graph.getRootLibrary(), uri.substring(6));
23 public static Resource resolveRelativeUri(ReadGraph graph, Resource parent, String uri) throws DatabaseException {
25 while(beginPos < uri.length()) {
27 for(endPos = beginPos+1; endPos < uri.length(); ++endPos) {
28 char c = uri.charAt(endPos);
29 if(c == '/' || c == '#')
33 char c = uri.charAt(beginPos);
35 String escapedName = uri.substring(beginPos+1, endPos);
36 String name = URIStringUtils.unescape(escapedName);
37 parent = graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(name);
39 throw new DatabaseException("Didn't find a child " + name + " for " + uri.substring(0, beginPos));
42 String escapedName = uri.substring(beginPos+1, endPos);
43 String name = URIStringUtils.unescape(escapedName);
44 parent = graph.syncRequest(new FunctionalStatementMapOfResource(parent)).get(name);
46 throw new DatabaseException("Didn't find a property " + name);
49 throw new IllegalArgumentException("Invalid relative URI '" + uri + "'.");
55 private static boolean initialized = false;
56 private static Resource ElementToComponent;
57 private static void initialize(ReadGraph graph) throws DatabaseException {
58 ElementToComponent = graph.getResource("http://www.simantics.org/Modeling-1.2/ElementToComponent");
62 public static Tuple2 getPrimaryFunctionalReference(ReadGraph graph, Resource r) throws DatabaseException {
65 Statement stat = graph.getPossibleStatement(r, ElementToComponent);
67 return statementToResult(graph, stat);
71 public static Tuple2 getSecondaryFunctionalReference(ReadGraph graph, Resource r) throws DatabaseException {
72 Layer0 L0 = Layer0.getInstance(graph);
73 Collection<Statement> statements = graph.getStatements(r, L0.PropertyOf);
74 if(statements.size() == 1)
75 for(Statement stat : statements)
76 return statementToResult(graph, stat);
80 private static Tuple2 statementToResult(ReadGraph graph, Statement stat) throws DatabaseException {
81 return new Tuple2(stat.getObject(), graph.getInverse(stat.getPredicate()));