AbstractPattern subclass: #FollowRelation
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'PSiGene-Patterns-JP'!



!FollowRelation methodsFor: 'smalltalk code templates'!

doLoopST
	"This method returns a Smalltalk-Template for a Pattern."

	^MacroText fromString: '"1:n relation."
	self {relation} connections do:
		[	:anObject|
			anObject {remoteOperation}{optionalParameter}
		].'!

dontLoopST
	"This method returns a Smalltalk-Template for a Pattern."

	^MacroText fromString: '"1:1 relation."
	self {relation} instance notNil ifTrue: 
		[ self {relation} instance {remoteOperation}{optionalParameter} ].'!

operationST
	"This method returns a Smalltalk-Template for a Pattern."
	"The template contains a method to get the value overall relations."

	^MacroText fromString: '{operation}{optionalParameter}
	"follow all relations."
	{loop}'!

optionalParameterST
	^MacroText fromString: ': {parameter}'! !


!FollowRelation methodsFor: 'pattern installation'!

installParticipants
	"Install all participants by code synthesis and c&p."

	self target: (self elementAtMacro: 'target').
	self categorySecondName: 'accessing'.
	self putMethod: 'operation' to: self target.
	^target! !


!FollowRelation methodsFor: 'initialize-release'!

setupMacros
	"Setup Macros being used inside this pattern."

	self addMacro: 'target' withType: #object; addMacro: 'relation' withType: #relation; addMacro: 'operation' withType: #method;
		addMacro: 'remoteOperation'
		withElement: '{operation}'
		withType: #(#interface);
		addMacro: 'parameter'
		withElement: #optional
		withType: #(#expression).
	^self! !


!FollowRelation methodsFor: 'code generation'!

loop
	| relSpec macroText suffix |
	suffix := self languageSuffix.
	relSpec := self target specificationsForRelation: (self elementAtMacro: 'relation').
	(relSpec at: #type)
		= #Relation_1toN
		ifTrue: [macroText := self perform: ('doLoop' , suffix) asSymbol]
		ifFalse: [macroText := self perform: ('dontLoop' , suffix) asSymbol].
	^macroText!

operation
	"Generates a method to get the contents of an variable."
	"Get the suffix for retrieving the language-dependend template."

	| suffix macroText |
	suffix := self languageSuffix.	"Read a MacroText for language 'suffix'."
	macroText := self perform: ('operation' , suffix) asSymbol.
	macroText := self replaceLoopIn: macroText withRelation: (self elementAtMacro: 'relation').
	(self elementAtMacro: 'parameter')
		~= #optional
		ifTrue: [macroText replaceMacro: 'optionalParameter' with: (self perform: ('optionalParameter' , suffix) asSymbol)]
		ifFalse: [macroText replaceMacro: 'optionalParameter' with: ''].
	macroText replaceMacrosUsing: self macrosAndElements.
	^macroText!

replaceLoopIn: aMacroText withRelation: aRelationDescription 
	| rels |
	rels := Compiler evaluate: aRelationDescription.
	(rels isKindOf: Array)
		ifTrue: 
			[rels
				do: 
					[:aRelation | 
					aMacroText replaceMacro: 'loop' with: '{loop}
	{nextLoop}'.
					self replaceLoopIn: aMacroText withRelation: aRelation.
					aMacroText replaceMacro: 'nextLoop' with: '{loop}'].
			aMacroText replaceMacro: 'loop' with: '']
		ifFalse: 
			["test, if single or multiple relations"
			self atMacro: 'relation' putElement: aRelationDescription.
			aMacroText replaceMacro: 'loop' with: (self perform: 'loop' asSymbol).
			aMacroText replaceMacrosUsing: self macrosAndElements].
	^aMacroText! !

To increase readability of this file, some HTML tags have been included

(click here for the original file)