Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe6c51ea1bbee33a406aec4130ff04486b5c940d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
/*******************************************************************************
 * Copyright (c) 2019 Willink Transformations and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *   E.D.Willink - Initial API and implementation
 *******************************************************************************/
package org.eclipse.qvtd.compiler.internal.qvts2qvts.partitioner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.pivot.utilities.ClassUtil;
import org.eclipse.ocl.pivot.utilities.UniqueList;
import org.eclipse.qvtd.compiler.internal.qvtb2qvts.AbstractTransformationAnalysis;
import org.eclipse.qvtd.compiler.internal.qvtm2qvts.QVTm2QVTs;
import org.eclipse.qvtd.compiler.internal.qvts2qvts.utilities.ReachabilityForest;
import org.eclipse.qvtd.compiler.internal.utilities.CompilerUtil;
import org.eclipse.qvtd.pivot.qvtschedule.BasicPartition;
import org.eclipse.qvtd.pivot.qvtschedule.BooleanLiteralNode;
import org.eclipse.qvtd.pivot.qvtschedule.ClassDatum;
import org.eclipse.qvtd.pivot.qvtschedule.Edge;
import org.eclipse.qvtd.pivot.qvtschedule.NavigableEdge;
import org.eclipse.qvtd.pivot.qvtschedule.NavigationEdge;
import org.eclipse.qvtd.pivot.qvtschedule.Node;
import org.eclipse.qvtd.pivot.qvtschedule.PropertyDatum;
import org.eclipse.qvtd.pivot.qvtschedule.Region;
import org.eclipse.qvtd.pivot.qvtschedule.Role;
import org.eclipse.qvtd.pivot.qvtschedule.SuccessEdge;
import org.eclipse.qvtd.pivot.qvtschedule.SuccessNode;
import org.eclipse.qvtd.pivot.qvtschedule.utilities.QVTscheduleUtil;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * ReachabilityPartitioningStrategy partitions in accordance with the reachability and dependencies avoiding
 * any special heuristics for when/where/....
 */
public class ReachabilityPartitioningStrategy extends AbstractPartitioningStrategy
{
	protected abstract static class AbstractReachabilityPartitionFactory extends AbstractSimplePartitionFactory
	{
		/**
		 * The strategy that supervises the partition and accumulates the novelties.
		 */
		protected final @NonNull ReachabilityPartitioningStrategy strategy;

		/**
		 * The name for the partition.
		 */
		protected final @NonNull String name;

		/**
		 * The depth and reachability of nodes in the partition.
		 */
		protected final @NonNull ReachabilityForest reachabilityForest;

		/**
		 * The nodes from the partitioned region that are the pattern match entry points for this partition.
		 */
		protected final @NonNull Iterable<@NonNull ? extends Node> headNodes;

		/**
		 * The nodes that must appear in the partitioned region and which do not appear in a preceding partition.
		 */
		protected final @Nullable Iterable<@NonNull ? extends Node> novelNodes;

		/**
		 * The edges that must appear in the partitioned region and which do not appear in a preceding partition.
		 */
		protected final @Nullable Iterable<@NonNull ? extends Edge> novelEdges;

		/**
		 * The partition factories for the partitions that precede this partition.
		 */
		private final @NonNull UniqueList<@NonNull AbstractReachabilityPartitionFactory> predecessors = new UniqueList<>();

		/**
		 * The nodes from the partitioned region that are to appear in this partition.
		 */
		private final @NonNull List<@NonNull Node> reachableNodes = new ArrayList<>();

		/**
		 * The edges from the partitioned region that are to appear in this partition.
		 */
		private final @NonNull List<@NonNull Edge> reachableEdges = new ArrayList<>();

		protected AbstractReachabilityPartitionFactory(@NonNull ReachabilityPartitioningStrategy strategy, @NonNull ReachabilityForest reachabilityForest,
				@NonNull Iterable<@NonNull ? extends Node> headNodes, @Nullable Iterable<@NonNull ? extends Node> novelNodes, @Nullable Iterable<@NonNull ? extends Edge> novelEdges) {
			super(strategy.mappingPartitioner);
			this.strategy = strategy;
			strategy.setCurrentPartitionFactory(this);
			this.name = computeName(ClassUtil.nonNullState(reachabilityForest.getDisambiguator()));
			this.reachabilityForest = reachabilityForest;
			this.headNodes = headNodes;
			this.novelNodes = novelNodes;
			this.novelEdges = novelEdges;
			if (novelNodes != null) {
				for (@NonNull Node node : novelNodes) {
					addReachableNode(node);
				}
			}
			if (novelEdges != null) {
				for (@NonNull Edge edge : novelEdges) {
					addReachableEdge(edge);
				}
			}
		}

