]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/internal/DebugUtils.java
306f7f9613c6d4a9f593c565524f546d14f46275
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / internal / DebugUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
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;
14
15 import java.util.Set;
16
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;
34
35 /**
36  * @author Tuukka Lehtonen 
37  */
38 public class DebugUtils {
39
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);
44         if (stm != null) {
45             String label = NameUtils.getSafeLabel(graph, r);
46             if (!label.isEmpty() && !stm.isAsserted(r))
47                 name += " (" + label + ")";
48         }
49         return name;
50     }
51
52     public static String getSafeURI(ReadGraph graph, Resource r) throws DatabaseException {
53         String uri = graph.getPossibleURI(r);
54         if (uri != null)
55             return uri;
56         String name = NameUtils.getSafeName(graph, r, true);
57         return name;
58     }
59
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) );
72                     }
73                 }
74             }
75         }
76         return uri;
77     }
78
79     @SuppressWarnings("unchecked")
80     public static void addResource(Session s, GraphDebugger debugger) throws DatabaseException {
81         Shell shell = debugger.getShell();
82
83         SearchResourceDialog rld = new SearchResourceDialog(s, false, shell, "Create New Resource");
84         rld.setBlockOnOpen(true);
85         rld.setResourceFilter(ResourceSearch.FILTER_TYPES);
86
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();
95         }
96         final Resource subject = subject_;
97
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();
105
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() {
114             @Override
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);
119                 result.set(r);
120             }
121         });
122
123         if (result.get()!=null)
124             debugger.changeLocation(result.get());
125     }
126
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");
131
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();
140         }
141         final Resource subject = subject_;
142
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();
150
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();
157
158         s.syncRequest(new WriteRequest() {
159             @Override
160             public void perform(WriteGraph g) throws DatabaseException {
161                 g.claim(subject, predicate, object);
162             }
163         });
164     }
165
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());
176         }
177     }
178
179 }