21 #ifndef CSVGRAPHIMPORT_H
22 #define CSVGRAPHIMPORT_H
24 #include <tulip/tuliphash.h>
26 #include <tulip/CSVContentHandler.h>
27 #include <tulip/Graph.h>
28 #include <tulip/tulipconf.h>
30 #include <QMessageBox>
33 class PropertyInterface;
35 #define DEF_VALUE_SEPARATOR ','
43 class TLP_QT_SCOPE CSVColumn {
45 CSVColumn(
const std::string &columnName =
"",
const std::string &columnType =
"")
46 : _used(true), _name(columnName), _type(columnType), _valueSeparator(DEF_VALUE_SEPARATOR) {}
51 const std::string &name()
const {
65 bool isDefault()
const {
66 return (_name == _def_name) && (_type == _def_type) &&
67 (_valueSeparator == DEF_VALUE_SEPARATOR) && _exceptions.empty();
73 const std::string &dataType()
const {
77 bool needMultiValues()
const {
78 return _valueSeparator != 0;
81 char getMultiValueSeparator()
const {
82 return _valueSeparator;
87 enum Action { ASSIGN_NO_VALUE = 0, SKIP_ROW = 1, ASSIGN_VALUE = 2 };
92 Exception(
const std::string &v, Action a) : value(v), action(a) {}
95 void addException(
const std::string &value, Action action) {
96 _exceptions.emplace_back(value, action);
99 void clearExceptions() {
104 Action getActionForToken(
const std::string &token) {
105 for (
const Exception &exception : _exceptions) {
106 if (exception.value == token)
107 return exception.action;
109 return Action::ASSIGN_VALUE;
114 std::string _name, _def_name;
115 std::string _type, _def_type;
116 char _valueSeparator;
117 std::vector<Exception> _exceptions;
125 class TLP_QT_SCOPE CSVImportParameters {
127 CSVImportParameters(
unsigned int fromLine = 0,
unsigned int toLine = UINT_MAX,
128 const std::vector<CSVColumn *> &columns = std::vector<CSVColumn *>());
129 virtual ~CSVImportParameters();
134 unsigned int columnNumber()
const;
139 bool importColumn(
unsigned int column)
const;
143 std::string getColumnName(
unsigned int column)
const;
147 std::string getColumnDataType(
unsigned int column)
const;
152 char getColumnMultiValueSeparator(
unsigned int column)
const;
157 CSVColumn::Action getColumnActionForToken(
unsigned int column,
const std::string &token)
const;
162 unsigned int getFirstLineIndex()
const;
166 unsigned int getLastLineIndex()
const;
171 bool importRow(
unsigned int row)
const;
174 unsigned int fromLine;
176 std::vector<CSVColumn *> columns;
192 class TLP_QT_SCOPE CSVToGraphDataMapping {
194 virtual ~CSVToGraphDataMapping() {}
195 virtual std::pair<tlp::ElementType, std::vector<unsigned int>>
196 getElementsForRow(
const std::vector<std::vector<std::string>> &tokens) = 0;
197 virtual void init(
unsigned int rowNumber) = 0;
207 class TLP_QT_SCOPE AbstractCSVToGraphDataMapping :
public CSVToGraphDataMapping {
210 const std::vector<unsigned int> &columnIds,
211 const std::vector<std::string> &propertyNames);
212 ~AbstractCSVToGraphDataMapping()
override {}
214 void init(
unsigned int rowNumber)
override;
215 std::pair<tlp::ElementType, std::vector<unsigned int>>
216 getElementsForRow(
const std::vector<std::vector<std::string>> &tokens)
override;
223 virtual unsigned int buildIndexForRow(
unsigned int row,
const std::vector<std::string> &keys) = 0;
226 tlp_hash_map<std::string, unsigned int> valueToId;
229 std::vector<unsigned int> columnIds;
230 std::vector<tlp::PropertyInterface *> keyProperties;
235 class TLP_QT_SCOPE CSVToNewNodeIdMapping :
public CSVToGraphDataMapping {
238 void init(
unsigned int rowNumber)
override;
239 std::pair<tlp::ElementType, std::vector<unsigned int>>
240 getElementsForRow(
const std::vector<std::vector<std::string>> &tokens)
override;
252 class TLP_QT_SCOPE CSVToGraphNodeIdMapping :
public AbstractCSVToGraphDataMapping {
263 CSVToGraphNodeIdMapping(
tlp::Graph *graph,
const std::vector<unsigned int> &columnIds,
264 const std::vector<std::string> &propertyNames,
bool createNode =
false);
265 void init(
unsigned int rowNumber)
override;
268 unsigned int buildIndexForRow(
unsigned int row,
const std::vector<std::string> &keys)
override;
271 bool createMissingNodes;
279 class TLP_QT_SCOPE CSVToGraphEdgeIdMapping :
public AbstractCSVToGraphDataMapping {
288 CSVToGraphEdgeIdMapping(
tlp::Graph *graph,
const std::vector<unsigned int> &columnIds,
289 const std::vector<std::string> &propertyNames);
292 unsigned int buildIndexForRow(
unsigned int row,
const std::vector<std::string> &keys)
override;
302 class TLP_QT_SCOPE CSVToGraphEdgeSrcTgtMapping :
public CSVToGraphDataMapping {
315 CSVToGraphEdgeSrcTgtMapping(
tlp::Graph *graph,
const std::vector<unsigned int> &srcColumnIds,
316 const std::vector<unsigned int> &tgtColumnIds,
317 const std::vector<std::string> &srcPropNames,
318 const std::vector<std::string> &tgtPropNames,
319 bool createMissinElements =
false);
320 std::pair<tlp::ElementType, unsigned int> getElementForRow(
unsigned int row);
321 void init(
unsigned int lineNumbers)
override;
322 std::pair<tlp::ElementType, std::vector<unsigned int>>
323 getElementsForRow(
const std::vector<std::vector<std::string>> &tokens)
override;
327 tlp_hash_map<std::string, unsigned int> srcValueToId;
328 tlp_hash_map<std::string, unsigned int> tgtValueToId;
329 std::vector<unsigned int> srcColumnIds;
330 std::vector<unsigned int> tgtColumnIds;
331 std::vector<tlp::PropertyInterface *> srcProperties;
332 std::vector<tlp::PropertyInterface *> tgtProperties;
333 bool sameSrcTgtProperties;
334 bool buildMissingElements;
342 class TLP_QT_SCOPE CSVImportColumnToGraphPropertyMapping {
344 virtual ~CSVImportColumnToGraphPropertyMapping() {}
353 const std::string &token) = 0;
363 class TLP_QT_SCOPE CSVImportColumnToGraphPropertyMappingProxy
364 :
public CSVImportColumnToGraphPropertyMapping {
366 CSVImportColumnToGraphPropertyMappingProxy(
tlp::Graph *graph,
367 const CSVImportParameters &importParameters,
368 QWidget *parent =
nullptr);
369 ~CSVImportColumnToGraphPropertyMappingProxy()
override {}
371 const std::string &token)
override;
375 CSVImportParameters importParameters;
376 tlp_hash_map<unsigned int, tlp::PropertyInterface *> propertiesBuffer;
377 QMessageBox::StandardButton overwritePropertiesButton;
379 PropertyInterface *generateApproximateProperty(
const std::string &name,
const std::string &type);
387 class TLP_QT_SCOPE CSVGraphImport :
public tlp::CSVContentHandler {
389 CSVGraphImport(CSVToGraphDataMapping *mapping,
390 CSVImportColumnToGraphPropertyMapping *propertiesManager,
391 const CSVImportParameters &importParameters);
392 ~CSVGraphImport()
override;
393 bool begin()
override;
394 bool line(
unsigned int row,
const std::vector<CSVToken> &lineTokens)
override;
395 bool end(
unsigned int rowNumber,
unsigned int columnNumber)
override;
398 CSVToGraphDataMapping *mapping;
399 CSVImportColumnToGraphPropertyMapping *propertiesManager;
400 CSVImportParameters importParameters;
PropertyInterface describes the interface of a graph property.