~ubuntu-branches/ubuntu/precise/rocs/precise-proposed

« back to all changes in this revision

Viewing changes to src/Plugins/FilePlugins/GMLParser/GMLGraphParsingHelper.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-17 15:07:00 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20111217150700-5ogxmpqlluwn29a3
Tags: 4:4.7.90-0ubuntu1
* New upstream beta release
* Add build-dep on libboost-graph1.46-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KGraphViewer.
 
2
   Copyright (C) 2006-2007 Gael de Chalendar <kleag@free.fr>
 
3
 
 
4
   KGraphViewer is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU General Public
 
6
   License as published by the Free Software Foundation, version 2.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; if not, write to the Free Software
 
15
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
16
   02110-1301, USA
 
17
*/
 
18
 
 
19
 
 
20
#include "GMLGraphParsingHelper.h"
 
21
// #include "dotgraph.h"
 
22
#include "GMLGrammar.h"
 
23
// #include "dotdefaults.h"
 
24
//#include "graphsubgraph.h"
 
25
// #include "graphnode.h"
 
26
// #include "graphedge.h"
 
27
 
 
28
#include <boost/throw_exception.hpp>
 
29
#include <boost/spirit/include/classic_core.hpp>
 
30
#include <boost/spirit/include/classic_distinct.hpp>
 
31
#include <boost/spirit/include/classic_loops.hpp>
 
32
#include <boost/spirit/include/classic_confix.hpp>
 
33
 
 
34
 
 
35
#include <iostream>
 
36
 
 
37
#include <kdebug.h>
 
38
 
 
39
#include <QFile>
 
40
#include<QUuid>
 
41
#include "DynamicPropertiesList.h"
 
42
#include "Document.h"
 
43
#include "Pointer.h"
 
44
 
 
45
// using namespace std;
 
46
using namespace Rocs;
 
47
using namespace GMLPlugin;
 
48
 
 
49
extern GMLGraphParsingHelper* phelper;
 
50
 
 
51
#define KGV_MAX_ITEMS_TO_LOAD std::numeric_limits<int>::max()
 
52
 
 
53
GMLGraphParsingHelper::GMLGraphParsingHelper():
 
54
         edgeSource(),
 
55
         edgeTarget(),
 
56
         _actualState(begin)
 
57
{
 
58
    actualGraph.reset();
 
59
    actualNode.reset();
 
60
    actualEdge.reset();
 
61
}
 
62
 
 
63
void GMLGraphParsingHelper::startList(const QString& key){
 
64
  kDebug () << "starting a list with key:" <<key;
 
65
  if (_actualState == begin && key.compare("graph", Qt::CaseInsensitive) == 0){
 
66
    createGraph();
 
67
    return;
 
68
  }else if (_actualState == graph ){
 
69
     if(key.compare("node", Qt::CaseInsensitive)==0){
 
70
        createNode();
 
71
        return;
 
72
    }else if(key.compare("edge", Qt::CaseInsensitive)==0){
 
73
        createEdge();
 
74
        return;
 
75
    }
 
76
  }
 
77
  _properties.append(key);
 
78
}
 
79
 
 
80
 
 
81
void GMLGraphParsingHelper::endList(){
 
82
  if (!_properties.isEmpty()){
 
83
    _properties.removeLast();
 
84
    return;
 
85
  }
 
86
  switch (_actualState){
 
87
    case begin: kDebug () << "Ending a list without begin a item??"; break;
 
88
    case node: actualNode.reset();
 
89
               _actualState = graph;
 
90
               break;
 
91
    case edge: actualEdge.reset();
 
92
               _actualState = graph;
 
93
               break;
 
94
    case graph:
 
95
              actualGraph.reset();
 
96
              _actualState = begin;
 
97
              break;
 
98
  }
 
99
 
 
100
 
 
101
}
 
102
 
 
103
const QString GMLGraphParsingHelper::processKey(const QString& key){
 
104
    QString ret = key;
 
105
    if (key.compare("id", Qt::CaseInsensitive) == 0){
 
106
      ret = "name";
 
107
    }
 
108
 
 
109
    return ret;
 
110
}
 