		public void addPredecessor(@NonNull AbstractReachabilityPartitionFactory predecessor) {
			predecessors.add(predecessor);
		}

		/**
		 * Add edge and its source/targert nodes to the partition.
		 *
		 * Returns true if edge added, false if already present.
		 */
		private boolean addReachableEdge(@NonNull Edge edge) {
			if (reachableEdges.contains(edge)) {
				return false;
			}
			addReachableNode(QVTscheduleUtil.getSourceNode(edge));
			addReachableNode(QVTscheduleUtil.getTargetNode(edge));
			reachableEdges.add(edge);
			strategy.addEdge(this, edge);
			return true;
		}

		/**
		 * Add node and all transitively preceding nodes to the partition.
		 *
		 * Returns true if node added, false if already present.
		 */
		private boolean addReachableNode(@NonNull Node node) {
			if (reachableNodes.contains(node)) {
				return false;
			}
			reachableNodes.add(node);
			strategy.addNode(this, node);
			for (@NonNull Node predecessor : reachabilityForest.getPredecessorsClosure(node)) {
				addReachableNode(predecessor);
			}
			return true;
		}

		public boolean containsNode(@NonNull Node node) {
			return reachableNodes.contains(node);
		}

		@Override
		public @NonNull PartitionAnalysis createPartitionAnalysis(@NonNull PartitionedTransformationAnalysis partitionedTransformationAnalysis) {
			//	Iterable<@NonNull Node> headNodes = QVTscheduleUtil.getHeadNodes(region);
			BasicPartition partition = createBasicPartition(name, headNodes);
			for (@NonNull Node node : reachableNodes) {
				Role role = resolveNodeRole(node);
				addNode(partition, node, role);
			}
			for (@NonNull Edge edge : reachableEdges) {
				if (!edge.isSecondary()) {
					Role role = resolveEdgeRole(edge);
					addEdge(partition, edge, role);
				}
			}



			//	Iterable<@NonNull Node> headNodes = QVTscheduleUtil.getHeadNodes(region);
			//	BasicPartition partition = createBasicPartition(name, headNodes);
			BasicPartitionAnalysis basicPartitionAnalysis = new BasicPartitionAnalysis(partitionedTransformationAnalysis, partition, reachabilityForest, "xyzzy", "xyzzy");
			//	initializePartition(basicPartitionAnalysis);
			if (QVTm2QVTs.DEBUG_GRAPHS.isActive()) {
				scheduleManager.writeDebugGraphs(partition, null);
			}
			return basicPartitionAnalysis;
		}

		protected void initPartitionFactory() {
			//
			//	Grow the nodes to include all not-yet reached reachable nodes.
			//
			for (int i = 0; i < reachableNodes.size(); i++) {
				@NonNull Node sourceNode = reachableNodes.get(i);
				for (@NonNull Edge edge : QVTscheduleUtil.getOutgoingEdges(sourceNode)) {
					Node targetNode = QVTscheduleUtil.getTargetNode(edge);
					AbstractReachabilityPartitionFactory targetPartitionFactory = strategy.basicGetPartitionFactory(targetNode);
					if (targetPartitionFactory == null) {
						if (edge.isLoaded()) {
							addReachableNode(targetNode);
						}
					}
				}
			}
			//
			//	Add the edges to nodes to include all not-yet reached reachable nodes.
			//
			for (@NonNull Node targetNode : reachableNodes) {
				for (@NonNull Edge edge : QVTscheduleUtil.getIncomingEdges(targetNode)) {
					Node sourceNode = QVTscheduleUtil.getSourceNode(edge);
					AbstractReachabilityPartitionFactory sourcePartitionFactory = strategy.basicGetPartitionFactory(sourceNode);
					if ((sourcePartitionFactory != null) && reachableNodes.contains(sourceNode)) {
						addReachableEdge(edge);
						if (sourcePartitionFactory != this) {
							addPredecessor(sourcePartitionFactory);
						}
					}
				}
			}
		}

		protected @NonNull Role resolveEdgeRole(@NonNull Edge edge) {
			Role edgeRole = QVTscheduleUtil.getEdgeRole(edge);
			if ((edgeRole == Role.REALIZED) && (strategy.getPartitionFactory(edge) != this)) {
				return Role.PREDICATED;
			}
			return edgeRole;
		}

