OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / opencog / atoms / flow / StreamValueOfLink.cc

102 lines · cc ref:
/*
 * StreamValueOfLink.cc
 *
 * Copyright (C) 2015, 2018, 2020 Linas Vepstas
 *
 * Author: Linas Vepstas <linasvepstas@gmail.com>  January 2009
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the
 * exceptions at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <opencog/atomspace/AtomSpace.h>
#include "StreamValueOfLink.h"
#include <opencog/atoms/value/ContainerValue.h>
#include <opencog/atoms/value/FloatValue.h>
#include <opencog/atoms/value/LinkValue.h>

using namespace opencog;

StreamValueOfLink::StreamValueOfLink(const HandleSeq&& oset, Type t)
	: ValueOfLink(std::move(oset), t)
{
	if (not nameserver().isA(t, STREAM_VALUE_OF_LINK))
	{
		const std::string& tname = nameserver().getTypeName(t);
		throw InvalidParamException(TRACE_INFO,
			"Expecting an StreamValueOfLink, got %s", tname.c_str());
	}
}

// ---------------------------------------------------------------

/// When executed, this will return a single sample from the stream
/// at the indicated key.
ValuePtr StreamValueOfLink::execute(AtomSpace* as, bool silent)
{
	size_t ary = _outgoing.size();
	if (2 != ary)
		throw SyntaxException(TRACE_INFO, "Expecting two atoms!");

	ValuePtr stream(get_literal(as, silent));
	if (nullptr == stream)
	{
		if (silent)
			throw SilentException();

		throw InvalidParamException(TRACE_INFO,
		   "No value for %s", to_string().c_str());
	}

	if (stream->is_type(FLOAT_VALUE))
	{
		// Sample a value out of the stream.
		FloatValuePtr fvp = FloatValueCast(stream);
		return createFloatValue(fvp->value());
	}

	if (stream->is_type(CONTAINER_VALUE))
	{
		// If the container is open for insertion, then
		// just return a single value out of it. This will
		// block if it is empty. If it is closed, just treat
		// it like a normal LinkValue (thus the fall-thru).
		ContainerValuePtr cvp = ContainerValueCast(stream);
		if (not cvp->is_closed())
			return createLinkValue(cvp->remove());
		// else fall through
	}

	if (stream->is_type(LINK_VALUE))
	{
		LinkValuePtr lvp = LinkValueCast(stream);
		const ValueSeq& vseq = lvp->value();
		if (vseq.size() == 1)
			return vseq[0];
		return createLinkValue(vseq);
	}

	if (silent)
		throw SilentException();

	throw InvalidParamException(TRACE_INFO,
	   "Expecting a stream for %s, got %s",
	   to_string().c_str(), stream->to_string().c_str());
}

DEFINE_LINK_FACTORY(StreamValueOfLink, STREAM_VALUE_OF_LINK)

/* ===================== END OF FILE ===================== */