111
 
 
112
 
 
113
void GMLGraphParsingHelper::setAtribute(const QString& key, const QString& value)
 
114
{
 
115
  kDebug() << "Setting attibute " << key;
 
116
  switch(_actualState){
 
117
    case begin: break;
 
118
    case graph:
 
119
        if (!_properties.isEmpty()){
 
120
           QString joined = _properties.join(".");
 
121
           joined.append('.').append(key);
 
122
           actualGraph->setProperty(joined.toAscii(),value);
 
123
        }else{
 
124
          kDebug() << "seting property to graph" << key << value;
 
125
//           if (!actualGraph->setProperty(processKey(key).toAscii(),value)){
 
126
            actualGraph->addDynamicProperty(processKey(key),value);  //is a dinamic property
 
127
//           }
 
128
        }
 
129
        break;
 
130
    case edge:
 
131
        if (!_properties.isEmpty()){ //is a list of properties of edge
 
132
           QString joined = _properties.join(".");
 
133
           joined.append('.').append(key);
 
134
           if (actualEdge){
 
135
              actualEdge->setProperty(joined.toAscii(),value);
 
136
           }else{
 
137
            _edgeProperties.insert(joined, value);
 
138
           }
 
139
        }else if (key.compare("source", Qt::CaseInsensitive) == 0){   // search for source....
 
140
              edgeSource = value;
 
141
              createEdge();
 
142
        }else if (key.compare("target", Qt::CaseInsensitive) == 0){   // .... and target
 
143
              edgeTarget = value;
 
144
              createEdge();
 
145
        }else if (actualEdge){       //if edge was created.
 
146
//               if(!actualEdge->setProperty(processKey(key).toAscii(),value)){
 
147
                kDebug() << "inserting edge key: " << key;
 
148
                actualEdge->addDynamicProperty(processKey(key),value);
 
149
// //               }
 
150
        }else{
 
151
            kDebug() << "Saving edge key: " << key;
 
152
            _edgeProperties.insert(processKey(key), value); //store to be inserted later
 
153
        }
 
154
        break;
 
155
    case node:
 
156
        if (!_properties.isEmpty()){
 
157
          QString joined = _properties.join(".");
 
158
           joined.append('.').append(key);
 
159
           actualNode->setProperty(joined.toAscii(),value);
 
160
        }else{
 
161
          kDebug() << "seting property to node" << key << value;
 
162
//           if(!actualNode->setProperty(processKey(key).toAscii(),value)){
 
163
            actualNode->addDynamicProperty(processKey(key), value);
 
164
//           }
 
165
        }
 
166
        break;
 
167
  }
 
168
}
 
169
 
 
170
 
 
171
void GMLGraphParsingHelper::createGraph(){
 
172
    if (_actualState == begin){
 
173
      actualGraph = gd->addDataStructure();
 
174
      _actualState = graph;
 
175
    }
 
176
}
 
177
 
 
178
void GMLGraphParsingHelper::createNode(){
 
179
  if (_actualState == graph){
 
180
    kDebug () << "Creating a node";
 
181
    _actualState = node;
 
182
    actualNode = actualGraph->addData("NewNode");
 
183
  }
 
184
}
 
185
 
 
186
 
 
187
 
 
188
void GMLGraphParsingHelper::createEdge(){
 
189
    if (!edgeSource.isEmpty() && !edgeTarget.isEmpty()){
 
190
        kDebug() << "Creating a edge";
 
191
        _actualState = edge;
 
192
        actualEdge = actualGraph->addPointer(edgeSource, edgeTarget);
 
193
        edgeSource.clear();;
 
194
        edgeTarget.clear();
 
195
        while( ! _edgeProperties.isEmpty()){
 
196
            QString property = _edgeProperties.keys().at(0);
 
197
            actualEdge->addDynamicProperty(property, _edgeProperties.value(property));
 
198
            _edgeProperties.remove(property);
 
199
        }
 
200
    }else if (_actualState == graph){
 
201
        kDebug () << "changing state Edge";
 
202
        _actualState = edge;
 
203
        actualEdge.reset();
 
204
    }
 
205
}
 
206