-
Notifications
You must be signed in to change notification settings - Fork 236
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 #2500 from linas/anchor-link
Post search results to an AnchorLink
- Loading branch information
Showing
11 changed files
with
308 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
; | ||
; anchor.scm - Obtaining search results incrementally. | ||
; | ||
; Both GetLink and BindLink return all search results wrapped in | ||
; a SetLink. This can be inconvenient in several ways. First, | ||
; in most typical uses, the contents of the SetLink are examined, | ||
; and the set itself is promptly discarded. If it is not discarded, | ||
; then one risks that it just hangs out in the AtomSpace, using up | ||
; storage, but otherwise forgetten and useless. Another downside | ||
; is that it is impossible to report any results, until all of them | ||
; are found. This can be a problem for extremely long-running searches. | ||
; | ||
; To avoid both of the above issues, one can specify a "drop-off | ||
; location" for the searches; as results arrive, they are attached | ||
; to these drop-off points with MemberLinks, and other parts of the | ||
; system can then grab results from there, and continue. (There are | ||
; also proposals for a promise/future-type mechanis in the same vein, | ||
; but it has not been implemented yet.) | ||
; | ||
; This example shows how to declare a drop-off point. Its actually | ||
; almost trivial. | ||
; | ||
(use-modules (opencog) (opencog exec)) | ||
|
||
; Some data that we will query over. | ||
(Evaluation (Predicate "foo") (List (Concept "A") (Concept "alpha"))) | ||
(Evaluation (Predicate "foo") (List (Concept "B") (Concept "alpha"))) | ||
(Evaluation (Predicate "foo") (List (Concept "C") (Concept "alpha"))) | ||
|
||
; ---------------------------------- | ||
; Define a search query. Just an ordinary GetLink - with one twist: | ||
; there is an AnchorNode in the variable declaration. This AnchorNode | ||
; will be used as the drop-off point. | ||
(define get-link | ||
(Get | ||
(VariableList | ||
(TypedVariable (Variable "$x") (Type 'ConceptNode)) | ||
(Anchor "get-results")) | ||
(Present | ||
(Evaluation (Predicate "foo") | ||
(List (Variable "$x") (Concept "alpha")))))) | ||
|
||
; Perform the query. This will return the Anchor, instead of a SetLink. | ||
; (cog-execute! get-link) | ||
|
||
; Verify that the expected results showed up. They will be attached | ||
; to the AnchorNode, with MemberLinks. | ||
; (cog-incoming-by-type (AnchorNode "get-results") 'MemberLink) | ||
; ---------------------------------- | ||
|
||
; Very nearly identical to the above, this shows that the BindLink | ||
; can be used in a similar fashion. | ||
(define bind-link | ||
(Bind | ||
(VariableList | ||
(TypedVariable (Variable "$z") (Type 'ConceptNode)) | ||
(Anchor "bind-results")) | ||
(Present | ||
(Evaluation (Predicate "foo") | ||
(List (Variable "$z") (Concept "alpha")))) | ||
(Inheritance (Variable "$z") (Concept "letters")))) | ||
|
||
; As above: perform the query, and verify that the results showed up. | ||
; (cog-execute! bind-link) | ||
; (cog-incoming-by-type (AnchorNode "bind-results") 'MemberLink) | ||
; ---------------------------------- |
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
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
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,139 @@ | ||
/* | ||
* tests/query/AnchorUTest.cxxtest | ||
* | ||
* Copyright (C) 2020 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 <thread> | ||
|
||
#include <opencog/guile/SchemeEval.h> | ||
#include <opencog/atoms/truthvalue/SimpleTruthValue.h> | ||
#include <opencog/atomspace/AtomSpace.h> | ||
#include <opencog/util/Logger.h> | ||
#include <cxxtest/TestSuite.h> | ||
|
||
using namespace opencog; | ||
|
||
#define al as->add_link | ||
#define an as->add_node | ||
|
||
class AnchorUTest: public CxxTest::TestSuite | ||
{ | ||
private: | ||
AtomSpace* as; | ||
|
||
public: | ||
AnchorUTest(void) | ||
{ | ||
// logger().set_level(Logger::FINE); | ||
logger().set_print_to_stdout_flag(true); | ||
logger().set_timestamp_flag(false); | ||
|
||
as = new AtomSpace(); | ||
SchemeEval* eval = SchemeEval::get_evaluator(as); | ||
eval->eval("(use-modules (opencog exec))"); | ||
eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")"); | ||
} | ||
|
||
~AnchorUTest() | ||
{ | ||
delete as; | ||
// Erase the log file if no assertions failed. | ||
if (!CxxTest::TestTracker::tracker().suiteFailed()) | ||
std::remove(logger().get_filename().c_str()); | ||
} | ||
|
||
void setUp(void); | ||
void tearDown(void); | ||
|
||
void Setter(void); | ||
void Getter(void); | ||
|
||
void test_get(void); | ||
void test_bind(void); | ||
void xtest_dual(void); | ||
}; | ||
|
||
void AnchorUTest::tearDown(void) | ||
{ | ||
} | ||
|
||
void AnchorUTest::setUp(void) | ||
{ | ||
} | ||
|
||
/* | ||
* GetLink unit test. | ||
*/ | ||
void AnchorUTest::test_get(void) | ||
{ | ||
logger().debug("BEGIN TEST: %s", __FUNCTION__); | ||
|
||
SchemeEval* eval = SchemeEval::get_evaluator(as); | ||
eval->eval("(load-from-path \"tests/query/anchor.scm\")"); | ||
|
||
Handle anchor = eval->eval_h("(cog-execute! getli)"); | ||
|
||
IncomingSet results = anchor->getIncomingSetByType(MEMBER_LINK); | ||
|
||
TSM_ASSERT_EQUALS("Expecting three answers", results.size(), 3); | ||
|
||
logger().debug("END TEST: %s", __FUNCTION__); | ||
} | ||
|
||
/* | ||
* BindLink unit test. | ||
*/ | ||
void AnchorUTest::test_bind(void) | ||
{ | ||
logger().debug("BEGIN TEST: %s", __FUNCTION__); | ||
|
||
SchemeEval* eval = SchemeEval::get_evaluator(as); | ||
eval->eval("(load-from-path \"tests/query/anchor.scm\")"); | ||
|
||
Handle anchor = eval->eval_h("(cog-execute! bindli)"); | ||
|
||
IncomingSet results = anchor->getIncomingSetByType(MEMBER_LINK); | ||
|
||
TSM_ASSERT_EQUALS("Expecting three answers", results.size(), 3); | ||
|
||
logger().debug("END TEST: %s", __FUNCTION__); | ||
} | ||
|
||
/* | ||
* DualLink unit test. | ||
* XXX FIXME -- there is currently no documented way of squeezing | ||
* an AnchorNode into an SRAI search, and so the unit test below | ||
* canot possibly pass until that design issue is fixed... | ||
*/ | ||
void AnchorUTest::xtest_dual(void) | ||
{ | ||
logger().debug("BEGIN TEST: %s", __FUNCTION__); | ||
|
||
SchemeEval* eval = SchemeEval::get_evaluator(as); | ||
eval->eval("(load-from-path \"tests/query/anchor.scm\")"); | ||
|
||
Handle anchor = eval->eval_h("(cog-execute! (Dual srai))"); | ||
|
||
IncomingSet results = anchor->getIncomingSetByType(MEMBER_LINK); | ||
|
||
TSM_ASSERT_EQUALS("Expecting two answers", results.size(), 2); | ||
|
||
logger().debug("END TEST: %s", __FUNCTION__); | ||
} |
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
Oops, something went wrong.