feat: started refactor to tile grammar instead
This commit is contained in:
parent
d049787762
commit
d410fa377a
2 changed files with 25 additions and 98 deletions
|
|
@ -1,88 +1,25 @@
|
|||
#include "generative_grammar/symbol.h"
|
||||
#include "core/error/error_macros.h"
|
||||
#include "generative_grammar/grammar.h"
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
#include "macros.h"
|
||||
|
||||
void Symbol::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::INT, symbol, PROPERTY_HINT_ENUM, Type_hint());
|
||||
BIND_HPROPERTY(Variant::ARRAY, children_array, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "Symbol"));
|
||||
void Sentence::_bind_methods() {
|
||||
BIND_PROPERTY(Variant::STRING, symbols_string);
|
||||
BIND_PROPERTY(Variant::INT, width);
|
||||
}
|
||||
|
||||
bool Symbol::is_terminal() const {
|
||||
switch (this->symbol) {
|
||||
default:
|
||||
bool Sentence::is_terminal() const {
|
||||
for (char32_t c : this->symbols) {
|
||||
if (String::char_uppercase(c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Symbol::set_children_array(TypedArray<Ref<Symbol>> array) {
|
||||
for (Ref<Symbol> symbol : this->children) {
|
||||
if (symbol.is_valid()) {
|
||||
symbol->set_parent(nullptr);
|
||||
}
|
||||
}
|
||||
this->children.clear();
|
||||
for (Variant var : array) {
|
||||
Ref<Symbol> child{ var };
|
||||
this->children.insert(var);
|
||||
if (child.is_valid()) {
|
||||
child->set_parent(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TypedArray<Ref<Symbol>> Symbol::get_children_array() const {
|
||||
TypedArray<Ref<Symbol>> array{};
|
||||
for (Ref<Symbol> symbol : this->children) {
|
||||
array.push_back(symbol);
|
||||
}
|
||||
return array;
|
||||
return true; // no non-terminals in sentence
|
||||
}
|
||||
|
||||
void Rule::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::OBJECT, pattern, PROPERTY_HINT_RESOURCE_TYPE, "Symbol");
|
||||
BIND_HPROPERTY(Variant::OBJECT, result, PROPERTY_HINT_RESOURCE_TYPE, "Symbol");
|
||||
BIND_HPROPERTY(Variant::OBJECT, pattern, PROPERTY_HINT_RESOURCE_TYPE, Sentence::get_class_static());
|
||||
BIND_HPROPERTY(Variant::OBJECT, result, PROPERTY_HINT_RESOURCE_TYPE, Sentence::get_class_static());
|
||||
}
|
||||
|
||||
bool Rule::match_symbols(Ref<Symbol> match, Ref<Symbol> pattern, HashMap<Ref<Symbol>, Ref<Symbol>> &matches) {
|
||||
if (match == pattern) {
|
||||
return true; // reference match or null match
|
||||
}
|
||||
ERR_FAIL_COND_V_EDMSG(match.is_valid() == false, false, "Attempt to match null symbol, is not possible");
|
||||
if (pattern.is_null()) {
|
||||
return false; // null matches nothing
|
||||
}
|
||||
if (match->get_symbol() != pattern->get_symbol()) {
|
||||
return false; // symbol type mismatch
|
||||
}
|
||||
if (match->get_children().size() < pattern->get_children().size()) {
|
||||
return false; // too little children to be able to match
|
||||
}
|
||||
// check for matching outgoing edges
|
||||
for (Ref<Symbol> pattern_symbol : pattern->get_children()) {
|
||||
bool match_found{ false };
|
||||
for (Ref<Symbol> match_symbol : match->get_children()) {
|
||||
if (match_symbols(match_symbol, pattern_symbol, matches)) {
|
||||
match_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!match_found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
matches.insert(pattern, match);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Rule::try_apply(Ref<Symbol> match) {
|
||||
// identify corresponding symbols
|
||||
HashMap<Ref<Symbol>, Ref<Symbol>> symbols{};
|
||||
if (!match_symbols(match, this->pattern, symbols)) {
|
||||
return false;
|
||||
}
|
||||
// clear matching edges
|
||||
for (KeyValue<Ref<Symbol>, Ref<Symbol>> kvp : symbols) {
|
||||
}
|
||||
return true;
|
||||
void Rule::try_apply(Ref<Sentence> sentence) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,48 +1,38 @@
|
|||
#pragma once
|
||||
|
||||
#include "core/io/resource.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
#include "core/templates/hash_set.h"
|
||||
#include "core/variant/type_info.h"
|
||||
#include "core/variant/typed_array.h"
|
||||
#include "macros.h"
|
||||
|
||||
class Symbol : public Resource {
|
||||
GDCLASS(Symbol, Resource);
|
||||
class Sentence : public Resource {
|
||||
GDCLASS(Sentence, Resource);
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
GDENUM(Type, Start, Goal, Challenge, Reward);
|
||||
bool is_terminal() const;
|
||||
|
||||
private:
|
||||
Type symbol{ Start };
|
||||
HashSet<Ref<Symbol>> children{};
|
||||
Symbol *parent{ nullptr };
|
||||
Vector<char32_t> symbols{};
|
||||
int width{ 0 };
|
||||
|
||||
public:
|
||||
GET_SET_FNS(Type, symbol);
|
||||
GET_SET_FNS(Symbol *, parent);
|
||||
GET_SET_REF_FNS(HashSet<Ref<Symbol>>, children);
|
||||
void set_children_array(TypedArray<Ref<Symbol>> array);
|
||||
TypedArray<Ref<Symbol>> get_children_array() const;
|
||||
void set_symbols_string(String value);
|
||||
String get_symbols_string() const;
|
||||
void set_width(int width);
|
||||
int get_width() const;
|
||||
};
|
||||
|
||||
MAKE_TYPE_INFO(Symbol::Type, Variant::INT);
|
||||
|
||||
struct Rule : public Resource {
|
||||
GDCLASS(Rule, Resource);
|
||||
static void _bind_methods();
|
||||
bool match_symbols(Ref<Symbol> match, Ref<Symbol> pattern, HashMap<Ref<Symbol>, Ref<Symbol>> &matches);
|
||||
|
||||
public:
|
||||
bool try_apply(Ref<Symbol> graph);
|
||||
void try_apply(Ref<Sentence> graph);
|
||||
|
||||
private:
|
||||
Ref<Symbol> pattern{};
|
||||
Ref<Symbol> result{};
|
||||
Ref<Sentence> pattern{};
|
||||
Ref<Sentence> result{};
|
||||
|
||||
public:
|
||||
GET_SET_FNS(Ref<Symbol>, pattern);
|
||||
GET_SET_FNS(Ref<Symbol>, result);
|
||||
GET_SET_FNS(Ref<Sentence>, pattern);
|
||||
GET_SET_FNS(Ref<Sentence>, result);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue