1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
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.typicals;
14 import java.awt.geom.Point2D;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.simantics.Simantics;
28 import org.simantics.databoard.Bindings;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Statement;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.common.CommentMetadata;
34 import org.simantics.db.common.NamedResource;
35 import org.simantics.db.common.primitiverequest.Adapter;
36 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
37 import org.simantics.db.common.request.ObjectsWithType;
38 import org.simantics.db.common.request.PossibleIndexRoot;
39 import org.simantics.db.common.request.WriteRequest;
40 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
41 import org.simantics.db.common.utils.CommonDBUtils;
42 import org.simantics.db.common.utils.NameUtils;
43 import org.simantics.db.exception.DatabaseException;
44 import org.simantics.db.layer0.adapter.Instances;
45 import org.simantics.db.layer0.request.ActiveModels;
46 import org.simantics.db.layer0.util.RemoverUtil;
47 import org.simantics.diagram.content.ConnectionUtil;
48 import org.simantics.diagram.handler.CopyPasteStrategy;
49 import org.simantics.diagram.handler.ElementObjectAssortment;
50 import org.simantics.diagram.handler.PasteOperation;
51 import org.simantics.diagram.handler.Paster;
52 import org.simantics.diagram.handler.Paster.RouteLine;
53 import org.simantics.diagram.stubs.DiagramResource;
54 import org.simantics.diagram.synchronization.CollectingModificationQueue;
55 import org.simantics.diagram.synchronization.CopyAdvisor;
56 import org.simantics.diagram.synchronization.SynchronizationHints;
57 import org.simantics.diagram.synchronization.graph.GraphSynchronizationContext;
58 import org.simantics.diagram.ui.DiagramModelHints;
59 import org.simantics.document.DocumentResource;
60 import org.simantics.g2d.canvas.ICanvasContext;
61 import org.simantics.g2d.diagram.DiagramClass;
62 import org.simantics.g2d.diagram.IDiagram;
63 import org.simantics.g2d.diagram.impl.Diagram;
64 import org.simantics.layer0.Layer0;
65 import org.simantics.modeling.ModelingResources;
66 import org.simantics.modeling.ModelingUtils;
67 import org.simantics.modeling.mapping.ModelingSynchronizationHints;
68 import org.simantics.modeling.typicals.rules.AuxKeys;
69 import org.simantics.modeling.typicals.rules.FlagRule;
70 import org.simantics.modeling.typicals.rules.InstanceOfRule;
71 import org.simantics.modeling.typicals.rules.LabelRule;
72 import org.simantics.modeling.typicals.rules.MonitorRule;
73 import org.simantics.modeling.typicals.rules.NameRule;
74 import org.simantics.modeling.typicals.rules.ProfileMonitorRule;
75 import org.simantics.modeling.typicals.rules.SVGElementRule;
76 import org.simantics.modeling.typicals.rules.TransformRule;
77 import org.simantics.scenegraph.g2d.events.command.Commands;
78 import org.simantics.scl.runtime.function.Function4;
79 import org.simantics.structural.stubs.StructuralResource2;
80 import org.simantics.utils.datastructures.MapSet;
81 import org.simantics.utils.strings.AlphanumComparator;
82 import org.simantics.utils.strings.EString;
83 import org.simantics.utils.ui.ErrorLogger;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
87 import gnu.trove.map.hash.THashMap;
88 import gnu.trove.set.hash.THashSet;
91 * A write request that synchronizes typical master templates and their
92 * instances as specified.
95 * Use {@link #SyncTypicalTemplatesToInstances(Resource[], MapSet)} to
96 * synchronize all instances of specified templates. Use
97 * {@link #syncSingleInstance(Resource)} to synchronize a single typical
98 * instance with its master template.
100 * @author Tuukka Lehtonen
102 * @see ReadTypicalInfo
104 * @see TypicalSynchronizationMetadata
106 public class SyncTypicalTemplatesToInstances extends WriteRequest {
107 private static final Logger LOGGER = LoggerFactory.getLogger(SyncTypicalTemplatesToInstances.class);
110 * A constant used as the second argument to
111 * {@link #SyncTemplates(Resource[], MapSet)} for stating that all specified
112 * templates should be fully synchronized to their instances.
114 * This is useful for forcing complete synchronization and unit testing.
116 public static final EmptyMapSet ALL = EmptyMapSet.INSTANCE;
118 public static class EmptyMapSet extends MapSet<Resource, Resource> {
120 public static final EmptyMapSet INSTANCE = new EmptyMapSet();
122 public EmptyMapSet() {
123 this.sets = Collections.emptyMap();
127 protected Set<Resource> getOrCreateSet(Resource key) {
128 throw new UnsupportedOperationException("immutable constant instance");
133 protected static final boolean DEBUG = false;
137 final private IProgressMonitor monitor;
139 * Typical diagram rules to apply
141 protected Set<Resource> selectedRules;
144 * Typical diagram templates to synchronize with their instances.
146 protected Resource[] templates;
149 * Typical diagram instances to synchronize with their templates.
151 protected Resource[] instances;
154 * For each template diagram in {@link #templates}, shall contain a set of
155 * elements that have changed and should be synchronized into the instance
156 * diagrams. Provided as an argument by the client. Allows optimizing
157 * real-time synchronization by not processing everything all the time.
159 * If the value is {@link #ALL}, all elements of the template shall be fully
162 protected MapSet<Resource, Resource> changedElementsByDiagram;
167 protected StructuralResource2 STR;
168 protected DiagramResource DIA;
169 protected ModelingResources MOD;
172 * Needed for using {@link Paster} in
173 * {@link #addMissingElements(WriteGraph, TypicalInfo, Resource, Resource, Set)}
175 protected GraphSynchronizationContext syncCtx;
178 * For collecting commit metadata during the processing of this request.
180 protected TypicalSynchronizationMetadata metadata;
183 * Necessary for using {@link CopyPasteStrategy} and {@link PasteOperation}
184 * for now. Will be removed in the future once IDiagram is removed from
187 protected IDiagram temporaryDiagram;
189 protected ConnectionUtil cu;
192 * Maps source -> target connection route nodes, i.e. connectors and
193 * interior route nodes (route lines). Inverse mapping of {@link #t2s}.
195 protected Map<Resource, Resource> s2t;
198 * Maps target -> source connection route nodes, i.e. connectors and
199 * interior route nodes (route lines). Inverse mapping of {@link #s2t}.
201 protected Map<Resource, Resource> t2s;
204 * An auxiliary resource map for extracting the correspondences between
205 * originals and copied resource when diagram contents are copied from
206 * template to instance.
208 protected Map<Object, Object> copyMap;
210 final private Map<Resource, List<String>> messageLogs = new HashMap<>();
212 public List<Resource> logs = new ArrayList<>();
214 private boolean writeLog;
220 * @param selectedRules
223 * @throws DatabaseException
225 public static void syncTypicals(WriteGraph graph, boolean log, List<Resource> templates, List<Resource> instances) throws DatabaseException {
227 new SyncTypicalTemplatesToInstances(
229 templates.toArray(Resource.NONE),
230 instances.toArray(Resource.NONE),
237 * @param templates typical diagram templates to completely synchronize with
240 public SyncTypicalTemplatesToInstances(Set<Resource> selectedRules, Resource... templates) {
241 this(selectedRules, templates, null, ALL, null);
245 * @param templates typical diagram templates to partially synchronize with
247 * @param changedElementsByDiagram see {@link #changedElementsByDiagram}
249 public SyncTypicalTemplatesToInstances(Set<Resource> selectedRules, Resource[] templates, MapSet<Resource, Resource> changedElementsByDiagram) {
250 this(selectedRules, templates, null, changedElementsByDiagram, null);
254 * Return a write request that completely synchronizes the specified
255 * instance diagram with its template.
260 public static SyncTypicalTemplatesToInstances syncSingleInstance(Set<Resource> selectedRules, Resource instance) {
261 return new SyncTypicalTemplatesToInstances(selectedRules, null, new Resource[] { instance }, ALL, null);
265 * @param templates typical diagram templates to synchronize with their instances
266 * @param instances typical diagram instances to synchronize with their templates
267 * @param changedElementsByDiagram see {@link #changedElementsByDiagram}
269 private SyncTypicalTemplatesToInstances(Set<Resource> selectedRules, Resource[] templates, Resource[] instances, MapSet<Resource, Resource> changedElementsByDiagram, IProgressMonitor monitor) {
270 this.selectedRules = selectedRules;
271 this.templates = templates;
272 this.instances = instances;
273 this.changedElementsByDiagram = changedElementsByDiagram;
274 this.monitor = monitor;
277 public SyncTypicalTemplatesToInstances logging(boolean writeLog) {
278 this.writeLog = writeLog;
282 private Resource getDiagramNameResource(ReadGraph graph, Resource diagram) throws DatabaseException {
283 Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
284 if(composite != null) return composite;
288 private Resource getElementNameResource(ReadGraph graph, Resource element) throws DatabaseException {
289 Resource corr = ModelingUtils.getPossibleElementCorrespondendence(graph, element);
290 if(corr != null) return corr;
294 private List<String> getLog(ReadGraph graph, Resource diagram) throws DatabaseException {
295 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(diagram));
296 if(indexRoot == null) throw new DatabaseException("FATAL: Diagram is not under any index root.");
297 List<String> log = messageLogs.get(indexRoot);
299 log = new ArrayList<>();
300 messageLogs.put(indexRoot, log);
305 private String elementName(ReadGraph graph, Resource element) throws DatabaseException {
307 StringBuilder b = new StringBuilder();
308 b.append(safeNameAndType(graph, element));
310 int spaces = 60-b.length();
311 for(int i=0;i<spaces;i++) b.append(" ");
313 Resource corr = ModelingUtils.getPossibleElementCorrespondendence(graph, element);
315 b.append(safeNameAndType(graph, corr));
325 public void perform(WriteGraph graph) throws DatabaseException {
326 this.L0 = Layer0.getInstance(graph);
327 this.STR = StructuralResource2.getInstance(graph);
328 this.DIA = DiagramResource.getInstance(graph);
329 this.MOD = ModelingResources.getInstance(graph);
331 this.syncCtx = GraphSynchronizationContext.getWriteInstance( graph, new CollectingModificationQueue() );
332 this.syncCtx.set(ModelingSynchronizationHints.MODELING_RESOURCE, ModelingResources.getInstance(graph));
334 this.metadata = new TypicalSynchronizationMetadata();
335 this.metadata.synchronizedTypicals = new ArrayList<>();
337 this.temporaryDiagram = Diagram.spawnNew(DiagramClass.DEFAULT);
338 this.temporaryDiagram.setHint(SynchronizationHints.CONTEXT, syncCtx);
340 this.cu = new ConnectionUtil(graph);
342 if (templates != null) {
343 // Look for typical template instances from the currently active models only.
344 Collection<Resource> activeModels = graph.syncRequest(new ActiveModels(Simantics.getProjectResource()));
345 if (!activeModels.isEmpty()) {
346 for (Resource template : templates) {
347 syncTemplate(graph, template, activeModels);
351 if (instances != null) {
352 for (Resource instance : instances) {
353 syncInstance(graph, instance);
358 for(Map.Entry<Resource, List<String>> entry : messageLogs.entrySet()) {
360 Resource indexRoot = entry.getKey();
361 List<String> messageLog = entry.getValue();
363 Layer0 L0 = Layer0.getInstance(graph);
364 DocumentResource DOC = DocumentResource.getInstance(graph);
366 Collection<Resource> libs = graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DOC.DocumentLibrary));
367 if(libs.isEmpty()) continue;
369 List<NamedResource> nrs = new ArrayList<>();
370 for(Resource lib : libs) nrs.add(new NamedResource(NameUtils.getSafeName(graph, lib), lib));
371 Collections.sort(nrs, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
372 Resource library = nrs.iterator().next().getResource();
374 CommonDBUtils.selectClusterSet(graph, library);
376 String text = "--- Created: " + new Date().toString() + " ---\n";
377 text += EString.implode(messageLog);
379 Resource log = graph.newResource();
380 graph.claim(log, L0.InstanceOf, null, DOC.PlainTextDocument);
381 graph.claimLiteral(log, L0.HasName, L0.String, "Typical Sync " + new Date().toString());
382 graph.claim(library, L0.ConsistsOf, L0.PartOf, log);
383 graph.claimLiteral(log, DOC.PlainTextDocument_text, L0.String, text);
389 if (!metadata.getTypicals().isEmpty()) {
390 graph.addMetadata(metadata);
392 // Add comment to change set.
393 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
394 graph.addMetadata( cm.add("Synchronized " + metadata.getTypicals().size() + " typical diagram instances (" + metadata.getTypicals() + ") with their templates.") );
397 temporaryDiagram = null;
401 private Collection<Resource> findInstances(ReadGraph graph, Resource ofType, Collection<Resource> indexRoots) throws DatabaseException {
402 Instances index = graph.adapt(ofType, Instances.class);
403 Set<Resource> instances = new HashSet<>();
404 for (Resource indexRoot : indexRoots)
405 instances.addAll( index.find(graph, indexRoot) );
409 private void syncTemplate(WriteGraph graph, Resource template, Collection<Resource> indexRoots) throws DatabaseException {
410 Resource templateType = graph.getPossibleType(template, DIA.Diagram);
411 if (templateType == null)
414 Collection<Resource> instances = findInstances(graph, templateType, indexRoots);
415 // Do not include the template itself as it is also an instance of templateType
416 instances.remove(template);
417 if (instances.isEmpty())
420 Set<Resource> templateElements = new THashSet<>( graph.syncRequest(
421 new ObjectsWithType(template, L0.ConsistsOf, DIA.Element) ) );
424 for (Resource instance : instances) {
425 this.temporaryDiagram.setHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE, instance);
426 syncInstance(graph, template, instance, templateElements);
428 } catch (Exception e) {
429 LOGGER.error("Template synchronization failed.", e);
431 this.temporaryDiagram.removeHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
435 private void syncInstance(WriteGraph graph, Resource instance) throws DatabaseException {
436 Resource template = graph.getPossibleObject(instance, MOD.HasDiagramSource);
437 if (template == null)
440 Set<Resource> templateElements = new THashSet<>( graph.syncRequest(
441 new ObjectsWithType(template, L0.ConsistsOf, DIA.Element) ) );
444 this.temporaryDiagram.setHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE, instance);
445 syncInstance(graph, template, instance, templateElements);
447 this.temporaryDiagram.removeHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
451 private Resource findInstanceCounterpart(ReadGraph graph, Resource instanceDiagram, Resource templateElement) throws DatabaseException {
452 Map<String,Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(instanceDiagram));
453 for(Resource child : children.values()) {
454 if(graph.hasStatement(child, MOD.HasElementSource, templateElement)) return child;
459 private boolean isSynchronizedConnector(ReadGraph graph, Resource templateConnection, Resource instanceConnector) throws DatabaseException {
460 DiagramResource DIA = DiagramResource.getInstance(graph);
461 Resource instanceConnection = graph.getPossibleObject(instanceConnector, DIA.IsConnectorOf);
462 if (instanceConnection == null)
464 return graph.hasStatement(instanceConnection, MOD.HasElementSource, templateConnection)
465 // If the master connection has been removed, this is all that's left
466 // to identify a connection that at least was originally synchronized
467 // from the typical master to this instance.
468 || graph.hasStatement(instanceConnection, MOD.IsTemplatized);
472 * Perform the following synchronization steps for the instance diagram:
474 * <li>remove such templatized elements from the instance diagram whose
475 * template counterpart no longer exists</li>
476 * <li>add elements to the instance diagram that are only in the template</li>
477 * <li>synchronize elements of the instance diagram that have been deemed
481 * @param graph database write access
482 * @param template the synchronization source diagram
483 * @param instance the synchronization target diagram
484 * @param currentTemplateElements the set of all elements currently in the
486 * @throws DatabaseException if anything goes wrong
488 private void syncInstance(WriteGraph graph, Resource template, Resource instance, Set<Resource> currentTemplateElements) throws DatabaseException {
490 List<String> messageLog = getLog(graph, instance);
492 messageLog.add("Synchronization of changed typical template: " + SyncTypicalTemplatesToInstances.safeNameAndType(graph, getDiagramNameResource(graph, template)));
493 messageLog.add("----\n\ttypical instance: " + safeNameAndType(graph, getDiagramNameResource(graph, instance)));
495 CommonDBUtils.selectClusterSet(graph, instance);
497 // Form instance element <-> template element bijection
498 TypicalInfoBean typicalInfoBean = graph.syncRequest(
499 new ReadTypicalInfo(instance),
500 TransientCacheListener.<TypicalInfoBean> instance());
501 // Must be able to modify the typicalInfo structure,
502 // therefore clone the query result.
503 typicalInfoBean = (TypicalInfoBean) typicalInfoBean.clone();
504 typicalInfoBean.templateElements = currentTemplateElements;
505 typicalInfoBean.auxiliary = new HashMap<>(1);
507 TypicalInfo info = new TypicalInfo();
508 info.monitor = monitor;
509 info.messageLog = messageLog;
510 info.bean = typicalInfoBean;
512 // Resolve naming function for this typical instance.
513 Resource compositeInstance = graph.getPossibleObject(instance, MOD.DiagramToComposite);
514 if (compositeInstance != null) {
515 Function4<ReadGraph, Resource, Resource, String, String> namingFunction = TypicalUtil.getTypicalNamingFunction(graph, compositeInstance);
516 if (namingFunction != null)
517 typicalInfoBean.auxiliary.put(AuxKeys.KEY_TYPICAL_NAMING_FUNCTION, namingFunction);
520 int dSizeAbs = Math.abs(typicalInfoBean.instanceElements.size() - currentTemplateElements.size());
523 System.out.println("typical <-> template mapping: " + typicalInfoBean.instanceToTemplate);
525 // Find elements to be removed from instance by looking for all
526 // instance elements that do not have a MOD.HasElementSource
527 // relation but have a MOD.IsTemplatized tag.
528 Set<Resource> instanceElementsRemovedFromTemplate = findInstanceElementsRemovedFromTemplate(
529 graph, info, new THashSet<>(dSizeAbs));
531 // Find elements in template that do not yet exist in the instance
532 Set<Resource> templateElementsAddedToTemplate = findTemplateElementsMissingFromInstance(
533 graph, currentTemplateElements, info,
534 new THashSet<>(dSizeAbs));
536 Set<Resource> changedTemplateElements = changedElementsByDiagram.removeValues(template);
539 System.out.println("ADDED: " + templateElementsAddedToTemplate.size() + ", REMOVED: " + instanceElementsRemovedFromTemplate.size() + ", CHANGED: " + changedTemplateElements.size());
542 for(Resource templateElement : graph.getObjects(template, L0.ConsistsOf)) {
543 if(graph.isInstanceOf(templateElement, DIA.RouteGraphConnection)) {
544 for(Resource connector : graph.getObjects(templateElement, DIA.HasConnector)) {
545 for(Statement elementStm : graph.getStatements(connector, STR.Connects)) {
546 Resource otherElement = elementStm.getObject();
547 if(!otherElement.equals(templateElement)) {
548 Resource counterPartElement = findInstanceCounterpart(graph, instance, otherElement);
549 if(counterPartElement != null) {
550 Resource diagramConnectionPoint = graph.getInverse(elementStm.getPredicate());
551 Resource connectionPoint = graph.getPossibleObject(diagramConnectionPoint, MOD.DiagramConnectionRelationToConnectionRelation);
552 if(connectionPoint != null) {
553 Statement stm = graph.getPossibleStatement(counterPartElement, diagramConnectionPoint);
555 if(graph.isInstanceOf(connectionPoint, L0.FunctionalRelation)) {
556 if(!isSynchronizedConnector(graph, templateElement, stm.getObject())) {
557 messageLog.add("\t\tABORTED: tried to connect to an already connected terminal " + NameUtils.getSafeName(graph, counterPartElement) + " " + NameUtils.getSafeName(graph, connectionPoint));
571 boolean changed = false;
572 changed |= synchronizeDiagramChanges(graph, info, template, instance);
573 changed |= removeElements(graph, info, instanceElementsRemovedFromTemplate);
574 changed |= addMissingElements(graph, info, template, instance, templateElementsAddedToTemplate);
575 changed |= synchronizeChangedElements(graph, info, template, instance, changedTemplateElements, templateElementsAddedToTemplate, changedElementsByDiagram == ALL);
578 metadata.addTypical(instance);
582 * Synchronize any configurable aspects of the typical diagram instance itself.
583 * Every rule executed here comes from the ontology, nothing is fixed.
589 * @return if any changes were made.
590 * @throws DatabaseException
592 private boolean synchronizeDiagramChanges(
594 TypicalInfo typicalInfo,
597 throws DatabaseException
599 boolean changed = false;
600 for (Resource rule : graph.getObjects(template, MOD.HasTypicalSynchronizationRule)) {
601 if (selectedRules != null && !selectedRules.contains(rule))
603 ITypicalSynchronizationRule r = graph.getPossibleAdapter(rule, ITypicalSynchronizationRule.class);
605 changed |= r.synchronize(graph, template, instance, typicalInfo);
611 * Add elements from template that do not yet exist in the instance.
616 * @param elementsAddedToTemplate
617 * @return <code>true</code> if changes were made to the instance
618 * @throws DatabaseException
620 private boolean addMissingElements(WriteGraph graph, TypicalInfo typicalInfo, Resource template,
621 Resource instance, Set<Resource> elementsAddedToTemplate)
622 throws DatabaseException {
623 if (elementsAddedToTemplate.isEmpty())
626 CopyAdvisor copyAdvisor = graph.syncRequest(new Adapter<CopyAdvisor>(instance, CopyAdvisor.class));
627 this.temporaryDiagram.setHint(SynchronizationHints.COPY_ADVISOR, copyAdvisor);
629 ElementObjectAssortment assortment = new ElementObjectAssortment(graph, elementsAddedToTemplate);
631 copyMap = new THashMap<>();
636 System.out.println("ADD MISSING ELEMENTS: " + assortment);
638 // initialCopyMap argument is needed for copying just connections
639 // when their end-points are not copied at the same time.
641 PasteOperation pasteOp = new PasteOperation(Commands.COPY,
642 (ICanvasContext) null, template, instance, temporaryDiagram,
643 assortment, false, new Point2D.Double(0, 0),
644 typicalInfo.bean.templateToInstance, copyMap)
645 .options(PasteOperation.ForceCopyReferences.INSTANCE);
647 new Paster(graph.getSession(), pasteOp).perform(graph);
649 boolean changed = false;
651 if(!elementsAddedToTemplate.isEmpty())
652 typicalInfo.messageLog.add("\tadded elements");
654 for (Resource addedElement : elementsAddedToTemplate) {
655 Resource copyElement = (Resource) copyMap.get(addedElement);
656 if (copyElement != null) {
657 graph.claim(copyElement, MOD.IsTemplatized, MOD.IsTemplatized, copyElement);
658 graph.claim(copyElement, MOD.HasElementSource, MOD.ElementHasInstance, addedElement);
660 typicalInfo.bean.instanceElements.add(copyElement);
661 typicalInfo.bean.instanceToTemplate.put(copyElement, addedElement);
662 typicalInfo.bean.templateToInstance.put(addedElement, copyElement);
664 typicalInfo.messageLog.add("\t\t" + safeNameAndType(graph, copyElement));
670 ModelingResources MOD = ModelingResources.getInstance(graph);
671 Resource instanceComposite = graph.getPossibleObject(instance, MOD.DiagramToComposite);
672 List<Resource> instanceComponents = new ArrayList<>(elementsAddedToTemplate.size());
674 // Post-process added elements after typicalInfo has been updated and
675 // template mapping statements are in place.
676 for (Resource addedElement : elementsAddedToTemplate) {
677 Resource copyElement = (Resource) copyMap.get(addedElement);
678 if (copyElement != null) {
679 postProcessAddedElement(graph, addedElement, copyElement, typicalInfo);
681 if (instanceComponents != null) {
682 // Gather all instance typical components for applying naming
684 Resource component = graph.getPossibleObject(copyElement, MOD.ElementToComponent);
685 if (component != null)
686 instanceComponents.add(component);
691 if (instanceComposite != null)
692 TypicalUtil.applySelectedModuleNames(graph, instanceComposite, instanceComponents);
697 private void postProcessAddedElement(WriteGraph graph,
698 Resource addedTemplateElement, Resource addedInstanceElement,
699 TypicalInfo typicalInfo) throws DatabaseException {
700 if (graph.isInstanceOf(addedInstanceElement, DIA.Monitor)) {
701 postProcessAddedMonitor(graph, addedTemplateElement, addedInstanceElement, typicalInfo);
705 private void postProcessAddedMonitor(WriteGraph graph,
706 Resource addedTemplateMonitor, Resource addedInstanceMonitor,
707 TypicalInfo typicalInfo) throws DatabaseException {
708 Resource monitor = addedInstanceMonitor;
709 Resource monitoredComponent = graph.getPossibleObject(monitor, DIA.HasMonitorComponent);
710 if (monitoredComponent != null) {
711 Resource monitoredTemplateElement = graph.getPossibleObject(monitoredComponent, MOD.ComponentToElement);
712 if (monitoredTemplateElement != null) {
713 Resource monitoredInstanceElement = typicalInfo.bean.templateToInstance.get(monitoredTemplateElement);
714 if (monitoredInstanceElement != null) {
715 Resource monitoredInstanceComponent = graph.getPossibleObject(monitoredInstanceElement, MOD.ElementToComponent);
716 if (monitoredInstanceComponent != null) {
717 // Ok, the monitor refers to a component within the
718 // template composite. Change it to refer to the
719 // instance composite.
720 graph.deny(monitor, DIA.HasMonitorComponent);
721 graph.claim(monitor, DIA.HasMonitorComponent, monitoredInstanceComponent);
728 private boolean removeElements(WriteGraph graph, TypicalInfo typicalInfo, Set<Resource> elementsRemovedFromTemplate) throws DatabaseException {
729 if (elementsRemovedFromTemplate.isEmpty())
732 // Remove mapped elements from instance that are removed from the template.
733 boolean changed = false;
735 if(!elementsRemovedFromTemplate.isEmpty())
736 typicalInfo.messageLog.add("\tremoved elements");
738 for (Resource removedElement : elementsRemovedFromTemplate) {
739 typicalInfo.messageLog.add("\t\t" + safeNameAndType(graph, removedElement));
741 RemoverUtil.remove(graph, removedElement);
743 typicalInfo.bean.instanceElements.remove(removedElement);
744 Resource template = typicalInfo.bean.instanceToTemplate.remove(removedElement);
745 if (template != null)
746 typicalInfo.bean.templateToInstance.remove(template);
753 private Set<Resource> findTemplateElementsMissingFromInstance(
754 WriteGraph graph, Collection<Resource> currentTemplateElements,
755 TypicalInfo typicalInfo, THashSet<Resource> result)
756 throws DatabaseException {
757 for (Resource templateElement : currentTemplateElements) {
758 Resource instanceElement = typicalInfo.bean.templateToInstance.get(templateElement);
759 if (instanceElement == null) {
761 System.out.println("No instance correspondence for template element " + NameUtils.getSafeName(graph, templateElement, true) + " => add");
762 result.add(templateElement);
768 public Set<Resource> findInstanceElementsRemovedFromTemplate(
769 ReadGraph graph, TypicalInfo typicalInfo,
770 THashSet<Resource> result) throws DatabaseException {
771 for (Resource instanceElement : typicalInfo.bean.instanceElements) {
772 if (!typicalInfo.bean.instanceToTemplate.containsKey(instanceElement)) {
773 if (typicalInfo.bean.isTemplatized.contains(instanceElement)) {
775 System.out.println("Templatized typical instance element " + NameUtils.getSafeName(graph, instanceElement, true) + " has no correspondence in template => remove");
776 result.add(instanceElement);
784 * Synchronize basic visual aspects of changed elements. For all elements,
785 * transform and label are synchronized. Otherwise synchronization is
786 * type-specific for connections, flags, monitors and svg elements.
792 * @param changedTemplateElements
793 * @param addedElements
794 * elements that have been added and thus need not be
796 * @param synchronizeAllElements
798 * @throws DatabaseException
800 private boolean synchronizeChangedElements(WriteGraph graph,
801 TypicalInfo typicalInfo, Resource template, Resource instance,
802 Collection<Resource> changedTemplateElements,
803 Set<Resource> addedElements,
804 boolean synchronizeAllElements) throws DatabaseException {
806 if (synchronizeAllElements) {
807 // For unit testing purposes.
808 changedTemplateElements = graph.syncRequest(new ObjectsWithType(template, L0.ConsistsOf, DIA.Element));
811 if (changedTemplateElements.isEmpty())
814 boolean changed = false;
816 typicalInfo.messageLog.add("\telement change analysis");
817 int analysisLogPosition = typicalInfo.messageLog.size();
819 for (Resource changedTemplateElement : changedTemplateElements) {
820 // Skip synchronization of elements that were just added and are
821 // thus already synchronized.
822 if (addedElements.contains(changedTemplateElement))
825 Resource instanceElement = typicalInfo.bean.templateToInstance.get(changedTemplateElement);
826 if (instanceElement == null) {
827 // There's an earlier problem in the sync process if this happens.
828 typicalInfo.messageLog.add("SKIPPING SYNC OF CHANGED TEMPLATE ELEMENT DUE TO MISSING INSTANCE: " + safeNameAndType(graph, getElementNameResource(graph, changedTemplateElement)));
832 typicalInfo.messageLog.add("\t\t" + elementName(graph, changedTemplateElement));
833 int currentLogSize = typicalInfo.messageLog.size();
835 changed |= InstanceOfRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
836 changed |= NameRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
837 changed |= TransformRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
838 changed |= LabelRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
840 Collection<Resource> types = graph.getTypes(changedTemplateElement);
841 if (types.contains(DIA.RouteGraphConnection)) {
842 changed |= synchronizeConnection(graph, changedTemplateElement, instanceElement, typicalInfo);
843 } else if (types.contains(DIA.Flag)) {
844 changed |= FlagRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
845 } else if (types.contains(DIA.Monitor)) {
846 changed |= MonitorRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
847 } else if (types.contains(DIA.SVGElement)) {
848 changed |= SVGElementRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
851 changed |= ProfileMonitorRule.INSTANCE.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
853 for (Resource rule : graph.getObjects(changedTemplateElement, MOD.HasTypicalSynchronizationRule)) {
854 if(selectedRules != null && !selectedRules.contains(rule)) continue;
855 ITypicalSynchronizationRule r = graph.getPossibleAdapter(rule, ITypicalSynchronizationRule.class);
857 changed |= r.synchronize(graph, changedTemplateElement, instanceElement, typicalInfo);
860 // Show element only if something has happened
861 if(currentLogSize == typicalInfo.messageLog.size())
862 typicalInfo.messageLog.remove(typicalInfo.messageLog.size()-1);
871 // Show analysis header only if something has happened
872 if(analysisLogPosition == typicalInfo.messageLog.size())
873 typicalInfo.messageLog.remove(typicalInfo.messageLog.size()-1);
878 private static class Connector {
879 public final Resource attachmentRelation;
880 public final Resource connector;
881 public RouteLine attachedTo;
883 public Connector(Resource attachmentRelation, Resource connector) {
884 this.attachmentRelation = attachmentRelation;
885 this.connector = connector;
890 * Synchronizes two route graph connection topologies if and only if the
891 * destination connection is not attached to any node elements besides
892 * the ones that exist in the source. This means that connections that
893 * have instance-specific connections to non-template nodes are ignored
897 * @param sourceConnection
898 * @param targetConnection
900 * @return <code>true</code> if changes were made
901 * @throws DatabaseException
903 private boolean synchronizeConnection(WriteGraph graph, Resource sourceConnection, Resource targetConnection, TypicalInfo typicalInfo)
904 throws DatabaseException {
907 System.out.println("connection " + NameUtils.getSafeName(graph, sourceConnection, true) + " to target connection " + NameUtils.getSafeName(graph, targetConnection, true));
909 boolean changed = false;
911 // Initialize utilities and data maps
912 s2t = newOrClear(s2t);
913 t2s = newOrClear(t2s);
916 cu = new ConnectionUtil(graph);
918 // 0.1. find mappings between source and target connection connectors
919 Collection<Statement> toTargetConnectors = graph.getStatements(targetConnection, DIA.HasConnector);
920 Map<Resource, Connector> targetConnectors = new THashMap<>(toTargetConnectors.size());
921 for (Statement toTargetConnector : toTargetConnectors) {
922 Resource targetConnector = toTargetConnector.getObject();
923 targetConnectors.put(targetConnector, new Connector(toTargetConnector.getPredicate(), targetConnector));
924 Statement toNode = cu.getConnectedComponentStatement(targetConnection, targetConnector);
925 if (toNode == null) {
926 // Corrupted target connection!
927 ErrorLogger.defaultLogError("Encountered corrupted typical template connection "
928 + NameUtils.getSafeName(graph, targetConnection, true) + " with a stray DIA.Connector instance "
929 + NameUtils.getSafeName(graph, targetConnector, true) + " that is not attached to any element.",
930 new Exception("trace"));
933 if (!graph.hasStatement(targetConnector, DIA.AreConnected)) {
934 // Corrupted target connection!
935 ErrorLogger.defaultLogError("Encountered corrupted typical template connection "
936 + NameUtils.getSafeName(graph, targetConnection, true) + " with a stray DIA.Connector instance "
937 + NameUtils.getSafeName(graph, targetConnector, true) + " that is not connected to any other route node.",
938 new Exception("trace"));
942 //Resource templateNode = typicalInfo.instanceToTemplate.get(toNode.getObject());
943 Resource templateNode = graph.getPossibleObject(toNode.getObject(), MOD.HasElementSource);
944 if (templateNode != null) {
945 Resource isConnectedTo = graph.getPossibleInverse(toNode.getPredicate());
946 if (isConnectedTo != null) {
947 Resource templateConnector = graph.getPossibleObject(templateNode, isConnectedTo);
948 if (templateConnector != null) {
949 Resource connectionOfTemplateConnector = ConnectionUtil.tryGetConnection(graph, templateConnector);
950 if (sourceConnection.equals(connectionOfTemplateConnector)) {
951 s2t.put(templateConnector, targetConnector);
952 t2s.put(targetConnector, templateConnector);
955 debug(typicalInfo, "Mapping connector "
956 + NameUtils.getSafeName(graph, templateConnector, true)
957 + " to " + NameUtils.getSafeName(graph, targetConnector, true));
964 // 0.2. find mapping between source and target route lines
965 Collection<Resource> sourceInteriorRouteNodes = graph.getObjects(sourceConnection, DIA.HasInteriorRouteNode);
966 Collection<Resource> targetInteriorRouteNodes = graph.getObjects(targetConnection, DIA.HasInteriorRouteNode);
967 Map<Resource, Paster.RouteLine> sourceToRouteLine = new THashMap<>();
968 Map<Resource, Paster.RouteLine> targetToRouteLine = new THashMap<>();
970 for (Resource source : sourceInteriorRouteNodes)
971 sourceToRouteLine.put(source, Paster.readRouteLine(graph, source));
972 for (Resource target : targetInteriorRouteNodes)
973 targetToRouteLine.put(target, Paster.readRouteLine(graph, target));
975 Map<Resource, Paster.RouteLine> originalSourceToRouteLine = new THashMap<>(sourceToRouteLine);
976 Map<Resource, Paster.RouteLine> originalTargetToRouteLine = new THashMap<>(targetToRouteLine);
979 for (Iterator<Map.Entry<Resource, Paster.RouteLine>> sourceIt = sourceToRouteLine.entrySet().iterator(); !targetToRouteLine.isEmpty() && sourceIt.hasNext();) {
980 Map.Entry<Resource, Paster.RouteLine> sourceEntry = sourceIt.next();
981 Paster.RouteLine sourceLine = sourceEntry.getValue();
982 for (Iterator<Map.Entry<Resource, Paster.RouteLine>> targetIt = targetToRouteLine.entrySet().iterator(); targetIt.hasNext();) {
983 Map.Entry<Resource, Paster.RouteLine> targetEntry = targetIt.next();
984 if (sourceLine.equals(targetEntry.getValue())) {
985 s2t.put(sourceEntry.getKey(), targetEntry.getKey());
986 t2s.put(targetEntry.getKey(), sourceEntry.getKey());
991 debug(typicalInfo, "Mapping routeline "
992 + NameUtils.getSafeName(graph, sourceEntry.getKey(), true)
993 + " - " + sourceEntry.getValue()
994 + " to " + NameUtils.getSafeName(graph, targetEntry.getKey(), true)
995 + " - " + targetEntry.getValue());
997 continue nextSourceLine;
1003 debug(typicalInfo, "Take 1: Source to target route nodes map : " + s2t);
1004 debug(typicalInfo, "Take 1: Target to source route nodes map : " + t2s);
1007 // 1.1. Temporarily disconnect instance-specific connectors from the the connection .
1008 // They will be added back to the connection after the templatized parts of the
1009 // connection have been synchronized.
1011 // Stores diagram connectors that are customizations in the synchronized instance.
1012 List<Connector> instanceOnlyConnectors = null;
1014 for (Connector connector : targetConnectors.values()) {
1015 if (!t2s.containsKey(connector.connector)) {
1016 typicalInfo.messageLog.add("\t\tencountered instance-specific diagram connector in target connection: " + NameUtils.getSafeName(graph, connector.connector));
1018 // Find the RouteLine this connectors is connected to.
1019 for (Resource rl : graph.getObjects(connector.connector, DIA.AreConnected)) {
1020 connector.attachedTo = originalTargetToRouteLine.get(rl);
1021 if (connector.attachedTo != null)
1025 // Disconnect connector from connection
1026 graph.deny(targetConnection, connector.attachmentRelation, connector.connector);
1027 graph.deny(connector.connector, DIA.AreConnected);
1029 // Keep track of the disconnected connector
1030 if (instanceOnlyConnectors == null)
1031 instanceOnlyConnectors = new ArrayList<>(targetConnectors.size());
1032 instanceOnlyConnectors.add(connector);
1036 // 1.2. add missing connectors to target
1037 Collection<Resource> sourceConnectors = graph.getObjects(sourceConnection, DIA.HasConnector);
1038 for (Resource sourceConnector : sourceConnectors) {
1039 if (!s2t.containsKey(sourceConnector)) {
1040 Statement sourceIsConnectorOf = graph.getSingleStatement(sourceConnector, DIA.IsConnectorOf);
1041 Statement connects = cu.getConnectedComponentStatement(sourceConnection, sourceConnector);
1042 if (connects == null) {
1043 // TODO: serious error!
1044 throw new DatabaseException("ERROR: connector is astray, i.e. not connected to a node element: " + safeNameAndType(graph, sourceConnector));
1046 Resource connectsInstanceElement = typicalInfo.bean.templateToInstance.get(connects.getObject());
1047 if (connectsInstanceElement == null) {
1048 // TODO: serious error!
1049 throw new DatabaseException("ERROR: could not find instance element to which template element " + safeNameAndType(graph, connects.getObject()) + " is connected to");
1051 Resource hasConnector = graph.getInverse(sourceIsConnectorOf.getPredicate());
1053 Resource newTargetConnector = cu.newConnector(targetConnection, hasConnector);
1054 graph.claim(newTargetConnector, connects.getPredicate(), connectsInstanceElement);
1057 s2t.put(sourceConnector, newTargetConnector);
1058 t2s.put(newTargetConnector, sourceConnector);
1060 typicalInfo.messageLog.add("\t\t\tadd new connector to target connection: " + NameUtils.getSafeName(graph, newTargetConnector) + " to map to source connector " + NameUtils.getSafeName(graph, sourceConnector));
1064 // 2. sync route lines and their connectivity:
1065 // 2.1. assign correspondences in target for each source route line
1066 // by reusing excess route lines in target and by creating new
1069 Resource[] targetRouteLines = targetToRouteLine.keySet().toArray(Resource.NONE);
1070 int targetRouteLine = targetRouteLines.length - 1;
1072 for (Iterator<Map.Entry<Resource, Paster.RouteLine>> sourceIt = sourceToRouteLine.entrySet().iterator(); sourceIt.hasNext();) {
1073 Map.Entry<Resource, Paster.RouteLine> sourceEntry = sourceIt.next();
1074 Resource source = sourceEntry.getKey();
1075 Paster.RouteLine sourceLine = sourceEntry.getValue();
1077 typicalInfo.messageLog.add("\t\t\tassign an instance-side routeline counterpart for " + NameUtils.getSafeName(graph, source, true) + " - " + sourceLine);
1079 // Assign target route line for source
1080 Resource target = null;
1081 if (targetRouteLine < 0) {
1082 // by creating new route lines
1083 target = cu.newRouteLine(targetConnection, sourceLine.getPosition(), sourceLine.isHorizontal());
1084 typicalInfo.messageLog.add("\t\t\tcreate new route line " + NameUtils.getSafeName(graph, target));
1087 // by reusing existing route line
1088 target = targetRouteLines[targetRouteLine--];
1089 copyRouteLine(graph, source, target);
1090 cu.disconnectFromAllRouteNodes(target);
1091 typicalInfo.messageLog.add("\t\t\treused existing route line " + NameUtils.getSafeName(graph, target));
1094 s2t.put(source, target);
1095 t2s.put(target, source);
1097 typicalInfo.messageLog.add("\t\t\tmapped source route line " + NameUtils.getSafeName(graph, source) + " to target route line " + NameUtils.getSafeName(graph, target));
1100 if (targetRouteLine >= 0) {
1101 typicalInfo.messageLog.add("\t\t\tremove excess route lines (" + (targetRouteLine + 1) + ") from target connection");
1102 for (; targetRouteLine >= 0; targetRouteLine--) {
1103 typicalInfo.messageLog.add("\t\t\t\tremove excess route line: " + NameUtils.getSafeName(graph, targetRouteLines[targetRouteLine], true));
1104 cu.removeConnectionPart(targetRouteLines[targetRouteLine]);
1109 debug(typicalInfo, "Take 2: Source to target route nodes map : " + s2t);
1110 debug(typicalInfo, "Take 2: Target to source route nodes map : " + t2s);
1113 // 2.2. Synchronize target connection topology (DIA.AreConnected)
1114 changed |= connectRouteNodes(graph, typicalInfo, sourceInteriorRouteNodes);
1115 changed |= connectRouteNodes(graph, typicalInfo, sourceConnectors);
1117 // 3. remove excess routelines & connectors from target connection
1118 changed |= cu.removeExtraInteriorRouteNodes(targetConnection) > 0;
1119 changed |= cu.removeUnusedConnectors(targetConnection) > 0;
1121 // 3.1. Ensure that all mapped route nodes in the target connection
1122 // are tagged with MOD.IsTemplatized. Future synchronization
1123 // can then take advantage of this information to more easily
1124 // decide which parts of the connection are originated from
1125 // the template and which are not.
1126 changed |= markMappedRouteNodesTemplatized(graph, s2t.values());
1128 // 4. Add temporarily disconnected instance-specific connectors
1129 // back to the synchronized connection. The route line to attach
1130 // to is based on a simple heuristic.
1131 if (instanceOnlyConnectors != null) {
1132 if (originalSourceToRouteLine.isEmpty()) {
1133 // If there are 0 route lines in the template connection,
1134 // then one must be added to the instance connection.
1135 // This can only happen if the template connection is
1136 // simple, i.e. just between two terminals without any
1139 // Attach all target connection connectors to the newly created route line
1140 Resource rl = cu.newRouteLine(targetConnection, null, null);
1141 for (Resource sourceConnector : sourceConnectors) {
1142 Resource targetConnector = s2t.get(sourceConnector);
1143 graph.deny(targetConnector, DIA.AreConnected);
1144 graph.claim(targetConnector, DIA.AreConnected, DIA.AreConnected, rl);
1147 // Copy orientation and position for new route line from original target route lines.
1148 // This is a simplification that will attach any amount of route lines in the original
1149 // target connection into just one route line. There is room for improvement here
1150 // but it will require a more elaborate algorithm to find and cut the non-templatized
1151 // route lines as well as connectors out of the connection before synchronizing it.
1153 // TODO: This implementation chooses the added route line position at random if
1154 // there are multiple route lines in the target connection.
1155 if (!originalTargetToRouteLine.isEmpty()) {
1156 RouteLine originalRl = originalTargetToRouteLine.values().iterator().next();
1157 setRouteLine(graph, rl, originalRl);
1160 // Attach the instance specific connectors also to the only route line
1161 for (Connector connector : instanceOnlyConnectors) {
1162 graph.claim(targetConnection, connector.attachmentRelation, connector.connector);
1163 graph.claim(connector.connector, DIA.AreConnected, DIA.AreConnected, rl);
1168 for (Connector connector : instanceOnlyConnectors) {
1169 // Find the route line that most closely matches the original
1170 // route line that the connector was connected to.
1171 Resource closestMatch = null;
1172 double closestDistance = Double.MAX_VALUE;
1173 if (connector.attachedTo != null) {
1174 for (Map.Entry<Resource, Paster.RouteLine> sourceLine : originalSourceToRouteLine.entrySet()) {
1175 double dist = distance(sourceLine.getValue(), connector.attachedTo);
1176 if (dist < closestDistance) {
1177 closestMatch = s2t.get(sourceLine.getKey());
1178 closestDistance = dist;
1182 closestMatch = originalSourceToRouteLine.keySet().iterator().next();
1184 graph.claim(targetConnection, connector.attachmentRelation, connector.connector);
1185 graph.claim(connector.connector, DIA.AreConnected, DIA.AreConnected, closestMatch);
1186 if (closestDistance > 0)
1188 typicalInfo.messageLog.add("\t\t\treattached instance-specific connector "
1189 + NameUtils.getSafeName(graph, connector.connector) + " to nearest existing route line "
1190 + NameUtils.getSafeName(graph, closestMatch) + " with distance " + closestDistance);
1198 private boolean markMappedRouteNodesTemplatized(WriteGraph graph, Iterable<Resource> routeNodes) throws DatabaseException {
1199 boolean changed = false;
1200 for (Resource rn : routeNodes) {
1201 if (!graph.hasStatement(rn, MOD.IsTemplatized)) {
1202 graph.claim(rn, MOD.IsTemplatized, MOD.IsTemplatized, rn);
1209 private static double distance(RouteLine l1, RouteLine l2) {
1210 double dist = Math.abs(l2.getPosition() - l1.getPosition());
1211 dist *= l2.isHorizontal() == l1.isHorizontal() ? 1 : 1000;
1215 private boolean connectRouteNodes(WriteGraph graph, TypicalInfo typicalInfo, Collection<Resource> sourceRouteNodes) throws DatabaseException {
1216 boolean changed = false;
1217 for (Resource src : sourceRouteNodes) {
1218 Resource dst = s2t.get(src);
1220 throw new DatabaseException("TARGET ROUTE NODE == NULL FOR SRC: " + NameUtils.getSafeName(graph, src));
1223 Collection<Resource> connectedToSrcs = graph.getObjects(src, DIA.AreConnected);
1224 Collection<Resource> connectedToDsts = graph.getObjects(dst, DIA.AreConnected);
1226 // Remove excess statements
1227 for (Resource connectedToDst : connectedToDsts) {
1228 Resource connectedToSrc = t2s.get(connectedToDst);
1229 if (connectedToSrc == null) {
1230 throw new DatabaseException("CONNECTED TO SRC == NULL FOR DST: " + NameUtils.getSafeName(graph, connectedToDst));
1232 if (connectedToSrc == null || !graph.hasStatement(src, DIA.AreConnected, connectedToSrc)) {
1233 graph.deny(dst, DIA.AreConnected, DIA.AreConnected, connectedToDst);
1235 typicalInfo.messageLog.add("\t\t\tdisconnected route nodes (" + NameUtils.getSafeName(graph, dst) + ", " + NameUtils.getSafeName(graph, connectedToDst) + ")");
1239 // Add necessary statements
1240 for (Resource connectedToSrc : connectedToSrcs) {
1241 Resource connectedToDst = s2t.get(connectedToSrc);
1242 if (connectedToDst == null) {
1243 throw new DatabaseException("CONNECTED TO DST == NULL FOR SRC: " + NameUtils.getSafeName(graph, connectedToSrc));
1245 if (!graph.hasStatement(dst, DIA.AreConnected, connectedToDst)) {
1246 graph.claim(dst, DIA.AreConnected, DIA.AreConnected, connectedToDst);
1248 typicalInfo.messageLog.add("\t\t\tconnected route nodes (" + NameUtils.getSafeName(graph, dst) + ", " + NameUtils.getSafeName(graph, connectedToDst) + ")");
1255 private void setRouteLine(WriteGraph graph, Resource line, double position, boolean horizontal) throws DatabaseException {
1256 graph.claimLiteral(line, DIA.HasPosition, L0.Double, position, Bindings.DOUBLE);
1257 graph.claimLiteral(line, DIA.IsHorizontal, L0.Boolean, horizontal, Bindings.BOOLEAN);
1260 private void setRouteLine(WriteGraph graph, Resource line, RouteLine rl) throws DatabaseException {
1261 setRouteLine(graph, line, rl.getPosition(), rl.isHorizontal());
1264 private void copyRouteLine(WriteGraph graph, Resource src, Resource tgt) throws DatabaseException {
1265 Double pos = graph.getPossibleRelatedValue(src, DIA.HasPosition, Bindings.DOUBLE);
1266 Boolean hor = graph.getPossibleRelatedValue(src, DIA.IsHorizontal, Bindings.BOOLEAN);
1271 graph.claimLiteral(tgt, DIA.HasPosition, L0.Double, pos, Bindings.DOUBLE);
1272 graph.claimLiteral(tgt, DIA.IsHorizontal, L0.Boolean, hor, Bindings.BOOLEAN);
1275 private static String safeNameAndType(ReadGraph graph, Resource r) throws DatabaseException {
1276 StringBuilder sb = new StringBuilder();
1277 sb.append(NameUtils.getSafeName(graph, r, true));
1279 boolean first = true;
1280 for (Resource type : graph.getPrincipalTypes(r)) {
1284 sb.append(NameUtils.getSafeName(graph, type, true));
1287 return sb.toString();
1290 private static <K, V> Map<K, V> newOrClear(Map<K, V> current) {
1291 if (current == null)
1292 return new THashMap<>();
1297 private void debug(TypicalInfo typicalInfo, String message) {
1299 System.out.println(message);
1300 typicalInfo.messageLog.add(message);