1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling;
15 import java.io.IOException;
16 import java.text.DateFormat;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
26 import java.util.TreeMap;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.NullProgressMonitor;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.SubMonitor;
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.layout.GridDataFactory;
37 import org.eclipse.jface.layout.GridLayoutFactory;
38 import org.eclipse.jface.resource.ImageDescriptor;
39 import org.eclipse.jface.viewers.IStructuredSelection;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.jface.window.Window;
42 import org.eclipse.jface.wizard.WizardDialog;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.FileDialog;
48 import org.eclipse.swt.widgets.Shell;
49 import org.eclipse.ui.IWorkbenchWizard;
50 import org.eclipse.ui.PlatformUI;
51 import org.eclipse.ui.wizards.IWizardDescriptor;
52 import org.simantics.Simantics;
53 import org.simantics.annotation.ontology.AnnotationResource;
54 import org.simantics.databoard.Bindings;
55 import org.simantics.databoard.binding.Binding;
56 import org.simantics.databoard.binding.mutable.Variant;
57 import org.simantics.databoard.container.DataContainer;
58 import org.simantics.databoard.container.DataContainers;
59 import org.simantics.databoard.container.DataFormatException;
60 import org.simantics.databoard.container.FormatHandler;
61 import org.simantics.databoard.serialization.SerializationException;
62 import org.simantics.databoard.type.Datatype;
63 import org.simantics.databoard.util.URIStringUtils;
64 import org.simantics.datatypes.literal.GUID;
65 import org.simantics.db.MetadataI;
66 import org.simantics.db.ReadGraph;
67 import org.simantics.db.RequestProcessor;
68 import org.simantics.db.Resource;
69 import org.simantics.db.Session;
70 import org.simantics.db.Statement;
71 import org.simantics.db.VirtualGraph;
72 import org.simantics.db.WriteGraph;
73 import org.simantics.db.WriteOnlyGraph;
74 import org.simantics.db.common.Indexing;
75 import org.simantics.db.common.NamedResource;
76 import org.simantics.db.common.QueryMemoryWatcher;
77 import org.simantics.db.common.changeset.GenericChangeListener;
78 import org.simantics.db.common.primitiverequest.IsInstanceOf;
79 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
80 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
81 import org.simantics.db.common.request.IndexRoot;
82 import org.simantics.db.common.request.ObjectsWithType;
83 import org.simantics.db.common.request.PossibleIndexRoot;
84 import org.simantics.db.common.request.ReadRequest;
85 import org.simantics.db.common.request.ResourceRead2;
86 import org.simantics.db.common.request.WriteOnlyRequest;
87 import org.simantics.db.common.request.WriteRequest;
88 import org.simantics.db.common.request.WriteResultRequest;
89 import org.simantics.db.common.utils.ListUtils;
90 import org.simantics.db.common.utils.Logger;
91 import org.simantics.db.common.utils.NameUtils;
92 import org.simantics.db.common.utils.OrderedSetUtils;
93 import org.simantics.db.common.utils.VersionInfo;
94 import org.simantics.db.common.utils.VersionInfoRequest;
95 import org.simantics.db.common.utils.Versions;
96 import org.simantics.db.exception.DatabaseException;
97 import org.simantics.db.layer0.QueryIndexUtils;
98 import org.simantics.db.layer0.SelectionHints;
99 import org.simantics.db.layer0.adapter.CopyHandler;
100 import org.simantics.db.layer0.adapter.GenericRelationIndex;
101 import org.simantics.db.layer0.adapter.Instances;
102 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
103 import org.simantics.db.layer0.adapter.impl.ImportAdvisorFactory;
104 import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest;
105 import org.simantics.db.layer0.genericrelation.DependencyChanges;
106 import org.simantics.db.layer0.genericrelation.IndexedRelations;
107 import org.simantics.db.layer0.migration.MigratedImportResult;
108 import org.simantics.db.layer0.migration.MigrationUtils;
109 import org.simantics.db.layer0.request.ActivateModel;
110 import org.simantics.db.layer0.request.ActiveModels;
111 import org.simantics.db.layer0.request.Configuration;
112 import org.simantics.db.layer0.request.IsLinkedTo;
113 import org.simantics.db.layer0.request.PossibleModel;
114 import org.simantics.db.layer0.util.ClipboardUtils;
115 import org.simantics.db.layer0.util.DraftStatusBean;
116 import org.simantics.db.layer0.util.ExternalDownloadBean;
117 import org.simantics.db.layer0.util.Layer0Utils;
118 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
119 import org.simantics.db.layer0.util.PasteEventHandler;
120 import org.simantics.db.layer0.util.RemoverUtil;
121 import org.simantics.db.layer0.util.SimanticsClipboard;
122 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
123 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
124 import org.simantics.db.layer0.util.SimanticsKeys;
125 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
126 import org.simantics.db.layer0.variable.Variable;
127 import org.simantics.db.layer0.variable.Variables;
128 import org.simantics.db.service.ClusterControl;
129 import org.simantics.db.service.CollectionSupport;
130 import org.simantics.db.service.GraphChangeListenerSupport;
131 import org.simantics.db.service.QueryControl;
132 import org.simantics.db.service.VirtualGraphSupport;
133 import org.simantics.diagram.stubs.DiagramResource;
134 import org.simantics.diagram.stubs.G2DResource;
135 import org.simantics.diagram.synchronization.graph.AddElement;
136 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
137 import org.simantics.graph.db.IImportAdvisor2;
138 import org.simantics.graph.db.ImportAdvisors;
139 import org.simantics.graph.db.MissingDependencyException;
140 import org.simantics.graph.db.StreamingTransferableGraphFileReader;
141 import org.simantics.graph.db.TransferableGraphException;
142 import org.simantics.graph.db.TransferableGraphSource;
143 import org.simantics.graph.db.TransferableGraphs;
144 import org.simantics.graph.representation.Identity;
145 import org.simantics.graph.representation.Root;
146 import org.simantics.graph.representation.TransferableGraph1;
147 import org.simantics.graph.representation.TransferableGraphUtils;
148 import org.simantics.issues.common.IssueSourceUtils;
149 import org.simantics.issues.ontology.IssueResource;
150 import org.simantics.layer0.Layer0;
151 import org.simantics.layer0.utils.direct.GraphUtils;
152 import org.simantics.modeling.adapters.ChangeInformation;
153 import org.simantics.modeling.template2d.ontology.Template2dResource;
154 import org.simantics.modeling.utils.OntologicalRequirementTracker;
155 import org.simantics.operation.Layer0X;
156 import org.simantics.project.ontology.ProjectResource;
157 import org.simantics.scenegraph.profile.ProfileUtils;
158 import org.simantics.scl.runtime.function.Function1;
159 import org.simantics.scl.runtime.tuple.Tuple;
160 import org.simantics.simulation.ontology.SimulationResource;
161 import org.simantics.structural.stubs.StructuralResource2;
162 import org.simantics.structural2.modelingRules.IModelingRules;
163 import org.simantics.structural2.scl.StructuralComponent;
164 import org.simantics.structural2.utils.StructuralUtils;
165 import org.simantics.utils.ObjectUtils;
166 import org.simantics.utils.datastructures.Pair;
167 import org.simantics.utils.datastructures.Triple;
168 import org.simantics.utils.datastructures.hints.HintContext;
169 import org.simantics.utils.ui.dialogs.ListDialog;
172 * @author Hannu Niemistö
174 public class ModelingUtils {
177 private WriteGraph wg;
179 private StructuralResource2 sr;
180 private DiagramResource dr;
181 public ModelingResources mr;
182 public SimulationResource SIMU;
184 public ModelingUtils(WriteGraph g) {
187 b = Layer0.getInstance(g);
188 sr = StructuralResource2.getInstance(g);
189 dr = DiagramResource.getInstance(g);
190 mr = ModelingResources.getInstance(g);
191 SIMU = SimulationResource.getInstance(g);
195 public Resource createSymbol2(String name) throws DatabaseException {
196 return createSymbol2(name, dr.Composite);
200 public Resource createSymbol2(String name, Resource diagramType) throws DatabaseException {
201 return createSymbol2(name, diagramType, dr.DefinedElement);
204 public Resource createSymbol2(String name, Resource diagramType, Resource symbolType) throws DatabaseException {
205 G2DResource g2d = G2DResource.getInstance(g);
207 // Resource visibleTag = wg.newResource();
208 // wg.claim(visibleTag, b.SubrelationOf, null, dr.IsVisible);
209 // Resource focusableTag = wg.newResource();
210 // wg.claim(focusableTag, b.SubrelationOf, null, dr.IsFocusable);
212 Double boxDimension = 6.0;
213 Collection<Statement> grid = g.getAssertedStatements(diagramType, dr.HasGridSize);
214 if(grid.size() == 1) {
215 Double d = g.getPossibleValue(grid.iterator().next().getObject(), Bindings.DOUBLE);
216 if(d != null) boxDimension = 2*d;
220 Resource element = GraphUtils.create(wg,
221 b.InstanceOf, dr.SVGElement,
223 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
224 // REMOVED by Tuukka, because This will cause
225 // parsers to get on the net and get the doctype
226 // definitions which can stall the UI for a long time.
227 // Besides, we're not using the validation for anything
228 // so it's useless to us.
229 //"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" +
230 "<svg xmlns=\"http://www.w3.org/2000/svg\" overflow=\"visible\" version=\"1.1\">" +
231 "<rect x=\"-" + boxDimension + "\" y=\"-" + boxDimension + "\" width=\"" + (2*boxDimension) + "\" height=\"" + (2*boxDimension) + "\" fill=\"none\" stroke=\"rgb(0,0,0)\" stroke-width=\"0.1\"/>" +
235 // wg.claim(element, visibleTag, element);
236 // wg.claim(element, focusableTag, element);
238 Resource orderedSet = OrderedSetUtils.create(wg, diagramType, element);
240 // wg.claim(orderedSet, dr.HasLayer, GraphUtils.create2(wg, dr.Layer,
241 // b.HasName, "Default",
242 // dr.IsActive, Boolean.TRUE,
243 // dr.HasVisibleTag, visibleTag,
244 // dr.HasFocusableTag, focusableTag));
246 Resource result = GraphUtils.create(wg,
249 sr.IsDefinedBy, orderedSet);
251 wg.claim(result, b.ConsistsOf, orderedSet);
252 wg.claimLiteral(orderedSet, b.HasName, "__DIAGRAM__", Bindings.STRING);
253 AddElement.claimFreshElementName(wg, orderedSet, element);
254 wg.claim(orderedSet, b.ConsistsOf, element);
256 wg.claim(result, b.Inherits, null, symbolType);
260 public static Collection<Resource> getElementCorrespondendences(ReadGraph g, Resource element) throws DatabaseException {
261 DiagramResource dr = DiagramResource.getInstance(g);
262 ModelingResources mr = ModelingResources.getInstance(g);
263 if(g.isInstanceOf(element, dr.Connection)) {
264 Resource mappedComponent = g.getPossibleObject(element, mr.ElementToComponent);
265 if(mappedComponent != null) return Collections.singletonList(mappedComponent);
266 Resource mappedConnection = g.getPossibleObject(element, mr.DiagramConnectionToConnection);
267 if(mappedConnection == null)
268 return Collections.emptyList();
269 ArrayList<Resource> result = new ArrayList<Resource>();
270 Collection<Resource> relatedMappedConnections = StructuralUtils.getRelatedConnections(g, mappedConnection);
271 for(Resource relatedMappedConnection : relatedMappedConnections)
272 for(Resource relatedConnection : g.getObjects(relatedMappedConnection, mr.ConnectionToDiagramConnection))
273 result.addAll(g.getObjects(relatedConnection, mr.ElementToComponent));
277 return g.getObjects(element, mr.ElementToComponent);
280 public static Resource getPossibleElement(ReadGraph g, Resource component) throws DatabaseException {
281 ModelingResources mr = ModelingResources.getInstance(g);
282 return g.getPossibleObject(component, mr.ComponentToElement);
285 public static Resource getPossibleElementCorrespondendence(ReadGraph g, Resource element) throws DatabaseException {
286 Collection<Resource> corrs = getElementCorrespondendences(g, element);
287 if(corrs.size() != 1) return null;
288 else return corrs.iterator().next();
291 public static Resource getSingleElementCorrespondendence(ReadGraph g, Resource element) throws DatabaseException {
292 Collection<Resource> corrs = getElementCorrespondendences(g, element);
293 if(corrs.size() != 1) throw new DatabaseException("Expected 1 element correspondence, got " + corrs.size());
294 else return corrs.iterator().next();
297 public static Resource createExperiment(WriteGraph graph, Resource model) throws DatabaseException {
299 Layer0 L0 = Layer0.getInstance(graph);
300 SimulationResource SIMU = SimulationResource.getInstance(graph);
301 Resource experiment = graph.newResource();
302 graph.claim(experiment, L0.InstanceOf, SIMU.Experiment);
303 graph.claimLiteral(experiment, L0.HasName, "Experiment");
304 graph.claim(model, L0.ConsistsOf, experiment);
309 public static Resource createModel(WriteGraph graph, Resource type) throws DatabaseException {
310 return createModel(graph, type, Simantics.getProjectResource(), null);
313 public static Resource createModel(WriteGraph graph, Resource type, String name) throws DatabaseException {
314 return createModel(graph, type, Simantics.getProjectResource(), name);
317 public static Resource createModel(WriteGraph graph, Resource type, final Resource target, String name) throws DatabaseException {
319 Layer0 L0 = Layer0.getInstance(graph);
320 SimulationResource SIMU = SimulationResource.getInstance(graph);
321 StructuralResource2 STR = StructuralResource2.getInstance(graph);
322 ModelingResources MOD = ModelingResources.getInstance(graph);
325 name = NameUtils.findFreshName(graph, "Model", target, L0.ConsistsOf, "%s%d");
327 final Resource model = graph.newResource();
328 graph.newClusterSet(model);
329 graph.claim(model, L0.InstanceOf, null, type);
330 graph.claimLiteral(model, L0.HasName, name);
332 graph.claim(target, L0.ConsistsOf, model);
334 Resource configurationType = graph.getPossibleObject(model, MOD.StructuralModel_HasConfigurationType);
335 if(configurationType == null) configurationType = STR.Composite;
337 Resource configuration = graph.newResource();
338 graph.claimLiteral(configuration, L0.HasName, "Configuration", Bindings.STRING);
339 graph.claim(configuration, L0.InstanceOf, null, configurationType);
340 graph.claim(model, L0.ConsistsOf, configuration);
341 graph.claim(model, SIMU.HasConfiguration, configuration);
343 Resource joinClusterSet = graph.newResource();
344 graph.newClusterSet(joinClusterSet);
345 graph.claim(joinClusterSet, L0.InstanceOf, L0.ClusterSet);
346 graph.claim(model, STR.HasJoinClusterSet, joinClusterSet);
348 linkOntologyDependenciesToModel(graph, model, target);
350 Resource ontology = graph.syncRequest(new PossibleIndexRoot(type));
351 if(ontology != null) {
352 graph.claim(model, L0.IsLinkedTo, ontology);
355 VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);
356 graph.asyncRequest(new WriteRequest(support.getWorkspacePersistent("activations")) {
359 public void perform(WriteGraph graph) throws DatabaseException {
360 Layer0X L0X = Layer0X.getInstance(graph);
361 Collection<Resource> actives = graph.syncRequest(new ActiveModels(target));
362 if(actives.isEmpty()) {
363 graph.claim(model, L0X.IsActivatedBy, target);
373 public static void linkOntologyDependenciesToModel(WriteGraph graph, Resource model, Resource target)
374 throws DatabaseException {
375 Layer0 L0 = Layer0.getInstance(graph);
376 ProjectResource PROJ = ProjectResource.getInstance(graph);
377 for(Resource dep : graph.getObjects(target, L0.IsLinkedTo)) {
378 if(graph.isInstanceOf(dep, PROJ.NamespaceRequirement)) {
379 for(Resource req : graph.getObjects(dep, PROJ.RequiresNamespace)) {
380 String uri = graph.getPossibleValue(req, Bindings.STRING);
382 Resource ns = graph.getResource(uri);
384 graph.claim(model, L0.IsLinkedTo, null, ns);
392 public static void addSCLMainToModel(WriteGraph graph, Resource model)
393 throws DatabaseException {
394 addSCLMainToModel(graph, model, "SCLMain", "include \"Simantics/All\"\n");
397 public static void addSCLMainToModel(WriteGraph graph, Resource model, String name, String contents)
398 throws DatabaseException {
399 Layer0 L0 = Layer0.getInstance(graph);
400 Resource sclmain = GraphUtils.create2(graph, L0.SCLModule, L0.PartOf, model, L0.HasName, name);
401 graph.claimLiteral(sclmain, L0.SCLModule_definition, contents, Bindings.STRING);
404 public static Resource createLocalLibrary(WriteGraph graph, Resource container, String name) throws DatabaseException {
405 Layer0 L0 = Layer0.getInstance(graph);
406 ModelingResources MOD = ModelingResources.getInstance(graph);
407 Resource library = graph.newResource();
408 graph.claim(library, L0.InstanceOf, null, L0.Library);
409 graph.addLiteral(library, L0.HasName, L0.NameOf, "Library", Bindings.STRING);
410 if (container != null) {
411 graph.claim(container, L0.ConsistsOf, L0.PartOf, library);
412 graph.claim(container, MOD.HasLocalLibrary, MOD.IsLocalLibraryOf, library);
417 public static void importModel(String fileName) {
419 Resource project = Simantics.getProject().get();
421 try (StreamingTransferableGraphFileReader importer = new StreamingTransferableGraphFileReader(new File(fileName))) {
422 TransferableGraphSource tg = importer.readTG();
424 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(project) {
426 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
429 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
430 library = Simantics.getProjectResource();
431 String newName = newName(graph, library, root.name);
432 nameMappings.put(root.name, newName);
436 TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor);
438 } catch (MissingDependencyException e) {
440 final Set<String> missingURIs = e.getMissingURIs();
442 class ErrorMessageDialog extends MessageDialog {
444 public ErrorMessageDialog(Shell shell) {
446 "Unsatisfied dependencies", null,
447 "The following dependencies were missing. Please import the dependencies and try again.",
448 MessageDialog.ERROR, new String[] { "Continue" }, 0);
452 protected Control createCustomArea(Composite composite) {
453 GridLayoutFactory.fillDefaults().applyTo(composite);
455 org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.READ_ONLY);
456 GridDataFactory.fillDefaults().grab(true, true).applyTo(list);
457 for(String s : missingURIs) list.add(s);
463 Display display = Display.getCurrent();
464 if(display != null) {
465 ErrorMessageDialog md = new ErrorMessageDialog(display.getActiveShell());
468 Display.getDefault().asyncExec(new Runnable() {
472 Shell shell = Display.getCurrent().getActiveShell();
473 ErrorMessageDialog md = new ErrorMessageDialog(shell);
481 } catch (Exception e) {
482 Logger.defaultLogError(e);
487 public static void primeVirtualGraphs() {
488 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
489 support.getWorkspacePersistent("activations");
490 support.getWorkspacePersistent("experiments");
491 support.getWorkspacePersistent("issues");
492 support.getWorkspacePersistent("preferences");
495 public static Resource createProfileEntry(WriteGraph graph, String name, Resource style, Resource group) throws DatabaseException {
497 Layer0 L0 = Layer0.getInstance(graph);
498 DiagramResource DIA = DiagramResource.getInstance(graph);
500 Resource entry = graph.newResource();
501 graph.claim(entry, L0.InstanceOf, null, DIA.GroupStyleProfileEntry);
502 graph.claimLiteral(entry, L0.HasName, name);
503 graph.claimLiteral(entry, L0.HasLabel, name);
504 graph.claim(entry, DIA.ProfileEntry_HasStyle, style);
505 graph.claim(entry, DIA.ProfileEntry_HasGroup, group);
511 public static Resource createProfile(WriteGraph graph, String profileName, Resource... entries) throws DatabaseException {
513 Layer0 L0 = Layer0.getInstance(graph);
514 DiagramResource DIA = DiagramResource.getInstance(graph);
516 Resource list = ListUtils.create(graph, DIA.Profile, entries);
518 Resource profile = graph.newResource();
519 graph.claim(profile, L0.InstanceOf, null, DIA.Profile);
520 graph.claimLiteral(profile, L0.HasName, profileName);
521 graph.claim(profile, DIA.HasEntries, null, list);
527 public static Resource createProfile(WriteGraph graph, String profileName, Collection<Resource> entries) throws DatabaseException {
528 return createProfile(graph, profileName, entries.toArray(new Resource[entries.size()]));
531 public static Resource createToplevelProfile(WriteGraph graph, Resource model, String name, Resource ... profiles) throws DatabaseException {
533 Resource work = createProfile(graph, name, profiles);
535 Layer0 L0 = Layer0.getInstance(graph);
536 DiagramResource DIA = DiagramResource.getInstance(graph);
538 graph.deny(model, DIA.HasActiveProfile);
539 graph.claim(model, DIA.HasActiveProfile, work);
540 graph.claim(model, L0.ConsistsOf, L0.PartOf, work);
546 public static Resource createToplevelProfile(WriteGraph graph, Resource model, String name, Collection<Resource> profiles) throws DatabaseException {
547 return createToplevelProfile(graph, model, name, profiles.toArray(new Resource[profiles.size()]));
550 public static void activateProfileEntries(WriteGraph graph, final Resource profile, final Resource ... entries) {
552 VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);
553 graph.asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
556 public void perform(WriteGraph graph) throws DatabaseException {
557 SimulationResource SIMU = SimulationResource.getInstance(graph);
558 for(Resource entry : entries)
559 graph.claim(profile, SIMU.IsActive, entry);
566 public static void activateProfileEntries(WriteGraph graph, Resource profile, Collection<Resource> entries) {
567 activateProfileEntries(graph, profile, entries.toArray(new Resource[entries.size()]));
570 public static void toggleProfileGroup(WriteGraph graph, Resource runtimeProfile, String groupName, boolean enabled) throws DatabaseException {
571 Layer0 L0 = Layer0.getInstance(graph);
572 for (Resource group : ProfileUtils.getProfileChildren(graph, runtimeProfile)) {
573 String name = graph.getRelatedValue2(group, L0.HasName);
574 if (name.equals(groupName)) {
575 toggleProfileGroup(graph, runtimeProfile, group, enabled);
581 public static void toggleProfileGroup(WriteGraph graph, Resource runtimeProfile, Resource group, boolean enabled) throws DatabaseException {
582 DiagramResource DIA = DiagramResource.getInstance(graph);
584 if(graph.isInstanceOf(group, DIA.Profile)) {
586 for(Resource child : ProfileUtils.getProfileChildren(graph, group)) {
587 toggleProfileGroup(graph, runtimeProfile, child, enabled);
590 } else if(graph.isInstanceOf(group, DIA.ProfileEntry)) {
593 graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, group);
595 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, group);
601 public static void untrackDependencies() {
602 untrackDependencies(Simantics.getSession());
605 public static void untrackDependencies(RequestProcessor processor) {
608 processor.syncRequest(new ReadRequest() {
611 public void run(ReadGraph graph) throws DatabaseException {
612 Layer0X L0X = Layer0X.getInstance(graph);
613 GenericRelationIndex index = graph.adapt(L0X.DependenciesRelation, GenericRelationIndex.class);
614 index.untrack(graph.getSession(), graph.getRootLibrary());
618 } catch (DatabaseException e) {
619 Logger.defaultLogError(e);
624 public static void trackDependencies() {
625 trackDependencies(Simantics.getSession());
628 public static void trackDependencies(RequestProcessor processor) {
631 processor.syncRequest(new ReadRequest() {
634 public void run(ReadGraph graph) throws DatabaseException {
635 Layer0X L0X = Layer0X.getInstance(graph);
636 GenericRelationIndex index = graph.adapt(L0X.DependenciesRelation, GenericRelationIndex.class);
637 index.trackAndIndex(graph.getSession(), graph.getRootLibrary());
641 } catch (DatabaseException e) {
642 Logger.defaultLogError(e);
647 public static void trackOntologicalRequirements() {
648 Session session = Simantics.getSession();
649 GraphChangeListenerSupport changeListenerSupport = session.getService(GraphChangeListenerSupport.class);
650 changeListenerSupport.addMetadataListener( OntologicalRequirementListener.getInstance() );
653 public static void untrackOntologicalRequirements() {
654 Session session = Simantics.getSession();
655 GraphChangeListenerSupport changeListenerSupport = session.getService(GraphChangeListenerSupport.class);
656 changeListenerSupport.removeMetadataListener( OntologicalRequirementListener.getInstance() );
659 public static class OntologicalRequirementListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
661 private static OntologicalRequirementListener INSTANCE;
663 public static OntologicalRequirementListener getInstance() {
664 if(INSTANCE == null) {
665 INSTANCE = new OntologicalRequirementListener();
670 private OntologicalRequirementListener() {
673 private OntologicalRequirementTracker changeInformationUpdater = new OntologicalRequirementTracker();
676 public boolean preEventRequest() {
677 return !Indexing.isDependenciesIndexingDisabled();
681 public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
682 changeInformationUpdater.update(graph, metadata, event);
687 public static void removeIndex(WriteGraph graph, Resource model) throws DatabaseException {
688 Layer0X L0X = Layer0X.getInstance(graph);
689 IndexedRelations ir = graph.getService(IndexedRelations.class);
690 // Deletes index files
691 ir.reset(null, graph, L0X.DependenciesRelation, model);
694 public static void resetIssueSources(WriteGraph graph, Resource model) throws DatabaseException {
695 Layer0 L0 = Layer0.getInstance(graph);
696 IssueResource ISSUE = IssueResource.getInstance(graph);
697 for(Resource source : graph.sync(new ObjectsWithType(model, L0.ConsistsOf, ISSUE.ContinuousIssueSource))) {
698 IssueSourceUtils.update(graph, source);
702 public static void copyAnnotationTypes(WriteGraph graph, Resource sourceModel, Resource targetModel) throws DatabaseException {
704 Layer0 L0 = Layer0.getInstance(graph);
705 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
707 Instances query = graph.adapt(ANNO.AnnotationType, Instances.class);
709 Resource library = graph.getPossibleObject(targetModel, ANNO.HasAnnotationTypeRoot);
711 if(library == null) {
712 library = graph.newResource();
713 graph.claim(library, L0.InstanceOf, null, ANNO.AnnotationTypeLibrary);
714 graph.claimLiteral(library, L0.HasName, L0.NameOf, L0.String, "Annotation types", Bindings.STRING);
715 graph.claim(library, L0.PartOf, L0.ConsistsOf, targetModel);
716 graph.claim(targetModel, ANNO.HasAnnotationTypeRoot, library);
719 for(Resource type : query.find(graph, sourceModel)) {
720 String name = graph.getRelatedValue(type, L0.HasName);
721 Resource existing = Layer0Utils.getPossibleChild(graph, library, name);
722 if(existing != null) {
723 RemoverUtil.remove(graph, existing);
725 Layer0Utils.copyTo(graph, library, type);
730 public static void deleteIndex(WriteGraph graph, Resource model) throws DatabaseException {
731 Layer0X L0X = Layer0X.getInstance(graph);
732 IndexedRelations ir = graph.getService(IndexedRelations.class);
733 // Deletes index files
734 ir.reset(null, graph, L0X.DependenciesRelation, model);
737 public static void disableDependencies(WriteGraph graph, Resource dummy) {
738 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
741 public static void releaseMemory(WriteGraph graph) {
743 QueryControl qc = graph.getService(QueryControl.class);
744 ClusterControl cc = graph.getService(ClusterControl.class);
750 public static List<Resource> filterByIndexRoot(ReadGraph graph, Resource indexRoot, List<Resource> resources) throws DatabaseException {
751 ArrayList<Resource> result = new ArrayList<Resource>(resources.size());
752 for (Resource r : resources) {
753 Resource root = graph.syncRequest(new PossibleIndexRoot(r));
754 if (indexRoot.equals(root))
761 public static List<Resource> searchByTypeShallow(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
762 return QueryIndexUtils.searchByTypeShallow(graph, model, type);
766 public static List<Resource> searchByType(ReadGraph graph, Resource model, Resource type) throws DatabaseException {
767 return QueryIndexUtils.searchByType(graph, model, type);
771 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, GUID guid) throws DatabaseException {
772 return QueryIndexUtils.searchByGUID(graph, indexRoot, guid);
776 public static List<Resource> searchByGUID(ReadGraph graph, Resource indexRoot, String indexString) throws DatabaseException {
777 return QueryIndexUtils.searchByGUID(graph, indexRoot, indexString);
781 public static List<Resource> searchByQueryShallow(ReadGraph graph, Resource model, String query) throws DatabaseException {
782 return QueryIndexUtils.searchByQueryShallow(graph, model, query);
786 public static List<Resource> searchByQuery(ReadGraph graph, Resource model, String query) throws DatabaseException {
787 return QueryIndexUtils.searchByQuery(graph, model, query);
791 public static List<Resource> searchByTypeAndFilter(ReadGraph graph, Resource model, Resource type, Function1<Resource,Boolean> filter) throws DatabaseException {
792 return QueryIndexUtils.searchByTypeAndFilter(graph, model, type, filter);
796 public static List<Triple<Resource, Resource, String>> getIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
797 return QueryIndexUtils.getIndexEntries(graph, model, filter);
801 public static String listIndexEntries(ReadGraph graph, Resource model, String filter) throws DatabaseException {
802 return QueryIndexUtils.listIndexEntries(graph, model, filter);
806 public static List<Resource> searchByTypeAndName(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
807 return QueryIndexUtils.searchByTypeAndName(graph, model, type, name);
811 public static List<Resource> searchByTypeAndNameShallow(ReadGraph graph, Resource model, Resource type, String name) throws DatabaseException {
812 return QueryIndexUtils.searchByTypeAndNameShallow(graph, model, type, name);
817 * database write access
818 * @param sourceContainer
819 * the source container to look for annotationProperty from to be
820 * used as the copy source
821 * @param targetContainer
822 * the target container for the copied annotationProperty
824 * @param annotationProperty
825 * the annotation property relation
826 * @return created copy of the original annotation or <code>null</code> if
827 * there was nothing to copy
828 * @throws DatabaseException
830 public static Resource copyPossibleAnnotation(WriteGraph graph, Resource sourceContainer, Resource targetContainer, Resource annotationProperty) throws DatabaseException {
831 return copyPossibleAnnotation2(graph, sourceContainer, targetContainer, annotationProperty, null);
834 public static List<String> getPossibleNamePath(ReadGraph graph, Resource resource) throws DatabaseException {
835 return getPossibleNamePath(graph, resource, null);
838 private static List<String> getPossibleNamePath(ReadGraph graph, Resource resource, List<String> result) throws DatabaseException {
840 Layer0 L0 = Layer0.getInstance(graph);
841 String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
842 if(name == null) return null;
844 if(result == null) result = new ArrayList<String>();
846 SimulationResource SIMU = SimulationResource.getInstance(graph);
847 if(graph.isInstanceOf(resource, SIMU.Model)) return result;
849 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
851 getPossibleNamePath(graph, parent, result);
862 public static Resource claimLibraryPath(WriteGraph graph, Resource resource, List<String> path) throws DatabaseException {
863 Layer0 L0 = Layer0.getInstance(graph);
864 for(int i=0;i<path.size()-1;i++) {
865 String p = path.get(i);
866 Resource child = Layer0Utils.getPossibleChild(graph, resource, p);
868 child = graph.newResource();
869 graph.claim(child, L0.InstanceOf, L0.Library);
870 graph.addLiteral(child, L0.HasName, L0.NameOf, L0.String, p, Bindings.STRING);
871 graph.claim(resource, L0.ConsistsOf, L0.PartOf, child);
880 * database write access
881 * @param sourceContainer
882 * the source container to look for annotationProperty from to be
883 * used as the copy source
884 * @param targetContainer
885 * the target container for the copied annotationProperty
887 * @param annotationProperty
888 * the annotation property relation
889 * @param annotationProperty
890 * the 2nd level annotation property relation or
891 * <code>null</code> if no 2nd level annotation exists
892 * @return created copy of the original annotation or <code>null</code> if
893 * there was nothing to copy
894 * @throws DatabaseException
896 public static Resource copyPossibleAnnotation2(WriteGraph graph,
897 Resource sourceContainer, Resource targetContainer,
898 Resource annotationProperty, Resource entryType) throws DatabaseException {
900 // Delete existing target value first
901 Layer0 L0 = Layer0.getInstance(graph);
902 Resource targetValue = graph.getPossibleObject(targetContainer, annotationProperty);
903 if (targetValue != null) {
904 if(!graph.hasStatement(targetValue, L0.PartOf))
905 RemoverUtil.remove(graph, targetValue);
906 graph.deny(targetContainer, annotationProperty);
909 Resource sourceValue = graph.getPossibleObject(sourceContainer, annotationProperty);
910 if (sourceValue == null)
913 List<String> sourceValuePath = getPossibleNamePath(graph, sourceValue);
914 if(sourceValuePath != null) {
915 Resource targetModel = graph.sync(new PossibleModel(targetContainer));
916 if(targetModel == null) throw new DatabaseException("No target model found for " + targetContainer);
917 Resource library = claimLibraryPath(graph, targetModel, sourceValuePath);
918 Resource existing = Layer0Utils.getPossibleChild(graph, library, sourceValuePath.get(sourceValuePath.size()-1));
919 if(existing == null) {
920 existing = doCopyPossibleAnnotation2(graph, sourceContainer, targetContainer, annotationProperty, entryType);
921 graph.claim(library, L0.ConsistsOf, L0.PartOf, existing);
923 graph.claim(targetContainer, annotationProperty, existing);
926 return doCopyPossibleAnnotation2(graph, sourceContainer, targetContainer, annotationProperty, entryType);
931 private static Resource doCopyPossibleAnnotation2(WriteGraph graph,
932 Resource sourceContainer, Resource targetContainer,
933 Resource annotationProperty, Resource entryType) throws DatabaseException {
935 Resource sourceValue = graph.getPossibleObject(sourceContainer, annotationProperty);
936 if (sourceValue == null)
939 // Copy 1st level annotation
940 Resource targetValue = createAnnotation(graph, targetContainer, annotationProperty, sourceValue);
942 // Copy possible 2nd level annotations and attach to 1st if entry
943 // property is defined.
944 Layer0 L0 = Layer0.getInstance(graph);
945 if (entryType != null) {
947 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
948 for (Resource entry : graph.getObjects(sourceValue, ANNO.Annotation_HasEntry)) {
950 String name = graph.getRelatedValue(entry, L0.HasName, Bindings.STRING);
952 List<String> entryPath = getPossibleNamePath(graph, entry);
953 if(entryPath != null) {
954 Resource targetModel = graph.sync(new PossibleModel(targetContainer));
955 if(targetModel == null) throw new DatabaseException("No target model found for " + targetContainer);
956 Resource library = claimLibraryPath(graph, targetModel, entryPath);
957 Resource existing = Layer0Utils.getPossibleChild(graph, library, entryPath.get(entryPath.size()-1));
958 if(existing == null) {
959 existing = createTypedAnnotation(graph, null, null, entry, entryType, name);
960 graph.claim(library, L0.ConsistsOf, L0.PartOf, existing);
962 graph.claim(targetValue, ANNO.Annotation_HasEntry, existing);
964 Resource result = createTypedAnnotation(graph, null, null, entry, entryType, name);
965 graph.claim(targetValue, ANNO.Annotation_HasEntry, result);
977 * database write access
979 * the container resource to attach the new annotation to
981 * the annotation property relation. The type of the created
982 * annotation is decided from this property relation's range
983 * @param sourceAnnotation
984 * the annotation to copy data from or <code>null</code> if
985 * nothing shall be copied
986 * @return the newly created annotation resource or <code>null</code> if the
987 * annotation property relation didn't have a single range type
988 * @throws DatabaseException
990 public static Resource createAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation) throws DatabaseException {
991 return createAnnotation(graph, container, property, sourceAnnotation, null);
996 * database write access
998 * the container resource to attach the new annotation to
1000 * the annotation property relation. The type of the created
1001 * annotation is decided from this property relation's range
1002 * @param sourceAnnotation
1003 * the annotation to copy data from or <code>null</code> if
1004 * nothing shall be copied
1006 * name for newly created annotation or <code>null</code> to copy
1007 * name from source if available
1008 * @return the newly created annotation resource or <code>null</code> if the
1009 * annotation property relation didn't have a single range type
1010 * @throws DatabaseException
1012 public static Resource createAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation, String name) throws DatabaseException {
1014 Layer0 L0 = Layer0.getInstance(graph);
1015 Resource annotationType = graph.getSingleObject(property, L0.HasRange);
1016 if (annotationType == null)
1019 return createTypedAnnotation(graph, container, property, sourceAnnotation, annotationType, name);
1025 * database write access
1027 * the container resource to attach the new annotation to
1029 * the annotation property relation
1030 * @param sourceAnnotation
1031 * the annotation to copy data from or <code>null</code> if
1032 * nothing shall be copied
1033 * @param annotationType
1034 * the type of the new annotation
1035 * @return the newly created annotation resource
1036 * @throws DatabaseException
1038 public static Resource createTypedAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation, Resource annotationType) throws DatabaseException {
1039 return createTypedAnnotation(graph, container, property, sourceAnnotation, annotationType, null);
1044 * database write access
1046 * the container resource to attach the new annotation to
1048 * the annotation property relation
1049 * @param sourceAnnotation
1050 * the annotation to copy data from or <code>null</code> if
1051 * nothing shall be copied
1052 * @param annotationType
1053 * the type of the new annotation
1055 * name for newly created annotation or <code>null</code> to copy
1056 * name from source if available
1057 * @return the newly created annotation resource
1058 * @throws DatabaseException
1060 public static Resource createTypedAnnotation(WriteGraph graph, Resource container, Resource property, Resource sourceAnnotation, Resource annotationType, String name) throws DatabaseException {
1062 Layer0 L0 = Layer0.getInstance(graph);
1064 Resource anno = graph.newResource();
1066 graph.claim(anno, L0.InstanceOf, null, annotationType);
1069 graph.addLiteral(anno, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
1071 if (sourceAnnotation != null) {
1074 String sourceName = graph.getPossibleRelatedValue(sourceAnnotation, L0.HasName, Bindings.STRING);
1075 if(sourceName != null) graph.addLiteral(anno, L0.HasName, L0.NameOf, L0.String, sourceName, Bindings.STRING);
1078 Resource sourceType = graph.getSingleType(sourceAnnotation);
1079 List<AnnotationMap> am = getAnnotationMap(graph, annotationType, sourceType);
1080 for (AnnotationMap a : am) {
1081 Resource object = graph.getSingleObject(sourceAnnotation, a.sourcePredicate);
1082 Collection<Resource> objectTypes = graph.getTypes(object);
1083 if (objectTypes.contains(L0.Literal)) {
1084 Object value = graph.getValue(object, a.defaultValueBinding);
1085 if (!ObjectUtils.objectEquals(value, a.defaultValue)) {
1086 graph.addLiteral(anno, a.annotationPredicate, a.annotationPredicateInverse, a.defaultValueType, value, a.defaultValueBinding);
1092 if (container != null && property != null) {
1093 graph.claim(container, property, anno);
1100 private static class AnnotationMap {
1102 final public Resource sourcePredicate;
1103 final public Resource sourcePredicateInverse;
1105 final public Resource annotationPredicate;
1106 final public Resource annotationPredicateInverse;
1108 final public Resource defaultValueType;
1109 final public Object defaultValue;
1110 final public Binding defaultValueBinding;
1112 public AnnotationMap(Resource sp, Resource spi, Resource tp, Resource tpi, Resource defaultValueType, Object defaultValue, Binding defaultValueBinding) {
1113 sourcePredicate = sp;
1114 sourcePredicateInverse = spi;
1115 annotationPredicate = tp;
1116 annotationPredicateInverse = tpi;
1117 this.defaultValueType = defaultValueType;
1118 this.defaultValue = defaultValue;
1119 this.defaultValueBinding = defaultValueBinding;
1124 public static List<AnnotationMap> getAnnotationMap(ReadGraph graph, Resource annotationType, Resource sourceType) throws DatabaseException {
1125 return graph.syncRequest(new AnnotationMapRequest(annotationType, sourceType), TransientCacheListener.<List<AnnotationMap>>instance());
1128 static class AnnotationMapRequest extends ResourceRead2<List<AnnotationMap>> {
1130 public AnnotationMapRequest(Resource annotationType, Resource sourceType) {
1131 super(annotationType, sourceType);
1135 public List<AnnotationMap> perform(ReadGraph graph) throws DatabaseException {
1137 Layer0 L0 = Layer0.getInstance(graph);
1139 ArrayList<AnnotationMap> result = new ArrayList<AnnotationMap>();
1141 Map<String, Resource> annotationPredicates = new HashMap<String, Resource>();
1142 for(Resource predicate : graph.getObjects(resource, L0.DomainOf)) {
1143 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
1144 annotationPredicates.put(name, predicate);
1146 Map<String, Resource> sourcePredicates = new HashMap<String, Resource>();
1147 for(Resource predicate : graph.getObjects(resource2, L0.DomainOf)) {
1148 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
1149 sourcePredicates.put(name, predicate);
1152 for(String key : sourcePredicates.keySet()) {
1153 Resource sourcePredicate = sourcePredicates.get(key);
1154 Resource anno = annotationPredicates.get(key);
1155 if(sourcePredicate != null && anno != null) {
1156 Resource defaultValueType = graph.getSingleObject(anno, L0.HasRange);
1157 Binding defaultValueBinding = null;
1158 Object defaultValue = null;
1159 Resource assertion = graph.getPossibleObject(anno, L0.HasPredicateInverse);
1160 if (assertion != null) {
1161 Resource object = graph.getPossibleObject(assertion, L0.HasObject);
1162 if (object != null) {
1163 if(graph.isInstanceOf(object, L0.Literal)) {
1164 Datatype dt = graph.getDataType(object);
1165 defaultValueBinding = Bindings.getBeanBinding(dt);
1166 defaultValue = graph.getPossibleValue(object);
1170 result.add(new AnnotationMap(
1171 sourcePredicate, graph.getInverse(sourcePredicate),
1172 anno, graph.getInverse(anno),
1173 defaultValueType, defaultValue, defaultValueBinding));
1183 public static final String DRAWING_TEMPLATE_FORMAT = "drawingTemplate";
1184 public static final String DRAWING_TEMPLATE_FORMAT_V1 = DRAWING_TEMPLATE_FORMAT + ":1";
1185 public static final String DRAWING_TEMPLATE_FORMAT_V2 = DRAWING_TEMPLATE_FORMAT + ":2";
1187 public static Resource importDrawingTemplate(final Resource model, final File file) throws IOException, SerializationException, DatabaseException, TransferableGraphException {
1191 final Resource library = Simantics.sync(new WriteResultRequest<Resource>() {
1194 public Resource perform(WriteGraph graph) throws DatabaseException {
1196 Layer0 L0 = Layer0.getInstance(graph);
1197 Template2dResource TEMPLATE = Template2dResource.getInstance(graph);
1198 Resource root = graph.getPossibleObject(model, TEMPLATE.HasDrawingTemplateRoot);
1200 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
1201 root = graph.newResource();
1202 graph.claim(root, L0.InstanceOf, null, TEMPLATE2D.DrawingTemplateLibrary);
1203 graph.claim(root, L0.InstanceOf, null, TEMPLATE2D.DrawingTemplateLibraryUI);
1204 graph.claimLiteral(root, L0.HasName, L0.NameOf, L0.String, "Diagram Templates", Bindings.STRING);
1205 graph.claim(root, L0.PartOf, L0.ConsistsOf, model);
1206 graph.claim(model, TEMPLATE2D.HasDrawingTemplateRoot, root);
1209 String name = new Path(file.getAbsolutePath()).removeFileExtension().lastSegment();
1211 Resource existing = Layer0Utils.getPossibleChild(graph, root, name);
1212 if(existing != null)
1213 graph.deny(root, L0.ConsistsOf, existing);
1222 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(library);
1225 HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();
1226 FormatHandler<Object> handler = new FormatHandler<Object>() {
1228 public Binding getBinding() {
1229 return TransferableGraph1.BINDING;
1233 public Object process(DataContainer container) throws Exception {
1234 TransferableGraphs.importGraph1(Simantics.getSession(), (TransferableGraph1)container.content.getValue(),
1239 handlers.put(DRAWING_TEMPLATE_FORMAT_V1, handler);
1240 handlers.put(DRAWING_TEMPLATE_FORMAT_V2, handler);
1242 DataContainers.readFile(file, handlers);
1243 } catch(DataFormatException e) {
1244 throw new IOException(e);
1245 } catch(IOException e) {
1247 } catch(Exception e) {
1248 if(e instanceof RuntimeException)
1249 throw (RuntimeException)e;
1251 throw new RuntimeException(e);
1254 } catch(IOException e) {
1257 return advisor.getRoot();
1259 } catch (Throwable t) {
1260 Logger.defaultLogError("Unexpected exception while importing diagram template.", t);
1268 public static Collection<Variable> getMappedVariables(ReadGraph graph, Variable source) throws DatabaseException {
1270 ArrayList<Variable> result = new ArrayList<Variable>();
1271 Resource represents = source.getPossibleRepresents(graph);
1272 if(represents == null) return Collections.emptyList();
1273 ModelingResources MOD = ModelingResources.getInstance(graph);
1274 for(Resource r : getElementCorrespondendences(graph, represents)) {
1275 result.add(Variables.getVariable(graph, r));
1277 for(Resource r : graph.getObjects(represents, MOD.ComponentToElement)) {
1278 result.add(Variables.getVariable(graph, r));
1280 for(Resource r : graph.getObjects(represents, MOD.DiagramToComposite)) {
1281 result.add(Variables.getVariable(graph, r));
1283 for(Resource r : graph.getObjects(represents, MOD.CompositeToDiagram)) {
1284 result.add(Variables.getVariable(graph, r));
1290 public static Resource getPossibleModel(ReadGraph graph, Resource resource) throws DatabaseException {
1292 PossibleModel pm = new PossibleModel(resource);
1293 Resource model = pm.perform(graph);
1297 public static Resource possibleIndexRoot(ReadGraph graph, Resource resource) throws DatabaseException {
1298 return graph.syncRequest(new PossibleIndexRoot(resource));
1301 public static Object getMonitorValue(StructuralComponent _variable, ReadGraph graph, String path) throws DatabaseException {
1302 Variable variable = ((VariableStructuralContext)_variable).variable;
1303 Variable var = variable.browse(graph, path);
1304 return var.getValue(graph);
1307 public static void createSharedOntologyWithUI(ReadGraph graph, Variable variable, Resource baseType) throws DatabaseException {
1308 createSharedOntologyWithUI(graph, baseType);
1311 public static void createSharedOntologyWithUI(ReadGraph graph, Resource baseType) throws DatabaseException {
1313 // Resource indexRoot_ = variable.getPossibleRepresents(graph);
1314 // if(indexRoot_ == null) return;
1316 final Map<Resource, Pair<String,ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();
1318 Layer0 L0 = Layer0.getInstance(graph);
1319 SimulationResource SIMU = SimulationResource.getInstance(graph);
1320 Instances query = graph.adapt(L0.IndexRootType, Instances.class);
1321 for(Resource ontology : Layer0Utils.listOntologies(graph)) {
1322 for(Resource type : query.find(graph, ontology)) {
1323 if(graph.isInheritedFrom(type, SIMU.Model)) continue;
1324 if(graph.hasStatement(type, L0.Abstract)) continue;
1325 if(!graph.isInheritedFrom(type, baseType)) continue;
1326 String name = graph.getPossibleRelatedValue(type, L0.HasLabel, Bindings.STRING);
1327 if(name == null) name = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
1328 map.put(type, new Pair<String,ImageDescriptor>(name, null));
1332 Display.getDefault().asyncExec(new Runnable() {
1335 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
1336 CreateSharedOntologyDialog page = new CreateSharedOntologyDialog(shell, map, "Select type and name of new shared library");
1337 if (page.open() == Window.OK) {
1338 Object[] result = page.getResult();
1339 if (result != null && result.length == 1) {
1340 final Resource res = (Resource)result[0];
1341 final String name = "http://" + page.getName();
1342 Simantics.getSession().asyncRequest(new WriteRequest() {
1345 public void perform(WriteGraph graph) throws DatabaseException {
1346 graph.markUndoPoint();
1347 Resource target = Simantics.applySCL("Simantics/SharedOntologies", "createSharedOntology", graph, name+"@A", res);
1349 ProjectResource PROJ = ProjectResource.getInstance(graph);
1350 Layer0 L0 = Layer0.getInstance(graph);
1351 for(Resource dep : graph.getObjects(Simantics.getProjectResource(), L0.IsLinkedTo)) {
1352 if(graph.isInstanceOf(dep, PROJ.NamespaceRequirement)) {
1353 for(Resource req : graph.getObjects(dep, PROJ.RequiresNamespace)) {
1354 String uri = graph.getPossibleValue(req, Bindings.STRING);
1356 Resource ns = graph.getResource(uri);
1358 graph.claim(target, L0.IsLinkedTo, null, ns);
1375 public static void unlinkSharedOntologyWithUI(ReadGraph graph, Variable variable, final List<Resource> libraries) throws DatabaseException {
1377 final Resource indexRoot = variable.getPossibleRepresents(graph);
1378 if(indexRoot == null) return;
1380 StructuralResource2 STR = StructuralResource2.getInstance(graph);
1382 final List<String> instances = new ArrayList<String>();
1384 DiagramResource DIA = DiagramResource.getInstance(graph);
1386 for(Resource library : libraries) {
1387 for(Resource type : ModelingUtils.searchByTypeShallow(graph, library, STR.ComponentType)) {
1388 for(Resource instance : ModelingUtils.searchByTypeShallow(graph, indexRoot, type)) {
1390 if(graph.isInstanceOf(instance, DIA.Element)) continue;
1391 String name = Versions.getStandardPathNameString(graph, instance);
1392 instances.add(name);
1397 if(instances.isEmpty()) {
1398 graph.getSession().asyncRequest(new WriteRequest() {
1401 public void perform(WriteGraph graph) throws DatabaseException {
1402 Layer0 L0 = Layer0.getInstance(graph);
1403 for(Resource library : libraries)
1404 graph.deny(indexRoot, L0.IsLinkedTo, library);
1411 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
1415 if (!PlatformUI.isWorkbenchRunning())
1418 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
1419 ListDialog<String> dialog = new ListDialog<String>(
1422 "Cannot unlink selected libraries",
1423 "Libraries cannot be unlinked since the following instances are referring to them.") {
1425 protected void createButtonsForButtonBar(Composite parent) {
1426 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
1430 int result = dialog.open();
1431 if (result != Dialog.OK)
1439 public static MigratedImportResult importSharedOntologyWithResult(String fileName) throws Exception {
1441 DataContainer dc = DataContainers.readFile(new File(fileName));
1442 TransferableGraph1 tg = (TransferableGraph1)dc.content.getValue(TransferableGraph1.BINDING);
1443 Variant draftStatus = dc.metadata.get(DraftStatusBean.EXTENSION_KEY);
1444 return MigrationUtils.importSharedOntology(Simantics.getSession(), tg, draftStatus == null);
1445 } catch (Exception e) {
1446 Logger.defaultLogError(e);
1451 public static void importSharedOntology(String fileName) throws Exception {
1452 importSharedOntologyWithResult(fileName);
1455 public static List<Resource> importSharedOntology2(String fileName) throws Exception {
1456 MigratedImportResult result = importSharedOntologyWithResult(fileName);
1457 return new ArrayList<Resource>(result.roots);
1460 public static void importSharedOntologyWithUI(ReadGraph graph, final Variable variable) throws DatabaseException {
1462 Display.getDefault().asyncExec(new Runnable() {
1466 IStructuredSelection sel = new StructuredSelection(variable);
1467 openWizard(Display.getCurrent(), sel, "org.simantics.modeling.ui.sharedOntologyImportWizard");
1474 public static class LibraryInfo implements Comparable<LibraryInfo> {
1475 public NamedResource library;
1476 public DraftStatusBean draft;
1477 public LibraryInfo(String name, Resource r, DraftStatusBean draft) {
1478 library = new NamedResource(name, r);
1482 public int compareTo(LibraryInfo o) {
1483 return library.compareTo(o.library);
1486 public int hashCode() {
1487 return library.hashCode();
1490 public boolean equals(Object object) {
1493 else if (object == null)
1495 else if (!(object instanceof LibraryInfo))
1497 LibraryInfo info = (LibraryInfo)object;
1498 return info.library.equals(library);
1503 public static void exportSharedOntologyWithUI(final Resource sharedOntology) {
1505 Display.getDefault().asyncExec(new Runnable() {
1509 HintContext hc = new HintContext();
1510 hc.setHint(SelectionHints.KEY_MAIN, sharedOntology);
1511 IStructuredSelection sel = new StructuredSelection(hc);
1512 openWizard(Display.getCurrent(), sel, "org.simantics.modeling.ui.sharedOntologyExportWizard");
1519 public static TransferableGraph1 exportSharedOntology(IProgressMonitor monitor, RequestProcessor processor, File location, String format, int version, final LibraryInfo info) throws DatabaseException, IOException {
1520 SubMonitor mon = SubMonitor.convert(monitor, "Exporting shared library", 100);
1522 // TODO: figure out a way to make the TG go directly into a file
1523 // instead of having it all in memory at once.
1525 SimanticsClipboard clipboard = processor.syncRequest((ReadGraph graph) -> {
1526 CopyHandler ch = graph.adapt(info.library.getResource(), CopyHandler.class);
1527 SimanticsClipboardImpl result = new SimanticsClipboardImpl();
1528 ch.copyToClipboard(graph, result, mon.split(5));
1532 TreeMap<String,Variant> metadata = getExportMetadata();
1533 DraftStatusBean draft = info.draft;
1535 metadata.put(DraftStatusBean.EXTENSION_KEY, new Variant(DraftStatusBean.BINDING ,draft));
1538 for (Set<Representation> object : clipboard.getContents()) {
1539 mon.subTask("Constructing exported material");
1540 TransferableGraph1 tg = ClipboardUtils.accept(processor, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
1543 Variant edb = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
1545 metadata.put(ExternalDownloadBean.EXTENSION_KEY, edb);
1548 if(location != null) {
1549 monitor.subTask("Writing transferable graph");
1550 DataContainers.writeFile(location, new DataContainer(
1552 metadata, new Variant(TransferableGraph1.BINDING, tg)));
1559 throw new DatabaseException("Export failed, no contents to export");
1562 public static TreeMap<String, Variant> getExportMetadata() {
1564 TreeMap<String,Variant> metadata = new TreeMap<String,Variant>();
1565 metadata.put("date", Variant.ofInstance(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date())));
1566 metadata.put("author", Variant.ofInstance(System.getProperty("user.name", "")));
1572 public static void createNewVersionWithoutUI(WriteGraph graph, Resource resource) throws DatabaseException {
1573 VersionInfo info = graph.syncRequest(new VersionInfoRequest(resource));
1574 int currentVersion = Integer.parseInt(info.version);
1575 String result = Integer.toString(currentVersion + 1);
1576 createNewVersion(graph, resource, info, result);
1579 public static void createNewVersionWithUI(ReadGraph graph, final Resource resource) throws DatabaseException {
1581 final VersionInfo info = graph.syncRequest(new VersionInfoRequest(resource));
1583 Display.getDefault().asyncExec(new Runnable() {
1586 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
1587 CreateVersionDialog dialog = new CreateVersionDialog(shell, info);
1588 if (dialog.open() == Window.OK) {
1589 final String result = dialog.getResult();
1590 Simantics.getSession().asyncRequest(new WriteRequest() {
1592 public void perform(WriteGraph graph) throws DatabaseException {
1593 createNewVersion(graph, resource, info, result);
1602 public static void createNewVersion(WriteGraph graph, Resource resource, final VersionInfo info, final String result) throws DatabaseException {
1603 graph.markUndoPoint();
1604 Layer0 L0 = Layer0.getInstance(graph);
1605 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
1606 if(parent == null) return;
1607 final String parentURI = graph.getPossibleURI(parent);
1608 if(parentURI == null) return;
1609 Layer0Utils.copyTo(graph, parent, resource, new PasteEventHandler() {
1612 public void postProcess(WriteGraph graph, Resource root) throws DatabaseException {
1613 Layer0 L0 = Layer0.getInstance(graph);
1614 graph.deny(root, L0.Entity_published);
1618 public IImportAdvisor2 createAdvisor(ReadGraph graph, ImportAdvisorFactory factory, Resource target) throws DatabaseException {
1619 Map<String,Object> context = new HashMap<String,Object>();
1620 String base = parentURI + "/" + URIStringUtils.escape( info.baseName ) + "@";
1621 Map<String,String> renameMap = new HashMap<String,String>();
1622 renameMap.put(base + info.version, base + result);
1623 renameMap.put(info.baseName + "@" + info.version, info.baseName + "@" + result);
1624 context.put(ImportAdvisors.RENAME_MAP, renameMap);
1625 return factory.create(graph, target, context);
1629 Layer0Utils.addCommentMetadata(graph, "Created new version of " + info.baseName);
1632 public static boolean isUserComponent(ReadGraph graph, Resource type) throws DatabaseException {
1633 StructuralResource2 STR = StructuralResource2.getInstance(graph);
1634 if(graph.isInstanceOf(type, STR.ProceduralComponentType)) return true;
1635 else if (graph.hasStatement(type, STR.IsDefinedBy)) return true;
1639 public static void publishComponentTypeWithUI(WriteGraph graph, final Resource componentType) throws DatabaseException {
1641 Layer0 L0 = Layer0.getInstance(graph);
1642 StructuralResource2 STR = StructuralResource2.getInstance(graph);
1643 Resource composite = graph.getPossibleObject(componentType, STR.IsDefinedBy);
1644 final List<String> instances = new ArrayList<String>();
1645 if(composite != null) {
1646 for(Resource component : graph.syncRequest(new ObjectsWithType(composite, L0.ConsistsOf, STR.Component))) {
1647 Resource type = graph.getPossibleType(component, STR.Component);
1648 if(type != null && isUserComponent(graph, type)) {
1649 if(!Layer0Utils.isPublished(graph, type)) instances.add(Versions.getStandardPathNameString(graph, component));
1654 if(instances.isEmpty()) {
1655 graph.getSession().asyncRequest(new WriteRequest() {
1658 public void perform(WriteGraph graph) throws DatabaseException {
1659 graph.markUndoPoint();
1660 publish(graph, componentType);
1667 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
1671 if (!PlatformUI.isWorkbenchRunning())
1674 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
1675 ListDialog<String> dialog = new ListDialog<String>(
1678 "Cannot publish user component",
1679 "The following instances are referring to unpublished user components.") {
1681 protected void createButtonsForButtonBar(Composite parent) {
1682 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
1686 int result = dialog.open();
1687 if (result != Dialog.OK)
1695 public static void publishSharedOntologyWithUI(WriteGraph graph, final Resource sharedOntology) throws DatabaseException {
1697 Layer0 L0 = Layer0.getInstance(graph);
1698 DiagramResource DIA = DiagramResource.getInstance(graph);
1699 StructuralResource2 STR = StructuralResource2.getInstance(graph);
1700 final List<String> instances = new ArrayList<String>();
1701 for(Resource type : searchByTypeShallow(graph, sharedOntology, STR.ComponentType)) {
1703 if(graph.isInheritedFrom(type, DIA.Element)) continue;
1704 if(!Layer0Utils.isPublished(graph, type)) instances.add(Versions.getStandardPathNameString(graph, type));
1706 for(Resource dep : graph.syncRequest(new ObjectsWithType(sharedOntology, L0.IsLinkedTo, L0.SharedOntology))) {
1707 if(!Layer0Utils.isPublished(graph, dep)) instances.add(Versions.getStandardPathNameString(graph, dep));
1710 if(instances.isEmpty()) {
1711 graph.getSession().asyncRequest(new WriteRequest() {
1714 public void perform(WriteGraph graph) throws DatabaseException {
1715 graph.markUndoPoint();
1716 publish(graph, sharedOntology);
1723 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
1727 if (!PlatformUI.isWorkbenchRunning())
1730 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
1731 ListDialog<String> dialog = new ListDialog<String>(
1734 "Cannot publish shared library",
1735 "The following dependencies are unpublished.") {
1737 protected void createButtonsForButtonBar(Composite parent) {
1738 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
1742 int result = dialog.open();
1743 if (result != Dialog.OK)
1751 public static Resource createSCLModuleDefault(WriteGraph graph, Resource target) throws DatabaseException {
1752 String name = NameUtils.findFreshEscapedName(graph, "SCLModule", target);
1753 return createSCLModule(graph, target, name);
1756 public static Resource createSCLModule(WriteGraph graph, Resource target, String name) throws DatabaseException {
1757 graph.markUndoPoint();
1758 Layer0 L0 = Layer0.getInstance(graph);
1759 Resource sclModule = GraphUtils.create2(graph, L0.SCLModule,
1762 L0.SCLModule_definition, "");
1763 Layer0Utils.addCommentMetadata(graph, "Created SCL Module " + name + " " + sclModule.toString());
1767 public static Resource setSCLModuleDefinition(WriteGraph graph, Resource module, String definition) throws DatabaseException {
1768 graph.markUndoPoint();
1769 Layer0 L0 = Layer0.getInstance(graph);
1770 graph.claimLiteral(module, L0.SCLModule_definition, definition, Bindings.STRING);
1771 Layer0Utils.addCommentMetadata(graph, "Set SCL module " + module + " definition.");
1775 public static Resource createSCLScriptDefault(WriteGraph graph, Resource target) throws DatabaseException {
1776 String name = NameUtils.findFreshEscapedName(graph, "SCLScript", target);
1777 return createSCLScript(graph, target, name);
1780 public static Resource createSCLScript(WriteGraph graph, Resource target, String name) throws DatabaseException {
1781 graph.markUndoPoint();
1782 Layer0 L0 = Layer0.getInstance(graph);
1783 Resource sclModule = GraphUtils.create2(graph, L0.SCLScript,
1786 L0.SCLScript_definition, "");
1787 Layer0Utils.addCommentMetadata(graph, "Created SCL Script " + name + " " + sclModule.toString());
1791 public static Resource setSCLScriptDefinition(WriteGraph graph, Resource script, String definition) throws DatabaseException {
1792 graph.markUndoPoint();
1793 Layer0 L0 = Layer0.getInstance(graph);
1794 graph.claimLiteral(script, L0.SCLScript_definition, definition, Bindings.STRING);
1795 Layer0Utils.addCommentMetadata(graph, "Set SCL script " + script + " definition.");
1799 public static Resource createPGraphDefault(WriteGraph graph, Resource target) throws DatabaseException {
1800 String name = NameUtils.findFreshEscapedName(graph, "Ontology Definition File", target);
1801 return createPGraph(graph, target, name);
1804 public static Resource createPGraph(WriteGraph graph, Resource target, String name) throws DatabaseException {
1805 graph.markUndoPoint();
1806 Layer0 L0 = Layer0.getInstance(graph);
1807 Resource file = GraphUtils.create2(graph, L0.PGraph,
1810 L0.PGraph_definition, "");
1811 Layer0Utils.addCommentMetadata(graph, "Created Ontology Definition File " + name + " " + file.toString());
1815 public static Resource setPGraphDefinition(WriteGraph graph, Resource pgraph, String definition) throws DatabaseException {
1816 graph.markUndoPoint();
1817 Layer0 L0 = Layer0.getInstance(graph);
1818 graph.claimLiteral(pgraph, L0.PGraph_definition, definition, Bindings.STRING);
1819 Layer0Utils.addCommentMetadata(graph, "Set PGraph " + pgraph + " definition.");
1823 public static void publish(WriteGraph graph, Resource target) throws DatabaseException {
1824 Layer0 L0 = Layer0.getInstance(graph);
1825 graph.claimLiteral(target, L0.Entity_published, true, Bindings.BOOLEAN);
1826 Layer0Utils.addCommentMetadata(graph, "Published " + graph.getPossibleRelatedValue2(target, L0.HasName, Bindings.STRING) + " " + target.toString());
1829 public static boolean isLinkedToDeep(ReadGraph graph, Resource r1, Resource r2) throws DatabaseException {
1830 return graph.syncRequest(new IsLinkedTo(r1, r2));
1833 public static void openWizard(Display display, IStructuredSelection selection, String id) {
1834 // First see if this is a "new wizard".
1835 IWizardDescriptor descriptor = PlatformUI.getWorkbench()
1836 .getNewWizardRegistry().findWizard(id);
1837 // If not check if it is an "import wizard".
1838 if (descriptor == null) {
1839 descriptor = PlatformUI.getWorkbench().getImportWizardRegistry()
1842 // Or maybe an export wizard
1843 if (descriptor == null) {
1844 descriptor = PlatformUI.getWorkbench().getExportWizardRegistry()
1848 // Then if we have a wizard, open it.
1849 if (descriptor != null) {
1850 IWorkbenchWizard wizard = descriptor.createWizard();
1851 wizard.init(PlatformUI.getWorkbench(), selection);
1852 WizardDialog wd = new WizardDialog(display.getActiveShell(), wizard);
1853 wd.setTitle(wizard.getWindowTitle());
1856 } catch (CoreException e) {
1857 e.printStackTrace();
1861 public static String withinEpsilon(double value, double reference, double epsilon) {
1862 if(Math.abs(value-reference) < epsilon) return "True";
1863 else return "Not within epsilon value=" + value + ", reference=" + reference + " , epsilon=" + epsilon;
1866 public static boolean needsIdentifier(ReadGraph graph, Resource r) throws DatabaseException {
1867 Layer0 L0 = Layer0.getInstance(graph);
1868 Collection<Resource> types = graph.getPrincipalTypes(r);
1869 for(Resource type : types)
1870 if(graph.syncRequest(new IsInstanceOf(type, L0.TypeWithIdentifier), TransientCacheAsyncListener.<Boolean>instance()))
1875 public static boolean needsModificationInfo(ReadGraph graph, Resource r) throws DatabaseException {
1876 ModelingResources MOD = ModelingResources.getInstance(graph);
1877 Collection<Resource> types = graph.getPrincipalTypes(r);
1878 for(Resource type : types)
1879 if(graph.syncRequest(new IsInstanceOf(type, MOD.TypeWithChangeInformation), TransientCacheAsyncListener.<Boolean>instance()))
1884 public static void attachCreationInformation(IProgressMonitor monitor, WriteGraph graph, Resource model) throws DatabaseException {
1886 if(monitor == null) monitor = new NullProgressMonitor();
1888 final String author = System.getProperty("user.name", "");
1889 final long time = System.currentTimeMillis();
1891 monitor.setTaskName("Attach creation information");
1893 ModelingResources MOD = ModelingResources.getInstance(graph);
1894 Collection<Resource> rs = ModelingUtils.searchByType(graph, model, MOD.TypeWithChangeInformation);
1895 Collection<Resource> supers = ModelingUtils.getMostUnspecificTypes(graph, rs);
1897 CollectionSupport cs = graph.getService(CollectionSupport.class);
1898 Collection<Resource> set = cs.createSet();
1899 for(Resource type : supers) {
1900 set.addAll(ModelingUtils.searchByTypeShallow(graph, model, type));
1902 Collection<Resource> instances = Layer0Utils.sortByCluster(graph, set);
1904 int pc = instances.size() / 100;
1908 for(Resource instance : instances) {
1909 ChangeInformation info = graph.getPossibleRelatedValue(instance, MOD.changeInformation, ChangeInformation.BINDING);
1911 info = new ChangeInformation();
1912 info.createdAt = time;
1913 info.createdBy = author;
1914 info.modifiedAt = time;
1915 info.modifiedBy = author;
1916 graph.claimLiteral(instance, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
1921 Double d = (100.0*done)/instances.size();
1922 monitor.setTaskName("Attach creation information " + d.intValue() + "%");
1927 monitor.setTaskName("Attach creation information - commit");
1931 public static class DiagramComponentInfo {
1933 private static String CHILD_PREFIX = "child:";
1935 final private String compositePathAndName;
1936 final private String componentName;
1937 final private GUID guid;
1939 public DiagramComponentInfo(String compositePathAndName, String componentName, GUID guid) {
1940 this.compositePathAndName = compositePathAndName;
1941 this.componentName = componentName;
1945 public static boolean isDiagramComponent(String tgName) {
1946 return tgName.startsWith(CHILD_PREFIX);
1949 public String getTGName(CompositeInfo info) {
1950 return CHILD_PREFIX + info.getOriginalPath() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + info.getEscapedName() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + getEscapedComponentName() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + guid.indexString();
1953 public boolean existsGUID(ReadGraph graph, Resource indexRoot) throws DatabaseException {
1954 Collection<Resource> res = ModelingUtils.searchByGUID(graph, indexRoot, guid);
1955 return !res.isEmpty();
1958 public static DiagramComponentInfo fromResource(ReadGraph graph, CompositeInfo info, Resource resource) throws DatabaseException {
1959 Layer0 L0 = Layer0.getInstance(graph);
1960 GUID childId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
1961 String childName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
1962 return new DiagramComponentInfo(info.getStateKey(), URIStringUtils.escape(childName), childId);
1965 public GUID getGUID() {
1969 public String getEscapedCompositePathAndName() {
1970 return compositePathAndName;
1973 public String getEscapedComponentName() {
1974 return componentName;
1977 public String getUnescapedComponentName() {
1978 return URIStringUtils.unescape(getEscapedComponentName());
1981 // "child:path#compositeName#componentName#guid"
1982 public static DiagramComponentInfo parse(String tgName) {
1984 String name = tgName.substring(CHILD_PREFIX.length());
1985 String compositePathAndName = "";
1986 String moduleName = name;
1987 GUID guid = GUID.invalid();
1988 int lastHash = name.lastIndexOf(ModelingUtils.COMPOSITE_SEPARATOR_CHAR);
1990 String first = name.substring(0, lastHash);
1991 String second = name.substring(lastHash+1);
1992 lastHash = first.lastIndexOf(ModelingUtils.COMPOSITE_SEPARATOR_CHAR);
1994 compositePathAndName = first.substring(0, lastHash);
1995 moduleName = first.substring(lastHash+1);
1996 guid = GUID.parseIndexString(second);
1998 compositePathAndName = first;
1999 moduleName = second;
2002 return new DiagramComponentInfo(compositePathAndName, moduleName, guid);
2008 public int hashCode() {
2009 final int prime = 31;
2011 result = prime * result + ((compositePathAndName == null) ? 0 : compositePathAndName.hashCode());
2012 result = prime * result + ((componentName == null) ? 0 : componentName.hashCode());
2017 public boolean equals(Object obj) {
2022 if (getClass() != obj.getClass())
2024 DiagramComponentInfo other = (DiagramComponentInfo) obj;
2025 if (compositePathAndName == null) {
2026 if (other.compositePathAndName != null)
2028 } else if (!compositePathAndName.equals(other.compositePathAndName))
2030 if (componentName == null) {
2031 if (other.componentName != null)
2033 } else if (!componentName.equals(other.componentName))
2040 public static class CompositeInfo {
2042 public static String COMPOSITE_PREFIX = "composite:";
2044 final private boolean useGuids;
2045 final private boolean applyPaths;
2046 final private String path;
2047 final private String name;
2048 final private GUID guid;
2050 private CompositeInfo(boolean useGuids, boolean applyPaths, String path, String name, GUID guid) {
2051 this.useGuids = useGuids;
2052 this.applyPaths = applyPaths;
2059 public String toString() {
2060 return "CompositeInfo[useGuids=" + useGuids + ", applyPaths=" + applyPaths + ", path=" + path + ", name=" + name + ", guid=" + guid.indexString() + "]";
2063 public static boolean isComposite(String tgName) {
2064 return tgName.startsWith(COMPOSITE_PREFIX);
2067 public String getTGName() {
2068 return COMPOSITE_PREFIX + getOriginalPath() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + getEscapedName() + ModelingUtils.COMPOSITE_SEPARATOR_CHAR + guid.indexString();
2071 public boolean existsGUID(ReadGraph graph, Resource indexRoot) throws DatabaseException {
2072 Collection<Resource> res = ModelingUtils.searchByGUID(graph, indexRoot, guid);
2073 return !res.isEmpty();
2076 public static CompositeInfo fromResource(ReadGraph graph, Resource resource) throws DatabaseException {
2078 Layer0 L0 = Layer0.getInstance(graph);
2079 GUID rootId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
2080 String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
2081 String escapedRootName = URIStringUtils.escape(rootName);
2082 String escapedPath = ModelingUtils.getDiagramCompositePath(graph, resource);
2083 return new CompositeInfo(true, true, escapedPath, escapedRootName, rootId);
2087 public static CompositeInfo parse(String tgName) {
2088 return parse(tgName, true, true);
2092 * Index 0 is root-relative folder path separated with '/' or null if target-relative positioning is used
2093 * Index 1 is diagram name
2095 public static CompositeInfo parse(String tgName, boolean applyPaths, boolean useGuids) {
2096 if(!tgName.startsWith(COMPOSITE_PREFIX)) return null;
2097 tgName = tgName.substring(COMPOSITE_PREFIX.length());
2098 if(!tgName.contains(COMPOSITE_SEPARATOR)) {
2099 if(useGuids) throw new IllegalStateException("GUID identifiers were not found for diagrams.");
2100 return new CompositeInfo(useGuids, applyPaths, null, tgName, null);
2102 String[] parts = tgName.split(COMPOSITE_SEPARATOR);
2103 if(parts.length == 2) {
2104 String name = parts[1];
2105 String path = applyPaths ? parts[0] : null;
2106 if(useGuids) throw new IllegalStateException("GUID identifiers were not found for diagrams.");
2107 return new CompositeInfo(useGuids, applyPaths, path, name, null);
2108 } else if(parts.length == 3) {
2109 String path = parts[0];
2110 String name = parts[1];
2111 GUID guid = GUID.parseIndexString(parts[2]);
2112 return new CompositeInfo(useGuids, applyPaths, path, name, guid);
2118 public GUID getGUID() {
2122 public String getEscapedName() {
2126 public String getUnescapedName() {
2127 return URIStringUtils.unescape(name);
2130 public String getOriginalPath() {
2134 public String getFinalPath() {
2135 if(applyPaths) return path;
2139 public CompositeInfo renamed(String newName) {
2140 return new CompositeInfo(useGuids, applyPaths, path, newName, guid);
2143 public String getStateKey() {
2144 return path + "#" + getEscapedName();
2147 private Resource getFromFolder(ReadGraph graph, Resource target) throws DatabaseException {
2148 Resource diagram = Layer0Utils.getPossibleChild(graph, target, URIStringUtils.unescape(name));
2149 if(diagram == null) return null;
2150 StructuralResource2 STR = StructuralResource2.getInstance(graph);
2151 return graph.isInstanceOf(diagram, STR.Composite) ? diagram : null;
2154 public Resource resolve(ReadGraph graph, Resource target) throws DatabaseException {
2156 if(useGuids && guid != null) {
2157 Resource indexRoot = graph.syncRequest(new IndexRoot(target));
2158 Collection<Resource> queryResult = searchByGUID(graph, indexRoot, guid);
2159 if(queryResult.size() == 1) {
2160 Resource composite = queryResult.iterator().next();
2161 StructuralResource2 STR = StructuralResource2.getInstance(graph);
2162 if(!graph.isInstanceOf(composite, STR.Composite)) return null;
2168 Resource folder = resolveFolder(graph, target);
2169 if(folder == null) return null;
2170 return getFromFolder(graph, folder);
2172 return getFromFolder(graph, target);
2177 public Resource resolveFolder(ReadGraph graph, Resource target) throws DatabaseException {
2179 String path = getFinalPath();
2180 if(path == null) return target;
2182 Resource folder = graph.syncRequest(new Configuration(target));
2183 String[] segments = path.split("/");
2184 for(int i=0;i<segments.length;i++) {
2185 if(segments[i].isEmpty()) continue;
2186 folder = Layer0Utils.getPossibleChild(graph, folder, URIStringUtils.unescape(segments[i]));
2187 if(folder == null) return null;
2196 public int hashCode() {
2198 if(useGuids) return guid.hashCode();
2200 final int prime = 31;
2201 int result = name.hashCode();
2202 result = prime * result + ((path == null) ? 0 : path.hashCode());
2208 public boolean equals(Object obj) {
2214 if (getClass() != obj.getClass())
2217 CompositeInfo other = (CompositeInfo) obj;
2219 if(useGuids) return guid.equals(other.guid);
2221 if (!name.equals(other.name))
2225 if (!path.equals(other.path))
2234 public static char COMPOSITE_SEPARATOR_CHAR = '#';
2235 public static String COMPOSITE_SEPARATOR = String.valueOf(COMPOSITE_SEPARATOR_CHAR);
2237 public static CompositeInfo parseCompositeNameFromRoot(Root root, boolean applyPaths, boolean useGuids) {
2238 return CompositeInfo.parse(root.name, applyPaths, useGuids);
2241 public static CompositeInfo parseCompositeNameFromRoot(Identity root, boolean applyPaths, boolean useGuids) {
2242 String coded = TransferableGraphUtils.getName(root);
2243 return CompositeInfo.parse(coded, applyPaths, useGuids);
2246 private static StringBuilder getDiagramCompositePathInternal(ReadGraph graph, Resource folder, StringBuilder builder) throws DatabaseException {
2247 SimulationResource SIMU = SimulationResource.getInstance(graph);
2248 Resource model = graph.getPossibleObject(folder, SIMU.IsConfigurationOf);
2249 if (model != null) return builder;
2251 Layer0 L0 = Layer0.getInstance(graph);
2252 String name = graph.getPossibleRelatedValue(folder, L0.HasName, Bindings.STRING);
2253 if (name == null) return null;
2254 Resource parent = graph.getPossibleObject(folder, L0.PartOf);
2255 if (parent == null) return null;
2257 StringBuilder sb = getDiagramCompositePathInternal(graph, parent, builder);
2258 if (sb == null) return null;
2259 if (sb.length() > 0)
2260 sb.append(URIStringUtils.NAMESPACE_PATH_SEPARATOR);
2261 sb.append( URIStringUtils.escape(name) );
2268 * @return diagram/folder path up until model configuration root with each
2269 * segment escaped using {@link URIStringUtils#escape(String)} and
2270 * {@value URIStringUtils#NAMESPACE_PATH_SEPARATOR} between each
2271 * segment or <code>null</code> if the specified diagram composite
2272 * is not part of any model configuration structure.
2273 * @throws DatabaseException
2275 public static String getDiagramCompositePath(ReadGraph graph, Resource diagram) throws DatabaseException {
2276 Layer0 L0 = Layer0.getInstance(graph);
2277 Resource parent = graph.getPossibleObject(diagram, L0.PartOf);
2278 if(parent == null) return null;
2279 StringBuilder sb = getDiagramCompositePathInternal(graph, parent, new StringBuilder());
2280 return sb != null ? sb.toString() : null;
2283 public static void exportModel(ReadGraph graph, Resource model, String fileName, String format, int version) throws DatabaseException {
2284 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, model, true, false);
2285 exportModel(graph, conf, fileName, format, version);
2288 public static void exportModel(ReadGraph graph, TransferableGraphConfiguration2 conf, String fileName, String format, int version) throws DatabaseException {
2291 TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
2292 TransferableGraphs.writeTransferableGraph(graph, format, version, s, new File(fileName));
2293 } catch (DatabaseException e) {
2295 } catch (Exception e) {
2296 throw new DatabaseException(e);
2301 public static TransferableGraph1 exportSharedOntology(ReadGraph graph, Resource library, String fileName, String format, int version) throws DatabaseException {
2303 Layer0 L0 = Layer0.getInstance(graph);
2304 String name = graph.getRelatedValue(library, L0.HasName, Bindings.STRING);
2306 DraftStatusBean draft = null;
2307 boolean published = Layer0Utils.isPublished(graph, library);
2308 if(!published) draft = new DraftStatusBean(new String[0]);
2310 LibraryInfo info = new LibraryInfo(name, library, draft);
2313 return exportSharedOntology(new NullProgressMonitor(), graph, fileName != null ? new File(fileName) : null, format, version, info);
2314 } catch (IOException e) {
2315 throw new DatabaseException(e);
2320 public static DraftStatusBean getDependencyDraftStatus(ReadGraph graph, Resource library) throws DatabaseException {
2321 Layer0 L0 = Layer0.getInstance(graph);
2322 List<String> drafts = new ArrayList<>();
2323 for (Resource shared : graph.syncRequest(new ObjectsWithType(library, L0.IsLinkedTo, L0.SharedOntology))) {
2324 boolean published = Layer0Utils.isPublished(graph, shared);
2326 drafts.add(graph.getURI(shared));
2328 return drafts.isEmpty() ? null : new DraftStatusBean(drafts);
2331 public static Set<Resource> getMostUnspecificTypes(final ReadGraph graph, Collection<Resource> types) throws DatabaseException {
2333 final Set<Resource> work = new HashSet<Resource>(types);
2334 for(Resource type : types) {
2335 Set<Resource> supers = graph.getSupertypes(type);
2336 if(!Collections.disjoint(supers, work)) work.remove(type);
2343 public static void rewriteGUIDS(WriteGraph graph, Resource root, boolean deep) throws DatabaseException {
2344 List<Resource> todo = new ArrayList<Resource>();
2346 Layer0 L0 = Layer0.getInstance(graph);
2347 while(!todo.isEmpty()) {
2348 Resource resource = todo.remove(todo.size()-1);
2349 Layer0Utils.claimNewIdentifier(graph, resource, false);
2351 todo.addAll(graph.getObjects(resource, L0.ConsistsOf));
2355 public static void createMissingGUIDs(IProgressMonitor monitor, WriteGraph graph, Collection<Resource> roots) throws DatabaseException {
2357 if(monitor == null) monitor = new NullProgressMonitor();
2359 Layer0 L0 = Layer0.getInstance(graph);
2361 // Allow this process to make 50k queries
2362 QueryMemoryWatcher memory = new QueryMemoryWatcher(graph, 50000);
2364 for(Resource root : roots) {
2366 boolean madeChanges = false;
2368 monitor.setTaskName("Creating missing GUID identifiers " + NameUtils.getSafeName(graph, root));
2369 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(root));
2370 for(Resource r : searchByTypeShallow(graph, indexRoot, L0.Entity)) {
2374 if(graph.isImmutable(r)) continue;
2376 if(!ModelingUtils.needsIdentifier(graph, r)) continue;
2378 GUID existing = graph.getPossibleRelatedValue(r, L0.identifier, GUID.BINDING);
2379 if(existing == null) {
2380 Layer0Utils.claimNewIdentifier(graph, r, true);
2387 ModelingUtils.deleteIndex(graph, root);
2394 public static boolean activateModel(WriteGraph graph, Resource model) throws DatabaseException {
2395 return graph.syncRequest(new ActivateModel(Simantics.getProjectResource(), model));
2398 public static File fileDialog(String title, List<Tuple> namesAndExtensions) {
2399 return new Runnable() {
2400 private File result;
2403 Display.getDefault().syncExec(this);
2409 result = showDialog();
2412 private File showDialog() {
2413 Display display = Display.getCurrent();
2414 Shell shell = display.getActiveShell();
2416 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
2417 dialog.setText(title);
2419 String[] extensions = new String[namesAndExtensions.size()];
2420 String[] filterNames = new String[namesAndExtensions.size()];
2422 for(Tuple t : namesAndExtensions) {
2423 String filterName = (String)t.get(0);
2424 String extension = (String)t.get(1);
2425 filterNames[index] = filterName;
2426 extensions[index] = extension;
2430 dialog.setFilterExtensions(extensions);
2431 dialog.setFilterNames(filterNames);
2432 final String fileName = dialog.open();
2433 if (fileName == null) return null;
2435 return new File(fileName);
2440 public static Resource createLibrary(WriteGraph graph, Resource parent) throws DatabaseException {
2441 Layer0 l0 = Layer0.getInstance(graph);
2442 return createLibrary(graph, parent, NameUtils.findFreshName(graph, "Library", parent, l0.ConsistsOf));
2445 public static Resource createLibrary(WriteGraph graph, Resource parent, String name) throws DatabaseException {
2446 graph.markUndoPoint();
2447 Layer0 l0 = Layer0.getInstance(graph);
2449 Resource library = graph.newResource();
2450 graph.claim(library, l0.InstanceOf, null, l0.Library);
2451 graph.addLiteral(library, l0.HasName, l0.NameOf, l0.String, name, Bindings.STRING);
2452 graph.claim(library, l0.PartOf, parent);
2454 Layer0Utils.addCommentMetadata(graph, "Created new Library named " + name + ", resource " + library);
2459 public static IModelingRules getModelingRules(ReadGraph graph, Resource diagramResource) throws DatabaseException {
2460 return DiagramGraphUtil.getModelingRules(graph, diagramResource, null);
2463 //-------------------------------------------------------------------------
2465 private static final String VG_CHANGE_INFORMATION = "changeInformation"; //$NON-NLS-1$
2467 public static void markChanged(WriteGraph graph, Resource r) throws DatabaseException {
2468 VirtualGraph vg = Simantics.getSession().getService(VirtualGraphSupport.class)
2469 .getWorkspacePersistent(VG_CHANGE_INFORMATION);
2470 ModelingResources MOD = ModelingResources.getInstance(graph);
2471 graph.syncRequest(new WriteOnlyRequest(vg) {
2473 public void perform(WriteOnlyGraph graph) throws DatabaseException {
2474 graph.claim(r, MOD.changed, MOD.changed, r);
2479 public static void markChanged(RequestProcessor processor, Iterable<Resource> rs) throws DatabaseException {
2480 VirtualGraph vg = Simantics.getSession().getService(VirtualGraphSupport.class)
2481 .getWorkspacePersistent(VG_CHANGE_INFORMATION);
2482 ModelingResources MOD = ModelingResources.getInstance(processor);
2483 processor.syncRequest(new WriteOnlyRequest(vg) {
2485 public void perform(WriteOnlyGraph graph) throws DatabaseException {
2486 for (Resource r : rs) {
2487 graph.claim(r, MOD.changed, MOD.changed, r);