OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-sensory commits / 45c6398

Rename TextStream to StringStream
45c6398517a546a8bd1754d3c26ab83894948b55
by Linas Vepstas <linasvepstas@gmail.com> · 2025-10-26T20:17:48.000Z

Changed files (9) +186 -188

opencog/atoms/sensory/CMakeLists.txt +2 -2
……@@ -7,7 +7,7 @@ ADD_LIBRARY (sensory SHARED
77 ReadStream.cc
88 SensoryNode.cc
99 StreamNode.cc
10 TextStream.cc
10 StringStream.cc
1111 TextStreamNode.cc
1212)
1313
……@@ -28,7 +28,7 @@ INSTALL (FILES
2828 ReadStream.h
2929 SensoryNode.h
3030 StreamNode.h
31 TextStream.h
31 StringStream.h
3232 TextStreamNode.h
3333 DESTINATION "include/opencog/atoms/sensory"
3434)
opencog/atoms/sensory/SensoryNode.h +1 -1
……@@ -37,7 +37,7 @@ namespace opencog
3737class SensoryNode : public Node
3838{
3939 friend class ReadStream;
40 friend class TextStream;
40 friend class StringStream;
4141
4242protected:
4343 SensoryNode(Type, const std::string&&);
opencog/atoms/sensory/StringStream.cc +112 -0
……@@ -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
32using namespace opencog;
33
34StringStream::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
44StringStream::~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.
57void 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
98std::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.
110DEFINE_VALUE_FACTORY(STRING_STREAM, createStringStream, Handle)
111
112// ====================================================================
opencog/atoms/sensory/StringStream.h +68 -0
……@@ -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
31namespace 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 */
48class StringStream
49 : public StringValue
50{
51protected:
52 SensoryNodePtr _snp;
53 virtual void update() const;
54
55public:
56 StringStream(const Handle&);
57 virtual ~StringStream();
58
59 virtual std::string to_string(const std::string& indent = "") const;
60};
61
62VALUE_PTR_DECL(StringStream)
63CREATE_VALUE_DECL(StringStream)
64
65/** @}*/
66} // namespace opencog
67
68#endif // _OPENCOG_STRING_STREAM_H
opencog/atoms/sensory/TextStream.cc +0 -112
……@@ -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
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 +0 -68
……@@ -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
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
opencog/atoms/sensory/TextStreamNode.cc +2 -3
……@@ -31,7 +31,7 @@
3131#include <opencog/atoms/value/VoidValue.h>
3232
3333#include <opencog/sensory/types/atom_types.h>
34#include "TextStream.h"
34#include "StringStream.h"
3535#include "TextStreamNode.h"
3636
3737using namespace opencog;
……@@ -112,10 +112,9 @@ void TextStreamNode::do_write(const ValuePtr& content)
112112
113113// ==============================================================
114114
115// Override stream() to return TextStream instead of ReadStream
116115ValuePtr TextStreamNode::stream(void) const
117116{
118 return createTextStream(get_handle());
117 return createStringStream(get_handle());
119118}
120119
121120// ==============================================================
opencog/atoms/sensory/TextStreamNode.h +0 -1
……@@ -61,7 +61,6 @@ protected:
6161 // Derived classes need to implement a handler.
6262 virtual void do_write(const std::string&) = 0;
6363
64 // Override to return TextStream instead of ReadStream
6564 virtual ValuePtr stream(void) const;
6665
6766public:
opencog/sensory/types/sensory_types.script +1 -1
……@@ -9,7 +9,7 @@
99READ_STREAM <- STREAM_VALUE,HANDLE_ARG
1010
1111// The StringValue provides a more suitable API for text.
12TEXT_STREAM <- STRING_VALUE,HANDLE_ARG
12STRING_STREAM <- STRING_VALUE,HANDLE_ARG
1313
1414// ---------------------------------------------
1515// Values above, Atoms below.