		@Override
		protected @Nullable Role resolveEdgeRole(@NonNull Role sourceNodeRole, @NonNull Edge edge, @NonNull Role targetNodeRole) {
			if (mappingPartitioner.hasRealizedEdge(edge)) {
				return Role.PREDICATED;
			}
			Role edgeRole = QVTscheduleUtil.getEdgeRole(edge);
			//	if (edgeRole == Role.REALIZED) {
			//		assert !mappingPartitioner.hasRealizedEdge(edge);
			//	}
			return edgeRole;
		}

		protected @NonNull Role resolveNodeRole(@NonNull Node node) {
			Role nodeRole = QVTscheduleUtil.getNodeRole(node);
			if ((nodeRole == Role.REALIZED) && (strategy.getPartitionFactory(node) != this)) {
				return Role.PREDICATED;
			}
			return nodeRole;
		}

		@Override
		public @NonNull String toString() {
			return toString(new StringBuilder(), "\n");
		}

		public @NonNull String toString(@NonNull StringBuilder s, @NonNull String prefix) {
			s.append(name);
			s.append(prefix + "\tpredecessors: ");
			for (@NonNull AbstractReachabilityPartitionFactory predecessor : predecessors) {
				s.append(" " + predecessor.name);
			}
			Iterable<@NonNull ? extends Node> novelNodes2 = novelNodes;
			if (novelNodes2 != null) {
				s.append(prefix + "\tnovel nodes");
				for (@NonNull Node node : novelNodes2) {
					s.append(prefix + "\t\t" + node);
				}
			}
			Iterable<@NonNull ? extends Edge> novelEdges2 = novelEdges;
			if (novelEdges2 != null) {
				s.append(prefix + "\tnovel edges");
				for (@NonNull Edge edge : novelEdges2) {
					s.append(prefix + "\t\t" + edge);
				}
			}
			s.append(prefix + "\treachable nodes");
			for (@NonNull Node node : reachableNodes) {
				s.append(prefix + "\t\t" + node);
			}
			s.append(prefix + "\treachable edges");
			for (@NonNull Edge edge : reachableEdges) {
				s.append(prefix + "\t\t" + edge);
			}
			return s.toString();
		}
	}

	/**
	 * The CtorPartitionFactory creates the trace node and links to the head node(s). If there are multiple
	 * headnodes, inter-head edges are also added so that once the ctor partition is hoisted into the caller,
	 * the caller may improve performance by considering only appropriately inter-related heads.
	 */
	protected static class CtorPartitionFactory extends AbstractReachabilityPartitionFactory
	{
		protected CtorPartitionFactory(@NonNull ReachabilityPartitioningStrategy strategy, @NonNull ReachabilityForest reachabilityForest,
				@NonNull Iterable<@NonNull ? extends Node> headNodes, @NonNull Iterable<@NonNull ? extends Node> novelNodes, @NonNull Iterable<@NonNull ? extends Edge> novelEdges) {
			super(strategy, reachabilityForest, headNodes, novelNodes, novelEdges);
			//	initPartitionFactory();						// no magic growth
		}

		@Override
		protected void initPartitionFactory() {
			throw new UnsupportedOperationException();		// All done in constructor, no magic growth.
		}

		@Override
		protected @NonNull Role resolveNodeRole(@NonNull Node node) {
			Role nodeRole = node.getNodeRole();
			if (nodeRole == Role.REALIZED) {
				return Role.SPECULATION;
			}
			return super.resolveNodeRole(node);
		}
	}

	/**
	 * The InitPartitionFactory checks all predicates that can be established independent of the success of concurrent partitions.
	 * The local success is assigned on completion.
	 */
	protected static class InitPartitionFactory extends AbstractReachabilityPartitionFactory
	{
		protected InitPartitionFactory(@NonNull ReachabilityPartitioningStrategy strategy, @NonNull ReachabilityForest reachabilityForest,
				@NonNull Iterable<@NonNull ? extends Node> headNodes, @NonNull Iterable<@NonNull ? extends Node> novelNodes) {
			super(strategy, reachabilityForest, headNodes, novelNodes, null);
			initPartitionFactory();
		}

		@Override
		protected @NonNull Role resolveNodeRole(@NonNull Node node) {
			if (node.isTrace()) {
				return Role.SPECULATED;
			}
			return super.resolveNodeRole(node);
		}
	}

