~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to db_import/readMarianna.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This program loads those brain region names from mariana.csv to brain region name database */
 
2
 
 
3
using namespace std;
 
4
 
 
5
#include "utils/br_util.h"
 
6
#include "tokenlist.h"
 
7
#include "vbutil.h"
 
8
#include <iostream>
 
9
#include <fstream>
 
10
#include <vector>
 
11
#include <string>
 
12
 
 
13
#define MAXLENGTH 5000
 
14
 
 
15
int addRegionName(int, string);
 
16
int addSynRec(int, string, string, string);
 
17
void addRelationship(int, string, string);
 
18
vector<string> parseRelStr(string inputStr, string separator);
 
19
 
 
20
const char * file_in = "marianna.csv";
 
21
string name_space("Marianna");
 
22
string dbHome("./");
 
23
string rDbName("region_name.db");
 
24
string sDbName("synonym.db");
 
25
string rrDbName("region_relation.db");
 
26
string aDbName("admin.db");
 
27
string admin_sField("Next Synonym ID");
 
28
string username("admin");
 
29
 
 
30
/* Main function */
 
31
int main()
 
32
{
 
33
  FILE * ifp = fopen(file_in, "r");
 
34
  if (!ifp) {
 
35
    printf("Fails to read the file: %s\n", file_in);
 
36
    exit(0);
 
37
  }
 
38
 
 
39
  char line[MAXLENGTH];
 
40
  int lineNo = 0;
 
41
  while (fgets(line, MAXLENGTH, ifp)) {
 
42
    lineNo++;
 
43
    // The first 4 lines are skipped
 
44
    if (lineNo < 5)
 
45
      continue;
 
46
 
 
47
    // Remove the trailing "return" key
 
48
    stripchars(line, "\n");
 
49
    string tmpStr(line);
 
50
    vector<string> myRow = parseCSV(tmpStr);
 
51
 
 
52
    // Print out error message if the line is not blank but the number of field is not 3
 
53
    if (myRow.size() != 3) {
 
54
      printf("Line #%d: the number of fields is %d\n", lineNo, myRow.size());
 
55
      for (uint i = 0; i < myRow.size(); i++) 
 
56
        cout << myRow[i] << endl;
 
57
      continue;
 
58
    }
 
59
    // add region name into region db
 
60
    string newName = myRow[0];
 
61
    if (newName.length() == 0) {
 
62
      printf("Line #%d: blank region name, line skipped\n", lineNo);
 
63
      continue;
 
64
    }
 
65
    // Note that region names in this namespace are not converted to lower case
 
66
    int foo = addRegionName(lineNo, newName);
 
67
    if (foo) {
 
68
      printf("Line #%d: fails to add region name into region db\n", lineNo);
 
69
      break;
 
70
    }
 
71
    // synonym is not converted to lowercase either
 
72
    string x_synonym = myRow[1];
 
73
    if (x_synonym == newName)
 
74
      printf("Line #%d: synonym is identical to region name", lineNo);
 
75
    else if (x_synonym.length()) {
 
76
      foo = addSynRec(lineNo, x_synonym, newName, "extra synonym from Marianna");
 
77
      if (foo)
 
78
        printf("Line #%d: fails to add extra synonym into synonym db\n", lineNo);
 
79
    }
 
80
    // add relationship
 
81
    string relStr = myRow[2];
 
82
    addRelationship(lineNo, newName, relStr);
 
83
  }
 
84
 
 
85
  return 0;
 
86
 
87
 
 
88
/* This function adds a new region name into region name database */
 
89
int addRegionName(int lineNo, string newName)
 
90
{
 
91
  int foo = chkRegionName(dbHome, rDbName, newName, name_space);
 
92
  if (foo < 0) {
 
93
    printf("Line #%d: fails to check region name availability in region db\n", lineNo);
 
94
    return foo;
 
95
  }
 
96
  // If region name already exists, skip it instead of terminating the program
 
97
  if (foo == 1) {
 
98
    printf("Line #%d: region \"%s\" exists in region name db\n", lineNo, newName.c_str());
 
99
    return 0;
 
100
  }
 
101
 
 
102
  foo = chkSynonymStr(dbHome, sDbName, newName, name_space);
 
103
  if (foo < 0) {
 
104
    printf("Line #%d: fails to check region name availability in synonym db\n", lineNo);
 
105
    return foo;
 
106
  }
 
107
  // If region name already exists, skip it instead of terminating the program
 
108
  if (foo == 1) {
 
109
    printf("Line #%d: region name \"%s\" exists in synonym name db\n", lineNo, newName.c_str());
 
110
    return 1;
 
111
  }
 
112
 
 
113
  regionRec regionData;
 
114
  regionData.setNameSpace(name_space);
 
115
  regionData.setName(newName);
 
116
  regionData.setSource("Marianna");
 
117
  regionData.setCreator(username);
 
118
  regionData.setAddDate(time(NULL));
 
119
  foo = addRegion_db(dbHome, rDbName, aDbName, regionData);
 
120
  if (foo) {
 
121
    printf("Line #%d: failed to add %s into region name db\n", lineNo, newName.c_str());
 
122
    return 1;
 
123
  }
 
124
  return 0;
 
125
}
 
126
 
 
127
/* Add a synonym record into synonym db */
 
128
int addSynRec(int lineNo, string name, string primary, string comments)
 
129
{
 
130
  int foo = chkRegionName(dbHome, rDbName, name, name_space);
 
131
  if (foo < 0) {
 
132
    printf("Line #%d: fails to check synonym's uniqueness in region db\n", lineNo);
 
133
    return -1;
 
134
  }
 
135
  // If region name already exists, skip it instead of terminating the program
 
136
  if (foo == 1) {
 
137
    printf("Line #%d: synonym \"%s\" exists in region name db\n", lineNo, name.c_str());
 
138
    return 0;
 
139
  }
 
140
 
 
141
  foo = chkSynonymStr(dbHome, sDbName, name, name_space);
 
142
  if (foo < 0) {
 
143
    printf("Line #%d: fails to check availability of %s in synonym db\n", lineNo, name.c_str());
 
144
    return -1;
 
145
  }
 
146
  // If synonym already exists, skip it instead of terminating the program
 
147
  if (foo == 1) {
 
148
    printf("Line #%d: synonym \"%s\" exists in synonym db\n", lineNo, name.c_str()); 
 
149
    return 0;
 
150
  }
 
151
 
 
152
  long sID = getAdmin_db(dbHome, aDbName, admin_sField);
 
153
  if (sID <= 0) {
 
154
    printf("Line #%d: fails to get next available synonym ID from admin db\n", lineNo);
 
155
    return -1;
 
156
  }
 
157
 
 
158
  synonymRec sData;
 
159
  sData.setID(sID);
 
160
  sData.setName(name);
 
161
  sData.setPrimary(primary);
 
162
  sData.setNameSpace(name_space);
 
163
  sData.setAddDate(time(NULL));
 
164
  sData.setCreator(username);
 
165
  sData.setComments(comments);
 
166
 
 
167
  Dbt key(&sID, sizeof(long));
 
168
  void *buff = sData.getBuffer();
 
169
  int size = sData.getBufferSize();
 
170
  Dbt data(buff, size);
 
171
 
 
172
  myDB sDB(dbHome, sDbName);
 
173
  try {
 
174
    foo = sDB.getDb().put(NULL, &key, &data, DB_NOOVERWRITE);
 
175
    if (foo == DB_KEYEXIST) {
 
176
      printf("Line %d: key %ld exists\n", lineNo, sID); 
 
177
      return 1;
 
178
    }
 
179
  }
 
180
  catch(DbException &e) {
 
181
    sDB.getDb().err(e.get_errno(), "Error in addSynRec()");
 
182
    return -1;
 
183
  } 
 
184
  catch(exception &e) {
 
185
    sDB.getDb().errx("Error in addSynRec(): %s", e.what());
 
186
    return -2;
 
187
  }
 
188
 
 
189
  sID++;
 
190
  foo = setAdmin_db(dbHome, aDbName, admin_sField, sID);
 
191
  if (foo < 0) {
 
192
    printf("Line #%d: fails to set next available synonym ID (%ld) from admin db\n", lineNo, sID);
 
193
    return -1;
 
194
  }
 
195
 
 
196
  return 0;
 
197
}
 
198
 
 
199
/* Parse relationship field and add it into relationship db */
 
200
void addRelationship(int lineNo, string regionName, string relStr)
 
201
{
 
202
  // "NONE" means no relationship 
 
203
  if (relStr == "NONE")
 
204
    return;
 
205
  // "equiv" means it is equivalent to the same region in NN2002
 
206
  if (relStr == "equiv") {
 
207
    string r2Name = toLowerCase(regionName);
 
208
    int foo = addRelation_db(dbHome, rDbName, rrDbName, aDbName, username, 
 
209
                   regionName, name_space, r2Name, "NN2002", "equiv"); 
 
210
    if (foo)
 
211
      printf("Error code %d: fails to add relationship in line #%d: %s\n", foo, lineNo, relStr.c_str());
 
212
    return;
 
213
  }
 
214
  // "equiv <string>" means it is equivalent to a region in NN2002 whose name is the string   
 
215
  if (relStr.length() > 6 && relStr.substr(0, 6) == "equiv ") {
 
216
    int strLen = relStr.length();
 
217
    string r2Name = relStr.substr(6, strLen - 6);
 
218
    int foo = addRelation_db(dbHome, rDbName, rrDbName, aDbName, username, 
 
219
                   regionName, name_space, r2Name, "NN2002", "equiv"); 
 
220
    if (foo)
 
221
      printf("Error code %d: fails to add relationship in line #%d: %s\n", foo, lineNo, relStr.c_str());
 
222
    return;
 
223
  }
 
224
  // deal with "includes <>; includes <> ..."
 
225
  vector<string> relList = parseRelStr(relStr, "; ");
 
226
  for (uint i = 0; i < relList.size(); i++) {
 
227
    if (relList[i].length() > 9 && relList[i].substr(0, 9) == "includes ") {
 
228
      int strLen = relList[i].length();
 
229
      string r2Name = relList[i].substr(9, strLen - 9);
 
230
      int foo = addRelation_db(dbHome, rDbName, rrDbName, aDbName, username, 
 
231
                     r2Name, "NN2002", regionName, name_space, "part-of"); 
 
232
      if (foo) 
 
233
        printf("Error code %d: fails to add relationship for %s in line #%d: %s\n", 
 
234
               foo, regionName.c_str(), lineNo, relList[i].c_str());
 
235
    }
 
236
    else
 
237
      printf("Unknown relationship in line #%d: %s\n", lineNo, relList[i].c_str());
 
238
  }
 
239
}
 
240
 
 
241
/* THis function parses an input string and separates it into individual sections.
 
242
 * The first argument is the input string, the second is the separator string. */
 
243
vector<string> parseRelStr(string inputStr, string sepStr)
 
244
{
 
245
  string foo = inputStr;
 
246
  vector <string> sections;
 
247
  int strLen = foo.length();
 
248
  int sepLen = sepStr.length();
 
249
  if (strLen == 0 || sepLen == 0) {
 
250
    sections.push_back(inputStr);
 
251
    return sections;
 
252
  }
 
253
    
 
254
  string bar;
 
255
  while (foo.find(sepStr) != string::npos) {
 
256
    int sepPost = foo.find(sepStr);
 
257
    if (sepPost == 0)
 
258
      bar = "";
 
259
    else
 
260
      bar = foo.substr(0, sepPost);
 
261
    sections.push_back(bar);
 
262
    foo = foo.substr(sepPost + sepLen, foo.length() - sepPost - sepLen);
 
263
  }
 
264
  sections.push_back(foo);
 
265
 
 
266
  return sections;
 
267
}