1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.text.DateFormat;
\r
17 import java.util.ArrayList;
\r
18 import java.util.Collection;
\r
19 import java.util.Collections;
\r
20 import java.util.Date;
\r
21 import java.util.HashMap;
\r
22 import java.util.HashSet;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\r
25 import java.util.Set;
\r
26 import java.util.TreeMap;
\r
28 import org.eclipse.core.runtime.CoreException;
\r
29 import org.eclipse.core.runtime.IProgressMonitor;
\r
30 import org.eclipse.core.runtime.NullProgressMonitor;
\r
31 import org.eclipse.core.runtime.Path;
\r
32 import org.eclipse.jface.dialogs.Dialog;
\r
33 import org.eclipse.jface.dialogs.IDialogConstants;
\r
34 import org.eclipse.jface.dialogs.MessageDialog;
\r
35 import org.eclipse.jface.layout.GridDataFactory;
\r
36 import org.eclipse.jface.layout.GridLayoutFactory;
\r
37 import org.eclipse.jface.resource.ImageDescriptor;
\r
38 import org.eclipse.jface.viewers.IStructuredSelection;
\r
39 import org.eclipse.jface.viewers.StructuredSelection;
\r
40 import org.eclipse.jface.window.Window;
\r
41 import org.eclipse.jface.wizard.WizardDialog;
\r
42 import org.eclipse.swt.SWT;
\r
43 import org.eclipse.swt.widgets.Composite;
\r
44 import org.eclipse.swt.widgets.Control;
\r
45 import org.eclipse.swt.widgets.Display;
\r
46 import org.eclipse.swt.widgets.FileDialog;
\r
47 import org.eclipse.swt.widgets.Shell;
\r
48 import org.eclipse.ui.IWorkbenchWizard;
\r
49 import org.eclipse.ui.PlatformUI;
\r
50 import org.eclipse.ui.wizards.IWizardDescriptor;
\r
51 import org.simantics.Simantics;
\r
52 import org.simantics.annotation.ontology.AnnotationResource;
\r
53 import org.simantics.databoard.Bindings;
\r
54 import org.simantics.databoard.binding.Binding;
\r
55 import org.simantics.databoard.binding.mutable.Variant;
\r
56 import org.simantics.databoard.container.DataContainer;
\r
57 import org.simantics.databoard.container.DataContainers;
\r
58 import org.simantics.databoard.container.DataFormatException;
\r
59 import org.simantics.databoard.container.FormatHandler;
\r
60 import org.simantics.databoard.serialization.SerializationException;
\r
61 import org.simantics.databoard.type.Datatype;
\r
62 import org.simantics.databoard.util.URIStringUtils;
\r
63 import org.simantics.datatypes.literal.GUID;
\r
64 import org.simantics.db.ReadGraph;
\r
65 import org.simantics.db.RequestProcessor;
\r
66 import org.simantics.db.Resource;
\r
67 import org.simantics.db.Statement;
\r
68 import org.simantics.db.WriteGraph;
\r
69 import org.simantics.db.common.NamedResource;
\r
70 import org.simantics.db.common.QueryMemoryWatcher;
\r
71 import org.simantics.db.common.primitiverequest.IsInstanceOf;
\r
72 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
\r
73 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
\r
74 import org.simantics.db.common.request.IndexRoot;
\r
75 import org.simantics.db.common.request.ObjectsWithType;
\r
76 import org.simantics.db.common.request.PossibleIndexRoot;
\r
77 import org.simantics.db.common.request.ReadRequest;
\r
78 import org.simantics.db.common.request.ResourceRead2;
\r
79 import org.simantics.db.common.request.WriteRequest;
\r
80 import org.simantics.db.common.request.WriteResultRequest;
\r
81 import org.simantics.db.common.utils.ListUtils;
\r
82 import org.simantics.db.common.utils.Logger;
\r
83 import org.simantics.db.common.utils.NameUtils;
\r
84 import org.simantics.db.common.utils.OrderedSetUtils;
\r
85 import org.simantics.db.common.utils.VersionInfo;
\r
86 import org.simantics.db.common.utils.VersionInfoRequest;
\r
87 import org.simantics.db.common.utils.Versions;
\r
88 import org.simantics.db.exception.DatabaseException;
\r
89 import org.simantics.db.layer0.SelectionHints;
\r
90 import org.simantics.db.layer0.adapter.CopyHandler;
\r
91 import org.simantics.db.layer0.adapter.GenericRelationIndex;
\r
92 import org.simantics.db.layer0.adapter.Instances;
\r
93 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
\r
94 import org.simantics.db.layer0.adapter.impl.ImportAdvisorFactory;
\r
95 import org.simantics.db.layer0.genericrelation.IndexedRelations;
\r
96 import org.simantics.db.layer0.migration.MigrationUtils;
\r
97 import org.simantics.db.layer0.request.ActivateModel;
\r
98 import org.simantics.db.layer0.request.ActiveModels;
\r
99 import org.simantics.db.layer0.request.Configuration;
\r
100 import org.simantics.db.layer0.request.IsLinkedTo;
\r
101 import org.simantics.db.layer0.request.PossibleModel;
\r
102 import org.simantics.db.layer0.util.ClipboardUtils;
\r
103 import org.simantics.db.layer0.util.DraftStatusBean;
\r
104 import org.simantics.db.layer0.util.Layer0Utils;
\r
105 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
\r
106 import org.simantics.db.layer0.util.PasteEventHandler;
\r
107 import org.simantics.db.layer0.util.RemoverUtil;
\r
108 import org.simantics.db.layer0.util.SimanticsClipboard;
\r
109 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
\r
110 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
\r
111 import org.simantics.db.layer0.util.SimanticsKeys;
\r
112 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
\r
113 import org.simantics.db.layer0.variable.Variable;
\r
114 import org.simantics.db.layer0.variable.Variables;
\r
115 import org.simantics.db.request.Read;
\r
116 import org.simantics.db.service.ClusterControl;
\r
117 import org.simantics.db.service.CollectionSupport;
\r
118 import org.simantics.db.service.QueryControl;
\r
119 import org.simantics.db.service.VirtualGraphSupport;
\r
120 import org.simantics.diagram.stubs.DiagramResource;
\r
121 import org.simantics.diagram.stubs.G2DResource;
\r
122 import org.simantics.diagram.synchronization.graph.AddElement;
\r
123 import org.simantics.graph.db.IImportAdvisor2;
\r
124 import org.simantics.graph.db.ImportAdvisors;
\r
125 import org.simantics.graph.db.MissingDependencyException;
\r
126 import org.simantics.graph.db.StreamingTransferableGraphFileReader;
\r
127 import org.simantics.graph.db.TransferableGraphException;
\r
128 import org.simantics.graph.db.TransferableGraphSource;
\r
129 import org.simantics.graph.db.TransferableGraphs;
\r
130 import org.simantics.graph.representation.Identity;
\r
131 import org.simantics.graph.representation.Root;
\r
132 import org.simantics.graph.representation.TransferableGraph1;
\r
133 import org.simantics.graph.representation.TransferableGraphUtils;
\r
134 import org.simantics.issues.common.IssueSourceUtils;
\r
135 import org.simantics.issues.ontology.IssueResource;
\r
136 import org.simantics.layer0.Layer0;
\r
137 import org.simantics.layer0.utils.direct.GraphUtils;
\r
138 import org.simantics.modeling.adapters.ChangeInformation;
\r
139 import org.simantics.modeling.template2d.ontology.Template2dResource;
\r
140 import org.simantics.operation.Layer0X;
\r
141 import org.simantics.project.ontology.ProjectResource;
\r
142 import org.simantics.scenegraph.profile.ProfileUtils;
\r
143 import org.simantics.scl.runtime.function.Function1;
\r
144 import org.simantics.scl.runtime.tuple.Tuple;
\r
145 import org.simantics.simulation.ontology.SimulationResource;
\r
146 import org.simantics.structural.stubs.StructuralResource2;
\r
147 import org.simantics.structural2.scl.StructuralComponent;
\r
148 import org.simantics.structural2.utils.StructuralUtils;
\r
149 import org.simantics.ui.SimanticsUI;
\r
150 import org.simantics.utils.ObjectUtils;
\r
151 import org.simantics.utils.datastructures.Pair;
\r
152 import org.simantics.utils.datastructures.Triple;
\r
153 import org.simantics.utils.datastructures.hints.HintContext;
\r
154 import org.simantics.utils.ui.dialogs.ListDialog;
\r
157 * @author Hannu Niemistö
\r
159 public class ModelingUtils {
\r
161 private ReadGraph g;
\r
162 private WriteGraph wg;
\r
164 private StructuralResource2 sr;
\r
165 private DiagramResource dr;
\r
166 public ModelingResources mr;
\r
167 public SimulationResource SIMU;
\r
169 public ModelingUtils(WriteGraph g) {
\r
172 b = Layer0.getInstance(g);
\r
173 sr = StructuralResource2.getInstance(g);
\r
174 dr = DiagramResource.getInstance(g);
\r
175 mr = ModelingResources.getInstance(g);
\r
176 SIMU = SimulationResource.getInstance(g);
\r
180 public Resource createSymbol2(String name) throws DatabaseException {
\r
181 return createSymbol2(name, dr.Composite);
\r
185 public Resource createSymbol2(String name, Resource type) throws DatabaseException {
\r
186 G2DResource g2d = G2DResource.getInstance(g);
\r
188 // Resource visibleTag = wg.newResource();
\r
189 // wg.claim(visibleTag, b.SubrelationOf, null, dr.IsVisible);
\r
190 // Resource focusableTag = wg.newResource();
\r
191 // wg.claim(focusableTag, b.SubrelationOf, null, dr.IsFocusable);
\r
193 Double boxDimension = 6.0;
\r
194 Collection<Statement> grid = g.getAssertedStatements(type, dr.HasGridSize);
\r
195 if(grid.size() == 1) {
\r
196 Double d = g.getPossibleValue(grid.iterator().next().getObject(), Bindings.DOUBLE);
\r
197 if(d != null) boxDimension = 2*d;
\r
201 Resource element = GraphUtils.create(wg,
\r
202 b.InstanceOf, dr.SVGElement,
\r
203 g2d.HasSVGDocument,
\r
204 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
\r
205 // REMOVED by Tuukka, because This will cause
\r
206 // parsers to get on the net and get the doctype
\r
207 // definitions which can stall the UI for a long time.
\r
208 // Besides, we're not using the validation for anything
\r
209 // so it's useless to us.
\r
210 //"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" +
\r
211 "<svg xmlns=\"http://www.w3.org/2000/svg\" overflow=\"visible\" version=\"1.1\">" +
\r
212 "<rect x=\"-" + boxDimension + "\" y=\"-" + boxDimension + "\" width=\"" + (2*boxDimension) + "\" height=\"" + (2*boxDimension) + "\" fill=\"none\" stroke=\"rgb(0,0,0)\" stroke-width=\"0.1\"/>" +
\r
216 // wg.claim(element, visibleTag, element);
\r
217 // wg.claim(element, focusableTag, element);
\r
219 Resource orderedSet = OrderedSetUtils.create(wg, type, element);
\r
221 // wg.claim(orderedSet, dr.HasLayer, GraphUtils.create2(wg, dr.Layer,
\r
222 // b.HasName, "Default",
\r
223 // dr.IsActive, Boolean.TRUE,
\r
224 // dr.HasVisibleTag, visibleTag,
\r
225 // dr.HasFocusableTag, focusableTag));
\r
227 Resource result = GraphUtils.create(wg,
\r
230 sr.IsDefinedBy, orderedSet);
\r
232 wg.claim(result, b.ConsistsOf, orderedSet);
\r
233 wg.claimLiteral(orderedSet, b.HasName, "__DIAGRAM__", Bindings.STRING);
\r
234 AddElement.claimFreshElementName(wg, orderedSet, element);
\r
235 wg.claim(orderedSet, b.ConsistsOf, element);
\r
237 wg.claim(result, b.Inherits, null, dr.DefinedElement);
\r
241 public static Collection<Resource> getElementCorrespondendences(ReadGraph g, Resource element) throws DatabaseException {
\r
242 DiagramResource dr = DiagramResource.getInstance(g);
\r
243 ModelingResources mr = ModelingResources.getInstance(g);
\r
244 if(g.isInstanceOf(element, dr.Connection)) {
\r
245 Resource mappedComponent = g.getPossibleObject(element, mr.ElementToComponent);
\r
246 if(mappedComponent != null) return Collections.singletonList(mappedComponent);
\r
247 Resource mappedConnection = g.getPossibleObject(element, mr.DiagramConnectionToConnection);
\r
248 if(mappedConnection == null)
\r
249 return Collections.emptyList();
\r
250 ArrayList<Resource> result = new ArrayList<Resource>();
\r
251 Collection<Resource> relatedMappedConnections = StructuralUtils.getRelatedConnections(g, mappedConnection);
\r
252 for(Resource relatedMappedConnection : relatedMappedConnections)
\r
253 for(Resource relatedConnection : g.getObjects(relatedMappedConnection, mr.ConnectionToDiagramConnection))
\r
254 result.addAll(g.getObjects(relatedConnection, mr.ElementToComponent));
\r
258 return g.getObjects(element, mr.ElementToComponent);
\r
261 public static Resource getPossibleElement(ReadGraph g, Resource component) throws DatabaseException {
\r
262 ModelingResources mr = ModelingResources.getInstance(g);
\r
263 return g.getPossibleObject(component, mr.ComponentToElement);
\r
266 public static Resource getPossibleElementCorrespondendence(ReadGraph g, Resource element) throws DatabaseException {
\r
267 Collection<Resource> corrs = getElementCorrespondendences(g, element);
\r
268 if(corrs.size() != 1) return null;
\r
269 else return corrs.iterator().next();
\r
272 public static Resource getSingleElementCorrespondendence(ReadGraph g, Resource element) throws DatabaseException {
\r
273 Collection<Resource> corrs = getElementCorrespondendences(g, element);
\r
274 if(corrs.size() != 1) throw new DatabaseException("Expected 1 element correspondence, got " + corrs.size());
\r
275 else return corrs.iterator().next();
\r
278 public static Resource createExperiment(WriteGraph graph, Resource model) throws DatabaseException {
\r
280 Layer0 L0 = Layer0.getInstance(graph);
\r
281 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
282 Resource experiment = graph.newResource();
\r
283 graph.claim(experiment, L0.InstanceOf, SIMU.Experiment);
\r
284 graph.claimLiteral(experiment, L0.HasName, "Experiment");
\r
285 graph.claim(model, L0.ConsistsOf, experiment);
\r
290 public static Resource createModel(WriteGraph graph, Resource type) throws DatabaseException {
\r
291 return createModel(graph, type, Simantics.getProjectResource(), null);
\r
294 public static Resource createModel(WriteGraph graph, Resource type, String name) throws DatabaseException {
\r
295 return createModel(graph, type, Simantics.getProjectResource(), name);
\r
298 public static Resource createModel(WriteGraph graph, Resource type, final Resource target, String name) throws DatabaseException {
\r
300 Layer0 L0 = Layer0.getInstance(graph);
\r
301 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
302 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
303 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
306 name = NameUtils.findFreshName(graph, "Model", target, L0.ConsistsOf, "%s%d");
\r
308 final Resource model = graph.newResource();
\r
309 graph.newClusterSet(model);
\r
310 graph.claim(model, L0.InstanceOf, null, type);
\r
311 graph.claimLiteral(model, L0.HasName, name);
\r
313 graph.claim(target, L0.ConsistsOf, model);
\r
315 Resource configurationType = graph.getPossibleObject(model, MOD.StructuralModel_HasConfigurationType);
\r
316 if(configurationType == null) configurationType = STR.Composite;
\r
318 Resource configuration = graph.newResource();
\r
319 graph.claimLiteral(configuration, L0.HasName, "Configuration", Bindings.STRING);
\r
320 graph.claim(configuration, L0.InstanceOf, null, configurationType);
\r
321 graph.claim(model, L0.ConsistsOf, configuration);
\r
322 graph.claim(model, SIMU.HasConfiguration, configuration);
\r
324 Resource joinClusterSet = graph.newResource();
\r
325 graph.newClusterSet(joinClusterSet);
\r
326 graph.claim(joinClusterSet, L0.InstanceOf, L0.ClusterSet);
\r
327 graph.claim(model, STR.HasJoinClusterSet, joinClusterSet);
\r
329 linkOntologyDependenciesToModel(graph, model, target);
\r
331 Resource ontology = graph.syncRequest(new PossibleIndexRoot(type));
\r
332 if(ontology != null) {
\r
333 graph.claim(model, L0.IsLinkedTo, ontology);
\r
336 VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);
\r
337 graph.asyncRequest(new WriteRequest(support.getWorkspacePersistent("activations")) {
\r
340 public void perform(WriteGraph graph) throws DatabaseException {
\r
341 Layer0X L0X = Layer0X.getInstance(graph);
\r
342 Collection<Resource> actives = graph.syncRequest(new ActiveModels(target));
\r
343 if(actives.isEmpty()) {
\r
344 graph.claim(model, L0X.IsActivatedBy, target);
\r
354 public static void linkOntologyDependenciesToModel(WriteGraph graph, Resource model, Resource target)
\r
355 throws DatabaseException {
\r
356 Layer0 L0 = Layer0.getInstance(graph);
\r
357 ProjectResource PROJ = ProjectResource.getInstance(graph);
\r
358 for(Resource dep : graph.getObjects(target, L0.IsLinkedTo)) {
\r
359 if(graph.isInstanceOf(dep, PROJ.NamespaceRequirement)) {
\r
360 for(Resource req : graph.getObjects(dep, PROJ.RequiresNamespace)) {
\r
361 String uri = graph.getPossibleValue(req, Bindings.STRING);
\r
363 Resource ns = graph.getResource(uri);
\r
365 graph.claim(model, L0.IsLinkedTo, null, ns);
\r
373 public static void addSCLMainToModel(WriteGraph graph, Resource model)
\r
374 throws DatabaseException {
\r
375 addSCLMainToModel(graph, model, "SCLMain", "include \"Simantics/All\"\n");
\r
378 public static void addSCLMainToModel(WriteGraph graph, Resource model, String name, String contents)
\r
379 throws DatabaseException {
\r
380 Layer0 L0 = Layer0.getInstance(graph);
\r
381 Resource sclmain = GraphUtils.create2(graph, L0.SCLModule, L0.PartOf, model, L0.HasName, name);
\r
382 graph.claimLiteral(sclmain, L0.SCLModule_definition, contents, Bindings.STRING);
\r
385 public static Resource createLocalLibrary(WriteGraph graph, Resource container, String name) throws DatabaseException {
\r
386 Layer0 L0 = Layer0.getInstance(graph);
\r
387 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
388 Resource library = graph.newResource();
\r
389 graph.claim(library, L0.InstanceOf, null, L0.Library);
\r
390 graph.addLiteral(library, L0.HasName, L0.NameOf, "Library", Bindings.STRING);
\r
391 if (container != null) {
\r
392 graph.claim(container, L0.ConsistsOf, L0.PartOf, library);
\r
393 graph.claim(container, MOD.HasLocalLibrary, MOD.IsLocalLibraryOf, library);
\r
398 public static void importModel(String fileName) {
\r
400 Resource project = SimanticsUI.getProject().get();
\r
404 StreamingTransferableGraphFileReader importer = new StreamingTransferableGraphFileReader(new File(fileName));
\r
405 TransferableGraphSource tg = importer.readTG();
\r
407 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(project) {
\r
409 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
\r
412 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
\r
413 library = Simantics.getProjectResource();
\r
414 String newName = newName(graph, library, root.name);
\r
415 nameMappings.put(root.name, newName);
\r
419 TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor);
\r
421 } catch (MissingDependencyException e) {
\r
423 final Set<String> missingURIs = e.getMissingURIs();
\r
425 class ErrorMessageDialog extends MessageDialog {
\r
427 public ErrorMessageDialog(Shell shell) {
\r
429 "Unsatisfied dependencies", null,
\r
430 "The following dependencies were missing. Please import the dependencies and try again.",
\r
431 MessageDialog.ERROR, new String[] { "Continue" }, 0);
\r
435 protected Control createCustomArea(Composite composite) {
\r
436 GridLayoutFactory.fillDefaults().applyTo(composite);
\r
438 org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.READ_ONLY);
\r
439 GridDataFactory.fillDefaults().grab(true, true).applyTo(list);
\r
440 for(String s : missingURIs) list.add(s);
\r
446 Display display = Display.getCurrent();
\r
447 if(display != null) {
\r
448 ErrorMessageDialog md = new ErrorMessageDialog(display.getActiveShell());
\r
451 Display.getDefault().asyncExec(new Runnable() {
\r
454 public void run() {
\r
455 Shell shell = Display.getCurrent().getActiveShell();
\r
456 ErrorMessageDialog md = new ErrorMessageDialog(shell);
\r
464 } catch (Exception e) {
\r
465 Logger.defaultLogError(e);
\r
470 public static void primeVirtualGraphs() {
\r
471 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
\r
472 support.getWorkspacePersistent("activations");
\r
473 support.getWorkspacePersistent("experiments");
\r
474 support.getWorkspacePersistent("issues");
\r
475 support.getWorkspacePersistent("preferences");
\r
478 public static Resource createProfileEntry(WriteGraph graph, String name, Resource style, Resource group) throws DatabaseException {
\r
480 Layer0 L0 = Layer0.getInstance(graph);
\r
481 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
483 Resource entry = graph.newResource();
\r
484 graph.claim(entry, L0.InstanceOf, null, DIA.GroupStyleProfileEntry);
\r
485 graph.claimLiteral(entry, L0.HasName, name);
\r
486 graph.claimLiteral(entry, L0.HasLabel, name);
\r
487 graph.claim(entry, DIA.ProfileEntry_HasStyle, style);
\r
488 graph.claim(entry, DIA.ProfileEntry_HasGroup, group);
\r
494 public static Resource createProfile(WriteGraph graph, String profileName, Resource... entries) throws DatabaseException {
\r
496 Layer0 L0 = Layer0.getInstance(graph);
\r
497 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
499 Resource list = ListUtils.create(graph, DIA.Profile, entries);
\r
501 Resource profile = graph.newResource();
\r
502 graph.claim(profile, L0.InstanceOf, null, DIA.Profile);
\r
503 graph.claimLiteral(profile, L0.HasName, profileName);
\r
504 graph.claim(profile, DIA.HasEntries, null, list);
\r
510 public static Resource createProfile(WriteGraph graph, String profileName, Collection<Resource> entries) throws DatabaseException {
\r
511 return createProfile(graph, profileName, entries.toArray(new Resource[entries.size()]));
\r
514 public static Resource createToplevelProfile(WriteGraph graph, Resource model, String name, Resource ... profiles) throws DatabaseException {
\r
516 Resource work = createProfile(graph, name, profiles);
\r
518 Layer0 L0 = Layer0.getInstance(graph);
\r
519 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
521 graph.deny(model, DIA.HasActiveProfile);
\r
522 graph.claim(model, DIA.HasActiveProfile, work);
\r
523 graph.claim(model, L0.ConsistsOf, L0.PartOf, work);
\r
529 public static Resource createToplevelProfile(WriteGraph graph, Resource model, String name, Collection<Resource> profiles) throws DatabaseException {
\r
530 return createToplevelProfile(graph, model, name, profiles.toArray(new Resource[profiles.size()]));
\r
533 public static void activateProfileEntries(WriteGraph graph, final Resource profile, final Resource ... entries) {
\r
535 VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);
\r
536 graph.asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
\r
539 public void perform(WriteGraph graph) throws DatabaseException {
\r
540 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
541 for(Resource entry : entries)
\r
542 graph.claim(profile, SIMU.IsActive, entry);
\r
549 public static void activateProfileEntries(WriteGraph graph, Resource profile, Collection<Resource> entries) {
\r
550 activateProfileEntries(graph, profile, entries.toArray(new Resource[entries.size()]));
\r
553 public static void toggleProfileGroup(WriteGraph graph, Resource runtimeProfile, String groupName, boolean enabled) throws DatabaseException {
\r
554 Layer0 L0 = Layer0.getInstance(graph);
\r
555 for (Resource group : ProfileUtils.getProfileChildren(graph, runtimeProfile)) {
\r
556 String name = graph.getRelatedValue2(group, L0.HasName);
\r
557 if (name.equals(groupName)) {
\r
558 toggleProfileGroup(graph, runtimeProfile, group, enabled);
\r
564 public static void toggleProfileGroup(WriteGraph graph, Resource runtimeProfile, Resource group, boolean enabled) throws DatabaseException {
\r
565 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
567 if(graph.isInstanceOf(group, DIA.Profile)) {
\r
569 for(Resource child : ProfileUtils.getProfileChildren(graph, group)) {
\r
570 toggleProfileGroup(graph, runtimeProfile, child, enabled);
\r
573 } else if(graph.isInstanceOf(group, DIA.ProfileEntry)) {
\r
576 graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, group);
\r
578 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, group);
\r
584 public static void untrackDependencies() {
\r
585 untrackDependencies(Simantics.getSession());
\r
588 public static void untrackDependencies(RequestProcessor processor) {
\r
591 processor.syncRequest(new ReadRequest() {
\r
594 public void run(ReadGraph graph) throws DatabaseException {
\r
595 Layer0X L0X = Layer0X.getInstance(graph);
\r
596 GenericRelationIndex index = graph.adapt(L0X.DependenciesRelation, GenericRelationIndex.class);
\r
597 index.untrack(graph.getSession(), graph.getRootLibrary());
\r
601 } catch (DatabaseException e) {
\r
602 Logger.defaultLogError(e);
\r
607 public static void trackDependencies() {
\r
608 trackDependencies(Simantics.getSession());
\r
611 public static void trackDependencies(RequestProcessor processor) {
\r
614 processor.syncRequest(new ReadRequest() {
\r
617 public void run(ReadGraph graph) throws DatabaseException {
\r
618 Layer0X L0X = Layer0X.getInstance(graph);
\r
619 GenericRelationIndex index = graph.adapt(L0X.DependenciesRelation, GenericRelationIndex.class);
\r
620 index.trackAndIndex(graph.getSession(), graph.getRootLibrary());
\r
624 } catch (DatabaseException e) {
\r
625 Logger.defaultLogError(e);
\r
630 public static void removeIndex(WriteGraph graph, Resource model) throws DatabaseException {
\r
631 Layer0X L0X = Layer0X.getInstance(graph);
\r
632 IndexedRelations ir = graph.getService(IndexedRelations.class);
\r
633 // Deletes index files
\r
634 ir.reset(null, graph, L0X.DependenciesRelation, model);
\r
637 public static void resetIssueSources(WriteGraph graph, Resource model) throws DatabaseException {
\r
638 Layer0 L0 = Layer0.getInstance(graph);
\r
639 IssueResource ISSUE = IssueResource.getInstance(graph);
\r
640 for(Resource source : graph.sync(new ObjectsWithType(model, L0.ConsistsOf, ISSUE.ContinuousIssueSource))) {
\r
641 IssueSourceUtils.update(graph, source);
\r
645 public static void copyAnnotationTypes(WriteGraph graph, Resource sourceModel, Resource targetModel) throws DatabaseException {
\r
647 Layer0 L0 = Layer0.getInstance(graph);
\r
648 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
650 Instances query = graph.adapt(ANNO.AnnotationType, Instances.class);
\r
652 Resource library = graph.getPossibleObject(targetModel, ANNO.HasAnnotationTypeRoot);
\r
654 if(library == null) {
\r
655 library = graph.newResource();
\r
656 graph.claim(library, L0.InstanceOf, null, ANNO.AnnotationTypeLibrary);
\r
657 graph.claimLiteral(library, L0.HasName, L0.NameOf, L0.String, "Annotation types", Bindings.STRING);
\r
658 graph.claim(library, L0.PartOf, L0.ConsistsOf, targetModel);
\r
659 graph.claim(targetModel, ANNO.HasAnnotationTypeRoot, library);
\r
662 for(Resource type : query.find(graph, sourceModel)) {
\r
663 String name = graph.getRelatedValue(type, L0.HasName);
\r
664 Resource existing = Layer0Utils.getPossibleChild(graph, library, name);
\r
665 if(existing != null) {
\r
666 RemoverUtil.remove(graph, existing);
\r
668 Layer0Utils.copyTo(graph, library, type);
\r
673 public static void deleteIndex(WriteGraph graph, Resource model) throws DatabaseException {
\r
674 Layer0X L0X = Layer0X.getInstance(graph);
\r
675 IndexedRelations ir = graph.getService(IndexedRelations.class);
\r
676 // Deletes index files
\r
677 ir.reset(null, graph, L0X.DependenciesRelation, model);
\r
680 public static void disableDependencies(WriteGraph graph, Resource dummy) {
\r
681 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
\r
684 public static void releaseMemory(WriteGraph graph) {
\r
686 QueryControl qc = graph.getService(QueryControl.class);
\r
687 ClusterControl cc = graph.getService(ClusterControl.class);
\r
688 cc.flushClusters();
\r
693 public static List<Resource> filterByIndexRoot(ReadGraph graph, Resource indexRoot, List<Resource> resources) throws DatabaseException {
\r
694 ArrayList<Resource> result = new ArrayList<Resource>(resources.size());
\r
695 for (Resource r : resources) {
\r
696 Resource root = graph.syncRequest(new PossibleIndexRoot(r));
\r
697 if (indexRoot.equals(root))
\r
703 public static List<Resource> searchByTypeShallow(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
\r
704 return filterByIndexRoot(graph, model, searchByType(graph, model, type));
\r
707 public static List<Resource> searchByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
\r
708 Instances query = graph.adapt(type, Instances.class);
\r
709 return Layer0Utils.sortByCluster(graph, query.find(graph, model));
\r
712 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, GUID guid) throws DatabaseException {
\r
713 return searchByGUID(graph, indexRoot, guid.indexString());
\r
716 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, String indexString) throws DatabaseException {
\r
717 return searchByQueryShallow(graph, indexRoot, "GUID:" + indexString);
\r
720 public static List<Resource> searchByQueryShallow(ReadGraph graph, Resource model, String query) throws DatabaseException {
\r
721 return filterByIndexRoot(graph, model, searchByQuery(graph, model, query));
\r
724 public static List<Resource> searchByQuery(ReadGraph graph, Resource model, String query) throws DatabaseException {
\r
725 Instances instances = graph.adapt(Layer0.getInstance(graph).Entity, Instances.class);
\r
726 Collection<Resource> queryResult = instances.find(graph, model, query);
\r
727 return Layer0Utils.sortByCluster(graph, queryResult);
\r
730 public static List<Resource> searchByTypeAndFilter(ReadGraph graph, Resource model, Resource type, Function1<Resource,Boolean> filter) throws DatabaseException {
\r
731 Instances query = graph.adapt(type, Instances.class);
\r
732 ArrayList<Resource> result = new ArrayList<Resource>();
\r
733 for(Resource r : query.find(graph, model)) {
\r
734 if(filter.apply(r))
\r
740 public static List<Triple<Resource, Resource, String>> getIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
\r
741 Layer0 L0 = Layer0.getInstance(graph);
\r
742 List<Resource> entries = searchByQuery(graph, model, filter);
\r
743 List<Triple<Resource, Resource, String>> listOfTriples = new ArrayList<Triple<Resource,Resource,String>>();
\r
744 for (Resource entry : entries) {
\r
745 Resource type = graph.getPossibleObject(entry, L0.InstanceOf);
\r
746 String name = NameUtils.getSafeName(graph, entry);
\r
747 listOfTriples.add(new Triple<Resource, Resource, String>(entry, type, name));
\r
749 return listOfTriples;
\r
752 public static String listIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
\r
753 List<Triple<Resource, Resource, String>> listOfTriples = getIndexEntries(graph, model, filter);
\r
754 StringBuilder sb = new StringBuilder();
\r
755 sb.append("== LISTING INDEX ENTRIES OF INDEX: " + NameUtils.getSafeName(graph, model) + ". AMOUNT OF ENTRIES: " + listOfTriples.size() + " ==\n");
\r
756 for (Triple<Resource, Resource, String> entry : listOfTriples) {
\r
757 String instanceOf = NameUtils.getSafeName(graph, entry.second);
\r
758 sb.append("Name: " + entry.third + " instanceOf: " + instanceOf + " Resource: " + entry.first.toString() + "\n");
\r
760 return sb.toString();
\r
763 public static List<Resource> searchByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
\r
764 Instances query = graph.adapt(type, Instances.class);
\r
765 ArrayList<Resource> result = new ArrayList<Resource>();
\r
766 for(Resource r : query.findByName(graph, model, name)) {
\r
767 if(graph.isInstanceOf(r, type))
\r
773 public static List<Resource> searchByTypeAndNameShallow(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
\r
774 return filterByIndexRoot(graph, model, searchByTypeAndName(graph, model, type, name));
\r
779 * database write access
\r
780 * @param sourceContainer
\r
781 * the source container to look for annotationProperty from to be
\r
782 * used as the copy source
\r
783 * @param targetContainer
\r
784 * the target container for the copied annotationProperty
\r
786 * @param annotationProperty
\r
787 * the annotation property relation
\r
788 * @return created copy of the original annotation or <code>null</code> if
\r
789 * there was nothing to copy
\r
790 * @throws DatabaseException
\r
792 public static Resource copyPossibleAnnotation(WriteGraph graph, Resource sourceContainer, Resource targetContainer, Resource annotationProperty) throws DatabaseException {
\r
793 return copyPossibleAnnotation2(graph, sourceContainer, targetContainer, annotationProperty, null);
\r
796 public static List<String> getPossibleNamePath(ReadGraph graph, Resource resource) throws DatabaseException {
\r
797 return getPossibleNamePath(graph, resource, null);
\r
800 private static List<String> getPossibleNamePath(ReadGraph graph, Resource resource, List<String> result) throws DatabaseException {
\r
802 Layer0 L0 = Layer0.getInstance(graph);
\r
803 String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
\r
804 if(name == null) return null;
\r
806 if(result == null) result = new ArrayList<String>();
\r
808 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
809 if(graph.isInstanceOf(resource, SIMU.Model)) return result;
\r
811 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
\r
812 if(parent != null) {
\r
813 getPossibleNamePath(graph, parent, result);
\r
824 public static Resource claimLibraryPath(WriteGraph graph, Resource resource, List<String> path) throws DatabaseException {
\r
825 Layer0 L0 = Layer0.getInstance(graph);
\r
826 for(int i=0;i<path.size()-1;i++) {
\r
827 String p = path.get(i);
\r
828 Resource child = Layer0Utils.getPossibleChild(graph, resource, p);
\r
829 if(child == null) {
\r
830 child = graph.newResource();
\r
831 graph.claim(child, L0.InstanceOf, L0.Library);
\r
832 graph.addLiteral(child, L0.HasName, L0.NameOf, L0.String, p, Bindings.STRING);
\r
833 graph.claim(resource, L0.ConsistsOf, L0.PartOf, child);
\r
842 * database write access
\r
843 * @param sourceContainer
\r
844 * the source container to look for annotationProperty from to be
\r
845 * used as the copy source
\r
846 * @param targetContainer
\r
847 * the target container for the copied annotationProperty
\r
849 * @param annotationProperty
\r
850 * the annotation property relation
\r
851 * @param annotationProperty
\r
852 * the 2nd level annotation property relation or
\r
853 * <code>null</code> if no 2nd level annotation exists
\r
854 * @return created copy of the original annotation or <code>null</code> if
\r
855 * there was nothing to copy
\r
856 * @throws DatabaseException
\r
858 public static Resource copyPossibleAnnotation2(WriteGraph graph,
\r
859 Resource sourceContainer, Resource targetContainer,
\r
860 Resource annotationProperty, Resource entryType) throws DatabaseException {
\r
862 // Delete existing target value first
\r
863 Layer0 L0 = Layer0.getInstance(graph);
\r
864 Resource targetValue = graph.getPossibleObject(targetContainer, annotationProperty);
\r
865 if (targetValue != null) {
\r
866 if(!graph.hasStatement(targetValue, L0.PartOf))
\r
867 RemoverUtil.remove(graph, targetValue);
\r
868 graph.deny(targetContainer, annotationProperty);
\r
871 Resource sourceValue = graph.getPossibleObject(sourceContainer, annotationProperty);
\r
872 if (sourceValue == null)
\r
875 List<String> sourceValuePath = getPossibleNamePath(graph, sourceValue);
\r
876 if(sourceValuePath != null) {
\r
877 Resource targetModel = graph.sync(new PossibleModel(targetContainer));
\r
878 if(targetModel == null) throw new DatabaseException("No target model found for " + targetContainer);
\r
879 Resource library = claimLibraryPath(graph, targetModel, sourceValuePath);
\r
880 Resource existing = Layer0Utils.getPossibleChild(graph, library, sourceValuePath.get(sourceValuePath.size()-1));
\r
881 if(existing == null) {
\r
882 existing = doCopyPossibleAnnotation2(graph, sourceContainer, targetContainer, annotationProperty, entryType);
\r
883 graph.claim(library, L0.ConsistsOf, L0.PartOf, existing);
\r
885 graph.claim(targetContainer, annotationProperty, existing);
\r
888 return doCopyPossibleAnnotation2(graph, sourceContainer, targetContainer, annotationProperty, entryType);
\r
893 private static Resource doCopyPossibleAnnotation2(WriteGraph graph,
\r
894 Resource sourceContainer, Resource targetContainer,
\r
895 Resource annotationProperty, Resource entryType) throws DatabaseException {
\r
897 Resource sourceValue = graph.getPossibleObject(sourceContainer, annotationProperty);
\r
898 if (sourceValue == null)
\r
901 // Copy 1st level annotation
\r
902 Resource targetValue = createAnnotation(graph, targetContainer, annotationProperty, sourceValue);
\r
904 // Copy possible 2nd level annotations and attach to 1st if entry
\r
905 // property is defined.
\r
906 Layer0 L0 = Layer0.getInstance(graph);
\r
907 if (entryType != null) {
\r
909 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
\r
910 for (Resource entry : graph.getObjects(sourceValue, ANNO.Annotation_HasEntry)) {
\r
912 String name = graph.getRelatedValue(entry, L0.HasName, Bindings.STRING);
\r
914 List<String> entryPath = getPossibleNamePath(graph, entry);
\r
915 if(entryPath != null) {
\r
916 Resource targetModel = graph.sync(new PossibleModel(targetContainer));
\r
917 if(targetModel == null) throw new DatabaseException("No target model found for " + targetContainer);
\r
918 Resource library = claimLibraryPath(graph, targetModel, entryPath);
\r
919 Resource existing = Layer0Utils.getPossibleChild(graph, library, entryPath.get(entryPath.size()-1));
\r
920 if(existing == null) {
\r
921 existing = createTypedAnnotation(graph, null, null, entry, entryType, name);
\r
922 graph.claim(library, L0.ConsistsOf, L0.PartOf, existing);
\r
924 graph.claim(targetValue, ANNO.Annotation_HasEntry, existing);
\r
926 Resource result = createTypedAnnotation(graph, null, null, entry, entryType, name);
\r
927 graph.claim(targetValue, ANNO.Annotation_HasEntry, result);
\r
933 return targetValue;
\r
939 * database write access
\r
941 * the container resource to attach the new annotation to
\r
943 * the annotation property relation. The type of the created
\r
944 * annotation is decided from this property relation's range
\r
945 * @param sourceAnnotation
\r
946 * the annotation to copy data from or <code>null</code> if
\r
947 * nothing shall be copied
\r
948 * @return the newly created annotation resource or <code>null</code> if the
\r
949 * annotation property relation didn't have a single range type
\r
950 * @throws DatabaseException
\r
952 public static Resource createAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation) throws DatabaseException {
\r
953 return createAnnotation(graph, container, property, sourceAnnotation, null);
\r
958 * database write access
\r
960 * the container resource to attach the new annotation to
\r
962 * the annotation property relation. The type of the created
\r
963 * annotation is decided from this property relation's range
\r
964 * @param sourceAnnotation
\r
965 * the annotation to copy data from or <code>null</code> if
\r
966 * nothing shall be copied
\r
968 * name for newly created annotation or <code>null</code> to copy
\r
969 * name from source if available
\r
970 * @return the newly created annotation resource or <code>null</code> if the
\r
971 * annotation property relation didn't have a single range type
\r
972 * @throws DatabaseException
\r
974 public static Resource createAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation, String name) throws DatabaseException {
\r
976 Layer0 L0 = Layer0.getInstance(graph);
\r
977 Resource annotationType = graph.getSingleObject(property, L0.HasRange);
\r
978 if (annotationType == null)
\r
981 return createTypedAnnotation(graph, container, property, sourceAnnotation, annotationType, name);
\r
987 * database write access
\r
989 * the container resource to attach the new annotation to
\r
991 * the annotation property relation
\r
992 * @param sourceAnnotation
\r
993 * the annotation to copy data from or <code>null</code> if
\r
994 * nothing shall be copied
\r
995 * @param annotationType
\r
996 * the type of the new annotation
\r
997 * @return the newly created annotation resource
\r
998 * @throws DatabaseException
\r
1000 public static Resource createTypedAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation, Resource annotationType) throws DatabaseException {
\r
1001 return createTypedAnnotation(graph, container, property, sourceAnnotation, annotationType, null);
\r
1006 * database write access
\r
1007 * @param container
\r
1008 * the container resource to attach the new annotation to
\r
1010 * the annotation property relation
\r
1011 * @param sourceAnnotation
\r
1012 * the annotation to copy data from or <code>null</code> if
\r
1013 * nothing shall be copied
\r
1014 * @param annotationType
\r
1015 * the type of the new annotation
\r
1017 * name for newly created annotation or <code>null</code> to copy
\r
1018 * name from source if available
\r
1019 * @return the newly created annotation resource
\r
1020 * @throws DatabaseException
\r
1022 public static Resource createTypedAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation, Resource annotationType, String name) throws DatabaseException {
\r
1024 Layer0 L0 = Layer0.getInstance(graph);
\r
1026 Resource anno = graph.newResource();
\r
1028 graph.claim(anno, L0.InstanceOf, null, annotationType);
\r
1031 graph.addLiteral(anno, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
\r
1033 if (sourceAnnotation != null) {
\r
1035 if (name == null) {
\r
1036 String sourceName = graph.getPossibleRelatedValue(sourceAnnotation, L0.HasName, Bindings.STRING);
\r
1037 if(sourceName != null) graph.addLiteral(anno, L0.HasName, L0.NameOf, L0.String, sourceName, Bindings.STRING);
\r
1040 Resource sourceType = graph.getSingleType(sourceAnnotation);
\r
1041 List<AnnotationMap> am = getAnnotationMap(graph, annotationType, sourceType);
\r
1042 for (AnnotationMap a : am) {
\r
1043 Resource object = graph.getSingleObject(sourceAnnotation, a.sourcePredicate);
\r
1044 Collection<Resource> objectTypes = graph.getTypes(object);
\r
1045 if (objectTypes.contains(L0.Literal)) {
\r
1046 Object value = graph.getValue(object, a.defaultValueBinding);
\r
1047 if (!ObjectUtils.objectEquals(value, a.defaultValue)) {
\r
1048 graph.addLiteral(anno, a.annotationPredicate, a.annotationPredicateInverse, a.defaultValueType, value, a.defaultValueBinding);
\r
1054 if (container != null && property != null) {
\r
1055 graph.claim(container, property, anno);
\r
1062 private static class AnnotationMap {
\r
1064 final public Resource sourcePredicate;
\r
1065 final public Resource sourcePredicateInverse;
\r
1067 final public Resource annotationPredicate;
\r
1068 final public Resource annotationPredicateInverse;
\r
1070 final public Resource defaultValueType;
\r
1071 final public Object defaultValue;
\r
1072 final public Binding defaultValueBinding;
\r
1074 public AnnotationMap(Resource sp, Resource spi, Resource tp, Resource tpi, Resource defaultValueType, Object defaultValue, Binding defaultValueBinding) {
\r
1075 sourcePredicate = sp;
\r
1076 sourcePredicateInverse = spi;
\r
1077 annotationPredicate = tp;
\r
1078 annotationPredicateInverse = tpi;
\r
1079 this.defaultValueType = defaultValueType;
\r
1080 this.defaultValue = defaultValue;
\r
1081 this.defaultValueBinding = defaultValueBinding;
\r
1086 public static List<AnnotationMap> getAnnotationMap(ReadGraph graph, Resource annotationType, Resource sourceType) throws DatabaseException {
\r
1087 return graph.syncRequest(new AnnotationMapRequest(annotationType, sourceType), TransientCacheListener.<List<AnnotationMap>>instance());
\r
1090 static class AnnotationMapRequest extends ResourceRead2<List<AnnotationMap>> {
\r
1092 public AnnotationMapRequest(Resource annotationType, Resource sourceType) {
\r
1093 super(annotationType, sourceType);
\r
1097 public List<AnnotationMap> perform(ReadGraph graph) throws DatabaseException {
\r
1099 Layer0 L0 = Layer0.getInstance(graph);
\r
1101 ArrayList<AnnotationMap> result = new ArrayList<AnnotationMap>();
\r
1103 Map<String, Resource> annotationPredicates = new HashMap<String, Resource>();
\r
1104 for(Resource predicate : graph.getObjects(resource, L0.DomainOf)) {
\r
1105 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
\r
1106 annotationPredicates.put(name, predicate);
\r
1108 Map<String, Resource> sourcePredicates = new HashMap<String, Resource>();
\r
1109 for(Resource predicate : graph.getObjects(resource2, L0.DomainOf)) {
\r
1110 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
\r
1111 sourcePredicates.put(name, predicate);
\r
1114 for(String key : sourcePredicates.keySet()) {
\r
1115 Resource sourcePredicate = sourcePredicates.get(key);
\r
1116 Resource anno = annotationPredicates.get(key);
\r
1117 if(sourcePredicate != null && anno != null) {
\r
1118 Resource defaultValueType = graph.getSingleObject(anno, L0.HasRange);
\r
1119 Binding defaultValueBinding = null;
\r
1120 Object defaultValue = null;
\r
1121 Resource assertion = graph.getPossibleObject(anno, L0.HasPredicateInverse);
\r
1122 if (assertion != null) {
\r
1123 Resource object = graph.getPossibleObject(assertion, L0.HasObject);
\r
1124 if (object != null) {
\r
1125 if(graph.isInstanceOf(object, L0.Literal)) {
\r
1126 Datatype dt = graph.getDataType(object);
\r
1127 defaultValueBinding = Bindings.getBeanBinding(dt);
\r
1128 defaultValue = graph.getPossibleValue(object);
\r
1132 result.add(new AnnotationMap(
\r
1133 sourcePredicate, graph.getInverse(sourcePredicate),
\r
1134 anno, graph.getInverse(anno),
\r
1135 defaultValueType, defaultValue, defaultValueBinding));
\r
1145 public static final String DRAWING_TEMPLATE_FORMAT = "drawingTemplate";
\r
1146 public static final String DRAWING_TEMPLATE_FORMAT_V1 = DRAWING_TEMPLATE_FORMAT + ":1";
\r
1147 public static final String DRAWING_TEMPLATE_FORMAT_V2 = DRAWING_TEMPLATE_FORMAT + ":2";
\r
1149 public static Resource importDrawingTemplate(final Resource model, final File file) throws IOException, SerializationException, DatabaseException, TransferableGraphException {
\r
1153 final Resource library = Simantics.sync(new WriteResultRequest<Resource>() {
\r
1156 public Resource perform(WriteGraph graph) throws DatabaseException {
\r
1158 Layer0 L0 = Layer0.getInstance(graph);
\r
1159 Template2dResource TEMPLATE = Template2dResource.getInstance(graph);
\r
1160 Resource root = graph.getPossibleObject(model, TEMPLATE.HasDrawingTemplateRoot);
\r
1161 if(root == null) {
\r
1162 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
\r
1163 root = graph.newResource();
\r
1164 graph.claim(root, L0.InstanceOf, null, TEMPLATE2D.DrawingTemplateLibrary);
\r
1165 graph.claim(root, L0.InstanceOf, null, TEMPLATE2D.DrawingTemplateLibraryUI);
\r
1166 graph.claimLiteral(root, L0.HasName, L0.NameOf, L0.String, "Diagram Templates", Bindings.STRING);
\r
1167 graph.claim(root, L0.PartOf, L0.ConsistsOf, model);
\r
1168 graph.claim(model, TEMPLATE2D.HasDrawingTemplateRoot, root);
\r
1171 String name = new Path(file.getAbsolutePath()).removeFileExtension().lastSegment();
\r
1172 if(name != null) {
\r
1173 Resource existing = Layer0Utils.getPossibleChild(graph, root, name);
\r
1174 if(existing != null)
\r
1175 graph.deny(root, L0.ConsistsOf, existing);
\r
1184 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(library);
\r
1187 HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();
\r
1188 FormatHandler<Object> handler = new FormatHandler<Object>() {
\r
1190 public Binding getBinding() {
\r
1191 return TransferableGraph1.BINDING;
\r
1195 public Object process(DataContainer container) throws Exception {
\r
1196 TransferableGraphs.importGraph1(Simantics.getSession(), (TransferableGraph1)container.content.getValue(),
\r
1201 handlers.put(DRAWING_TEMPLATE_FORMAT_V1, handler);
\r
1202 handlers.put(DRAWING_TEMPLATE_FORMAT_V2, handler);
\r
1204 DataContainers.readFile(file, handlers);
\r
1205 } catch(DataFormatException e) {
\r
1206 throw new IOException(e);
\r
1207 } catch(IOException e) {
\r
1209 } catch(Exception e) {
\r
1210 if(e instanceof RuntimeException)
\r
1211 throw (RuntimeException)e;
\r
1213 throw new RuntimeException(e);
\r
1216 } catch(IOException e) {
\r
1219 return advisor.getRoot();
\r
1221 } catch (Throwable t) {
\r
1222 Logger.defaultLogError("Unexpected exception while importing diagram template.", t);
\r
1230 public static Collection<Variable> getMappedVariables(ReadGraph graph, Variable source) throws DatabaseException {
\r
1232 ArrayList<Variable> result = new ArrayList<Variable>();
\r
1233 Resource represents = source.getPossibleRepresents(graph);
\r
1234 if(represents == null) return Collections.emptyList();
\r
1235 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
1236 for(Resource r : getElementCorrespondendences(graph, represents)) {
\r
1237 result.add(Variables.getVariable(graph, r));
\r
1239 for(Resource r : graph.getObjects(represents, MOD.ComponentToElement)) {
\r
1240 result.add(Variables.getVariable(graph, r));
\r
1242 for(Resource r : graph.getObjects(represents, MOD.DiagramToComposite)) {
\r
1243 result.add(Variables.getVariable(graph, r));
\r
1245 for(Resource r : graph.getObjects(represents, MOD.CompositeToDiagram)) {
\r
1246 result.add(Variables.getVariable(graph, r));
\r
1252 public static Resource getPossibleModel(ReadGraph graph, Resource resource) throws DatabaseException {
\r
1254 PossibleModel pm = new PossibleModel(resource);
\r
1255 Resource model = pm.perform(graph);
\r
1259 public static Resource possibleIndexRoot(ReadGraph graph, Resource resource) throws DatabaseException {
\r
1260 return graph.syncRequest(new PossibleIndexRoot(resource));
\r
1263 public static Object getMonitorValue(StructuralComponent _variable, ReadGraph graph, String path) throws DatabaseException {
\r
1264 Variable variable = ((VariableStructuralContext)_variable).variable;
\r
1265 Variable var = variable.browse(graph, path);
\r
1266 return var.getValue(graph);
\r
1269 public static void createSharedOntologyWithUI(ReadGraph graph, Variable variable, Resource baseType) throws DatabaseException {
\r
1270 createSharedOntologyWithUI(graph, baseType);
\r
1273 public static void createSharedOntologyWithUI(ReadGraph graph, Resource baseType) throws DatabaseException {
\r
1275 // Resource indexRoot_ = variable.getPossibleRepresents(graph);
\r
1276 // if(indexRoot_ == null) return;
\r
1278 final Map<Resource, Pair<String,ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();
\r
1280 Layer0 L0 = Layer0.getInstance(graph);
\r
1281 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
1282 Instances query = graph.adapt(L0.IndexRootType, Instances.class);
\r
1283 for(Resource ontology : Layer0Utils.listOntologies(graph)) {
\r
1284 for(Resource type : query.find(graph, ontology)) {
\r
1285 if(graph.isInheritedFrom(type, SIMU.Model)) continue;
\r
1286 if(graph.hasStatement(type, L0.Abstract)) continue;
\r
1287 if(!graph.isInheritedFrom(type, baseType)) continue;
\r
1288 String name = graph.getPossibleRelatedValue(type, L0.HasLabel, Bindings.STRING);
\r
1289 if(name == null) name = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
\r
1290 map.put(type, new Pair<String,ImageDescriptor>(name, null));
\r
1294 Display.getDefault().asyncExec(new Runnable() {
\r
1296 public void run() {
\r
1297 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
1298 CreateSharedOntologyDialog page = new CreateSharedOntologyDialog(shell, map, "Select type and name of new shared library");
\r
1299 if (page.open() == Window.OK) {
\r
1300 Object[] result = page.getResult();
\r
1301 if (result != null && result.length == 1) {
\r
1302 final Resource res = (Resource)result[0];
\r
1303 final String name = "http://" + page.getName();
\r
1304 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
1307 public void perform(WriteGraph graph) throws DatabaseException {
\r
1308 graph.markUndoPoint();
\r
1309 Resource target = Simantics.applySCL("Simantics/SharedOntologies", "createSharedOntology", graph, name+"@A", res);
\r
1311 ProjectResource PROJ = ProjectResource.getInstance(graph);
\r
1312 Layer0 L0 = Layer0.getInstance(graph);
\r
1313 for(Resource dep : graph.getObjects(Simantics.getProjectResource(), L0.IsLinkedTo)) {
\r
1314 if(graph.isInstanceOf(dep, PROJ.NamespaceRequirement)) {
\r
1315 for(Resource req : graph.getObjects(dep, PROJ.RequiresNamespace)) {
\r
1316 String uri = graph.getPossibleValue(req, Bindings.STRING);
\r
1318 Resource ns = graph.getResource(uri);
\r
1320 graph.claim(target, L0.IsLinkedTo, null, ns);
\r
1337 public static void unlinkSharedOntologyWithUI(ReadGraph graph, Variable variable, final List<Resource> libraries) throws DatabaseException {
\r
1339 final Resource indexRoot = variable.getPossibleRepresents(graph);
\r
1340 if(indexRoot == null) return;
\r
1342 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
1344 final List<String> instances = new ArrayList<String>();
\r
1346 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
1348 for(Resource library : libraries) {
\r
1349 for(Resource type : ModelingUtils.searchByTypeShallow(graph, library, STR.ComponentType)) {
\r
1350 for(Resource instance : ModelingUtils.searchByTypeShallow(graph, indexRoot, type)) {
\r
1352 if(graph.isInstanceOf(instance, DIA.Element)) continue;
\r
1353 String name = Versions.getStandardPathNameString(graph, instance);
\r
1354 instances.add(name);
\r
1359 if(instances.isEmpty()) {
\r
1360 graph.getSession().asyncRequest(new WriteRequest() {
\r
1363 public void perform(WriteGraph graph) throws DatabaseException {
\r
1364 Layer0 L0 = Layer0.getInstance(graph);
\r
1365 for(Resource library : libraries)
\r
1366 graph.deny(indexRoot, L0.IsLinkedTo, library);
\r
1373 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
1375 public void run() {
\r
1377 if (!PlatformUI.isWorkbenchRunning())
\r
1380 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
1381 ListDialog<String> dialog = new ListDialog<String>(
\r
1384 "Cannot unlink selected libraries",
\r
1385 "Libraries cannot be unlinked since the following instances are referring to them.") {
\r
1387 protected void createButtonsForButtonBar(Composite parent) {
\r
1388 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
\r
1392 int result = dialog.open();
\r
1393 if (result != Dialog.OK)
\r
1401 public static void importSharedOntology(String fileName) throws Exception {
\r
1403 DataContainer dc = DataContainers.readFile(new File(fileName));
\r
1404 TransferableGraph1 tg = (TransferableGraph1)dc.content.getValue(TransferableGraph1.BINDING);
\r
1405 Variant draftStatus = dc.metadata.get(DraftStatusBean.EXTENSION_KEY);
\r
1406 MigrationUtils.importSharedOntology(Simantics.getSession(), tg, draftStatus == null);
\r
1407 } catch (Exception e) {
\r
1408 Logger.defaultLogError(e);
\r
1413 public static void importSharedOntologyWithUI(ReadGraph graph, final Variable variable) throws DatabaseException {
\r
1415 Display.getDefault().asyncExec(new Runnable() {
\r
1418 public void run() {
\r
1419 IStructuredSelection sel = new StructuredSelection(variable);
\r
1420 openWizard(Display.getCurrent(), sel, "org.simantics.modeling.ui.sharedOntologyImportWizard");
\r
1427 public static class LibraryInfo implements Comparable<LibraryInfo> {
\r
1428 public NamedResource library;
\r
1429 public DraftStatusBean draft;
\r
1430 public LibraryInfo(String name, Resource r, DraftStatusBean draft) {
\r
1431 library = new NamedResource(name, r);
\r
1432 this.draft = draft;
\r
1435 public int compareTo(LibraryInfo o) {
\r
1436 return library.compareTo(o.library);
\r
1439 public int hashCode() {
\r
1440 return library.hashCode();
\r
1443 public boolean equals(Object object) {
\r
1444 if (this == object)
\r
1446 else if (object == null)
\r
1448 else if (!(object instanceof LibraryInfo))
\r
1450 LibraryInfo info = (LibraryInfo)object;
\r
1451 return info.library.equals(library);
\r
1456 public static void exportSharedOntologyWithUI(final Resource sharedOntology) {
\r
1458 Display.getDefault().asyncExec(new Runnable() {
\r
1461 public void run() {
\r
1462 HintContext hc = new HintContext();
\r
1463 hc.setHint(SelectionHints.KEY_MAIN, sharedOntology);
\r
1464 IStructuredSelection sel = new StructuredSelection(hc);
\r
1465 openWizard(Display.getCurrent(), sel, "org.simantics.modeling.ui.sharedOntologyExportWizard");
\r
1472 public static void exportSharedOntology(IProgressMonitor monitor, RequestProcessor processor, File location, String format, int version, final LibraryInfo info) throws DatabaseException, IOException {
\r
1474 if(monitor == null) monitor = new NullProgressMonitor();
\r
1476 // TODO: figure out a way to make the TG go directly into a file
\r
1477 // instead of having it all in memory at once.
\r
1479 monitor.beginTask("Exporting shared library...", 100);
\r
1480 SimanticsClipboard clipboard = processor.syncRequest(new Read<SimanticsClipboard>() {
\r
1482 public SimanticsClipboard perform(ReadGraph graph) throws DatabaseException {
\r
1483 CopyHandler ch = graph.adapt(info.library.getResource(), CopyHandler.class);
\r
1484 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
\r
1485 ch.copyToClipboard(graph, clipboard);
\r
1490 TreeMap<String,Variant> metadata = getExportMetadata();
\r
1491 DraftStatusBean draft = info.draft;
\r
1492 if(draft != null) {
\r
1493 metadata.put(DraftStatusBean.EXTENSION_KEY, new Variant(DraftStatusBean.BINDING ,draft));
\r
1496 for (Set<Representation> object : clipboard.getContents()) {
\r
1498 TransferableGraph1 tg = ClipboardUtils.accept(processor, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
\r
1499 monitor.worked(95);
\r
1501 monitor.setTaskName("Writing transferable graph...");
\r
1502 DataContainers.writeFile(location, new DataContainer(
\r
1504 metadata, new Variant(TransferableGraph1.BINDING, tg)));
\r
1506 monitor.worked(5);
\r
1510 public static TreeMap<String, Variant> getExportMetadata() {
\r
1512 TreeMap<String,Variant> metadata = new TreeMap<String,Variant>();
\r
1513 metadata.put("date", Variant.ofInstance(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date())));
\r
1514 metadata.put("author", Variant.ofInstance(System.getProperty("user.name", "")));
\r
1520 public static void createNewVersionWithoutUI(WriteGraph graph, Resource resource) throws DatabaseException {
\r
1521 VersionInfo info = graph.syncRequest(new VersionInfoRequest(resource));
\r
1522 int currentVersion = Integer.parseInt(info.version);
\r
1523 String result = Integer.toString(currentVersion + 1);
\r
1524 createNewVersion(graph, resource, info, result);
\r
1527 public static void createNewVersionWithUI(ReadGraph graph, final Resource resource) throws DatabaseException {
\r
1529 final VersionInfo info = graph.syncRequest(new VersionInfoRequest(resource));
\r
1531 Display.getDefault().asyncExec(new Runnable() {
\r
1533 public void run() {
\r
1534 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
1535 CreateVersionDialog dialog = new CreateVersionDialog(shell, info);
\r
1536 if (dialog.open() == Window.OK) {
\r
1537 final String result = dialog.getResult();
\r
1538 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
1540 public void perform(WriteGraph graph) throws DatabaseException {
\r
1541 createNewVersion(graph, resource, info, result);
\r
1550 public static void createNewVersion(WriteGraph graph, Resource resource, final VersionInfo info, final String result) throws DatabaseException {
\r
1551 graph.markUndoPoint();
\r
1552 Layer0 L0 = Layer0.getInstance(graph);
\r
1553 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
\r
1554 if(parent == null) return;
\r
1555 final String parentURI = graph.getPossibleURI(parent);
\r
1556 if(parentURI == null) return;
\r
1557 Layer0Utils.copyTo(graph, parent, resource, new PasteEventHandler() {
\r
1560 public void postProcess(WriteGraph graph, Resource root) throws DatabaseException {
\r
1561 Layer0 L0 = Layer0.getInstance(graph);
\r
1562 graph.deny(root, L0.Entity_published);
\r
1566 public IImportAdvisor2 createAdvisor(ReadGraph graph, ImportAdvisorFactory factory, Resource target) throws DatabaseException {
\r
1567 Map<String,Object> context = new HashMap<String,Object>();
\r
1568 String base = parentURI + "/" + URIStringUtils.escape( info.baseName ) + "@";
\r
1569 Map<String,String> renameMap = new HashMap<String,String>();
\r
1570 renameMap.put(base + info.version, base + result);
\r
1571 renameMap.put(info.baseName + "@" + info.version, info.baseName + "@" + result);
\r
1572 context.put(ImportAdvisors.RENAME_MAP, renameMap);
\r
1573 return factory.create(graph, target, context);
\r
1577 Layer0Utils.addCommentMetadata(graph, "Created new version of " + info.baseName);
\r
1580 public static boolean isUserComponent(ReadGraph graph, Resource type) throws DatabaseException {
\r
1581 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
1582 if(graph.isInstanceOf(type, STR.ProceduralComponentType)) return true;
\r
1583 else if (graph.hasStatement(type, STR.IsDefinedBy)) return true;
\r
1587 public static void publishComponentTypeWithUI(WriteGraph graph, final Resource componentType) throws DatabaseException {
\r
1589 Layer0 L0 = Layer0.getInstance(graph);
\r
1590 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
1591 Resource composite = graph.getPossibleObject(componentType, STR.IsDefinedBy);
\r
1592 final List<String> instances = new ArrayList<String>();
\r
1593 if(composite != null) {
\r
1594 for(Resource component : graph.syncRequest(new ObjectsWithType(composite, L0.ConsistsOf, STR.Component))) {
\r
1595 Resource type = graph.getPossibleType(component, STR.Component);
\r
1596 if(type != null && isUserComponent(graph, type)) {
\r
1597 if(!Layer0Utils.isPublished(graph, type)) instances.add(Versions.getStandardPathNameString(graph, component));
\r
1602 if(instances.isEmpty()) {
\r
1603 graph.getSession().asyncRequest(new WriteRequest() {
\r
1606 public void perform(WriteGraph graph) throws DatabaseException {
\r
1607 graph.markUndoPoint();
\r
1608 publish(graph, componentType);
\r
1615 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
1617 public void run() {
\r
1619 if (!PlatformUI.isWorkbenchRunning())
\r
1622 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
1623 ListDialog<String> dialog = new ListDialog<String>(
\r
1626 "Cannot publish user component",
\r
1627 "The following instances are referring to unpublished user components.") {
\r
1629 protected void createButtonsForButtonBar(Composite parent) {
\r
1630 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
\r
1634 int result = dialog.open();
\r
1635 if (result != Dialog.OK)
\r
1643 public static void publishSharedOntologyWithUI(WriteGraph graph, final Resource sharedOntology) throws DatabaseException {
\r
1645 Layer0 L0 = Layer0.getInstance(graph);
\r
1646 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
1647 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
1648 final List<String> instances = new ArrayList<String>();
\r
1649 for(Resource type : searchByTypeShallow(graph, sharedOntology, STR.ComponentType)) {
\r
1651 if(graph.isInheritedFrom(type, DIA.Element)) continue;
\r
1652 if(!Layer0Utils.isPublished(graph, type)) instances.add(Versions.getStandardPathNameString(graph, type));
\r
1654 for(Resource dep : graph.syncRequest(new ObjectsWithType(sharedOntology, L0.IsLinkedTo, L0.SharedOntology))) {
\r
1655 if(!Layer0Utils.isPublished(graph, dep)) instances.add(Versions.getStandardPathNameString(graph, dep));
\r
1658 if(instances.isEmpty()) {
\r
1659 graph.getSession().asyncRequest(new WriteRequest() {
\r
1662 public void perform(WriteGraph graph) throws DatabaseException {
\r
1663 graph.markUndoPoint();
\r
1664 publish(graph, sharedOntology);
\r
1671 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
1673 public void run() {
\r
1675 if (!PlatformUI.isWorkbenchRunning())
\r
1678 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
1679 ListDialog<String> dialog = new ListDialog<String>(
\r
1682 "Cannot publish shared library",
\r
1683 "The following dependencies are unpublished.") {
\r
1685 protected void createButtonsForButtonBar(Composite parent) {
\r
1686 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
\r
1690 int result = dialog.open();
\r
1691 if (result != Dialog.OK)
\r
1700 public static void createSCLModuleDefault(WriteGraph graph, Resource target) throws DatabaseException {
\r
1701 String name = NameUtils.findFreshEscapedName(graph, "SCLModule", target);
\r
1702 createSCLModule(graph, target, name);
\r
1705 public static void createSCLModule(WriteGraph graph, Resource target, String name) throws DatabaseException {
\r
1706 graph.markUndoPoint();
\r
1707 Layer0 L0 = Layer0.getInstance(graph);
\r
1708 Resource sclModule = GraphUtils.create2(graph, L0.SCLModule,
\r
1710 L0.PartOf, target,
\r
1711 L0.SCLModule_definition, "");
\r
1712 Layer0Utils.addCommentMetadata(graph, "Created SCL Module " + name + " " + sclModule.toString());
\r
1715 public static void createPGraphDefault(WriteGraph graph, Resource target) throws DatabaseException {
\r
1716 String name = NameUtils.findFreshEscapedName(graph, "Ontology Definition File", target);
\r
1717 createPGraph(graph, target, name);
\r
1720 public static void createPGraph(WriteGraph graph, Resource target, String name) throws DatabaseException {
\r
1721 graph.markUndoPoint();
\r
1722 Layer0 L0 = Layer0.getInstance(graph);
\r
1723 Resource file = GraphUtils.create2(graph, L0.PGraph,
\r
1725 L0.PartOf, target,
\r
1726 L0.PGraph_definition, "");
\r
1727 Layer0Utils.addCommentMetadata(graph, "Created Ontology Definition File " + name + " " + file.toString());
\r
1730 public static void publish(WriteGraph graph, Resource target) throws DatabaseException {
\r
1731 Layer0 L0 = Layer0.getInstance(graph);
\r
1732 graph.claimLiteral(target, L0.Entity_published, true, Bindings.BOOLEAN);
\r
1733 Layer0Utils.addCommentMetadata(graph, "Published " + graph.getPossibleRelatedValue2(target, L0.HasName, Bindings.STRING) + " " + target.toString());
\r
1736 public static boolean isLinkedToDeep(ReadGraph graph, Resource r1, Resource r2) throws DatabaseException {
\r
1737 return graph.syncRequest(new IsLinkedTo(r1, r2));
\r
1740 public static void openWizard(Display display, IStructuredSelection selection, String id) {
\r
1741 // First see if this is a "new wizard".
\r
1742 IWizardDescriptor descriptor = PlatformUI.getWorkbench()
\r
1743 .getNewWizardRegistry().findWizard(id);
\r
1744 // If not check if it is an "import wizard".
\r
1745 if (descriptor == null) {
\r
1746 descriptor = PlatformUI.getWorkbench().getImportWizardRegistry()
\r
1749 // Or maybe an export wizard
\r
1750 if (descriptor == null) {
\r
1751 descriptor = PlatformUI.getWorkbench().getExportWizardRegistry()
\r
1755 // Then if we have a wizard, open it.
\r
1756 if (descriptor != null) {
\r
1757 IWorkbenchWizard wizard = descriptor.createWizard();
\r
1758 wizard.init(PlatformUI.getWorkbench(), selection);
\r
1759 WizardDialog wd = new WizardDialog(display.getActiveShell(), wizard);
\r
1760 wd.setTitle(wizard.getWindowTitle());
\r
1763 } catch (CoreException e) {
\r
1764 e.printStackTrace();
\r
1768 public static String withinEpsilon(double value, double reference, double epsilon) {
\r
1769 if(Math.abs(value-reference) < epsilon) return "True";
\r
1770 else return "Not within epsilon value=" + value + ", reference=" + reference + " , epsilon=" + epsilon;
\r
1773 public static boolean needsIdentifier(ReadGraph graph, Resource r) throws DatabaseException {
\r
1774 Layer0 L0 = Layer0.getInstance(graph);
\r
1775 Collection<Resource> types = graph.getPrincipalTypes(r);
\r
1776 for(Resource type : types)
\r
1777 if(graph.syncRequest(new IsInstanceOf(type, L0.TypeWithIdentifier), TransientCacheAsyncListener.<Boolean>instance()))
\r
1782 public static boolean needsModificationInfo(ReadGraph graph, Resource r) throws DatabaseException {
\r
1783 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
1784 Collection<Resource> types = graph.getPrincipalTypes(r);
\r
1785 for(Resource type : types)
\r
1786 if(graph.syncRequest(new IsInstanceOf(type, MOD.TypeWithChangeInformation), TransientCacheAsyncListener.<Boolean>instance()))
\r
1791 public static void attachCreationInformation(IProgressMonitor monitor, WriteGraph graph, Resource model) throws DatabaseException {
\r
1793 if(monitor == null) monitor = new NullProgressMonitor();
\r
1795 final String author = System.getProperty("user.name", "");
\r
1796 final long time = System.currentTimeMillis();
\r
1798 monitor.setTaskName("Attach creation information");
\r
1800 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
1801 Collection<Resource> rs = ModelingUtils.searchByType(graph, model, MOD.TypeWithChangeInformation);
\r
1802 Collection<Resource> supers = ModelingUtils.getMostUnspecificTypes(graph, rs);
\r
1804 CollectionSupport cs = graph.getService(CollectionSupport.class);
\r
1805 Collection<Resource> set = cs.createSet();
\r
1806 for(Resource type : supers) {
\r
1807 set.addAll(ModelingUtils.searchByTypeShallow(graph, model, type));
\r
1809 Collection<Resource> instances = Layer0Utils.sortByCluster(graph, set);
\r
1811 int pc = instances.size() / 100;
\r
1815 for(Resource instance : instances) {
\r
1816 ChangeInformation info = graph.getPossibleRelatedValue(instance, MOD.changeInformation, ChangeInformation.BINDING);
\r
1817 if(info == null) {
\r
1818 info = new ChangeInformation();
\r
1819 info.createdAt = time;
\r
1820 info.createdBy = author;
\r
1821 info.modifiedAt = time;
\r
1822 info.modifiedBy = author;
\r
1823 graph.claimLiteral(instance, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
\r
1828 Double d = (100.0*done)/instances.size();
\r
1829 monitor.setTaskName("Attach creation information " + d.intValue() + "%");
\r
1834 monitor.setTaskName("Attach creation information - commit");
\r
1838 public static class DiagramComponentInfo {
\r
1840 private static String CHILD_PREFIX = "child:";
\r
1842 final private String compositePathAndName;
\r
1843 final private String componentName;
\r
1844 final private GUID guid;
\r
1846 public DiagramComponentInfo(String compositePathAndName, String componentName, GUID guid) {
\r
1847 this.compositePathAndName = compositePathAndName;
\r
1848 this.componentName = componentName;
\r
1852 public static boolean isDiagramComponent(String tgName) {
\r
1853 return tgName.startsWith(CHILD_PREFIX);
\r
1856 public String getTGName(CompositeInfo info) {
\r
1857 return CHILD_PREFIX + info.getOriginalPath() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + info.getEscapedName() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + getEscapedComponentName() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + guid.indexString();
\r
1860 public boolean existsGUID(ReadGraph graph, Resource indexRoot) throws DatabaseException {
\r
1861 Collection<Resource> res = ModelingUtils.searchByGUID(graph, indexRoot, guid);
\r
1862 return !res.isEmpty();
\r
1865 public static DiagramComponentInfo fromResource(ReadGraph graph, CompositeInfo info, Resource resource) throws DatabaseException {
\r
1866 Layer0 L0 = Layer0.getInstance(graph);
\r
1867 GUID childId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
\r
1868 String childName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
\r
1869 return new DiagramComponentInfo(info.getStateKey(), URIStringUtils.escape(childName), childId);
\r
1872 public GUID getGUID() {
\r
1876 public String getEscapedCompositePathAndName() {
\r
1877 return compositePathAndName;
\r
1880 public String getEscapedComponentName() {
\r
1881 return componentName;
\r
1884 public String getUnescapedComponentName() {
\r
1885 return URIStringUtils.unescape(getEscapedComponentName());
\r
1888 // "child:path#compositeName#componentName#guid"
\r
1889 public static DiagramComponentInfo parse(String tgName) {
\r
1891 String name = tgName.substring(CHILD_PREFIX.length());
\r
1892 String compositePathAndName = "";
\r
1893 String moduleName = name;
\r
1894 GUID guid = GUID.invalid();
\r
1895 int lastHash = name.lastIndexOf(ModelingUtils.COMPOSITE_SEPARATOR_CHAR);
\r
1896 if(lastHash >= 0) {
\r
1897 String first = name.substring(0, lastHash);
\r
1898 String second = name.substring(lastHash+1);
\r
1899 lastHash = first.lastIndexOf(ModelingUtils.COMPOSITE_SEPARATOR_CHAR);
\r
1900 if(lastHash >= 0) {
\r
1901 compositePathAndName = first.substring(0, lastHash);
\r
1902 moduleName = first.substring(lastHash+1);
\r
1903 guid = GUID.parseIndexString(second);
\r
1905 compositePathAndName = first;
\r
1906 moduleName = second;
\r
1909 return new DiagramComponentInfo(compositePathAndName, moduleName, guid);
\r
1915 public int hashCode() {
\r
1916 final int prime = 31;
\r
1918 result = prime * result + ((compositePathAndName == null) ? 0 : compositePathAndName.hashCode());
\r
1919 result = prime * result + ((componentName == null) ? 0 : componentName.hashCode());
\r
1924 public boolean equals(Object obj) {
\r
1929 if (getClass() != obj.getClass())
\r
1931 DiagramComponentInfo other = (DiagramComponentInfo) obj;
\r
1932 if (compositePathAndName == null) {
\r
1933 if (other.compositePathAndName != null)
\r
1935 } else if (!compositePathAndName.equals(other.compositePathAndName))
\r
1937 if (componentName == null) {
\r
1938 if (other.componentName != null)
\r
1940 } else if (!componentName.equals(other.componentName))
\r
1947 public static class CompositeInfo {
\r
1949 public static String COMPOSITE_PREFIX = "composite:";
\r
1951 final private boolean useGuids;
\r
1952 final private boolean applyPaths;
\r
1953 final private String path;
\r
1954 final private String name;
\r
1955 final private GUID guid;
\r
1957 private CompositeInfo(boolean useGuids, boolean applyPaths, String path, String name, GUID guid) {
\r
1958 this.useGuids = useGuids;
\r
1959 this.applyPaths = applyPaths;
\r
1966 public String toString() {
\r
1967 return "CompositeInfo[useGuids=" + useGuids + ", applyPaths=" + applyPaths + ", path=" + path + ", name=" + name + ", guid=" + guid.indexString() + "]";
\r
1970 public static boolean isComposite(String tgName) {
\r
1971 return tgName.startsWith(COMPOSITE_PREFIX);
\r
1974 public String getTGName() {
\r
1975 return COMPOSITE_PREFIX + getOriginalPath() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + getEscapedName() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + guid.indexString();
\r
1978 public boolean existsGUID(ReadGraph graph, Resource indexRoot) throws DatabaseException {
\r
1979 Collection<Resource> res = ModelingUtils.searchByGUID(graph, indexRoot, guid);
\r
1980 return !res.isEmpty();
\r
1983 public static CompositeInfo fromResource(ReadGraph graph, Resource resource) throws DatabaseException {
\r
1985 Layer0 L0 = Layer0.getInstance(graph);
\r
1986 GUID rootId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
\r
1987 String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
\r
1988 String escapedRootName = URIStringUtils.escape(rootName);
\r
1989 String escapedPath = ModelingUtils.getDiagramCompositePath(graph, resource);
\r
1990 return new CompositeInfo(true, true, escapedPath, escapedRootName, rootId);
\r
1994 public static CompositeInfo parse(String tgName) {
\r
1995 return parse(tgName, true, true);
\r
1999 * Index 0 is root-relative folder path separated with '/' or null if target-relative positioning is used
\r
2000 * Index 1 is diagram name
\r
2002 public static CompositeInfo parse(String tgName, boolean applyPaths, boolean useGuids) {
\r
2003 if(!tgName.startsWith(COMPOSITE_PREFIX)) return null;
\r
2004 tgName = tgName.substring(COMPOSITE_PREFIX.length());
\r
2005 if(!tgName.contains(COMPOSITE_SEPARATOR)) {
\r
2006 if(useGuids) throw new IllegalStateException("GUID identifiers were not found for diagrams.");
\r
2007 return new CompositeInfo(useGuids, applyPaths, null, tgName, null);
\r
2009 String[] parts = tgName.split(COMPOSITE_SEPARATOR);
\r
2010 if(parts.length == 2) {
\r
2011 String name = parts[1];
\r
2012 String path = applyPaths ? parts[0] : null;
\r
2013 if(useGuids) throw new IllegalStateException("GUID identifiers were not found for diagrams.");
\r
2014 return new CompositeInfo(useGuids, applyPaths, path, name, null);
\r
2015 } else if(parts.length == 3) {
\r
2016 String path = parts[0];
\r
2017 String name = parts[1];
\r
2018 GUID guid = GUID.parseIndexString(parts[2]);
\r
2019 return new CompositeInfo(useGuids, applyPaths, path, name, guid);
\r
2025 public GUID getGUID() {
\r
2029 public String getEscapedName() {
\r
2033 public String getUnescapedName() {
\r
2034 return URIStringUtils.unescape(name);
\r
2037 public String getOriginalPath() {
\r
2041 public String getFinalPath() {
\r
2042 if(applyPaths) return path;
\r
2046 public CompositeInfo renamed(String newName) {
\r
2047 return new CompositeInfo(useGuids, applyPaths, path, newName, guid);
\r
2050 public String getStateKey() {
\r
2051 return path + "#" + getEscapedName();
\r
2054 private Resource getFromFolder(ReadGraph graph, Resource target) throws DatabaseException {
\r
2055 Resource diagram = Layer0Utils.getPossibleChild(graph, target, URIStringUtils.unescape(name));
\r
2056 if(diagram == null) return null;
\r
2057 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
2058 return graph.isInstanceOf(diagram, STR.Composite) ? diagram : null;
\r
2061 public Resource resolve(ReadGraph graph, Resource target) throws DatabaseException {
\r
2063 if(useGuids && guid != null) {
\r
2064 Resource indexRoot = graph.syncRequest(new IndexRoot(target));
\r
2065 Collection<Resource> queryResult = searchByGUID(graph, indexRoot, guid);
\r
2066 if(queryResult.size() == 1) {
\r
2067 Resource composite = queryResult.iterator().next();
\r
2068 StructuralResource2 STR = StructuralResource2.getInstance(graph);
\r
2069 if(!graph.isInstanceOf(composite, STR.Composite)) return null;
\r
2075 Resource folder = resolveFolder(graph, target);
\r
2076 if(folder == null) return null;
\r
2077 return getFromFolder(graph, folder);
\r
2079 return getFromFolder(graph, target);
\r
2084 public Resource resolveFolder(ReadGraph graph, Resource target) throws DatabaseException {
\r
2086 String path = getFinalPath();
\r
2087 if(path == null) return target;
\r
2089 Resource folder = graph.syncRequest(new Configuration(target));
\r
2090 String[] segments = path.split("/");
\r
2091 for(int i=0;i<segments.length;i++) {
\r
2092 if(segments[i].isEmpty()) continue;
\r
2093 folder = Layer0Utils.getPossibleChild(graph, folder, URIStringUtils.unescape(segments[i]));
\r
2094 if(folder == null) return null;
\r
2103 public int hashCode() {
\r
2105 if(useGuids) return guid.hashCode();
\r
2107 final int prime = 31;
\r
2108 int result = name.hashCode();
\r
2109 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
2115 public boolean equals(Object obj) {
\r
2121 if (getClass() != obj.getClass())
\r
2124 CompositeInfo other = (CompositeInfo) obj;
\r
2126 if(useGuids) return guid.equals(other.guid);
\r
2128 if (!name.equals(other.name))
\r
2132 if (!path.equals(other.path))
\r
2141 public static char COMPOSITE_SEPARATOR_CHAR = '#';
\r
2142 public static String COMPOSITE_SEPARATOR = String.valueOf(COMPOSITE_SEPARATOR_CHAR);
\r
2144 public static CompositeInfo parseCompositeNameFromRoot(Root root, boolean applyPaths, boolean useGuids) {
\r
2145 return CompositeInfo.parse(root.name, applyPaths, useGuids);
\r
2148 public static CompositeInfo parseCompositeNameFromRoot(Identity root, boolean applyPaths, boolean useGuids) {
\r
2149 String coded = TransferableGraphUtils.getName(root);
\r
2150 return CompositeInfo.parse(coded, applyPaths, useGuids);
\r
2153 private static StringBuilder getDiagramCompositePathInternal(ReadGraph graph, Resource folder, StringBuilder builder) throws DatabaseException {
\r
2154 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
2155 Resource model = graph.getPossibleObject(folder, SIMU.IsConfigurationOf);
\r
2156 if (model != null) return builder;
\r
2158 Layer0 L0 = Layer0.getInstance(graph);
\r
2159 String name = graph.getPossibleRelatedValue(folder, L0.HasName, Bindings.STRING);
\r
2160 if (name == null) return null;
\r
2161 Resource parent = graph.getPossibleObject(folder, L0.PartOf);
\r
2162 if (parent == null) return null;
\r
2164 StringBuilder sb = getDiagramCompositePathInternal(graph, parent, builder);
\r
2165 if (sb == null) return null;
\r
2166 if (sb.length() > 0)
\r
2167 sb.append(URIStringUtils.NAMESPACE_PATH_SEPARATOR);
\r
2168 sb.append( URIStringUtils.escape(name) );
\r
2175 * @return diagram/folder path up until model configuration root with each
\r
2176 * segment escaped using {@link URIStringUtils#escape(String)} and
\r
2177 * {@value URIStringUtils#NAMESPACE_PATH_SEPARATOR} between each
\r
2178 * segment or <code>null</code> if the specified diagram composite
\r
2179 * is not part of any model configuration structure.
\r
2180 * @throws DatabaseException
\r
2182 public static String getDiagramCompositePath(ReadGraph graph, Resource diagram) throws DatabaseException {
\r
2183 Layer0 L0 = Layer0.getInstance(graph);
\r
2184 Resource parent = graph.getPossibleObject(diagram, L0.PartOf);
\r
2185 if(parent == null) return null;
\r
2186 StringBuilder sb = getDiagramCompositePathInternal(graph, parent, new StringBuilder());
\r
2187 return sb != null ? sb.toString() : null;
\r
2190 public static void exportModel(ReadGraph graph, Resource model, String fileName, String format, int version) throws DatabaseException {
\r
2193 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, model, true, false);
\r
2194 TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
\r
2195 TransferableGraphs.writeTransferableGraph(graph, format, version, s, new File(fileName));
\r
2196 } catch (Exception e) {
\r
2197 throw new DatabaseException(e);
\r
2202 public static void exportSharedOntology(ReadGraph graph, Resource library, String fileName, String format, int version) throws DatabaseException {
\r
2204 Layer0 L0 = Layer0.getInstance(graph);
\r
2205 String name = graph.getRelatedValue(library, L0.HasName, Bindings.STRING);
\r
2207 DraftStatusBean draft = null;
\r
2208 boolean published = Layer0Utils.isPublished(graph, library);
\r
2209 if(!published) draft = new DraftStatusBean(new String[0]);
\r
2211 LibraryInfo info = new LibraryInfo(name, library, draft);
\r
2214 exportSharedOntology(new NullProgressMonitor(), graph, new File(fileName), format, version, info);
\r
2215 } catch (IOException e) {
\r
2216 throw new DatabaseException(e);
\r
2221 public static DraftStatusBean getDependencyDraftStatus(ReadGraph graph, Resource library) throws DatabaseException {
\r
2222 Layer0 L0 = Layer0.getInstance(graph);
\r
2223 List<String> drafts = new ArrayList<>();
\r
2224 for (Resource shared : graph.syncRequest(new ObjectsWithType(library, L0.IsLinkedTo, L0.SharedOntology))) {
\r
2225 boolean published = Layer0Utils.isPublished(graph, shared);
\r
2227 drafts.add(graph.getURI(shared));
\r
2229 return drafts.isEmpty() ? null : new DraftStatusBean(drafts);
\r
2232 public static Set<Resource> getMostUnspecificTypes(final ReadGraph graph, Collection<Resource> types) throws DatabaseException {
\r
2234 final Set<Resource> work = new HashSet<Resource>(types);
\r
2235 for(Resource type : types) {
\r
2236 Set<Resource> supers = graph.getSupertypes(type);
\r
2237 if(!Collections.disjoint(supers, work)) work.remove(type);
\r
2244 public static void rewriteGUIDS(WriteGraph graph, Resource root, boolean deep) throws DatabaseException {
\r
2245 List<Resource> todo = new ArrayList<Resource>();
\r
2247 Layer0 L0 = Layer0.getInstance(graph);
\r
2248 while(!todo.isEmpty()) {
\r
2249 Resource resource = todo.remove(todo.size()-1);
\r
2250 graph.claimLiteral(resource, L0.identifier, L0.GUID, GUID.random(), GUID.BINDING);
\r
2252 todo.addAll(graph.getObjects(resource, L0.ConsistsOf));
\r
2256 public static void createMissingGUIDs(IProgressMonitor monitor, WriteGraph graph, Collection<Resource> roots) throws DatabaseException {
\r
2258 if(monitor == null) monitor = new NullProgressMonitor();
\r
2260 Layer0 L0 = Layer0.getInstance(graph);
\r
2262 // Allow this process to make 50k queries
\r
2263 QueryMemoryWatcher memory = new QueryMemoryWatcher(graph, 50000);
\r
2265 for(Resource root : roots) {
\r
2267 boolean madeChanges = false;
\r
2269 monitor.setTaskName("Creating missing GUID identifiers " + NameUtils.getSafeName(graph, root));
\r
2270 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(root));
\r
2271 for(Resource r : searchByType(graph, indexRoot, L0.Entity)) {
\r
2273 memory.maintain();
\r
2275 if(graph.isImmutable(r)) continue;
\r
2277 if(!ModelingUtils.needsIdentifier(graph, r)) continue;
\r
2279 GUID existing = graph.getPossibleRelatedValue(r, L0.identifier, GUID.BINDING);
\r
2280 if(existing == null) {
\r
2281 graph.addLiteral(r, L0.identifier, L0.identifier_Inverse, L0.GUID, GUID.random(), GUID.BINDING);
\r
2282 madeChanges = true;
\r
2288 ModelingUtils.deleteIndex(graph, root);
\r
2295 public static boolean activateModel(WriteGraph graph, Resource model) throws DatabaseException {
\r
2296 return graph.syncRequest(new ActivateModel(Simantics.getProjectResource(), model));
\r
2299 public static File fileDialog(String title, List<Tuple> namesAndExtensions) {
\r
2301 Display display = Display.getCurrent();
\r
2302 Shell shell = display.getActiveShell();
\r
2304 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
\r
2305 dialog.setText(title);
\r
2307 String[] extensions = new String[namesAndExtensions.size()];
\r
2308 String[] filterNames = new String[namesAndExtensions.size()];
\r
2310 for(Tuple t : namesAndExtensions) {
\r
2311 String filterName = (String)t.get(0);
\r
2312 String extension = (String)t.get(1);
\r
2313 filterNames[index] = filterName;
\r
2314 extensions[index] = extension;
\r
2318 dialog.setFilterExtensions(extensions);
\r
2319 dialog.setFilterNames(filterNames);
\r
2320 final String fileName = dialog.open();
\r
2321 if (fileName == null) return null;
\r
2323 return new File(fileName);
\r
2327 public static Resource createLibrary(WriteGraph graph, Resource parent) throws DatabaseException {
\r
2328 Layer0 l0 = Layer0.getInstance(graph);
\r
2329 return createLibrary(graph, parent, NameUtils.findFreshName(graph, "Library", parent, l0.ConsistsOf));
\r
2332 public static Resource createLibrary(WriteGraph graph, Resource parent, String name) throws DatabaseException {
\r
2333 graph.markUndoPoint();
\r
2334 Layer0 l0 = Layer0.getInstance(graph);
\r
2336 Resource library = graph.newResource();
\r
2337 graph.claim(library, l0.InstanceOf, null, l0.Library);
\r
2338 graph.addLiteral(library, l0.HasName, l0.NameOf, l0.String, name, Bindings.STRING);
\r
2339 graph.claim(library, l0.PartOf, parent);
\r
2341 Layer0Utils.addCommentMetadata(graph, "Created new Library named " + name + ", resource " + library);
\r