-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3021 from linas/incoming-set-of
Provide an IncomingSetOfLink
- Loading branch information
Showing
15 changed files
with
418 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* CollectionOfLink.cc | ||
* | ||
* Copyright (C) 2015, 2022 Linas Vepstas | ||
* | ||
* Author: Linas Vepstas <[email protected]> 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 <opencog/atoms/value/LinkValue.h> | ||
|
||
#include "CollectionOfLink.h" | ||
|
||
using namespace opencog; | ||
|
||
CollectionOfLink::CollectionOfLink(const HandleSeq&& oset, Type t) | ||
: FunctionLink(std::move(oset), t) | ||
{ | ||
if (not nameserver().isA(t, COLLECTION_OF_LINK)) | ||
{ | ||
const std::string& tname = nameserver().getTypeName(t); | ||
throw InvalidParamException(TRACE_INFO, | ||
"Expecting an CollectionOfLink, got %s", tname.c_str()); | ||
} | ||
|
||
if (1 != _outgoing.size()) | ||
throw InvalidParamException(TRACE_INFO, | ||
"CollectionOfLink expects one args"); | ||
} | ||
|
||
// --------------------------------------------------------------- | ||
|
||
/// Return a SetLink vector. | ||
ValuePtr CollectionOfLink::execute(AtomSpace* as, bool silent) | ||
{ | ||
// If the given Atom is executable, then execute it. | ||
// In effectively all cases, we expect it to be executable! | ||
Handle base(_outgoing[0]); | ||
if (not base->is_executable()) | ||
{ | ||
// Consume quotes | ||
Type bt = base->get_type(); | ||
if (DONT_EXEC_LINK != bt and LOCAL_QUOTE_LINK != bt) | ||
return as->add_link(SET_LINK, base); | ||
|
||
return as->add_link(SET_LINK, base->getOutgoingAtom(0)); | ||
} | ||
|
||
ValuePtr vp = base->execute(as, silent); | ||
if (vp->is_atom()) | ||
return as->add_link(SET_LINK, HandleCast(vp)); | ||
|
||
// If its a FloatValue, we could maybe return a NumberNode?? | ||
// so be linke NumberOfLink ??? | ||
// if (vp->is_type(FLOAT_VALUE)) | ||
|
||
if (not vp->is_type(LINK_VALUE)) | ||
throw InvalidParamException(TRACE_INFO, | ||
"CollectionOfLink expects a LinkValue, got %s", | ||
vp->to_string().c_str()); | ||
|
||
LinkValuePtr lvp = LinkValueCast(vp); | ||
HandleSeq hs = lvp->to_handle_seq(); | ||
return as->add_link(SET_LINK, std::move(hs)); | ||
} | ||
|
||
DEFINE_LINK_FACTORY(CollectionOfLink, COLLECTION_OF_LINK) | ||
|
||
/* ===================== END OF FILE ===================== */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* opencog/atoms/flow/CollectionOfLink.h | ||
* | ||
* Copyright (C) 2015, 2022 Linas Vepstas | ||
* All Rights Reserved | ||
* | ||
* 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. | ||
*/ | ||
|
||
#ifndef _OPENCOG_COLLECTION_OF_LINK_H | ||
#define _OPENCOG_COLLECTION_OF_LINK_H | ||
|
||
#include <opencog/atoms/core/FunctionLink.h> | ||
|
||
namespace opencog | ||
{ | ||
/** \addtogroup grp_atomspace | ||
* @{ | ||
*/ | ||
|
||
/// The CollectionOfLink returns a SetLink holding the contents of | ||
/// a LinkValue that resulted from the execution of whatever was | ||
/// wrapped. Thus, if a pipeline produces a LinkValue, and you | ||
/// wanted a SetLink instead, just wrap it with this. | ||
/// | ||
class CollectionOfLink : public FunctionLink | ||
{ | ||
public: | ||
CollectionOfLink(const HandleSeq&&, Type = COLLECTION_OF_LINK); | ||
CollectionOfLink(const CollectionOfLink&) = delete; | ||
CollectionOfLink& operator=(const CollectionOfLink&) = delete; | ||
|
||
// Return a SetLink | ||
virtual ValuePtr execute(AtomSpace*, bool); | ||
|
||
static Handle factory(const Handle&); | ||
}; | ||
|
||
LINK_PTR_DECL(CollectionOfLink) | ||
#define createCollectionOfLink CREATE_DECL(CollectionOfLink) | ||
|
||
/** @}*/ | ||
} | ||
|
||
#endif // _OPENCOG_COLLECTION_OF_LINK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* IncomingOfLink.cc | ||
* | ||
* Copyright (C) 2015, 2022 Linas Vepstas | ||
* | ||
* Author: Linas Vepstas <[email protected]> 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/atoms/core/TypeNode.h> | ||
#include <opencog/atoms/value/LinkValue.h> | ||
|
||
#include "IncomingOfLink.h" | ||
|
||
using namespace opencog; | ||
|
||
IncomingOfLink::IncomingOfLink(const HandleSeq&& oset, Type t) | ||
: FunctionLink(std::move(oset), t) | ||
{ | ||
if (not nameserver().isA(t, INCOMING_OF_LINK)) | ||
{ | ||
const std::string& tname = nameserver().getTypeName(t); | ||
throw InvalidParamException(TRACE_INFO, | ||
"Expecting an IncomingOfLink, got %s", tname.c_str()); | ||
} | ||
|
||
size_t sz = _outgoing.size(); | ||
|
||
if (1 != sz and 2 != sz) | ||
throw InvalidParamException(TRACE_INFO, | ||
"IncomingOfLink expects one or two args, got %lu", sz); | ||
} | ||
|
||
// --------------------------------------------------------------- | ||
|
||
/// Return a LinkValue vector. | ||
ValuePtr IncomingOfLink::execute(AtomSpace* as, bool silent) | ||
{ | ||
// If the given Atom is executable, then execute it. | ||
Handle base(_outgoing[0]); | ||
if (base->is_executable()) | ||
{ | ||
base = HandleCast(base->execute(as, silent)); | ||
if (nullptr == base) return createLinkValue(); | ||
} | ||
else | ||
{ | ||
// consume quotes | ||
Type bt = base->get_type(); | ||
if (DONT_EXEC_LINK == bt or LOCAL_QUOTE_LINK == bt) | ||
base = base->getOutgoingAtom(0); | ||
} | ||
|
||
// Simple case. Get IncomingSet. | ||
if (1 == _outgoing.size()) | ||
return createLinkValue(base->getIncomingSet()); | ||
|
||
// Get incoming set by type. | ||
Handle tnode(_outgoing[1]); | ||
if (tnode->is_executable()) | ||
tnode = HandleCast(tnode->execute(as, silent)); | ||
|
||
if (not tnode->is_type(TYPE_NODE)) | ||
throw RuntimeException(TRACE_INFO, | ||
"IncomingOfLink expects a type; got %s", | ||
tnode->to_string().c_str()); | ||
|
||
TypeNodePtr tnp = TypeNodeCast(tnode); | ||
Type intype = tnp->get_kind(); | ||
HandleSeq iset(base->getIncomingSetByType(intype)); | ||
return createLinkValue(iset); | ||
} | ||
|
||
DEFINE_LINK_FACTORY(IncomingOfLink, INCOMING_OF_LINK) | ||
|
||
/* ===================== END OF FILE ===================== */ |
Oops, something went wrong.