	/**
	 * The LoopPartitionFactory defines the decision stage in the execution.
	 *
	 * The preceding CtorPartitionFactory creates the trace. Then the preceding InitPartitionFactory checks all
	 * predicates that are dependent solely on earlier executing partitions.
	 *
	 * The LoopPartitionFactory checks the remaining non-corrolary predicates that have cyclic dependencies on
	 * concurrently executing partitions. Once cycle execution has been validated, success is assigned true, and
	 * all remaining realized nodes are created.
	 *
	 * Thereafter the RestPartitionFactory instances realize edges that are predicated solely on corrolary
	 * predicates.
	 */
	protected static class LoopPartitionFactory extends AbstractReachabilityPartitionFactory
	{
		protected LoopPartitionFactory(@NonNull ReachabilityPartitioningStrategy strategy, @NonNull ReachabilityForest reachabilityForest,
				@NonNull Iterable<@NonNull ? extends Node> headNodes, @NonNull Iterable<@NonNull ? extends Node> novelNodes, @NonNull Iterable<@NonNull ? extends Edge> novelEdges) {
			super(strategy, reachabilityForest, headNodes, novelNodes, novelEdges);
			initPartitionFactory();
		}

		@Override
		protected @NonNull Role resolveEdgeRole(@NonNull Edge edge) {
			CyclicRegionAnalysis cyclicRegionAnalysis = strategy.basicGetCyclicRegionAnalysis();
			if (cyclicRegionAnalysis != null) {
				if ((edge.getEdgeRole() == Role.PREDICATED) && (edge instanceof SuccessEdge)) {
					assert !edge.isSecondary();
					SuccessEdge navigationEdge = (SuccessEdge) edge;
					PropertyDatum propertyDatum = strategy.scheduleManager.getPropertyDatum(navigationEdge);
					if (cyclicRegionAnalysis.isCyclic(propertyDatum)) {
						Node targetNode = QVTscheduleUtil.getTargetNode(navigationEdge);
						if ((targetNode instanceof BooleanLiteralNode) && ((BooleanLiteralNode)targetNode).isBooleanValue()) {
							return Role.SPECULATED;
						}
					}
				}
			}
			return super.resolveEdgeRole(edge);
		}

		@Override
		protected @NonNull Role resolveNodeRole(@NonNull Node node) {
			if (node.isTrace()) {
				return Role.SPECULATED;
			}
			if (node == strategy.localSuccessNode) {
				return Role.CONSTANT_SUCCESS_TRUE;
			}
			return super.resolveNodeRole(node);
		}
	}

	protected static class RestPartitionFactory extends AbstractReachabilityPartitionFactory
	{
		protected RestPartitionFactory(@NonNull ReachabilityPartitioningStrategy strategy, @NonNull ReachabilityForest reachabilityForest,
				@NonNull Iterable<@NonNull ? extends Node> headNodes, @NonNull Iterable<@NonNull ? extends Node> novelNodes, @NonNull Iterable<@NonNull ? extends Edge> novelEdges) {
			super(strategy, reachabilityForest, headNodes, novelNodes, novelEdges);
			initPartitionFactory();
		}

		@Override
		protected @NonNull Role resolveNodeRole(@NonNull Node node) {
			if (node instanceof SuccessNode) {
				return Role.CONSTANT_SUCCESS_TRUE;
			}
			return super.resolveNodeRole(node);
		}
	}

	/**
	 * The region to be partitioned,
	 */
	protected final @NonNull Region region;

	/**
	 * The nodes in the synthesized region. Excludes the additional localSuccess node.
	 */
	private final @NonNull Iterable<@NonNull Node> originalNodes;

	/**
	 * The edges in the synthesized region. Excludes the additional localSuccess edge.
	 */
	private final @NonNull Iterable<@NonNull Edge> originalEdges;

	// FIXME Merge state into mappingPartitioner once other factories obsolete.
	/**
	 * The ordered list of all ProtoPartition to support the Region.
	 */
	private final @NonNull List<@NonNull AbstractReachabilityPartitionFactory> partitionFactories = new ArrayList<>();

	/**
	 * Mapping from each Region node to the first ProtoPartition in which the node is used.
	 */
	private final @NonNull Map<@NonNull Node, @NonNull AbstractReachabilityPartitionFactory> node2partitionFactory = new HashMap<>();

	/**
	 * Mapping from each Region edge to the first ProtoPartition in which the edge is used.
	 */
	private final @NonNull Map<@NonNull Edge, @NonNull AbstractReachabilityPartitionFactory> edge2partitionFactory = new HashMap<>();

	/**
	 * The node that represemts the transformation instance.
	 */
	//	private final @Nullable Node thisNode;

	/**
	 * The node that traces the region execution.
	 *
	 * Currently always a node. If multiple nodes are ever needed for a merge we will probably need
	 * to actually merge the trace and so again have just one trace node.
	 */
	//	private final @NonNull Node traceNode;

	private final @NonNull Iterable<@NonNull Node> thisAndTraceNodes;

	/**
	 * The loop/global success node that is realized in the region.
	 */
	private final @NonNull SuccessNode globalSuccessNode;

