/*************************************************************************/ /* Copyright (c) 2012, 2013 Linas Vepstas */ /* All rights reserved */ /* */ /* Use of the Viterbi parsing system is subject to the terms of the */ /* license set forth in the LICENSE file included with this software. */ /* This license allows free redistribution and use in source and binary */ /* forms, with or without modification, subject to certain conditions. */ /* */ /*************************************************************************/ #include #include "compile.h" #include "compile-base.h" #include "utilities.h" // needed for assert using namespace link_grammar::viterbi; namespace atombase { // ============================================================ Atom* Atom::upcaster() { if (!this) return this; const Node* n = dynamic_cast(this); const Link* l = dynamic_cast(this); switch (get_type()) { // Links case AND: if (dynamic_cast(this)) return this; return new And(l->get_outgoing_set(), _tv); case OR: if (dynamic_cast(this)) return this; return new Or(l->get_outgoing_set(), _tv); case SEQ: if (dynamic_cast(this)) return this; return new Seq(l->get_outgoing_set(), _tv); case SET: if (dynamic_cast(this)) return this; return new Set(l->get_outgoing_set(), _tv); // Nodes case NUMBER: if (dynamic_cast(this)) return this; return new Number(atof(n->get_name().c_str()), _tv); case CONNECTOR: if (dynamic_cast(this)) return this; return new Connector(n->get_name(), _tv); case WORD: if (dynamic_cast(this)) return this; return new Word(n->get_name(), _tv); default: assert(0, "Atom::upcaster(): implement me!"); } } // ============================================================ Link* Link::append(Atom* a) const { OutList ol = get_outgoing_set(); ol.push_back(a); switch (get_type()) { // Links case AND: return new And(ol, _tv); case OR: return new Or(ol, _tv); case SEQ: return new Seq(ol, _tv); case SET: return new Set(ol, _tv); default: std::cerr << "unhandled atom type "<< type_name(get_type()) << std::endl; assert(0, "atombase::Link::append: implement me!"); } } // ============================================================ /// Replace stary with nowy. Link* Link::replace(Atom* novi, Atom* ctari) const { OutList ol = get_outgoing_set(); // Loo for stary, replace with nowy if found. size_t sz = ol.size(); size_t i = 0; for (; i