| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
#ifdef KONCLUDE_REDLAND_INTEGRATION |
| 21 |
|
| 22 |
#include "CRedlandStoredTriplesDataModelIterator.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Konclude { |
| 26 |
|
| 27 |
namespace Reasoner { |
| 28 |
|
| 29 |
namespace Triples { |
| 30 |
|
| 31 |
|
| 32 |
CRedlandStoredTriplesDataModelIterator::CRedlandStoredTriplesDataModelIterator(librdf_stream* stream) { |
| 33 |
mStream = stream; |
| 34 |
mDefaultDatatypeString = PREFIX_XML_STRING_DATATYPE; |
| 35 |
} |
| 36 |
|
| 37 |
CRedlandStoredTriplesDataModelIterator::~CRedlandStoredTriplesDataModelIterator() { |
| 38 |
librdf_free_stream(mStream); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
QString CRedlandStoredTriplesDataModelIterator::getNodeString(cint64 pos) { |
| 43 |
librdf_statement* statement = librdf_stream_get_object(mStream); |
| 44 |
|
| 45 |
librdf_node* node = nullptr; |
| 46 |
if (pos == 0) { |
| 47 |
node = librdf_statement_get_subject(statement); |
| 48 |
} else if (pos == 1) { |
| 49 |
node = librdf_statement_get_predicate(statement); |
| 50 |
} else if (pos == 2) { |
| 51 |
node = librdf_statement_get_object(statement); |
| 52 |
} |
| 53 |
|
| 54 |
TRIPLE_NODE_TYPE type = getNodeType(node); |
| 55 |
if (type == CTriplesDataIterator::TRIPLE_URI_NODE_TYPE) { |
| 56 |
librdf_uri* nodeUri = librdf_node_get_uri(node); |
| 57 |
return QString::fromUtf8((const char*)librdf_uri_as_string(nodeUri)); |
| 58 |
|
| 59 |
} else if (type == CTriplesDataIterator::TRIPLE_BLANK_NODE_TYPE) { |
| 60 |
const char* nodeBlankIdentifier = (const char*)librdf_node_get_blank_identifier(node); |
| 61 |
return QString::fromUtf8(nodeBlankIdentifier); |
| 62 |
|
| 63 |
} else { |
| 64 |
size_t litValLen = 0; |
| 65 |
const char* literalValue = (const char*)librdf_node_get_literal_value_as_counted_string(node, &litValLen); |
| 66 |
librdf_uri* datatypeUri = librdf_node_get_literal_value_datatype_uri(node); |
| 67 |
QString datatypeUriString = mDefaultDatatypeString; |
| 68 |
if (datatypeUri) { |
| 69 |
datatypeUriString = QString::fromUtf8((const char*)librdf_uri_as_string(datatypeUri)); |
| 70 |
} |
| 71 |
QString literalValueString = QString::fromUtf8(literalValue, litValLen); |
| 72 |
QString dataLiteralString = QString("\"%1\"^^%2").arg(literalValueString).arg(datatypeUriString); |
| 73 |
return dataLiteralString; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
CTriplesDataIterator::TRIPLE_NODE_TYPE CRedlandStoredTriplesDataModelIterator::getNodeType(cint64 pos) { |
| 80 |
librdf_statement* statement = librdf_stream_get_object(mStream); |
| 81 |
librdf_node* node = nullptr; |
| 82 |
if (pos == 0) { |
| 83 |
node = librdf_statement_get_subject(statement); |
| 84 |
} else if (pos == 1) { |
| 85 |
node = librdf_statement_get_predicate(statement); |
| 86 |
} else if (pos == 2) { |
| 87 |
node = librdf_statement_get_object(statement); |
| 88 |
} |
| 89 |
return getNodeType(node); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
bool CRedlandStoredTriplesDataModelIterator::hasValue() { |
| 95 |
return !librdf_stream_end(mStream); |
| 96 |
} |
| 97 |
|
| 98 |
CTriplesDataIterator* CRedlandStoredTriplesDataModelIterator::moveNext() { |
| 99 |
librdf_stream_next(mStream); |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
CRedlandStoredTriplesDataModelIterator::TRIPLE_NODE_TYPE CRedlandStoredTriplesDataModelIterator::getNodeType(librdf_node* node) { |
| 105 |
if (librdf_node_is_resource(node)) { |
| 106 |
return CTriplesDataIterator::TRIPLE_URI_NODE_TYPE; |
| 107 |
} else if (librdf_node_is_blank(node)) { |
| 108 |
return CTriplesDataIterator::TRIPLE_BLANK_NODE_TYPE; |
| 109 |
} else { |
| 110 |
return CTriplesDataIterator::TRIPLE_LITERAL_NODE_TYPE; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
}; // end namespace Triples |
| 117 |
|
| 118 |
}; // end namespace Reasoner |
| 119 |
|
| 120 |
}; // end namespace Konclude |
| 121 |
|
| 122 |
#endif // !KONCLUDE_REDLAND_INTEGRATION |