| … | … | @@ -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 | |
| 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 | // ==================================================================== |