	/**
	 * The init/local success edge that is realized in the region.
	 */
	private @Nullable SuccessEdge localSuccessEdge = null;
	private @Nullable SuccessNode localSuccessNode = null;

	//	private @Nullable List<@NonNull Node> constantSourceNodes;

	private @NonNull List<@NonNull Node> thisAndTraceAndConstantSourceNodes = new ArrayList<>();

	private @Nullable AbstractReachabilityPartitionFactory currentPartitionFactory = null;

	public ReachabilityPartitioningStrategy(@NonNull PartitionedTransformationAnalysis partitionedTransformationAnalysis, @NonNull MappingPartitioner mappingPartitioner) {
		super(partitionedTransformationAnalysis, mappingPartitioner);
		assert scheduleManager.useActivators();
		assert false;
		this.region = regionAnalysis.getRegion();
		this.originalNodes = Lists.newArrayList(QVTscheduleUtil.getOwnedNodes(region));
		this.originalEdges = Lists.newArrayList(QVTscheduleUtil.getOwnedEdges(region));
		SuccessNode globalSuccessNode = null;
		Node thisNode = null;
		Node traceNode = null;
		for (@NonNull Node node : originalNodes) {
			if (node.isThis()) {
				assert thisNode == null : "Only one this node permitted in " + region.getName();
				thisNode = node;
				thisAndTraceAndConstantSourceNodes.add(node);
			}
			else if (node.isTrace()) {
				assert traceNode == null : "Only one trace node permitted in " + region.getName();
				traceNode = node;
				thisAndTraceAndConstantSourceNodes.add(node);
			}
			else if (node.isSuccess()) {
				assert globalSuccessNode == null : "Only one success node permitted in " + region.getName();
				globalSuccessNode = (SuccessNode)node;
			}
			else if (node.isConstant() && Iterables.isEmpty(node.getIncomingEdges())) {
				thisAndTraceAndConstantSourceNodes.add(node);
			}
		}
		assert globalSuccessNode != null : "No global success node in " + region.getName();
		this.globalSuccessNode = globalSuccessNode;
		//	this.thisNode = thisNode;
		assert traceNode != null : "No trace node in " + region.getName();
		//	this.traceNode = traceNode;
		this.thisAndTraceNodes = thisNode != null ? Lists.newArrayList(thisNode, traceNode) : Collections.singletonList(traceNode);
	}

	private void addEdge(@NonNull AbstractReachabilityPartitionFactory partitionFactory, @NonNull Edge edge) {
		if (!edge2partitionFactory.containsKey(edge)) {
			edge2partitionFactory.put(edge, partitionFactory);
		}
	}

	private void addNode(@NonNull AbstractReachabilityPartitionFactory partitionFactory, @NonNull Node node) {
		if (!node2partitionFactory.containsKey(node)) {
			node2partitionFactory.put(node, partitionFactory);
		}
	}

	private @Nullable CyclicRegionAnalysis basicGetCyclicRegionAnalysis() {
		return transformationAnalysis.basicGetCyclicRegionAnalysis(regionAnalysis);
	}

	protected @Nullable AbstractReachabilityPartitionFactory basicGetPartitionFactory(@NonNull Edge edge) {
		return edge2partitionFactory.get(edge);
	}

	protected @Nullable AbstractReachabilityPartitionFactory basicGetPartitionFactory(@NonNull Node node) {
		return node2partitionFactory.get(node);
	}

	@Override
	protected void check() {
		super.check();
		for (@NonNull Node node : QVTscheduleUtil.getOwnedNodes(region)) {
			if (node.isPredicated()) {
				if (node2partitionFactory.get(node) == null) {
					CompilerUtil.addRegionError(mappingPartitioner.getProblemHandler(), region, "Should have predicated " + node);
				}
			}
		}
		for (@NonNull Edge edge : QVTscheduleUtil.getOwnedEdges(region)) {
			if (edge.isPredicated()) {
				if (edge2partitionFactory.get(edge) == null) {
					CompilerUtil.addRegionError(mappingPartitioner.getProblemHandler(), region, "Should have predicated " + edge);
				}
			}
		}
	}

