| … | … | @@ -7,7 +7,7 @@ ADD_LIBRARY (sensory SHARED |
| 7 | 7 | ReadStream.cc |
| 8 | 8 | SensoryNode.cc |
| 9 | 9 | StreamNode.cc |
| 10 | TextStream.cc | |
| 10 | StringStream.cc | |
| 11 | 11 | TextStreamNode.cc |
| 12 | 12 | ) |
| 13 | 13 | |
| … | … | @@ -28,7 +28,7 @@ INSTALL (FILES |
| 28 | 28 | ReadStream.h |
| 29 | 29 | SensoryNode.h |
| 30 | 30 | StreamNode.h |
| 31 | TextStream.h | |
| 31 | StringStream.h | |
| 32 | 32 | TextStreamNode.h |
| 33 | 33 | DESTINATION "include/opencog/atoms/sensory" |
| 34 | 34 | ) |
| … | … | @@ -37,7 +37,7 @@ namespace opencog |
| 37 | 37 | class SensoryNode : public Node |
| 38 | 38 | { |
| 39 | 39 | friend class ReadStream; |
| 40 | friend class TextStream; | |
| 40 | friend class StringStream; | |
| 41 | 41 | |
| 42 | 42 | protected: |
| 43 | 43 | SensoryNode(Type, const std::string&&); |
| … | … | @@ -0,0 +1,112 @@ |
| 1 | /* | |
| 2 | * opencog/atoms/sensory/StringStream.cc | |
| 3 | * | |
| 4 | * Copyright (C) 2025 BrainyBlaze Dynamics, Inc. | |
| 5 | * All Rights Reserved | |
| 6 | * | |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU Affero General Public License v3 as | |
| 9 | * published by the Free Software Foundation and including the exceptions | |
| 10 | * at http://opencog.org/wiki/Licenses | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU Affero General Public License | |
| 18 | * along with this program; if not, write to: | |
| 19 | * Free Software Foundation, Inc., | |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 21 | */ | |
| 22 | ||
| 23 | #include <opencog/util/exceptions.h> | |
| 24 | #include <opencog/util/oc_assert.h> | |
| 25 | #include <opencog/atoms/base/Node.h> | |
| 26 | #include <opencog/atoms/value/StringValue.h> | |
| 27 | #include <opencog/atoms/value/ValueFactory.h> | |
| 28 | ||
| 29 | #include <opencog/sensory/types/atom_types.h> | |
| 30 | #include "StringStream.h" | |
| 31 | ||
| 32 | using namespace opencog; | |
| 33 | ||
| 34 | StringStream::StringStream(const Handle& senso) | |
| 35 | : StringValue(STRING_STREAM, std::vector<std::string>()) | |
| 36 | { | |
| 37 | if (not senso->is_type(SENSORY_NODE)) | |
| 38 | throw RuntimeException(TRACE_INFO, | |
| 39 | "Expecting SensoryNode, got %s\n", senso->to_string().c_str()); | |
| 40 | ||
| 41 | _snp = SensoryNodeCast(senso); | |
| 42 | } | |
| 43 | ||
| 44 | StringStream::~StringStream() | |
| 45 | { | |
| 46 | } | |
| 47 | ||
| 48 | // ============================================================== | |
| 49 | ||
| 50 | // This will read one item at a time from the object, and return | |
| 51 | // that item as a string. | |
| 52 | // So, still, a line-oriented, buffered interface. For now. | |
| 53 | // | |
| 54 | // This accesses the SensoryNode private methods directly; the | |
| 55 | // alternative would be to call the public getValue() method, but | |
| 56 | // that does nothing except add overhead. | |
| 57 | void StringStream::update() const | |
| 58 | { | |
| 59 | if (not _snp->connected()) | |
| 60 | { | |
| 61 | _value.clear(); | |
| 62 | return; | |
| 63 | } | |
| 64 | ||
| 65 | ValuePtr vp = _snp->read(); | |
| 66 | ||
| 67 | // nullpointr and VoidValue denote explicit EOF | |
| 68 | if (nullptr == vp or VOID_VALUE == vp->get_type()) | |
| 69 | { | |
| 70 | _value.clear(); | |
| 71 | return; | |
| 72 | } | |
| 73 | ||
| 74 | // Convert the value to a string | |
| 75 | std::string str; | |
| 76 | if (vp->is_type(STRING_VALUE)) | |
| 77 | { | |
| 78 | std::vector<std::string> next = StringValueCast(vp)->value(); | |
| 79 | _value.swap(next); | |
| 80 | return; | |
| 81 | } | |
| 82 | ||
| 83 | if (vp->is_type(NODE)) | |
| 84 | { | |
| 85 | _value.resize(1); | |
| 86 | _value[0] = HandleCast(vp)->get_name(); | |
| 87 | return; | |
| 88 | } | |
| 89 | ||
| 90 | // Don't know what else to do, if not handled above. | |
| 91 | _value.clear(); | |
| 92 | } | |
| 93 | ||
| 94 | // ============================================================== | |
| 95 | // XXX TODO. Someday, we need a working to_short_string() that | |
| 96 | // can be used by RocksStorage to ... store this thing. I guess. | |
| 97 | ||
| 98 | std::string StringStream::to_string(const std::string& indent) const | |
| 99 | { | |
| 100 | std::string rv = indent + "(" + nameserver().getTypeName(_type); | |
| 101 | rv += "\n" + _snp->to_short_string(indent + " ") + ")\n"; | |
| 102 | rv += indent + "; Current sample:\n"; | |
| 103 | rv += StringValue::to_string(indent + "; ", STRING_VALUE); | |
| 104 | return rv; | |
| 105 | } | |
| 106 | ||
| 107 | // ============================================================== | |
| 108 | ||
| 109 | // Adds factory when library is loaded. | |
| 110 | DEFINE_VALUE_FACTORY(STRING_STREAM, createStringStream, Handle) | |
| 111 | ||
| 112 | // ==================================================================== |
| … | … | @@ -0,0 +1,68 @@ |
| 1 | /* | |
| 2 | * opencog/atoms/sensory/StringStream.h | |
| 3 | * | |
| 4 | * Copyright (C) 2024 Linas Vepstas | |
| 5 | * Copyright (C) 2025 BrainyBlaze Dynamics Inc. | |
| 6 | * All Rights Reserved | |
| 7 | * | |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU Affero General Public License v3 as | |
| 10 | * published by the Free Software Foundation and including the exceptions | |
| 11 | * at http://opencog.org/wiki/Licenses | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Affero General Public License | |
| 19 | * along with this program; if not, write to: | |
| 20 | * Free Software Foundation, Inc., | |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 22 | */ | |
| 23 | ||
| 24 | #ifndef _OPENCOG_STRING_STREAM_H | |
| 25 | #define _OPENCOG_STRING_STREAM_H | |
| 26 | ||
| 27 | #include <stdio.h> | |
| 28 | #include <opencog/atoms/sensory/SensoryNode.h> | |
| 29 | #include <opencog/atoms/value/StringValue.h> | |
| 30 | ||
| 31 | namespace opencog | |
| 32 | { | |
| 33 | ||
| 34 | /** \addtogroup grp_atomspace | |
| 35 | * @{ | |
| 36 | */ | |
| 37 | ||
| 38 | /** | |
| 39 | * StringStream wraps ObjectNodes that provide one-at-a-time *-read-* | |
| 40 | * methods, and provides a stream of string values obtained by reading, | |
| 41 | * on demand. | |
| 42 | * | |
| 43 | * This is similar to ReadStream but inherits from StringValue instead | |
| 44 | * of LinkValue, making it more suitable for text-oriented streaming. | |
| 45 | * | |
| 46 | * This is experimental. | |
| 47 | */ | |
| 48 | class StringStream | |
| 49 | : public StringValue | |
| 50 | { | |
| 51 | protected: | |
| 52 | SensoryNodePtr _snp; | |
| 53 | virtual void update() const; | |
| 54 | ||
| 55 | public: | |
| 56 | StringStream(const Handle&); | |
| 57 | virtual ~StringStream(); | |
| 58 | ||
| 59 | virtual std::string to_string(const std::string& indent = "") const; | |
| 60 | }; | |
| 61 | ||
| 62 | VALUE_PTR_DECL(StringStream) | |
| 63 | CREATE_VALUE_DECL(StringStream) | |
| 64 | ||
| 65 | /** @}*/ | |
| 66 | } // namespace opencog | |
| 67 | ||
| 68 | #endif // _OPENCOG_STRING_STREAM_H |
| … | … | @@ -1,112 +0,0 @@ |
| 1 | /* | |
| 2 | * opencog/atoms/sensory/TextStream.cc | |
| 3 | * | |
| 4 | * Copyright (C) 2025 BrainyBlaze Dynamics, Inc. | |
| 5 | * All Rights Reserved | |
| 6 | * | |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU Affero General Public License v3 as | |
| 9 | * published by the Free Software Foundation and including the exceptions | |
| 10 | * at http://opencog.org/wiki/Licenses | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU Affero General Public License | |
| 18 | * along with this program; if not, write to: | |
| 19 | * Free Software Foundation, Inc., | |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 21 | */ | |
| 22 | ||
| 23 | #include <opencog/util/exceptions.h> | |
| 24 | #include <opencog/util/oc_assert.h> | |
| 25 | #include <opencog/atoms/base/Node.h> | |
| 26 | #include <opencog/atoms/value/StringValue.h> | |
| 27 | #include <opencog/atoms/value/ValueFactory.h> | |
| 28 | ||
| 29 | #include <opencog/sensory/types/atom_types.h> | |
| 30 | #include "TextStream.h" | |
| 31 | ||
| 32 | using namespace opencog; | |
| 33 | ||
| 34 | TextStream::TextStream(const Handle& senso) | |
| 35 | : StringValue(TEXT_STREAM, std::vector<std::string>()) | |
| 36 | { | |
| 37 | if (not senso->is_type(SENSORY_NODE)) | |
| 38 | throw RuntimeException(TRACE_INFO, | |
| 39 | "Expecting SensoryNode, got %s\n", senso->to_string().c_str()); | |
| 40 | ||
| 41 | _snp = SensoryNodeCast(senso); | |
| 42 | } | |
| 43 | ||
| 44 | TextStream::~TextStream() | |
| 45 | { | |
| 46 | } | |
| 47 | ||
| 48 | // ============================================================== | |
| 49 | ||
| 50 | // This will read one item at a time from the object, and return | |
| 51 | // that item as a string. | |
| 52 | // So, still, a line-oriented, buffered interface. For now. | |
| 53 | // | |
| 54 | // This accesses the SensoryNode private methods directly; the | |
| 55 | // alternative would be to call the public getValue() method, but | |
| 56 | // that does nothing except add overhead. | |
| 57 | void TextStream::update() const | |
| 58 | { | |
| 59 | if (not _snp->connected()) | |
| 60 | { | |
| 61 | _value.clear(); | |
| 62 | return; | |
| 63 | } | |
| 64 | ||
| 65 | ValuePtr vp = _snp->read(); | |
| 66 | ||
| 67 | // nullpointr and VoidValue denote explicit EOF | |
| 68 | if (nullptr == vp or VOID_VALUE == vp->get_type()) | |
| 69 | { | |
| 70 | _value.clear(); | |
| 71 | return; | |
| 72 | } | |
| 73 | ||
| 74 | // Convert the value to a string | |
| 75 | std::string str; | |
| 76 | if (vp->is_type(STRING_VALUE)) | |
| 77 | { | |
| 78 | std::vector<std::string> next = StringValueCast(vp)->value(); | |
| 79 | _value.swap(next); | |
| 80 | return; | |
| 81 | } | |
| 82 | ||
| 83 | if (vp->is_type(NODE)) | |
| 84 | { | |
| 85 | _value.resize(1); | |
| 86 | _value[0] = HandleCast(vp)->get_name(); | |
| 87 | return; | |
| 88 | } | |
| 89 | ||
| 90 | // Don't know what else to do, if not handled above. | |
| 91 | _value.clear(); | |
| 92 | } | |
| 93 | ||
| 94 | // ============================================================== | |
| 95 | // XXX TODO. Someday, we need a working to_short_string() that | |
| 96 | // can be used by RocksStorage to ... store this thing. I guess. | |
| 97 | ||
| 98 | std::string TextStream::to_string(const std::string& indent) const | |
| 99 | { | |
| 100 | std::string rv = indent + "(" + nameserver().getTypeName(_type); | |
| 101 | rv += "\n" + _snp->to_short_string(indent + " ") + ")\n"; | |
| 102 | rv += indent + "; Current sample:\n"; | |
| 103 | rv += StringValue::to_string(indent + "; ", STRING_VALUE); | |
| 104 | return rv; | |
| 105 | } | |
| 106 | ||
| 107 | // ============================================================== | |
| 108 | ||
| 109 | // Adds factory when library is loaded. | |
| 110 | DEFINE_VALUE_FACTORY(TEXT_STREAM, createTextStream, Handle) | |
| 111 | ||
| 112 | // ==================================================================== |
| … | … | @@ -1,68 +0,0 @@ |
| 1 | /* | |
| 2 | * opencog/atoms/sensory/TextStream.h | |
| 3 | * | |
| 4 | * Copyright (C) 2024 Linas Vepstas | |
| 5 | * Copyright (C) 2025 BrainyBlaze Dynamics Inc. | |
| 6 | * All Rights Reserved | |
| 7 | * | |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU Affero General Public License v3 as | |
| 10 | * published by the Free Software Foundation and including the exceptions | |
| 11 | * at http://opencog.org/wiki/Licenses | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Affero General Public License | |
| 19 | * along with this program; if not, write to: | |
| 20 | * Free Software Foundation, Inc., | |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 22 | */ | |
| 23 | ||
| 24 | #ifndef _OPENCOG_TEXT_STREAM_H | |
| 25 | #define _OPENCOG_TEXT_STREAM_H | |
| 26 | ||
| 27 | #include <stdio.h> | |
| 28 | #include <opencog/atoms/sensory/SensoryNode.h> | |
| 29 | #include <opencog/atoms/value/StringValue.h> | |
| 30 | ||
| 31 | namespace opencog | |
| 32 | { | |
| 33 | ||
| 34 | /** \addtogroup grp_atomspace | |
| 35 | * @{ | |
| 36 | */ | |
| 37 | ||
| 38 | /** | |
| 39 | * TextStream wraps ObjectNodes that provide one-at-a-time *-read-* | |
| 40 | * methods, and provides a stream of string values obtained by reading, | |
| 41 | * on demand. | |
| 42 | * | |
| 43 | * This is similar to ReadStream but inherits from StringValue instead | |
| 44 | * of LinkValue, making it more suitable for text-oriented streaming. | |
| 45 | * | |
| 46 | * This is experimental. | |
| 47 | */ | |
| 48 | class TextStream | |
| 49 | : public StringValue | |
| 50 | { | |
| 51 | protected: | |
| 52 | SensoryNodePtr _snp; | |
| 53 | virtual void update() const; | |
| 54 | ||
| 55 | public: | |
| 56 | TextStream(const Handle&); | |
| 57 | virtual ~TextStream(); | |
| 58 | ||
| 59 | virtual std::string to_string(const std::string& indent = "") const; | |
| 60 | }; | |
| 61 | ||
| 62 | VALUE_PTR_DECL(TextStream) | |
| 63 | CREATE_VALUE_DECL(TextStream) | |
| 64 | ||
| 65 | /** @}*/ | |
| 66 | } // namespace opencog | |
| 67 | ||
| 68 | #endif // _OPENCOG_TEXT_STREAM_H |
| … | … | @@ -31,7 +31,7 @@ |
| 31 | 31 | #include <opencog/atoms/value/VoidValue.h> |
| 32 | 32 | |
| 33 | 33 | #include <opencog/sensory/types/atom_types.h> |
| 34 | #include "TextStream.h" | |
| 34 | #include "StringStream.h" | |
| 35 | 35 | #include "TextStreamNode.h" |
| 36 | 36 | |
| 37 | 37 | using namespace opencog; |
| … | … | @@ -112,10 +112,9 @@ void TextStreamNode::do_write(const ValuePtr& content) |
| 112 | 112 | |
| 113 | 113 | // ============================================================== |
| 114 | 114 | |
| 115 | // Override stream() to return TextStream instead of ReadStream | |
| 116 | 115 | ValuePtr TextStreamNode::stream(void) const |
| 117 | 116 | { |
| 118 | return createTextStream(get_handle()); | |
| 117 | return createStringStream(get_handle()); | |
| 119 | 118 | } |
| 120 | 119 | |
| 121 | 120 | // ============================================================== |
| … | … | @@ -61,7 +61,6 @@ protected: |
| 61 | 61 | // Derived classes need to implement a handler. |
| 62 | 62 | virtual void do_write(const std::string&) = 0; |
| 63 | 63 | |
| 64 | // Override to return TextStream instead of ReadStream | |
| 65 | 64 | virtual ValuePtr stream(void) const; |
| 66 | 65 | |
| 67 | 66 | public: |
| … | … | @@ -9,7 +9,7 @@ |
| 9 | 9 | READ_STREAM <- STREAM_VALUE,HANDLE_ARG |
| 10 | 10 | |
| 11 | 11 | // The StringValue provides a more suitable API for text. |
| 12 | TEXT_STREAM <- STRING_VALUE,HANDLE_ARG | |
| 12 | STRING_STREAM <- STRING_VALUE,HANDLE_ARG | |
| 13 | 13 | |
| 14 | 14 | // --------------------------------------------- |
| 15 | 15 | // Values above, Atoms below. |