• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

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

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal