Messages from list : cormas@cirad.fr

Choose a topic among the following archives :

Re: reading external data in CORMAS

New Message Reply Date view Thread view Subject view Author view

Subject: Re: reading external data in CORMAS
From: Christophe Le Page (christophe.le_page@cirad.fr)
Date: Tue Aug 26 2003 - 18:40:35 CEST

Hi Marrit,

To import ASCII data, you may use the generic method
CormasModel>>readDataForClass:format:
(not included yet in the last release of Cormas, so you have to "file in"
the attached file called readDataForClassformat.st into your
cormas2003.im image).

Then, you should prepare a datafile called "Classname.txt". The first
line of the ASCII
file should be the list of attributes names, then the next lines are
giving the values of the
corresponding attributes for the created instances of the class.
Cautions:
1. The datafile "Classname.txt" should be located in the data
subdirectory of the model
    under development.
2. Separator of the ASCII datafile should be a tab.
3. The instances should have been created before, to use the method. The
association
    between an instance and a line of the ASCII datafile is made throw
the primary key "id".

Below is an example to initialize instances of a class Exploitant (see
the corresponding input
datafile called "Exploitant.txt" attached to this email). The method
invoking the reading
of external data looks like:

readExploitantsData
| dataFormat |
    dataFormat := List new.
    dataFormat
        add: #id: -> #asNumber;
        add: #age: -> #asNumber;
        add: #formation: -> #asBoolean;
        add: #pleinTemps: -> #asBoolean.
    self readDataForClass: 'Exploitant' format: dataFormat

To write the dataFormat, you need to read the header (first line) of the
input datafile, and to
check the type of data associated to each attribute in the corresponding
column.

There are also possibilities to establish a durect connexion with Excel
through ComConnect.
But maybe this way should be sufficient ?

Hope this help,
clp

Berg, Marrit van den a écrit:

>Hi,
>
>Can anyone tell me how to import external data (EXCEL or ASCCI) into CORMAS?
>
>Thanks,
>
>Marrit
>
>
>

id age formation pleinTemps
1 36 true true
2 53 false true
3 35 true true
4 35 false true
5 75 false false
6 70 false false
7 68 false false
8 40 true true
9 47 false false
10 34 true true
11 31 true true
12 38 false true
13 37 true true
14 66 false true
15 36 true true
16 65 false true
17 55 false true
18 40 false false
19 48 false true
20 27 false true
21 53 true true
22 37 true true
23 41 true true
24 50 true false
25 37 false true
26 63 false false
27 52 false true
28 58 false false
29 36 true true
30 72 false false
31 55 false true
32 49 false false
33 32 true true
34 44 true true
35 54 true true
36 50 true true
37 50 true true
38 43 false false
39 44 false false
40 57 false false
41 55 true true
42 36 false true
43 76 false false
44 31 false false
45 42 false true
46 58 false true

'From VisualWorks® NonCommercial, Release 7 of June 14, 2002 on August 26, 2003 at 9:18:38 am'!


!CormasNS.Kernel.CormasModel methodsFor: 'data'!

readDataForClass: aString format: aFormat
| filename stream line values instances file instance |
        instances := self perform: ('the', aString, 's') asSymbol.
        filename := aString, '.txt'.
"Reading the datafile, line by line, each line is giving the values for the attributes of an instance of the class"
        file := (Cormas dataPath: self class name) construct: filename.
        file exists
                ifFalse: [Dialog warn: 'This particular datafile does not exist !!!!!!!!!!!!!!!!!!!!!!!!'. ^nil]
                ifTrue:
                        [stream := file readStream.
                        "just ignore the first line (attributes's names)..."
                        stream upTo: Character cr.
                        "...then read the data"
                        [stream atEnd] whileFalse:
                                        [line := (stream upTo: Character cr) readStream.
                                        values := line collection tokensBasedOn: Character tab.
                                        instance := instances detect: [:i | i id = values first asNumber] ifNone: [nil].
                                        instance isNil ifTrue: [self halt]
                                                ifFalse:
                                                        [2 to: aFormat size do: [:i | instance perform: (aFormat at: i) key with: ((values at: i) perform: (aFormat at: i) value)]]].
                        stream close]! !

New Message Reply Date view Thread view Subject view Author view
 

Back to home