1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - index based searching and graph manipulation (#4255)
12 *******************************************************************************/
13 package org.simantics.debug.ui.internal;
17 import org.eclipse.swt.widgets.Shell;
18 import org.simantics.databoard.util.URIStringUtils;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.Statement;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.PossibleIndexRoot;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.common.utils.NameUtils;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.debug.ui.GraphDebugger;
29 import org.simantics.debug.ui.ResourceSearch;
30 import org.simantics.debug.ui.SearchResourceDialog;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.utils.Container;
33 import org.simantics.utils.DataContainer;
36 * @author Tuukka Lehtonen
38 public class DebugUtils {
40 public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {
41 Layer0 l0 = Layer0.getInstance(graph);
42 String name = NameUtils.getSafeName(graph, r);
43 Statement stm = graph.getPossibleStatement(r, l0.HasLabel);
45 String label = NameUtils.getSafeLabel(graph, r);
46 if (!label.isEmpty() && !stm.isAsserted(r))
47 name += " (" + label + ")";
52 public static String getSafeURI(ReadGraph graph, Resource r) throws DatabaseException {
53 String uri = graph.getPossibleURI(r);
56 String name = NameUtils.getSafeName(graph, r, true);
60 public static String getPossibleRootRelativePath(ReadGraph graph, Resource r) throws DatabaseException {
61 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));
62 String uri = graph.getPossibleURI(r);
63 if (indexRoot != null && uri != null) {
64 Layer0 L0 = Layer0.getInstance(graph);
65 Set<Resource> types = graph.getTypes(indexRoot);
66 if (!types.contains(L0.Ontology)) {
67 Resource indexRootParent = graph.getPossibleObject(indexRoot, L0.PartOf);
68 if (indexRootParent != null) {
69 String rootParentUri = graph.getPossibleURI(indexRootParent);
70 if (rootParentUri != null) {
71 return URIStringUtils.unescape( uri.substring(rootParentUri.length()+1) );
79 @SuppressWarnings("unchecked")
80 public static void addResource(Session s, GraphDebugger debugger) throws DatabaseException {
81 Shell shell = debugger.getShell();
83 SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Create New Resource");
84 rld.setBlockOnOpen(true);
85 rld.setResourceFilter(ResourceSearch.FILTER_TYPES);
87 Resource subject_ = debugger.getDebuggerLocation();
88 if (subject_ == null) {
89 rld.setBlockOnOpen(true);
90 rld.setMessage("Select Subject");
91 rld.setInitialSelections(new Object[] {});
92 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
93 if (rld.getResult()==null) return;
94 subject_ = ((Container<Resource>)rld.getResult()[0]).get();
96 final Resource subject = subject_;
98 rld.setBlockOnOpen(true);
99 rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);
100 rld.setMessage("Select Predicate");
101 rld.setInitialSelections(new Object[] {});
102 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
103 if (rld.getResult()==null) return;
104 final Resource predicate = ((Container<Resource>)rld.getResult()[0]).get();
106 rld.setMessage("Select Type of New Object Instance");
107 rld.setResourceFilter(ResourceSearch.FILTER_TYPES);
108 rld.setInitialSelections(new Object[] {});
109 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
110 if (rld.getResult()==null) return;
111 final Resource type = ((Container<Resource>)rld.getResult()[0]).get();
112 final DataContainer<Resource> result = new DataContainer<Resource>();
113 s.syncRequest(new WriteRequest() {
115 public void perform(WriteGraph g) throws DatabaseException {
116 Resource r = g.newResource();
117 g.claim(r, Layer0.getInstance(g).InstanceOf, type);
118 g.claim(subject, predicate, r);
123 if (result.get()!=null)
124 debugger.changeLocation(result.get());
127 @SuppressWarnings("unchecked")
128 public static void addStatement(Session s, GraphDebugger debugger) throws DatabaseException {
129 Shell shell = debugger.getShell();
130 SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Create New Statement");
132 Resource subject_ = debugger.getDebuggerLocation();
133 if (subject_ == null) {
134 rld.setBlockOnOpen(true);
135 rld.setMessage("Select Subject");
136 rld.setInitialSelections(new Object[] {});
137 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
138 if (rld.getResult()==null) return;
139 subject_ = ((Container<Resource>)rld.getResult()[0]).get();
141 final Resource subject = subject_;
143 rld.setBlockOnOpen(true);
144 rld.setResourceFilter(ResourceSearch.FILTER_RELATIONS);
145 rld.setMessage("Select Predicate");
146 rld.setInitialSelections(new Object[] {});
147 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
148 if (rld.getResult()==null) return;
149 final Resource predicate = ((Container<Resource>)rld.getResult()[0]).get();
151 rld.setResourceFilter(ResourceSearch.FILTER_ALL);
152 rld.setMessage("Select Object");
153 rld.setInitialSelections(new Object[] {});
154 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
155 if (rld.getResult()==null) return;
156 final Resource object = ((Container<Resource>)rld.getResult()[0]).get();
158 s.syncRequest(new WriteRequest() {
160 public void perform(WriteGraph g) throws DatabaseException {
161 g.claim(subject, predicate, object);
166 public static void find(Session s, GraphDebugger debugger) {
167 Shell shell = debugger.getShell();
168 SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Select Resource to View");
169 rld.setBlockOnOpen(true);
170 if (rld.open()!=org.eclipse.jface.window.Window.OK) return;
171 if (rld.getResult()==null) return;
172 for (Object o : rld.getResult()) {
173 @SuppressWarnings("unchecked")
174 Container<Resource> rc = (Container<Resource>) o;
175 debugger.changeLocation(rc.get());