Skip to content

Commit

Permalink
Add CondLink
Browse files Browse the repository at this point in the history
Fix include path
  • Loading branch information
kasimebrahim committed Feb 6, 2019
1 parent 4d9ce16 commit 371efb5
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions opencog/atoms/execution/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ADD_LIBRARY (execution
Instantiator.cc
MapLink.cc
LibraryManager.cc
CondLink.cc

This comment has been minimized.

Copy link
@linas

linas Feb 6, 2019

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.

This comment has been minimized.

Copy link
@kasimebrahim

kasimebrahim Feb 7, 2019

Author Owner

Oh Thanks @linas. I will do that.

This comment has been minimized.

Copy link
@kasimebrahim

kasimebrahim Feb 7, 2019

Author Owner

Hey @linas, I need the If-Then-Else to return atoms for the AS-MOSES port, I have looked in the wiki, the examples and etc. But I still have no idea how I can achieve that without a new link, can you give me some ideas? Thanks.

This comment has been minimized.

Copy link
@linas

linas Feb 7, 2019

The example examples/pattern-matcher/condition.scm shows one way of doing conditions. It is a bit old, so I am cleaning it up now; I will also try to write a new example that (I hope) will be much closer to what you are trying to do with CondLink . It might also be useful to review examples/pattern-matcher/sequence.scm

This comment has been minimized.

Copy link
@linas

linas Feb 7, 2019

I just opened opencog/asmoses#46 as a better place to discuss this. I tried to explain what the problem is with a naive cond-link, and what a better replacement for it could be.

Also, I think it would be simpler and less confusing if you started working on implementing the reduct engine first, and work about cond link later, after most if reduct is working.

)

# Without this, parallel make will race and crap up the generated files.
Expand Down Expand Up @@ -54,5 +55,6 @@ ENDIF (HAVE_GUILE)
# are fixed, first.
INSTALL (FILES
MapLink.h
CondLink.h
DESTINATION "include/opencog/atoms/core"
)
72 changes: 72 additions & 0 deletions opencog/atoms/execution/CondLink.cc
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)
60 changes: 60 additions & 0 deletions opencog/atoms/execution/CondLink.h
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

0 comments on commit 371efb5

Please sign in to comment.