diff --git a/src/lib/advice/binding.cpp b/src/lib/advice/binding.cpp index 94faf762b..9500b8a50 100644 --- a/src/lib/advice/binding.cpp +++ b/src/lib/advice/binding.cpp @@ -24,33 +24,25 @@ #include "lib/advice/binding.hpp" #include "lib/symbol.hpp" -//#include #include -#include #include #include using lib::Literal; -using boost::algorithm::join; -using boost::lexical_cast; - using boost::regex; using boost::smatch; -//using boost::regex_search; using boost::sregex_iterator; using boost::match_continuous; - - using boost::hash_combine; +using boost::lexical_cast; namespace lib { namespace advice { - -// using std::tr1::hash; - + + /////////////////////TICKET #613 : centralise generally useful RegExps @@ -109,8 +101,8 @@ namespace advice { REQUIRE (spec); parse_and_append (spec); } - - + + Binding::operator string() const { @@ -148,7 +140,7 @@ namespace advice { { hash_combine (hash, pos->sym()); hash_combine (hash, pos->arity()); - hash_combine (hash, pos->arg()); //////////////TODO: not in final version with variable arguments + hash_combine (hash, pos->arg()); //////////////TICKET #615 : not in final version with variable arguments } return hash; diff --git a/src/lib/advice/binding.hpp b/src/lib/advice/binding.hpp index 9a487ead1..428caeb41 100644 --- a/src/lib/advice/binding.hpp +++ b/src/lib/advice/binding.hpp @@ -62,15 +62,9 @@ #include "lib/error.hpp" -//#include "proc/asset.hpp" -//#include "proc/asset/struct-scheme.hpp" -//#include "lib/hash-indexed.hpp" -//#include "lib/util.hpp" #include "lib/symbol.hpp" #include "lib/query.hpp" -//#include -//#include #include #include #include @@ -87,13 +81,17 @@ namespace advice { /** * Conjunction of predicates to be matched * against a collaboration partner for establishing - * an Advice connection. + * an Advice connection. The binding is defined by a + * textual spec in prolog-like syntax. The internal + * representation is immediately \em normalised. + * Typically the goal is just to create a #Matcher + * (Functor) to be stored for later match checks * TODO type comment */ class Binding { - /** + /** * single predicate * as part of an advice binding pattern */ @@ -123,9 +121,9 @@ namespace advice { && arg_ == oa.arg_; } - int + int compare (Atom const& oa) const ///< @note when #compare returns 0, the corresponding Atom counts as duplicate - { + { int res; if (0 != (res=sym().compare (oa.sym()))) return res; if (0 != (res=arity() - (oa.arity()))) return res; @@ -146,7 +144,7 @@ namespace advice { public: - /** + /** * Functor object for matching against another Binding. * Contains precompiled information necessary for * determining a match. @@ -175,7 +173,7 @@ namespace advice { /** create the empty binding, equivalent to \c true */ Binding(); - + /** create the binding as defined by the given textual definition. * @note implicit type conversion deliberately intended */ Binding (Literal spec); @@ -197,8 +195,8 @@ namespace advice { operator string() const; friend bool operator== (Binding const&, Binding const&); - - + + private: /** internal: parse into atoms, and insert them */ void parse_and_append (Literal def); @@ -208,16 +206,16 @@ namespace advice { inline std::ostream& operator<< (std::ostream& os, Binding const& bi) { - return os << string(bi); + return os << string(bi); } - + template inline void Binding::addTypeGuard() { atoms_.insert (Atom ("advice.type."+lumiera::query::buildTypeID())); } - + diff --git a/tests/40components.tests b/tests/40components.tests index ff99224f0..38a3e34eb 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -39,6 +39,21 @@ END PLANNED "Advice binding patterns" AdviceBindingPattern_test <Binding[] +out: aSymbol --->Binding[aSymbol/0()] +out: aSymbol followed by Garbage §&Ω%€GΩ%€ar☠☠☠bäaäääge --->Binding[Garbage/0(), aSymbol/0(), by/0(), followed/0()] +out: §&Ω%€GΩ%€ar☠☠☠baäääääge --->Binding[] +out: a, list , of ,symbols. --->Binding[a/0(), list/0(), of/0(), symbols./0()] +out: nullary(). --->Binding[nullary/0()] +out: nullary( ) --->Binding[nullary/0()] +out: nothing () --->Binding[nothing/0()] +out: predicate( with-argument ) --->Binding[predicate/1(with-argument)] +out: no (valid definition here) --->Binding[no/0()] +out: Binding[advice.type.n3lib6advice4test12_GLOBAL__N_111DummyAdviceE/0(), one/0(), three/1(four), two/0()] +out: b0==Binding[] +out: b1==Binding[cat1/0(), cat2/0()] +out: b2==Binding[cat1/0(), cat2/0()] +out: b2==Binding[advice.type.n7lumiera4TimeE/0(), cat1/0(), cat2/0(), cat3/1(zzz)] return: 0 END diff --git a/tests/lib/advice/advice-binding-pattern-test.cpp b/tests/lib/advice/advice-binding-pattern-test.cpp index 61f1f06e1..392a2737f 100644 --- a/tests/lib/advice/advice-binding-pattern-test.cpp +++ b/tests/lib/advice/advice-binding-pattern-test.cpp @@ -22,45 +22,23 @@ #include "lib/test/run.hpp" -//#include "lib/test/test-helper.hpp" #include "lib/advice.hpp" -//#include "lib/p.hpp" -//#include "proc/assetmanager.hpp" -//#include "proc/asset/inventory.hpp" -//#include "proc/mobject/session/clip.hpp" -//#include "proc/mobject/session/track.hpp" -//#include "lib/meta/trait-special.hpp" -//#include "lib/util-foreach.hpp" #include "lib/time.h" -//#include "lib/symbol.hpp" -//#include -//#include #include -//#include -//using lib::test::showSizeof; -//using lib::test::randStr; -//using util::isSameObject; -//using util::and_all; -//using util::for_each; -//using util::isnil; -//using lib::Literal; -//using lib::Symbol; -//using lumiera::P; -//using std::string; -//using boost::hash; using std::cout; using std::endl; -namespace lib { -namespace advice { -namespace test { +namespace lib { +namespace advice{ +namespace test { namespace { + class DummyAdvice { }; } @@ -104,17 +82,17 @@ namespace test { _PARSE_AND_SHOW (""); _PARSE_AND_SHOW ("aSymbol"); - _PARSE_AND_SHOW ("aSymbol followed by Garbage : §&Ω%€GΩ%€ar☠☠☠baäääääge"); + _PARSE_AND_SHOW ("aSymbol followed by Garbage §&Ω%€GΩ%€ar☠☠☠bäaäääge"); _PARSE_AND_SHOW ("§&Ω%€GΩ%€ar☠☠☠baäääääge"); _PARSE_AND_SHOW ("a, list , of ,symbols."); _PARSE_AND_SHOW ("nullary()."); _PARSE_AND_SHOW ("nullary( )"); _PARSE_AND_SHOW ("nothing ()"); _PARSE_AND_SHOW ("predicate( with-argument )"); - _PARSE_AND_SHOW ("no( valid definition here)"); + _PARSE_AND_SHOW ("no (valid definition here)"); Binding testBinding; - testBinding.addTypeGuard(); + testBinding.addTypeGuard(); testBinding.addPredicate("one two(), three( four )."); cout << testBinding << endl; @@ -126,7 +104,7 @@ namespace test { { Binding b0, b00; Binding b1 ("cat1(), cat2()."); - Binding b2 (" cat2( ) cat1 ****"); + Binding b2 (" cat2 cat1 ***"); cout << "b0==" << b0 << endl; cout << "b1==" << b1 << endl; @@ -176,7 +154,7 @@ namespace test { Binding b1 ("pred()"); Binding b2 ("pred"); Binding b3 ("pred, pred(x)"); - Binding b4 ("pred ( x ) , pred()."); + Binding b4 ("pred( x ) , pred()."); CHECK ( matches (b1,b2)); CHECK ( matches (b3,b4)); @@ -201,13 +179,27 @@ namespace test { /** @test match against patterns containing variables, * verify the created solution arguments - * @todo this is a future extension + * @todo this is a future extension and its not clear + * if we need it and what the exact semantics + * could be ///////////////////////////////TICKET #615 */ void verifyDynamicMatch() { -#if false /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #605 -#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #605 +#if false /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #615 + CHECK ( matches (Binding("pred(u)"), Binding("pred(X)"))); + CHECK ( matches (Binding("pred(f(u))"), Binding("pred(f(X))"))); + CHECK ( matches (Binding("pred(f(u,Y))"), Binding("pred(f(X,v))"))); + CHECK ( matches (Binding("pred(f(u,X))"), Binding("pred(f(X,v))"))); // the so called "standardisation apart" + + CHECK (!matches (Binding("pred(u,v)"), Binding("pred(X)"))); + CHECK (!matches (Binding("pred(f(u))"), Binding("pred(f(v))"))); + CHECK (!matches (Binding("pred(f(u))"), Binding("pred(g(X))"))); + CHECK (!matches (Binding("pred(f(u,v))"), Binding("pred(f(X,X))"))); + + //////TODO should also cover the difference between equality and match, which gets tangible only in conjunction with variables + +#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #615 } };