dbbr

package module
v0.0.0-...-569af99 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

README

dbbr

A golang package to use the DBBR format.

The DBBR format allows to repressent a binary trees, in a simplistic manner.
Each tree describes a rule, which will be exported by name.
Each rule is made out of nodes. Each node has 2 sub-nodes: + for true and - for false, based on the output of the node's logic. The indentation difference between a node and it's sub-nodes is 1 tab.

The format does not specify the logic used to chek the rules, which should be passed to the builder, thus allowing to use the format for different purposes.


DBBR Format Structure:

general rule structure:

	<rule_name>: SENTENCE
	[<\t>	<+|-> SENTENCE ]

	SENTENCE := <boolean_logic>|<:rule_name:>

builtin rules:

  • TRUE: always returns true.
  • FALSE: always returns false.

Note: True and False are defaulted to + and - respectively, and are not needed to be explicitly written.


Usage

In order to create a builder, which will build the rules, a logic parser must be implemented. The parser should implement the dbbr.LogicParser interface. basicly, it needs to implement the function ParseLine(string) (dbbr.RuleLogic, error).

Examples and demos can be found at the demo folder. To run each demo:

	go run <demofile.go>

Documentation

Overview

dbbr Implements the DBBR format for binary tree creation. It is used to create simple (and even complex) true/false rules from a true/false logic tree.

For more info, look at the README.md file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcIndent

func CalcIndent(s string) int

func ParseFile

func ParseFile(path string, parser LogicParser) (map[string]Rule, error)

ParseFile creates rules from a given file, using the given parser.

func SectionLines

func SectionLines(lines []Line) [][]Line

SectionLines creates sections of rules from lines.

Types

type BuildTree

type BuildTree struct {
	Name  string
	True  *BuildTree
	False *BuildTree
	Logic string
	Line  int
	// if set to true, use Logic as rule name
	IsPointer bool
}

func NewBuildTree

func NewBuildTree(name, logic string, line int) *BuildTree

func (*BuildTree) UpdateNext

func (bt *BuildTree) UpdateNext(b bool, next *BuildTree)

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder builds

func NewBuilder

func NewBuilder(parser LogicParser) *Builder

NewBuilder creates a Builder with builtin True and False.

func (*Builder) Build

func (b *Builder) Build(sections [][]Line) (map[string]Rule, error)

Build builds Rules from a sections of lines, each slice corresponds to 1 rule.

func (*Builder) BuildRuleTree

func (b *Builder) BuildRuleTree(lines []Line) (base *BuildTree, err error)

BulidRuleTree builds a single BuildTree for a rule, from a section of lines.

func (*Builder) HasRule

func (b *Builder) HasRule(name string) bool

HasRule checks if Builder has a given rule.

type Line

type Line struct {
	Number int
	Value  string
}

Line repressents a line in DBBR configration file

func ConvertToLines

func ConvertToLines(strLines []string) []Line

ConvertToLines converts a slice of strings to Lines.

func ReadLinesFromFile

func ReadLinesFromFile(path string) ([]Line, error)

ReadLinesFromFile converts textual file to lines.

type LogicParser

type LogicParser interface {
	ParseLine(string) (RuleLogic, error)
}

LogicParser is an interface for parsing the logic of a rule when building.

type Rule

type Rule interface {
	Name() string
	// Check runs the input against the full rule tree logic
	Check(interface{}) bool
	// CheckLogic runs the input against only the current rule
	CheckLogic(interface{}) bool
	// NextTrue returns the next rule node if true
	NextTrue() Rule
	// NextFalse returns the next rule node if false
	NextFalse() Rule
	// Next returns the next rule by boolean value
	Next(bool) Rule
}

Rule is a binary decision tree with a logic functions.

var (
	// True is alwyas true.
	True Rule = &rule{
		name:   "True",
		nTrue:  nil,
		nFalse: nil,
		logic:  func(_ interface{}) bool { return true },
	}
	//False is always false.
	False Rule = &rule{
		name:   "False",
		nTrue:  nil,
		nFalse: nil,
		logic:  func(_ interface{}) bool { return false },
	}
)

func NewRule

func NewRule(name string, logic RuleLogic, nTrue Rule, nFalse Rule) Rule

NewRule creates a new Rule node.

type RuleLogic

type RuleLogic func(interface{}) bool

RuleLogic repressents the function which detemines the logic of a rule.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL