OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-sensory commits / ba1b803

Implement a new TextStream specialized for strings.
ba1b8037efa8604479b22e456f82a6b24a1780e7
by Linas Vepstas <linasvepstas@gmail.com> · 2025-10-26T19:53:14.000Z

Changed files (4) +183 -0

opencog/atoms/sensory/CMakeLists.txt +2 -0
……@@ -7,6 +7,7 @@ ADD_LIBRARY (sensory SHARED
77 ReadStream.cc
88 SensoryNode.cc
99 StreamNode.cc
10 TextStream.cc
1011 TextStreamNode.cc
1112)
1213
……@@ -27,6 +28,7 @@ INSTALL (FILES
2728 ReadStream.h
2829 SensoryNode.h
2930 StreamNode.h
31 TextStream.h
3032 TextStreamNode.h
3133 DESTINATION "include/opencog/atoms/sensory"
3234)
opencog/atoms/sensory/SensoryNode.h +1 -0
……@@ -37,6 +37,7 @@ namespace opencog
3737class SensoryNode : public Node
3838{
3939 friend class ReadStream;
40 friend class TextStream;
4041
4142protected:
4243 SensoryNode(Type, const std::string&&);
opencog/atoms/sensory/TextStream.cc +112 -0
……@@ -0,0 +1,112 @@
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
32using namespace opencog;
33
34TextStream::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
44TextStream::~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.
57void 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
98std::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.
110DEFINE_VALUE_FACTORY(TEXT_STREAM, createTextStream, Handle)
111
112// ====================================================================
opencog/atoms/sensory/TextStream.h +68 -0
……@@ -0,0 +1,68 @@
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
31namespace 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 */
48class TextStream
49 : public StringValue
50{
51protected:
52 SensoryNodePtr _snp;
53 virtual void update() const;
54
55public:
56 TextStream(const Handle&);
57 virtual ~TextStream();
58
59 virtual std::string to_string(const std::string& indent = "") const;
60};
61
62VALUE_PTR_DECL(TextStream)
63CREATE_VALUE_DECL(TextStream)
64
65/** @}*/
66} // namespace opencog
67
68#endif // _OPENCOG_TEXT_STREAM_H