	private void createCtorPartitionFactory() {
		Iterable<@NonNull Node> headNodes = QVTscheduleUtil.getHeadNodes(region);
		List<@NonNull Node> novelCtorNodes = new ArrayList<>();
		List<@NonNull NavigableEdge> novelCtorEdges = new ArrayList<>();
		//
		//	Gather all head-to-head edges
		//
		for (@NonNull Node headNode : headNodes) {
			novelCtorNodes.add(headNode);
			for (@NonNull Edge novelEdge : QVTscheduleUtil.getOutgoingEdges(headNode)) {
				if ((novelEdge instanceof NavigableEdge) && novelEdge.isLoaded() && Iterables.contains(headNodes, QVTscheduleUtil.getTargetNode(novelEdge))) {
					novelCtorEdges.add((NavigableEdge)novelEdge);
				}
			}
		}
		//
		//	Gather all head-to-trace edges
		//
		for (@NonNull Node traceNode : thisAndTraceNodes) {
			if (!novelCtorNodes.contains(traceNode)) {				// dispatched head is a trace node
				novelCtorNodes.add(traceNode);
				for (@NonNull Edge novelEdge : QVTscheduleUtil.getOutgoingEdges(traceNode)) {
					if ((novelEdge instanceof NavigableEdge) && Iterables.contains(headNodes, QVTscheduleUtil.getTargetNode(novelEdge))) {
						novelCtorEdges.add((NavigableEdge)novelEdge);
					}
				}
			}
		}
		if (!novelCtorEdges.isEmpty() || (novelCtorNodes.size() > 1)) {		// Skip trivial trace only partition
			ReachabilityForest ctorReachabilityForest = new ReachabilityForest("ctor", headNodes, novelCtorEdges);
			new CtorPartitionFactory(this, ctorReachabilityForest, headNodes, novelCtorNodes, novelCtorEdges);
		}
	}

	private void createInitPartitionFactory() {
		//
		//	Create the InitPartitionFactory for acyclic elements that precede the cycle.
		//
		// FIXME an ordering of the acyclic unpartitioned regions could allow many predicate steps to
		//  be merged from the outset.
		//

		CyclicRegionAnalysis cyclicRegionAnalysis = basicGetCyclicRegionAnalysis();
		//
		//	Gather the acyclic edges and remember if there is a cyclic edge.
		//
		boolean hasCyclicEdge = false;
		List<@NonNull NavigableEdge> reachingInitEdges = new ArrayList<>();
		//	Set<@NonNull Node> oldNodes1 = new HashSet<>();
		for (@NonNull Edge edge : originalEdges) {
			boolean isReaching = false;
			if (edge.isNavigable()) {
				NavigationEdge navigationEdge = (NavigationEdge) edge;
				if (basicGetPartitionFactory(edge) != null) {
					isReaching = true;
				}
				if (edge.isOld() && !edge.isSecondary()) {
					if (cyclicRegionAnalysis != null) {
						PropertyDatum propertyDatum = scheduleManager.getPropertyDatum(navigationEdge);
						if (cyclicRegionAnalysis.isCyclic(propertyDatum)) {
							hasCyclicEdge = true;
						}
						else {
							isReaching = true;
						}
					}
					else {
						isReaching = true;
					}
				}
				if (isReaching) {
					reachingInitEdges.add(navigationEdge);
				}
			}
			//	else {//if (edge.isOld() && !edge.isSecondary()) {
			//		isReaching = true;
			//	}
			//	if (isReaching) {
			//		oldNodes1.add(QVTscheduleUtil.getSourceNode(edge));
			//		oldNodes1.add(QVTscheduleUtil.getTargetNode(edge));
			//	}
		}
		/*		Set<@NonNull Node> oldNodes1 = new HashSet<>();
		for (@NonNull Node node : allNodes) {
			if (node.isOld()) {
				//	ClassDatum classDatum = QVTscheduleUtil.getClassDatum(node);
				//	if (!classDatum.isCollectionType()) {		// Collection DataTypes are non 1:1 edge ends
				oldNodes1.add(node);
				//	}
			}
		} */
		//		assert hasCyclicEdge1 == hasCyclicEdge2;
		//		Collections.sort(navigableInitEdges1, NameUtil.NAMEABLE_COMPARATOR);
		//		Collections.sort(navigableInitEdges2, NameUtil.NAMEABLE_COMPARATOR);
		//		assert navigableInitEdges1.equals(navigableInitEdges2);

		//	strategy.regionAnalysis.createLocalSuccess();
		ReachabilityForest initReachabilityForest = new ReachabilityForest("init", thisAndTraceAndConstantSourceNodes, reachingInitEdges);
		//	Set<@NonNull Node> oldNodes2 = Sets.newHashSet(initReachabilityForest.getMostReachableFirstNodes());
		//				assert oldNodes1.equals(oldNodes2);
		List<@NonNull Node> novelInitNodes1 = null;
		for (@NonNull Node node : initReachabilityForest.getMostReachableFirstNodes()) {
			if ((basicGetPartitionFactory(node) == null) && !Iterables.contains(thisAndTraceNodes, node)) {
				if (novelInitNodes1 == null) {
					novelInitNodes1 = new ArrayList<>();
				}
				novelInitNodes1.add(node);
			}
		}
		/*	List<@NonNull Node> novelInitNodes2 = null;
		for (@NonNull Node node : allNodes) { //initReachabilityForest.getMostReachableFirstNodes()) {
			AbstractReachabilityPartitionFactory partitionFactory = basicGetPartitionFactory(node);
			if (partitionFactory == null) {
				if (novelInitNodes2 == null) {
					novelInitNodes2 = new ArrayList<>();
				}
				novelInitNodes2.add(node);
			}
		} */
		if (novelInitNodes1 != null) {
			//	assert novelInitNodes1.equals(novelInitNodes2);  -- not even close
			if (!hasCyclicEdge) {			// Acyclic - can do realizes now
				for (@NonNull Node node : originalNodes) {
					if (node.isRealized() && !novelInitNodes1.contains(node) && (basicGetPartitionFactory(node) == null)) {
						ClassDatum classDatum = QVTscheduleUtil.getClassDatum(node);
						if (!classDatum.isCollectionType()) {		// Collection DataTypes are non 1:1 edge ends
							novelInitNodes1.add(node);
						}
					}
				}
			}
			if (!novelInitNodes1.contains(globalSuccessNode)) {
				novelInitNodes1.add(getLocalSuccessNode());
			}
			new InitPartitionFactory(this, initReachabilityForest, thisAndTraceNodes, novelInitNodes1);
		}
	}

