-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallel Get/Put links #2316
Parallel Get/Put links #2316
Changes from all commits
72e2d4c
62ad46c
29719ed
82e990f
076c8b2
2daac23
c8fafe1
8b086fc
50fbc7b
325c67f
b89c25f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
#include "DefineLink.h" | ||
#include "LambdaLink.h" | ||
#include "PutLink.h" | ||
#include <opencog/atomspace/AtomSpace.h> | ||
#include <opencog/atoms/value/QueueValue.h> | ||
|
||
using namespace opencog; | ||
|
||
|
@@ -255,6 +257,16 @@ static inline Handle reddy(PrenexLinkPtr& subs, const HandleSeq& oset) | |
return subs->beta_reduce(oset); | ||
} | ||
|
||
static inline void reddy(HandleSeq& bset, PrenexLinkPtr& subs, const HandleSeq& oset) | ||
{ | ||
try | ||
{ | ||
bset.emplace_back(reddy(subs, oset)); | ||
} | ||
catch (const TypeCheckException& ex) {} | ||
|
||
} | ||
|
||
// If arg is executable, then run it, and unwrap the set link, too. | ||
// We unwrap the SetLinks cause that is what GetLinks return. | ||
static inline Handle expand(const Handle& arg, bool silent) | ||
|
@@ -431,24 +443,38 @@ Handle PutLink::do_reduce(void) const | |
// If there is only one variable in the PutLink body... | ||
if (1 == nvars) | ||
{ | ||
if (SET_LINK != vtype) | ||
|
||
if (SET_LINK == vtype) | ||
{ | ||
return reddy(subs, {args}); | ||
// If the arguments are given in a set, then iterate over the set... | ||
HandleSeq bset; | ||
for (const Handle& h : args->getOutgoingSet()) | ||
{ | ||
reddy(bset, subs, {h}); | ||
} | ||
return createLink(bset, SET_LINK); | ||
} | ||
|
||
// If the arguments are given in a set, then iterate over the set... | ||
HandleSeq bset; | ||
for (const Handle& h : args->getOutgoingSet()) | ||
if (SET_NODE == vtype) | ||
{ | ||
HandleSeq oset; | ||
oset.emplace_back(h); | ||
try | ||
// If the argument is SetNode, then process atoms from queue | ||
// stored in SetNode value | ||
AtomSpace* as = getAtomSpace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is atomspace required here? It is not used in code below |
||
ValuePtr value = args->getValue(QueueValue::QUEUE_VALUE_KEY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to |
||
if (as && value) | ||
{ | ||
bset.emplace_back(reddy(subs, oset)); | ||
Handle h; | ||
HandleSeq bset; | ||
HandleClosableQueuePtr queue = QueueValueCast(value)->get_queue(); | ||
while (queue->pop(h)) | ||
{ | ||
reddy(bset, subs, {h}); | ||
} | ||
return createLink(bset, SET_LINK); | ||
} | ||
catch (const TypeCheckException& ex) {} | ||
} | ||
return createLink(bset, SET_LINK); | ||
|
||
return reddy(subs, {args}); | ||
} | ||
|
||
// If we are here, then there are multiple variables in the body. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* ParallelGetLink.cc | ||
* | ||
* Copyright (C) 2019 Linas Vepstas | ||
* | ||
* 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/atom_types/NameServer.h> | ||
#include <opencog/atoms/core/UnorderedLink.h> | ||
#include <opencog/query/Satisfier.h> | ||
|
||
#include "ParallelGetLink.h" | ||
|
||
using namespace opencog; | ||
|
||
void ParallelGetLink::init(void) | ||
{ | ||
Type t = get_type(); | ||
if (not nameserver().isA(t, PARALLEL_GET_LINK)) | ||
{ | ||
const std::string& tname = nameserver().getTypeName(t); | ||
throw InvalidParamException(TRACE_INFO, | ||
"Expecting a ParallelGetLink, got %s", tname.c_str()); | ||
} | ||
} | ||
|
||
ParallelGetLink::ParallelGetLink(const HandleSeq& hseq, Type t) | ||
: PatternLink(hseq, t) | ||
{ | ||
init(); | ||
} | ||
|
||
ParallelGetLink::ParallelGetLink(const Link &l) | ||
: PatternLink(l) | ||
{ | ||
init(); | ||
} | ||
|
||
/* ================================================================= */ | ||
|
||
HandleSet ParallelGetLink::do_execute(AtomSpace* as, bool silent) | ||
{ | ||
if (nullptr == as) as = _atom_space; | ||
|
||
ParallelSatisfier sater(as, _target); | ||
this->satisfy(sater); | ||
|
||
return sater._satisfying_set; | ||
} | ||
|
||
ValuePtr ParallelGetLink::execute(AtomSpace* as, bool silent) | ||
{ | ||
|
||
|
||
#define PLACE_RESULTS_IN_ATOMSPACE | ||
#ifdef PLACE_RESULTS_IN_ATOMSPACE | ||
// Shoot. XXX FIXME. Most of the unit tests require that the atom | ||
// that we return is in the atomspace. But it would be nice if we | ||
// could defer this indefinitely, until its really needed. | ||
|
||
HandleSet handle_set = do_execute(as, silent); | ||
for (auto h: handle_set) | ||
{ | ||
|
||
HandleSeq handle_seq; | ||
handle_seq.push_back(h); | ||
handle_seq.push_back(_target); | ||
Handle member_link = createLink(handle_seq, MEMBER_LINK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't add results to the atomspace in the |
||
if (as) member_link = as->add_atom(member_link); | ||
} | ||
#endif /* PLACE_RESULTS_IN_ATOMSPACE */ | ||
|
||
return _target; | ||
} | ||
|
||
DEFINE_LINK_FACTORY(ParallelGetLink, PARALLEL_GET_LINK) | ||
|
||
/* ===================== END OF FILE ===================== */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* opencog/atoms/pattern/ParallelGetLink.h | ||
* | ||
* Copyright (C) 2019 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_PARALLEL_GET_LINK_H | ||
#define _OPENCOG_PARALLEL_GET_LINK_H | ||
|
||
#include <opencog/atoms/pattern/PatternLink.h> | ||
|
||
namespace opencog | ||
{ | ||
/** \addtogroup grp_atomspace | ||
* @{ | ||
*/ | ||
class ParallelGetLink : public PatternLink | ||
{ | ||
protected: | ||
void init(void); | ||
virtual HandleSet do_execute(AtomSpace*, bool silent); | ||
|
||
public: | ||
ParallelGetLink(const HandleSeq&, Type=PARALLEL_GET_LINK); | ||
explicit ParallelGetLink(const Link &l); | ||
|
||
virtual bool is_executable() const { return true; } | ||
virtual ValuePtr execute(AtomSpace*, bool silent=false); | ||
|
||
static Handle factory(const Handle&); | ||
}; | ||
|
||
typedef std::shared_ptr<ParallelGetLink> ParallelGetLinkPtr; | ||
static inline ParallelGetLinkPtr ParallelGetLinkCast(const Handle& h) | ||
{ AtomPtr a(h); return std::dynamic_pointer_cast<ParallelGetLink>(a); } | ||
static inline ParallelGetLinkPtr ParallelGetLinkCast(AtomPtr a) | ||
{ return std::dynamic_pointer_cast<ParallelGetLink>(a); } | ||
|
||
#define createParallelGetLink std::make_shared<ParallelGetLink> | ||
|
||
/** @}*/ | ||
} | ||
|
||
#endif // _OPENCOG_PARALLEL_GET_LINK_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* opencog/atoms/value/QueueValue.cc | ||
* | ||
* Copyright (C) 2015, 2016 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. | ||
*/ | ||
|
||
#include <opencog/atoms/value/QueueValue.h> | ||
#include <opencog/atoms/value/ValueFactory.h> | ||
|
||
using namespace opencog; | ||
|
||
const Handle QueueValue::QUEUE_VALUE_KEY = createNode(CONCEPT_NODE, "QUEUE_VALUE_KEY"); | ||
|
||
bool QueueValue::operator==(const Value& other) const | ||
{ | ||
if (QUEUE_VALUE != other.get_type()) return false; | ||
|
||
const QueueValue* cv = (const QueueValue*) &other; | ||
return _name == cv->_name; | ||
} | ||
|
||
std::string QueueValue::to_string(const std::string& indent) const | ||
{ | ||
std::string rv = indent + "(" + nameserver().getTypeName(_type); | ||
rv += std::string(" \"") + _name + "\")"; | ||
return rv; | ||
} | ||
|
||
// Adds factory when library is loaded. | ||
DEFINE_VALUE_FACTORY(QUEUE_VALUE, | ||
createQueueValue, std::string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be inherited from PATTERN_LINK instead?