-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ ADD_LIBRARY (execution | |
Instantiator.cc | ||
MapLink.cc | ||
LibraryManager.cc | ||
CondLink.cc | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kasimebrahim
Author
Owner
|
||
) | ||
|
||
# Without this, parallel make will race and crap up the generated files. | ||
|
@@ -54,5 +55,6 @@ ENDIF (HAVE_GUILE) | |
# are fixed, first. | ||
INSTALL (FILES | ||
MapLink.h | ||
CondLink.h | ||
DESTINATION "include/opencog/atoms/core" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* opencog/atoms/execution/CondLink.cc | ||
* | ||
* Copyright (C) 2019 Kasim Ebrahim | ||
* | ||
* 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/base/ClassServer.h> | ||
#include <opencog/atoms/execution/Instantiator.h> | ||
#include <opencog/atoms/core/FindUtils.h> | ||
|
||
#include "CondLink.h" | ||
#include "EvaluationLink.h" | ||
|
||
using namespace opencog; | ||
|
||
void CondLink::init(void) | ||
{ | ||
if (0 == _outgoing.size()) | ||
throw SyntaxException(TRACE_INFO, | ||
"CondLink is expected to be arity greater-than 0!"); | ||
|
||
if (1 == _outgoing.size()) { | ||
def_exp = _outgoing[0]; | ||
return; | ||
} | ||
|
||
for (unsigned i = 0; i < _outgoing.size(); ++i) { | ||
if (i % 2 == 0) { | ||
if (i == _outgoing.size() - 1) { | ||
def_exp = _outgoing[i]; | ||
return; | ||
} | ||
conds.push_back(_outgoing[i]); | ||
} else { | ||
exps.push_back(_outgoing[i]); | ||
} | ||
} | ||
} | ||
|
||
CondLink::CondLink(const HandleSeq &oset, Type t) | ||
: FunctionLink(oset, t) | ||
{ | ||
if (not nameserver().isA(t, COND_LINK)) { | ||
const std::string &tname = nameserver().getTypeName(t); | ||
throw SyntaxException(TRACE_INFO, | ||
"Expecting a CondLink, got %s", tname.c_str()); | ||
} | ||
|
||
// Derived types have a different initialization sequence. | ||
if (COND_LINK != t) return; | ||
init(); | ||
} | ||
|
||
Handle CondLink::execute(AtomSpace *scratch) const | ||
{} | ||
|
||
DEFINE_LINK_FACTORY(CondLink, COND_LINK) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* opencog/atoms/execution/CondLink.h | ||
* | ||
* Copyright (C) 2019 Kasim Ebrahim | ||
* 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_COND_LINK_H | ||
#define _OPENCOG_COND_LINK_H | ||
|
||
#include <opencog/atoms/core/FunctionLink.h> | ||
#include <opencog/atoms/core/ScopeLink.h> | ||
#include <opencog/atoms/core/Quotation.h> | ||
|
||
namespace opencog | ||
{ | ||
class CondLink : public FunctionLink | ||
{ | ||
protected: | ||
HandleSeq conds; | ||
HandleSeq exps; | ||
Handle def_exp; | ||
|
||
void init(void); | ||
|
||
public: | ||
CondLink(const HandleSeq&, Type=COND_LINK); | ||
|
||
virtual Handle execute(AtomSpace* = nullptr) const; | ||
|
||
static Handle factory(const Handle&); | ||
}; | ||
|
||
typedef std::shared_ptr<CondLink> CondLinkPtr; | ||
static inline CondLinkPtr CondLinkCast(const Handle& h) | ||
{ AtomPtr a(h); return std::dynamic_pointer_cast<CondLink>(a); } | ||
static inline CondLinkPtr CondLinkCast(AtomPtr a) | ||
{ return std::dynamic_pointer_cast<CondLink>(a); } | ||
|
||
#define createCondLink std::make_shared<CondLink> | ||
|
||
/** @}*/ | ||
} | ||
|
||
#endif // _OPENCOG_COND_LINK_H |
Kasim, CondLink already exists, by a different name. This can already be done in the atomspace, you do not need to create a new link type for this. You should get familiar with what the contents of the wiki, and the atomspace examples, before adding new code. In this particular case, you can use the already existing links GetLink, SequentialAndLink, SequentialOrLink to perform an If-Then-Else test to run specific code and return answers. The stop-go example in the examples directory:
examples/pattern-matcher/sequence.scm
shows how to do if-then-else in Atomese.