	private void createLoopPartitionFactory() {
		//
		//	Determine the reaching edges for the LoopPartitionFactory.
		//
		List<@NonNull NavigableEdge> reachingLoopEdges = new ArrayList<>();
		for (@NonNull Edge edge : originalEdges) {
			if (edge.isNavigable()) {
				boolean isReaching = false;
				NavigationEdge navigationEdge = (NavigationEdge)edge;
				if (basicGetPartitionFactory(edge) != null) {
					isReaching = true;		// Gather all already predicated/realized edges.
				}
				else if (edge.isOld() && !edge.isSecondary() && !transformationAnalysis.isCorollary(navigationEdge)) {
					isReaching = true;		// Gather all predicated edges that cannot be deferred to the rest partition as corrolaries.
				}
				if (isReaching) {
					reachingLoopEdges.add(navigationEdge);
				}
			}
		}
		if (localSuccessEdge != null) {
			reachingLoopEdges.add(localSuccessEdge);
		}
		//
		//	Determine the novel nodes for the LoopPartitionFactory.
		//
		ReachabilityForest loopReachabilityForest = new ReachabilityForest("loop", thisAndTraceAndConstantSourceNodes, reachingLoopEdges);
		List<@NonNull Node> novelLoopNodes = null;
		for (@NonNull Node node : loopReachabilityForest.getMostReachableFirstNodes()) {
			if (basicGetPartitionFactory(node) == null) {
				if (novelLoopNodes == null) {
					novelLoopNodes = new ArrayList<>();
				}
				novelLoopNodes.add(node);
			}
		}
		if (novelLoopNodes != null) {
			if (localSuccessNode != null) {
				novelLoopNodes.add(localSuccessNode);
			}
			for (@NonNull Node node : originalNodes) {
				if (node.isRealized() && !novelLoopNodes.contains(node) && (basicGetPartitionFactory(node) == null)) {
					ClassDatum classDatum = QVTscheduleUtil.getClassDatum(node);
					if (!classDatum.isCollectionType()) {		// Collection DataTypes are non 1:1 edge ends
						novelLoopNodes.add(node);
					}
				}
			}
			//			if (!novelLoopNodes.equals(novelLoopNodes2)) {
			//				getClass();
			//			}
			List<@NonNull Edge> novelLoopEdges = new ArrayList<>();
			for (@NonNull Edge edge : originalEdges) {
				if (edge.isRealized() && (basicGetPartitionFactory(edge) == null)) {
					Node sourceNode = QVTscheduleUtil.getSourceNode(edge);
					Node targetNode = QVTscheduleUtil.getTargetNode(edge);
					if (((loopReachabilityForest.basicGetCost(sourceNode) != null) || Iterables.contains(novelLoopNodes, sourceNode))
							&& ((loopReachabilityForest.basicGetCost(targetNode) != null) || Iterables.contains(novelLoopNodes, targetNode))) {
						novelLoopEdges.add(edge);
					}
				}
			}
			new LoopPartitionFactory(this, loopReachabilityForest, thisAndTraceNodes, novelLoopNodes, novelLoopEdges);
		}
	}

	protected void createNonPartition() {
		newPartitionAnalyses.add(new NonPartitionFactory(mappingPartitioner).createPartitionAnalysis(partitionedTransformationAnalysis));
	}

	private void createRestPartitionFactory() {
		if ("FtoN1_Family2SurnameContainment".equals(region.getName())) {
			getClass();
		}
		//
		//	Create RestPartitionFactory to progress the remaining realized edges.
		//
		List<@NonNull Edge> novelRestEdges = null;
		for (@NonNull Edge edge : originalEdges) {
			if (edge.isNavigable()) {
				NavigationEdge navigationEdge = (NavigationEdge) edge;
				if (edge.isRealized() && (basicGetPartitionFactory(edge) == null)) {
					if (novelRestEdges == null) {
						novelRestEdges = new ArrayList<>();
					}
					novelRestEdges.add(navigationEdge);
				}
			}
		}
		if (novelRestEdges != null) {
			List<@NonNull NavigableEdge> reachingRestEdges = new ArrayList<>();
			for (@NonNull Edge edge : originalEdges) {
				if (edge.isNavigable() && !novelRestEdges.contains(edge)) {
					reachingRestEdges.add((NavigableEdge)edge);
				}
			}
			Iterable<@NonNull Node> novelRestNodes;
			SuccessEdge localSuccessEdge2 = localSuccessEdge;
			if ((localSuccessEdge2 != null) && (basicGetPartitionFactory(globalSuccessNode) == null)) {
				reachingRestEdges.add(localSuccessEdge2);
				novelRestNodes = Lists.newArrayList(QVTscheduleUtil.getTargetNode(localSuccessEdge2), globalSuccessNode);
			}
			else {
				novelRestNodes = Collections.singletonList(globalSuccessNode);
			}
			ReachabilityForest restReachabilityForest = new ReachabilityForest("rest", thisAndTraceAndConstantSourceNodes, reachingRestEdges);
			new RestPartitionFactory(this, restReachabilityForest, thisAndTraceNodes, novelRestNodes, novelRestEdges);
		}
	}

	private @NonNull SuccessNode getLocalSuccessNode() {
		SuccessNode localSuccessNode2 = localSuccessNode;
		if (localSuccessNode2 == null) {
			this.localSuccessEdge = regionAnalysis.createLocalSuccess();
			this.localSuccessNode = localSuccessNode2 = (SuccessNode) QVTscheduleUtil.getTargetNode(localSuccessEdge);
		}
		return localSuccessNode2;
	}

	private @NonNull AbstractReachabilityPartitionFactory getPartitionFactory(@NonNull Edge edge) {
		AbstractReachabilityPartitionFactory partitionFactory = edge2partitionFactory.get(edge);
		return ClassUtil.nonNullState(partitionFactory);
	}

	private @NonNull AbstractReachabilityPartitionFactory getPartitionFactory(@NonNull Node node) {
		AbstractReachabilityPartitionFactory partitionFactory = node2partitionFactory.get(node);
		return ClassUtil.nonNullState(partitionFactory);
	}

	public @NonNull Region getRegion() {
		return region;
	}

	public @NonNull AbstractTransformationAnalysis getTransformationAnalysis() {
		return transformationAnalysis;
	}

	@Override
	public @NonNull Iterable<@NonNull PartitionAnalysis> partition() {
		String name = regionAnalysis.getName();
		if ("mapVariable_qvtr".equals(name)) {
			getClass();
		}
		//
		//	Create the PartitionFactories.
		//
		createCtorPartitionFactory();
		createInitPartitionFactory();
		createLoopPartitionFactory();
		createRestPartitionFactory();
		//
		//	Create the partitions.
		//
		for (@NonNull AbstractReachabilityPartitionFactory partitionFactory : partitionFactories) {
			newPartitionAnalyses.add(partitionFactory.createPartitionAnalysis(partitionedTransformationAnalysis));
		}
		check();
		return newPartitionAnalyses;
	}

	private void setCurrentPartitionFactory(@NonNull AbstractReachabilityPartitionFactory currentPartitionFactory) {
		if (this.currentPartitionFactory != null) {
			currentPartitionFactory.addPredecessor(this.currentPartitionFactory);
		}
		this.currentPartitionFactory = currentPartitionFactory;
		partitionFactories.add(currentPartitionFactory);
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append(mappingPartitioner.getName());
		for (@NonNull AbstractReachabilityPartitionFactory partitionFactory : partitionFactories) {
			s.append("\n\t");
			partitionFactory.toString(s, "\n\t");
		}
		return s.toString();
	}

	@Override
	protected boolean useActivators() {
		return true;
	}
}

Back to the top