Commit 2171b3c5 authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

important refactoring classes KeyS and KeySI

they compute Runs and not Transitions
parent 812a9ec7
......@@ -28,6 +28,7 @@
#include "Transition.hpp"
#include "ParsingEnv.hpp"
namespace Parsing{
/// @brief template for keys.
......
......@@ -8,6 +8,7 @@
#include "KeyS.hpp"
namespace Parsing{
......@@ -22,178 +23,147 @@ _state(s)
{ }
// copy
KeyS::KeyS(const KeyS& k):
_state(k._state)
{ }
// firstChild
KeyS::KeyS(const KeyS& parent,
const Transition& tr,
KeyS::KeyS(const KeyS& k,
const RunInner<KeyS>& r,
const ParsingEnv& env)
{
assert(tr.inner());
assert(parent.inner(tr, env));
_state = tr.state(0);
assert(r.inner());
// first child
if (r.empty())
{
assert(r.partial());
const Transition& t = r.origin();
assert(t.inner());
assert(t.size() > 0);
_state = t.state(0);
}
// instanciate head (inner case)
else if (r.complete())
{
_state = k._state; // simple copy
}
// next sibling
else
{
assert(r.partial());
size_t i = r.size(); // index of next sibling in r
const Transition& t = r.origin();
assert(t.inner());
assert(0 < i);
assert(i < t.size());
_state = t.state(i);
}
}
// nextSibling
// i = pos. of nextSibling
KeyS::KeyS(const KeyS& previous,
size_t i,
const KeyS& parent,
const Transition& tr,
const ParsingEnv& env)
{
assert(previous.complete());
assert(tr.inner());
assert(1 <= i);
assert(i < tr.size());
assert(previous.state() == tr.state(i-1));
_state = tr.state(i);
}
// copy
KeyS::KeyS(const KeyS& k):
_state(k._state)
{ }
// instanciate = copy
KeyS::KeyS(const KeyS& head,
label_t a,
const KeyS& lastchild,
const ParsingEnv& env)
{
assert(head.partial()); // will fail for KeyS
assert(lastchild.complete());
assert(SymbLabel::inner(a));
_state = head.state();
}
KeyS::~KeyS()
{ }
// complete leaf = copy in this case
KeyS::KeyS(const KeyS& head,
label_t a,
const ParsingEnv& env):
KeyS(head)
KeyS& KeyS::operator= (const KeyS& k)
{
assert(head.partial()); // will fail for KeyS
if(this != &k)
{
_state = k._state;
};
return *this;
}
bool KeyS::inner(const Transition& tr,
const ParsingEnv& env) const
bool KeyS::operator==(const KeyS& k) const
{
return true;
return (k._state == _state);
}
size_t KeyS::leaf(const Transition& tr,
const ParsingEnv& env) const
bool KeyS::operator!=(const KeyS& k) const
{
return 1;
return (! (*this == k));
}
void KeyS::leaves(const Transition& tr,
const ParsingEnv& env,
std::queue<KeyS>& q) const
// KeyS always complete
bool KeyS::instance(const KeyS& k) const
{
assert(this->partial()); // will fail
return (k._state == _state);
}
label_t KeyS::label(const Transition& tr,
const ParsingEnv& env) const
bool KeyS::complete() const
{
return tr.label();
return true;
}
Weight KeyS::weight(label_t a,
const Transition& tr,
const ParsingEnv& env) const
void KeyS::runs(std::stack<RunInner<KeyS>*>& si,
std::stack<std::pair<const KeyS*, RunTerm<KeyS>*>>& st,
ParsingEnv& env) const
{
return tr.weight();
RunFactory<KeyS>::runs(*this, si, st, env);
}
void KeyS::transitions(std::stack<const Transition*>& st,
const ParsingEnv& env) const
RunTerm<KeyS>* KeyS::failRun(const ParsingEnv& env) const
{
assert(State::isWTA(_state));
assert(env.wta);
assert(env.wta->isRegistered(_state));
for (Tlist<Transition>::const_iterator
i = env.wta->cbegin(this->state());
i != env.wta->cend(this->state()); i++)
{
const Transition* t = &(*i);
st.push(t);
}
return RunFactory<KeyS>::failRun(*this, env);
}
//typename Tlist<Transition>::const_iterator
//KeyS::cbegin(const ParsingEnv& env) const
//{
// assert(State::isWTA(_state));
// assert(env.wta);
// assert(env.wta->isRegistered(_state));
// return (env.wta->cbegin(this->state()));
//}
//
//
//typename Tlist<Transition>::const_iterator
//KeyS::cend(const ParsingEnv& env) const
//{
// assert(State::isWTA(_state));
// assert(env.wta);
// return (env.wta->cend(this->state()));
//}
KeyS& KeyS::operator= (const KeyS& k)
// protected
void KeyS::RunsTerminal(const Transition& t,
std::stack<std::pair<const KeyS*, RunTerm<KeyS>*>>& st,
const ParsingEnv& env) const
{
if(this != &k)
{
_state = k._state;
};
return *this;
RunTerm<KeyS>* r = new RunTerm<KeyS>(t, 0); // length = 0
TRACE("NEW terminal run {} -> {} for {}", *r, *this, t);
st.push(std::make_pair((KeyS*) NULL, r)); // this key is complete
}
bool KeyS::operator==(const KeyS& k) const
{
return (k._state == _state);
}
// protected
void KeyS::RunsInner(const Transition& t,
std::stack<RunInner<KeyS>*>& si,
const ParsingEnv& env) const
bool KeyS::operator!=(const KeyS& k) const
{
return (! (*this == k));
assert(State::isWTA(_state));
// generic method
RunFactory<KeyS>::innerRun(*this, t, si);
}
// always complete
bool KeyS::instance(const KeyS& k) const
{
return (k._state == _state);
}
//void KeyS::runsWTA(std::stack<RunInner<KeyS>*>& si,
// std::stack<std::pair<KeyS, RunTerm<KeyS>*>>& st,
// const ParsingEnv& env) const
//{
// assert(State::isWTA(_state)); // no treament of bars for this class of key
// RunFactory<KeyS>::runsWTA(*this, si, st, env);
//}
bool KeyS::complete() const
{
return true;
}
//void KeyS::runsBar(std::stack<RunInner<KeyS>*>& si,
// std::stack<std::pair<KeyS, RunTerm<KeyS>*>>& st,
// ParsingEnv& env) const
//{
// RunFactory<KeyS>::runsBar(*this, si, st, env);
//}
//void KeyS::init_leafq(label_t a,
// const Transition& tr,
// const ParsingEnv& env)
//const Transition& KeyS::barTransition0(ParsingEnv& env) const
//{
// assert(f_leafq == false);
// assert(leafq.empty());
// // no instance
// f_leafq = true;
// RunFactory<KeyS>::barTransition0(*this, env);
//}
//
//
//const Transition& KeyS::barTransition2(ParsingEnv& env) const
//{
// RunFactory<KeyS>::barTransition2(*this, env);
//}
......@@ -203,4 +173,5 @@ std::ostream& operator<<(std::ostream& o, const KeyS& k)
return o;
}
} // end namespace Parsing
......@@ -21,17 +21,72 @@
#include "SymbLabel.hpp"
#include "Transition.hpp"
#include "ParsingEnv.hpp"
#include "Key.hpp"
#include "Ranked.hpp"
#include "Runey.hpp"
#include "RunInner.hpp"
#include "RunTerm.hpp"
#include "RunFactory.hpp"
namespace Parsing{
// for printing the keys
#define KEY_LPAR '<'
#define KEY_RPAR '>'
/// @brief generalities on the Key classes.
/// A key is an augmented state,
/// acting as index for best runs in a parse table.
///
/// It is made of:
/// - one state of a base SWTA,
/// - extended with attributes taking values in different domains,
/// and of two kinds:
/// - synthesized attributes are propagated bottom-up,
/// - inherited attributes are propagated top-down.
///
/// - A key is partial if only its inherited attributes are valued.
/// - A key is complete if its all inherited and synthesized attributes are
/// valued
/// (keys without synthesized attributes are always complete).
///
/// A augmented transition has the form
/// a(K1, ..., Kn) -> K0
/// where
/// - a is a label of arity n.
/// - (K1, ..., Kn) is a tuple of complete keys called body of the transition.
/// - K0 is a complete key called head of the transition.
/// K0, K1, ..., Kn are abject of the same concrete class K derived
/// from this class.
///
/// concrete transitions are built with the following functions:
/// - initial augmented states can be built with `top`.
/// - from a given a partial augmented state `K0` for the head
/// of an inner augmented transition,
/// one can built a partial first child by calling `firstChild` on `K0`.
/// - then every other child `Ki+1` can be built by calling `nextSibling`
/// on sibling `Ki`, for `i > 0`.
/// - from a given a partial augmented state `K0` for the head
/// of a terminal (leaf) augmented transition,
/// one can built possible complete instances by calling `leaf`.
/// - the (partial) states for siblings are instanciated
/// by repeating from step 3 down to leaves.
/// - once all the children augmented states are complete,
/// the head 'K0' can be instanciated by calling `instanciate` on `K0`
/// (with the last child complete state in argument).
///
/// Some synthesized attribute contain weight values.
/// The weight of a key is computed from the attributes and an input symbol.
/// Key made of one state of a base WTA (trivial augmented state).
/// no attributes.
/// extended with rank for k-best algo (not used for 1-best).
class KeyS : public Key<KeyS>
class KeyS
{
template<typename> friend class RunFactory;
public:
/// @brief top constructor.
......@@ -40,230 +95,126 @@ public:
/// @param s initial state of the base WTA.
/// @param env input environment.
KeyS(state_t s, const ParsingEnv& env);
//KeyS* top(state_t s, const ParsingEnv& env);
/// @brief copy
KeyS(const KeyS& k);
/// @brief constructor for building first child.
/// construct a partial key to be instanciated as the first child of parent
/// in the body of an augmented transition.
/// @param parent head key of the augmented transition.
/// partial or complete.
/// @param tr WTA transition, augmented by the transition partially
/// constructed here. must be inner.
/// @brief constructor of a successor of a key to complete an inner run.
/// @param k target key for the run. can be partial.
/// @param r an inner run to be completed with the key constructed.
/// @param env input environment.
/// @see Key::firstChild
KeyS(const KeyS& parent,
const Transition& tr,
const ParsingEnv& env);
/// @brief constructor of next sibling.
/// construct a partial key to be instanciated as sibling
/// in the body of an augmented transition.
/// @param previous previous sibling of this key in the body of the
/// augmented transition. must be complete.
/// @param i position of the new sibling in body, in 1..arity-1
/// (0 is the position of the first sibling).
/// @param parent head key of the augmented transition
/// partial or complete.
/// @param tr SWTA transition, augmented by the transition partially
/// constructed here.
/// @param env input environment.
/// @see Key::nextSibling
KeyS(const KeyS& previous,
size_t i,
const KeyS& parent,
const Transition& tr,
const ParsingEnv& env);
/// @brief constructor of complete instance.
/// construct a complete upgrade of the head key, by copy,
/// to be the head of an augmented transition.
/// @param head a key to be instanciated.
/// must be partial.
/// @param a label of the augmented transition.
/// @param lastchild last child in the body of the augmented transition.
/// must be complete.
/// There are 4 cases of construction:
/// - if r is partial and empty, construct the first child k0 to add to r.
/// k is the parent of k0 in this case.
/// - if r is partial but not empty, construct the next sibling kn to add
/// to r. k is the parent of kn in this case.
/// - if r is inner and complete, construct a complete instance of k
/// that will be the (complete) target of run r.
KeyS(const KeyS& k, const RunInner<KeyS>& r, const ParsingEnv& env);
/// @brief constructor of an instance of a key targeting a terminal run.
/// @param k target key for the run in 2 cases below. must be partial.
/// @param r a complete terminal run targeting the key constructed.
/// @param env input environment.
/// @warning should not be called for this class (head must be partial).
/// @see Key::instanciate
KeyS(const KeyS& head,
label_t a,
const KeyS& lastchild,
const ParsingEnv& env);
/// @brief constructor of complete instance for a leaf.
/// @param head a key to instanciate as leaf. must be partial
/// @param a label of the augmented transition. must be terminal.
/// @param env parsing environment.
/// @warning should not be called on this class (head must be partial)
/// @warning leaf for head, with same args, must return 1.
KeyS(const KeyS& head,
label_t a,
const ParsingEnv& env);
protected:
/// @brief constructor for internal use
KeyS(state_t s);
public:
// allocate and return a special Run representing a parse failure for this key.
// @param env parsing environment.
// @return a pointer to a newly allocated fail run, with
// - transition with dummy symbol, empty body, weight one in the
// domain of the base wta in the passed environment.
// - fail symbol
// - weight one (in the domain of the base wta).
// This returned run will answer succesfully to Run.fail().
// Run<KeyS>* failRun(const ParsingEnv& env) const; // default
/// @brief this key can be an inner node, target of tr, in a run.
/// @param tr a SWTA transition, augmented by the transition partially
/// constructed here.
/// @param env parsing environment.
virtual bool inner(const Transition& tr,
const ParsingEnv& env) const;
/// @brief number of leaf instances that can be obtained
/// from this key and the given args.
/// @param tr a SWTA transition, augmented by the transition partially constructed here.
/// must be nullary (leaf, terminal).
/// @param env parsing environment.
/// @return - 0 if there no leaf instance compatible with tr.
/// if this key is complete, it means that it is not compatible with tr, a, and env.
/// - 1 if there is exactly one leaf instance of this key compatible with tr, a and env.
/// if this key is complete, it means that it is compatible with tr, a, and env.
/// if this key is partial, the leaf constructor can be use to built the compatible instance.
/// can be used instead of Key::leaf.
/// - 2 if there are 2 or more leaf instances compatible with tr.
/// it means that this key is partial, the compatible instances can be built with leaves.
/// @see Key::leaf
virtual size_t leaf(const Transition& tr,
const ParsingEnv& env) const;
/// @brief completion of this partial leaf key to be the head
/// of a nullary (leaf) transition augmenting tr.
/// Build and store in the given queue q
/// all ther new complete instances of this key,
/// using the parsing environment env.
/// @param tr a SWTA transition, augmented by the transition partially
/// constructed here. must be nullary (leaf, terminal).
/// @param env parsing environment.
/// @param q empty queue used to store the complete instances.
/// @warning should not be called on this class (only for partial keys)
/// @warning this key must be partial and leaf return at least 2.
/// @see Key::leaves
virtual void leaves(const Transition& tr,
const ParsingEnv& env,
std::queue<KeyS>& q) const;
/// Best label of terminal transition augmenting tr targeted to this key.
/// @param tr a SWTA transition, possibly unlabelled or partialy labelled
/// @param env parsing environment.
/// @return the label maximizing the weight function for this label, this transition, this env.
/// @warning this key must be complete.
virtual label_t label(const Transition& tr,
const ParsingEnv& env) const;
/// @brief The weight of an augmented transition headed by an instance
/// of this Key (augmented state) and augmenting tr.
/// @param a label for the augmented transition.
/// @param tr SWTA transition, augmented by the transition partially
/// constructed here.
/// @param env parsing environment.
/// @return the weight of a transition headed by an instance of this key
/// and augmenting tr.
/// @return a returned weight of zero (absorbing in the semiring)
/// indicates incompatibility between this key and the transition.
/// @warning if tr is nullary (leaf), this key must be complete.
/// @warning if tr is not nullary (inner), this key can be partial
/// or complete.
/// @warning The weight of a run headed by the augmented transition
/// is the semiring product of this value with the product
/// of weights of the subruns.
/// @see Key::weight
virtual Weight weight(label_t a,
const Transition& tr,
const ParsingEnv& env) const;
/// @brief return WTA transitions to be generalized
/// to augmented transitions targeted by this key or an instance.
/// @param s a stack to be filled with WTA transitions to be augmented.
/// @param env parsing environment, containing a wta.
virtual void transitions(std::stack<const Transition*>& s,
const ParsingEnv& env) const;
// /// @return a constant iterator to the first base transition in given environment
// /// with head state the state component of this Key.
// /// @param env parsing environment.
// /// @warning the base SWTA and the pool SWTA must be non-NULL in env.
// /// @todo TBR replaced by transitions()
// virtual typename Tlist<Transition>::const_iterator
// cbegin(const ParsingEnv& env) const;
//
//
// /// @return a constant iterator to the past-the-end base transition
// /// in given environment
// /// with head state the state component of this Key.
// /// @param env parsing environment.
// /// @warning the base SWTA and the pool SWTA must be non-NULL in env.
// /// @todo TBR replaced by transitions()
// virtual typename Tlist<Transition>::const_iterator
// cend(const ParsingEnv& env) const;
/// Construct a complete instance of k that will be the target of run r.
KeyS(const KeyS& k, const RunTerm<KeyS>& r, const ParsingEnv& env);
/// @brief copy
KeyS(const KeyS& k);
/// destructor
virtual ~KeyS();
/// Key assignement.
virtual KeyS& operator= (const KeyS& k);
/// for use as key in a unordered_multimap.
virtual bool operator==(const KeyS& k) const;
virtual bool operator!=(const KeyS& k) const;
/// return wether this Key is an instance of p.
/// = equality in this case because KeyS are always complete.
virtual bool instance(const KeyS& k) const;
/// all attributes of the key are valued
virtual bool complete() const;
/// one atribute at least of the key is not valued
inline bool partial() const { return(! this->complete()); }
/// accessor
inline state_t state() const { return _state; }
/// @brief construct (augmented) runs targeting complete instances of
/// this key.
/// @param si a stack to be filled with inner runs. must be empty.
/// @param st a stack to be filled with pairs containing terminal runs.
/// must be empty.
/// @param env parsing environment, containing a wta.
// @return how many runs have been added to the stacks.
/// @warning the runs constructed are added to the given stacks.
/// - si will contain inner runs, partial or complete.
/// - st will contains pairs made of
/// - a complete instance k' of k if k is partial, and NULL otherwise.
/// - a complete terminal run targeting r'
/// All the runs in stacks must be deallocate elsewhere.
virtual void runs(std::stack<RunInner<KeyS>*>& si,
std::stack<std::pair<const KeyS*, RunTerm<KeyS>*>>& st,
ParsingEnv& env) const;
/// allocate and return a special Run representing a parse failure for this key.
/// @param env parsing environment.
/// @return a pointer to a newly allocated fail run, with
/// - transition with dummy symbol, empty body, weight zero in the
/// domain of the base wta in the passed environment.
/// - fail symbol