From VisualWorks® NonCommercial, 7.4 of December 5, 2005 on March 29, 2006 at 2:12:53 pm
CormasNS.Kernel.CormasModel util_files
createAndLoadAttributesFromClass: aClass fromFile: aStringOrFilename
"Load data from a File, create as many instances of aClass as necessary and set the values of the attributs according to the format declared into the file.
If the patchID is declared into the file, the located entities are moved to the right cells.
!!Be carefull, you should use this method to create spatial entity instances !!!"
self createInstancesFromClass: aClass fromFile: aStringOrFilename.
self loadAttributesForClass: aClass fromFile: aStringOrFilename
createAndLoadLocatedEntity: aClass fromFile: aStringOrFilename
"Load data from a File, create as many instances of aClass as necessary and set the values of the attributs according to the format declared into the file.
If the patchID is declared into the file, the located entities are moved to the right cells"
self createAndLoadAttributesFromClass: aClass fromFile: aStringOrFilename
exportAttribute: attributeName fromClass: className
"attributeName <Symbol>
className <String>"
| stream filename instances |
filename := (Cormas dataPath: self class name)
construct: className asString ,'_', attributeName asString , '.txt'.
stream := filename asFilename writeStream.
instances := self perform: ('the' , className , 's') asSymbol.
instances isEmpty ifFalse: [instances do:
[:anInstance |
stream
store: (anInstance perform: attributeName);
cr]].
stream close
getDataFromFile: aStringOrFilename
"Returns a collection of collections containing the elements of one line of the file. Ex:
OrderedCollection (OrderedCollection ('energy(Number)' 'bidon(Number)' 'rien(String)' 'patchID(Number)') OrderedCollection ('1' '10' 'dutout' '581')
.....
OrderedCollection ('3' '30' 'dutout' '263')
OrderedCollection ('4' '40' 'dutout' '654'))
The elements of the file are separated by a Tabulation.
Creates a 'read' connexion to a file (aStringOrFilename) located into the current model 'data'
directory and copy all data from this file into a collection."
^self getDataFromFile: aStringOrFilename separator: Character tab
loadAttributesForClass: aClass fromFile: aStringOrFilename
"Load data from a File and set the values of the attributs according to the format declared into the file"
"Reading the datafile, line by line, each line is giving the values for the attributes of an instance of the class"
| stream aFilename attribut_type dataLine couple name type numLigne i valeur instances method fileSize lines |
aFilename := self stringToFilename: aStringOrFilename.
lines := (self getDataFromFile: aFilename) select: [:line | line isEmpty not].
fileSize := lines size.
instances := self theEntities: aClass.
(fileSize-1) = instances size
ifFalse: [Dialog warn: 'Different number of instances declared in ', aStringOrFilename asString , ' and in ' , ('the' , aClass name asString , 's') , ' !!'.
self halt].
stream := aFilename readStream.
"On devine le format des lignes"
stream lineEndAuto.
"Reading the first line containing the attributes' name. Ex: energy(Number) bidon(Number)"
attribut_type := OrderedCollection new.
dataLine := (stream upTo: Character cr) readStream.
[dataLine atEnd] whileFalse:
[couple := Array new: 2. "couple = ['energy: ' 'Number']"
name := dataLine upTo: $(.
name := (name , ':') asSymbol.
type := dataLine upTo: $).
type := ('as' , type) asSymbol.
dataLine skipSeparators.
couple
at: 1 put: name;
at: 2 put: type.
attribut_type add: couple].
dataLine close.
"lecture des valeurs des attributs, et maj des entites spatiales a partir de ces valeurs"
"balayage de gauche a droite et de bas en haut"
numLigne := 0.
[stream atEnd] whileFalse:
[dataLine := (stream upTo: Character cr) readStream.
numLigne := numLigne + 1.
i := 0.
[dataLine atEnd] whileFalse:
[i := i + 1.
valeur := dataLine upTo: (Character tab).
method := (attribut_type at: i) first.
valeur = 'nil'
ifFalse: [valeur := valeur perform: (attribut_type at: i) last]
ifTrue: [valeur := nil].
valeur isNil
ifFalse:
[method = #patchID:
ifTrue:
[(aClass inheritsFrom: AgentLocation) ifTrue: [method := #moveTo:].
(aClass inheritsFrom: ObjectLocation) ifTrue: [method := #isMovedTo:].
valeur := self spaceModel elementaryEntities
detect: [:cell | cell id = valeur]]].
(instances at: numLigne) perform: method with: valeur].
dataLine close].
stream close
saveAttributes: attributes fromClass: aClass inFile: aStringOrFilename
"Save the attributes of the intances of aClass on a file 'aStringOrFilename'
attributes <OrderedCollection> ex: OrderedCollection ('seed' 'context')
file <Filename> ex: aFATFilename('d:\vw7nc\cormas\Models\TSE\data\titi.txt') or
file <String> ex: 'titi.txt' it will be save into data/"
self
saveAttributes: attributes
fromClass: aClass
inFile: aStringOrFilename
separator: Character tab
saveLocatedAgent: aClass withAttributs: attributes inFile: aStringOrFilename
"Save the attributes of the intances of aClass (SituatedAgent or LocatedObject) on a file 'aStringOrFilename'
attributes <OrderedCollection> ex: OrderedCollection ('seed' 'context')
file <Filename> ex: aFATFilename('d:\vw7nc\cormas\Models\TSE\data\titi.txt') or
file <String> ex: 'titi.txt' it will be save into data/"
attributes add: 'patchID'.
self saveAttributes: attributes fromClass: aClass inFile: aStringOrFilename