Skip to content

Commit

Permalink
Introduce MeetLink as non Set-using version of GetLink
Browse files Browse the repository at this point in the history
This partly resolves issues opencog#2530 and opencog#1502
  • Loading branch information
linas committed May 5, 2020
1 parent e1b83a8 commit df2cb46
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 17 deletions.
3 changes: 2 additions & 1 deletion opencog/atoms/atom_types/atom_types.script
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ SATISFYING_LINK <- PATTERN_LINK
// Both return SetLinks holding the results.
// QueryLink is identical to BindLink, except it returns a LinkValue
// holding the result, instead of a SetLink. (Less atomspace pollution).
GET_LINK <- SATISFYING_LINK // Finds all groundings, returns them
MEET_LINK <- SATISFYING_LINK // Finds all groundings, returns them
GET_LINK <- MEET_LINK // Finds all groundings, returns them
QUERY_LINK <- SATISFYING_LINK // Finds all groundings, substitutes.
BIND_LINK <- QUERY_LINK // Finds all groundings, substitutes.

Expand Down
2 changes: 2 additions & 0 deletions opencog/atoms/pattern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ADD_LIBRARY (lambda
BindLink.cc
DualLink.cc
GetLink.cc
MeetLink.cc
PatternLink.cc
PatternTerm.cc
PatternUtils.cc
Expand All @@ -32,6 +33,7 @@ INSTALL (FILES
BindLink.h
DualLink.h
GetLink.h
MeetLink.h
PatternLink.h
Pattern.h
PatternTerm.h
Expand Down
14 changes: 2 additions & 12 deletions opencog/atoms/pattern/GetLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,16 @@ void GetLink::init(void)
}

GetLink::GetLink(const HandleSeq&& hseq, Type t)
: PatternLink(std::move(hseq), t)
: MeetLink(std::move(hseq), t)
{
init();
}

/* ================================================================= */

QueueValuePtr GetLink::do_execute(AtomSpace* as, bool silent)
{
if (nullptr == as) as = _atom_space;

SatisfyingSet sater(as);
this->satisfy(sater);

return sater.get_result_queue();
}

ValuePtr GetLink::execute(AtomSpace* as, bool silent)
{
QueueValuePtr qv(do_execute(as, silent));
QueueValuePtr qv(MeetLink::do_execute(as, silent));
OC_ASSERT(qv->is_closed(), "Unexpected queue state!");
HandleSeq hs(qv->to_handle_seq());

Expand Down
6 changes: 2 additions & 4 deletions opencog/atoms/pattern/GetLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@
#ifndef _OPENCOG_GET_LINK_H
#define _OPENCOG_GET_LINK_H

#include <opencog/atoms/pattern/PatternLink.h>
#include <opencog/atoms/pattern/MeetLink.h>

namespace opencog
{
/** \addtogroup grp_atomspace
* @{
*/
class GetLink : public PatternLink
class GetLink : public MeetLink
{
protected:
void init(void);
virtual QueueValuePtr do_execute(AtomSpace*, bool silent);

public:
GetLink(const HandleSeq&&, Type=GET_LINK);

GetLink(const GetLink&) = delete;
GetLink operator=(const GetLink&) = delete;

virtual bool is_executable() const { return true; }
virtual ValuePtr execute(AtomSpace*, bool silent=false);

static Handle factory(const Handle&);
Expand Down
67 changes: 67 additions & 0 deletions opencog/atoms/pattern/MeetLink.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* MeetLink.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/util/oc_assert.h>
#include <opencog/atoms/atom_types/NameServer.h>
#include <opencog/atoms/core/UnorderedLink.h>
#include <opencog/query/Satisfier.h>

#include "MeetLink.h"

using namespace opencog;

void MeetLink::init(void)
{
Type t = get_type();
if (not nameserver().isA(t, MEET_LINK))
{
const std::string& tname = nameserver().getTypeName(t);
throw InvalidParamException(TRACE_INFO,
"Expecting a MeetLink, got %s", tname.c_str());
}
}

MeetLink::MeetLink(const HandleSeq&& hseq, Type t)
: PatternLink(std::move(hseq), t)
{
init();
}

/* ================================================================= */

QueueValuePtr MeetLink::do_execute(AtomSpace* as, bool silent)
{
if (nullptr == as) as = _atom_space;

SatisfyingSet sater(as);
this->satisfy(sater);

return sater.get_result_queue();
}

ValuePtr MeetLink::execute(AtomSpace* as, bool silent)
{
return do_execute(as, silent);
}

DEFINE_LINK_FACTORY(MeetLink, MEET_LINK)

/* ===================== END OF FILE ===================== */
61 changes: 61 additions & 0 deletions opencog/atoms/pattern/MeetLink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* opencog/atoms/pattern/MeetLink.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_MEET_LINK_H
#define _OPENCOG_MEET_LINK_H

#include <opencog/atoms/pattern/PatternLink.h>

namespace opencog
{
/** \addtogroup grp_atomspace
* @{
*/
class MeetLink : public PatternLink
{
protected:
void init(void);
virtual QueueValuePtr do_execute(AtomSpace*, bool silent);

public:
MeetLink(const HandleSeq&&, Type=MEET_LINK);

MeetLink(const MeetLink&) = delete;
MeetLink operator=(const MeetLink&) = delete;

virtual bool is_executable() const { return true; }
virtual ValuePtr execute(AtomSpace*, bool silent=false);

static Handle factory(const Handle&);
};

typedef std::shared_ptr<MeetLink> MeetLinkPtr;
static inline MeetLinkPtr MeetLinkCast(const Handle& h)
{ AtomPtr a(h); return std::dynamic_pointer_cast<MeetLink>(a); }
static inline MeetLinkPtr MeetLinkCast(AtomPtr a)
{ return std::dynamic_pointer_cast<MeetLink>(a); }

#define createMeetLink std::make_shared<MeetLink>

/** @}*/
}

#endif // _OPENCOG_MEET_LINK_H

0 comments on commit df2cb46

Please sign in to comment.