suiron

package
v0.0.0-...-b53b565 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

package main

package main

Index

Constants

View Source
const (
	NONE = iota

	//----------term types----------
	ATOM
	INTEGER
	FLOAT
	VARIABLE
	COMPLEX    // prof(...)
	LINKEDLIST // [...]
	ANONYMOUS  // anonymous (don't care) variable, $_
	FUNCTION

	//--------parsing tokens--------
	SUBGOAL
	COMMA
	SEMICOLON
	LPAREN
	RPAREN
	GROUP
	AND
	OR

	//-----------INFIXES-----------
	UNIFY // =    Unify does unification.
	EQUAL // ==   No unification. Simply compares.
	GREATER_THAN
	LESS_THAN
	GREATER_THAN_OR_EQUAL
	LESS_THAN_OR_EQUAL
)
View Source
const FORMAT_SPECIFIER = "%s"

Variables

This section is empty.

Functions

func ClearStartTime

func ClearStartTime()

ClearStartTime - clears the start time to 0. This will prevent a time-out.

func DBG

func DBG(args ...interface{})

DBG - prints a debug message. This function takes a variable number of arguments. In order to remove debug messages after testing, it is useful to include a string of x's, For example:

DBG("xxxxxx function arguments ", args)

When testing is done, debugging messages can be grepped:

> grep xxx *.go

func DBKB

func DBKB(kb KnowledgeBase)

DBKB - prints all rules and facts in the knowledge base.

func DBLL

func DBLL(message string, list LinkedListStruct)

DBLL - displays a linked list for debugging purposes.

func ElapsedTime

func ElapsedTime() int64

ElapsedTime - returns time (in nanoseconds) since the start of the query.

func FormatSolution

func FormatSolution(query Complex, bindings SubstitutionSet) string

FormatSolution - formats a string to display the variable bindings of a solution. For example, if the query were: grandfather(Godwin, $X), then the function would return: $X = Harold Params:

query
bindings (substitution set)

Return:

solution in string format

func HasTimedOut

func HasTimedOut() bool

HasTimedOut - returns true if the maximum execution time has been exceeded. If no start time was set (zero time), return false.

func LetterNumberHyphen

func LetterNumberHyphen(ch rune) bool

LetterNumberHyphen - determines whether the given character (rune) is a letter, a number, or a hyphen. This excludes punctuation.

Param: character (rune) Return: true/false

func LoadKBFromFile

func LoadKBFromFile(kb KnowledgeBase, fileName string) error

LoadKBFromFile - reads rules and facts from a text file, parses them, then adds them to the knowledge base. If a parsing error is generated, add the previous line to the error message.

Params: knowledge base

filename

Return: error or nil

func LoadParseError

func LoadParseError(previous string, err error) error

LoadParseError - If a parse error occurs while loading rules, this function adds the previous line for context. Params: previous line

parsing error

Return: new error

func MakeTimer

func MakeTimer() *time.Timer

MakeTimer - makes a timer to limit the runtime of the inference engine.

func ReadFactsAndRules

func ReadFactsAndRules(fileName string) ([]string, error)

ReadFactsAndRules - reads Suiron facts and rules from a text file. Strips out all comments. (Comments are preceded by #, % or // .) Param: file name Return: array (slice) of rules

error

func SetMaxTimeMilliseconds

func SetMaxTimeMilliseconds(maxTime int64)

SetMaxTimeMilliseconds - sets the maximum execution time. Param: maxTime (in milliseconds)

func SetStartTime

func SetStartTime()

SetStartTime - sets the start time before starting the query.

func StringToRules

func StringToRules(str string) ([]string, error)

StringToRules - Divides string into an array of facts and rules. Strips out all comments. (Comments are preceded by #, % or // .)

Param: string Return: array (slice) of facts and rules

error

Types

type AddStruct

type AddStruct BuiltInPredicateStruct

func Add

func Add(arguments ...Unifiable) AddStruct

Add - creates an AddStruct, which holds the function's name and arguments. Add requires at least 2 arguments. Params: arguments (Unifiable) Return: AddStruct

func (AddStruct) RecreateVariables

func (as AddStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (AddStruct) ReplaceVariables

func (as AddStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (AddStruct) String

func (as AddStruct) String() string

String - creates a string representation. Returns: function_name(arg1, arg2, arg3)

func (AddStruct) TermType

func (as AddStruct) TermType() int

TermType - returns a constant which identifies this type.

func (AddStruct) Unify

func (as AddStruct) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - unifies the result of a function with another term (usually a variable).

Params:

other unifiable term
substitution set

Returns:

updated substitution set
success/failure flag

type AndOp

type AndOp Operator

func And

func And(operands ...Goal) AndOp

And - creates a logical And operator (Conjunction). Params: operands

func (AndOp) GetSolver

func (a AndOp) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for And operator.

func (AndOp) RecreateVariables

func (a AndOp) RecreateVariables(vars VarMap) Expression

RecreateVariables - Operators must implement RecreateVariables(), in order to satisfy the Expression and Goal interfaces. Refer to comments in expression.go.

func (AndOp) ReplaceVariables

func (a AndOp) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Operators must implement ReplaceVariables(), in order to satisfy the Expression and Goal interfaces. Refer to comments in expression.go.

func (AndOp) String

func (a AndOp) String() string

String - Creates a string for debugging purposes.

type AndSolutionNodeStruct

type AndSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*AndSolutionNodeStruct) GetParentNode

func (n *AndSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*AndSolutionNodeStruct) HasNextRule

func (n *AndSolutionNodeStruct) HasNextRule() bool

HasNextRule - returns true if the knowledge base contains untried rules for this node's goal. False otherwise.

func (*AndSolutionNodeStruct) NextRule

func (n *AndSolutionNodeStruct) NextRule() RuleStruct

NextRule - fetches the next rule from the database, according to ruleNumber. The method HasNextRule must be called to ensure that a rule can be fetched from the knowledge base. If GetRule is called with invalid parameters, the knowledge base will panic.

func (*AndSolutionNodeStruct) NextSolution

func (n *AndSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - recursively calls NextSolution on all subgoals. If the search succeeds, the boolean return value is true, and the substitution set is updated. If the search fails, the boolean value is false.

func (*AndSolutionNodeStruct) SetNoBackTracking

func (n *AndSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag. This flag is used to implement Cuts.

type Anonymous

type Anonymous struct{}

func Anon

func Anon() Anonymous

Anon - returns the anonymous variable ($_).

func (Anonymous) RecreateVariables

func (a Anonymous) RecreateVariables(vars VarMap) Expression

RecreateVariables - creates unique variables every time the inference engine fetches a rule from the knowledge base. For the anonymous variable, RecreateVariables() simply returns itself. This function satisfies the Expression interface.

func (Anonymous) ReplaceVariables

func (a Anonymous) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - replaces a bound variable with its binding. For the anonymous variable, ReplaceVariables() simply returns itself. This function satisfies the Expression interface.

func (Anonymous) String

func (a Anonymous) String() string

String - return this term as a string.

func (Anonymous) TermType

func (a Anonymous) TermType() int

TermType - Returns an integer constant which identifies the type.

func (Anonymous) Unify

func (a Anonymous) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - unifies a variable with another unifiable expression. For the anonymous variable ($_), no substitution necessary.

type AppendSolutionNodeStruct

type AppendSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*AppendSolutionNodeStruct) GetParentNode

func (n *AppendSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*AppendSolutionNodeStruct) NextSolution

func (sn *AppendSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*AppendSolutionNodeStruct) SetNoBackTracking

func (sn *AppendSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type AppendStruct

type AppendStruct BuiltInPredicateStruct

func Append

func Append(arguments ...Unifiable) AppendStruct

Append - creates an AppendStruct, which holds the predicate's name and arguments. Append requires at least 2 arguments. Params: arguments (Unifiable) Return: AppendStruct

func (AppendStruct) GetSolver

func (as AppendStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for Append predicate.

func (AppendStruct) RecreateVariables

func (as AppendStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (AppendStruct) ReplaceVariables

func (as AppendStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (AppendStruct) String

func (as AppendStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type Atom

type Atom string

Atom is equivalent to a string. In this inference engine, an atom can start with an upper case or a lower case letter. (Unlike Prolog.)

func (Atom) RecreateVariables

func (a Atom) RecreateVariables(m VarMap) Expression

RecreateVariables - creates unique variables every time the inference engine fetches a rule from the knowledge base. A constant is not a variable, so this function simply returns the constant. This function satisfies the Expression interface.

func (Atom) ReplaceVariables

func (a Atom) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables() is called after a solution has been found. It replaces logic variables with the constants which they are bound to, in order to display results. For constants, ReplaceVariables() simply returns the constant. This function satisfies the Expression interface.

func (Atom) String

func (a Atom) String() string

String - return this term as a string.

func (Atom) TermType

func (a Atom) TermType() int

TermType - Returns an integer constant which identifies this type.

func (Atom) Unify

func (a Atom) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - unifies an Atom with another term. If both terms are Atoms, and equal, then Unify succeeds. If they are not equal, Unify fails. If one of the terms is an unbound Variable, then Unify binds the Variable to the Atom, records the binding in the substitution set, and returns with success. If the Variable is already bound to a different Atom, Unify will fail.

type BIFTemplateStruct

type BIFTemplateStruct BuiltInPredicateStruct

Built-in functions and built-in predicates use the same struct.

func BIFTemplate

func BIFTemplate(arguments ...Unifiable) BIFTemplateStruct

BIFTemplate - creates the struct which defines this built-in function. Checks input arguments.

func (BIFTemplateStruct) RecreateVariables

func (s BIFTemplateStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (BIFTemplateStruct) ReplaceVariables

func (s BIFTemplateStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (BIFTemplateStruct) String

func (s BIFTemplateStruct) String() string

String - creates a string representation. Returns: function_name(arg1, arg2, arg3)

func (BIFTemplateStruct) TermType

func (s BIFTemplateStruct) TermType() int

TermType - returns a constant which identifies this type.

func (BIFTemplateStruct) Unify

Unify - unifies the result of a function with another term (usually a variable). Params:

other unifiable term
substitution set

Returns:

updated substitution set
success/failure flag

type BIPTemplateSolutionNodeStruct

type BIPTemplateSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

A solution node holds the current state of the search for a solution.

func (*BIPTemplateSolutionNodeStruct) GetParentNode

func (n *BIPTemplateSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*BIPTemplateSolutionNodeStruct) NextSolution

func (sn *BIPTemplateSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*BIPTemplateSolutionNodeStruct) SetNoBackTracking

func (sn *BIPTemplateSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type BIPTemplateStruct

type BIPTemplateStruct BuiltInPredicateStruct

func BIPTemplate

func BIPTemplate(arguments ...Unifiable) BIPTemplateStruct

BIPTemplate - creates the struct which defines this built-in predicate. Checks input arguments.

func (BIPTemplateStruct) GetSolver

func (s BIPTemplateStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (BIPTemplateStruct) RecreateVariables

func (s BIPTemplateStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (BIPTemplateStruct) ReplaceVariables

func (s BIPTemplateStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (BIPTemplateStruct) String

func (s BIPTemplateStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type BuiltInPredicateStruct

type BuiltInPredicateStruct struct {
	Name      string
	Arguments []Unifiable
}

func (BuiltInPredicateStruct) RecreateVariables

func (bips BuiltInPredicateStruct) RecreateVariables(vars VarMap) *BuiltInPredicateStruct

RecreateVariables - The scope of a logic variable is the rule in which it is defined. When the algorithm tries to solve a goal, it calls this function to ensure that the variables are unique. See comments in expression.go.

func (BuiltInPredicateStruct) ReplaceVariables

func (bips BuiltInPredicateStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - replaces a bound variable with its binding. This method is used for displaying final results. Refer to comments in expression.go.

func (BuiltInPredicateStruct) String

func (bips BuiltInPredicateStruct) String() string

String - creates a string for debugging purposes. Format: PredName(arg1, arg2, arg3)

type Complex

type Complex []Unifiable

func MakeQuery

func MakeQuery(terms ...Unifiable) Complex

func ParseComplex

func ParseComplex(str string) (Complex, error)

ParseComplex - parses a string to produce a complex term.

Example of usage:

c := ParseComplex("symptom(covid, fever)")

Important: Backslash is used to escape characters, such as the comma. For example:

c := ParseComplex("punctuation(comma, \\,)")

The backslash is doubled, because the Go compiler also interprets the backslash.

Params: string representation Return: complex term

error

func ParseQuery

func ParseQuery(str string) (Complex, error)

ParseQuery - creates a query (Complex term) from a text string, and ensures that all logic variables have unique IDs.

Params: string representation of query Return: query (Complex term)

error

func Solve

func Solve(query Complex, kb KnowledgeBase, ss SubstitutionSet) (solution Complex, failure string)

Solve - finds one solution for the given query. The solution is returned as a string. A second string indicates the reason for failure, as follows:

"" (success)
"No" (no solution)
"Other reason"

Note: This method only finds the first result. See SolveAll below. Params: query

knowledgebase
substitution set (previous bindings)

Returns: solution

reason for failure

func SolveAll

func SolveAll(query Complex, kb KnowledgeBase, ss SubstitutionSet) (solutions []Complex, failure string)

SolveAll - finds all solutions for the given query. The solutions are returned as a list of complex terms. A second return value indicates the reason for failure, as follows:

"" (success)
"No" (no solution)
"Other reason"

Params: query

knowledge base
substitution set (previous bindings)

Returns: solutions, failure

func (Complex) Arity

func (c Complex) Arity() int

Arity - Returns the arity of a complex term. address(Tokyo, Shinjuku, Takadanobaba) has an arity of 3.

func (Complex) GetFunctor

func (c Complex) GetFunctor() Atom

GetFunctor - The functor is the first term: [functor, term1, term2, term3] Return: functor as Atom

func (Complex) GetSolver

func (c Complex) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - returns a solution node for Complex terms.

func (Complex) GetTerm

func (c Complex) GetTerm(index int) Unifiable

GetTerm - Returns the indexed term. Term 0 is the functor. No error checking. Params: index Return: term as unifiable

func (Complex) Key

func (c Complex) Key() string

Key - Creates a key (functor/arity) for indexing the knowledge base. Eg. loves(Chandler, Monica) --> loves/2 Return: key as string

func (Complex) RecreateVariables

func (c Complex) RecreateVariables(vars VarMap) Expression

RecreateVariables - In Prolog, and in this inference engine, the scope of a logic variable is the rule or goal in which it is defined. When the algorithm tries to solve a goal, it calls this method to ensure that the variables are unique. See comments in expression.go.

func (Complex) ReplaceVariables

func (c Complex) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - replaces a bound variable with its binding. This method is used for displaying final results. Refer to comments in expression.go.

func (Complex) String

func (c Complex) String() string

String - returns a string representation of this complex term. For example: "owns(John, house)"

func (Complex) TermType

func (c Complex) TermType() int

TermType - Returns an integer constant which identifies the type. Return: COMPLEX

func (Complex) Unify

func (c Complex) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - Unifies this complex term with another unifiable expression. Please refer to unifiable.go.

type ComplexSolutionNodeStruct

type ComplexSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*ComplexSolutionNodeStruct) GetChild

GetChild - returns the child node.

func (*ComplexSolutionNodeStruct) GetParentNode

func (n *ComplexSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*ComplexSolutionNodeStruct) HasNextRule

func (n *ComplexSolutionNodeStruct) HasNextRule() bool

HasNextRule - returns true if the knowledge base contains untried rules for this node's goal. False otherwise.

func (*ComplexSolutionNodeStruct) NextRule

func (n *ComplexSolutionNodeStruct) NextRule() RuleStruct

NextRule - fetches the next rule from the database, according to ruleNumber. The method HasNextRule must called to ensure that a rule can be fetched from the knowledge base. If GetRule is called with invalid parameters, the knowledge base will panic.

func (*ComplexSolutionNodeStruct) NextSolution

func (n *ComplexSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - initiates or continues the search for a solution. If the search succeeds, the method returns the updated substitution set, and sets the success flag to true. If the search fails, the success flag is set to false.

func (*ComplexSolutionNodeStruct) SetNoBackTracking

func (n *ComplexSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag. This flag is used to implement Cuts.

type CountSolutionNodeStruct

type CountSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

A solution node holds the current state of the search for a solution.

func (*CountSolutionNodeStruct) GetParentNode

func (n *CountSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*CountSolutionNodeStruct) NextSolution

func (sn *CountSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*CountSolutionNodeStruct) SetNoBackTracking

func (sn *CountSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type CountStruct

type CountStruct BuiltInPredicateStruct

func Count

func Count(arguments ...Unifiable) CountStruct

Count - creates the struct which defines this built-in predicate. Checks input arguments.

func (CountStruct) GetSolver

func (s CountStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (CountStruct) RecreateVariables

func (s CountStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (CountStruct) ReplaceVariables

func (s CountStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (CountStruct) String

func (s CountStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type CutOp

type CutOp Operator

func Cut

func Cut(operands ...Goal) CutOp

Cut - creates a logical Cut operator.

func (CutOp) GetSolver

func (c CutOp) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for Cut operator.

func (CutOp) RecreateVariables

func (c CutOp) RecreateVariables(vars VarMap) Expression

RecreateVariables Refer to comments in expression.go.

func (CutOp) ReplaceVariables

func (c CutOp) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables Refer to comments in expression.go.

func (CutOp) String

func (c CutOp) String() string

String - Creates a string representation of this operator.

type CutSolutionNodeStruct

type CutSolutionNodeStruct SolutionNodeStruct

func (*CutSolutionNodeStruct) GetParentNode

func (n *CutSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*CutSolutionNodeStruct) NextSolution

func (n *CutSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NexSolution

func (*CutSolutionNodeStruct) SetNoBackTracking

func (n *CutSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag. This flag is used to implement Cuts.

type DivideStruct

type DivideStruct BuiltInPredicateStruct

func Divide

func Divide(arguments ...Unifiable) DivideStruct

Divide - creates a DivideStruct, which holds the function's name and arguments. Divide requires at least 2 arguments. Params: arguments (Unifiable) Return: DivideStruct

func (DivideStruct) RecreateVariables

func (ms DivideStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (DivideStruct) ReplaceVariables

func (ms DivideStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (DivideStruct) String

func (ms DivideStruct) String() string

String - creates a string representation. Returns: function_name(arg1, arg2, arg3)

func (DivideStruct) TermType

func (as DivideStruct) TermType() int

TermType - returns a constant which identifies this type.

func (DivideStruct) Unify

Unify - unifies the result of a function with another term (usually a variable).

Params:

other unifiable term
substitution set

Returns:

updated substitution set
success/failure flag

type EqualSolutionNodeStruct

type EqualSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*EqualSolutionNodeStruct) GetParentNode

func (sn *EqualSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode This function satisfies the SolutionNode interface.

func (*EqualSolutionNodeStruct) NextSolution

func (sn *EqualSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - compares two numbers, float or integer. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*EqualSolutionNodeStruct) SetNoBackTracking

func (sn *EqualSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type EqualStruct

type EqualStruct BuiltInPredicateStruct

func Equal

func Equal(arguments ...Unifiable) EqualStruct

Equal - creates a comparison predicate, EqualStruct, which holds the predicate's name and arguments. Equal requires 2 arguments, which can be Integers or Floats. Params: arguments (Unifiable) Return: EqualStruct

func ParseEqual

func ParseEqual(str string) (EqualStruct, bool)

ParseEqual - creates a EqualStruct from a string. If the string does not contain "<", the function returns with the success flag set to false. If there is an error in parsing one of the terms, the function causes a panic. Params:

string, eg.: $X < 18

Return:

equal predicate
success/failure flag

func (EqualStruct) GetSolver

func (s EqualStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (EqualStruct) RecreateVariables

func (eq EqualStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (EqualStruct) ReplaceVariables

func (eq EqualStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (EqualStruct) String

func (eq EqualStruct) String() string

String - creates a string representation of this comparison. For example: $X < 8. Returns: string representation

type ExcludeSolutionNodeStruct

type ExcludeSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*ExcludeSolutionNodeStruct) GetParentNode

func (n *ExcludeSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*ExcludeSolutionNodeStruct) NextSolution

func (sn *ExcludeSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*ExcludeSolutionNodeStruct) SetNoBackTracking

func (sn *ExcludeSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type ExcludeStruct

type ExcludeStruct BuiltInPredicateStruct

func Exclude

func Exclude(arguments ...Unifiable) ExcludeStruct

Exclude - creates an ExcludeStruct, which holds the name and arguments. Exclude requires 3 arguments. Params: arguments (Unifiable) Return: ExcludeStruct

func (ExcludeStruct) GetSolver

func (xs ExcludeStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for the Exclude predicate.

func (ExcludeStruct) RecreateVariables

func (xs ExcludeStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (ExcludeStruct) ReplaceVariables

func (xs ExcludeStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (ExcludeStruct) String

func (xs ExcludeStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type Expression

type Expression interface {

	// RecreateVariables - creates unique variables every time the
	// inference engine fetches a rule from the knowledge base.
	//
	// The scope of a variable is the rule in which it is defined.
	// For example, in the knowledge base we have:
	//
	//    grandparent($X, $Y) :- parent($X, $Z), parent($Z, $Y).
	//    parent($X, $Y) :- father($X, $Y).
	//    parent($X, $Y) :- mother($X, $Y).
	//    mother(Martha, Jackie).
	//    ... other facts and rules
	//
	// To find a solution for the goal 'grandparent(Frank, $X)',
	// the inference engine will fetch the rule parent/2 from the
	// knowledge base. Each time the rule is fetched, the variables
	// $X and $Y must be unique, different from the $X and $Y which
	// were previously fetched.
	//
	// A variable is identified by its print name and a unique id
	// number. For example, the first time the parent/2 rule is
	// fetched, the variable $X might become '$X_22'. The second
	// time it might become '$X_23'.
	//
	RecreateVariables(newVars VarMap) Expression

	// ReplaceVariables() is called after a solution has been found.
	// It replaces logic variables with the constants which they are
	// bound to, in order to display results.
	//
	// For example, consider the goal 'grandfather(Frank, $X)'.
	// If the substitution set (i.e. solution), has $X bound to $Y
	// bound to Cindy, $X would be replaced by Cindy to give:
	//
	//     grandfather(Frank, Cindy).
	//
	ReplaceVariables(ss SubstitutionSet) Expression

	// String - returns a string representation of the expression.
	String() string
}

type FailOp

type FailOp []Goal

func Fail

func Fail(operands ...Goal) FailOp

Fail - creates a logical Fail operator.

func (FailOp) GetSolver

func (f FailOp) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for Fail operator.

func (FailOp) RecreateVariables

func (c FailOp) RecreateVariables(vars VarMap) Expression

RecreateVariables Refer to comments in expression.go.

func (FailOp) ReplaceVariables

func (c FailOp) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables Refer to comments in expression.go.

func (FailOp) String

func (c FailOp) String() string

String - Creates a string representation of this operator.

type FailSolutionNodeStruct

type FailSolutionNodeStruct SolutionNodeStruct

func (*FailSolutionNodeStruct) GetParentNode

func (n *FailSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*FailSolutionNodeStruct) NextSolution

func (n *FailSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NexSolution The Fail operator always fails. Return false.

func (*FailSolutionNodeStruct) SetNoBackTracking

func (n *FailSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag.

type Float

type Float float64

-------------------------------------------- Define a 64 bit floating point number.

func (Float) RecreateVariables

func (f Float) RecreateVariables(m VarMap) Expression

RecreateVariables - creates unique variables every time the inference engine fetches a rule from the knowledge base. A constant is not a variable, so this function simply returns the constant. This function satisfies the Expression interface.

func (Float) ReplaceVariables

func (f Float) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables() is called after a solution has been found. It replaces logic variables with the constants which they are bound to, in order to display results. For constants, ReplaceVariables() simply returns the constant. This function satisfies the Expression interface.

func (Float) String

func (f Float) String() string

String - return this term as a string.

func (Float) TermType

func (f Float) TermType() int

TermType - Returns an integer constant which identifies this type.

func (Float) Unify

func (f Float) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - unifies a floating point number with another term. If both terms are floating point numbers, and equal, then Unify succeeds. If they are not equal, Unify fails. If one of the terms is an unbound Variable, then Unify binds the Variable to the floating point number, records the binding in the substitution set, and returns with success. If the Variable is already bound to a different floating point number, Unify will fail.

type Function

type Function interface {
	RecreateVariables(VarMap) Expression
	ReplaceVariables(SubstitutionSet) Expression
	String() string
	Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)
	TermType() int
}

func ParseFunction

func ParseFunction(str string) (Function, error)

ParseFunction - parses a string to produce a built-in Suiron function. ParseFunction is similar to ParseComplex in complex.go. Perhaps some consolidation could be done in future.

Example of usage:

c := ParseFunction("add(7, 9, 4)")

Params: string representation Return: built-in suiron function

error

type FunctorSolutionNodeStruct

type FunctorSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*FunctorSolutionNodeStruct) GetParentNode

func (n *FunctorSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*FunctorSolutionNodeStruct) NextSolution

func (sn *FunctorSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*FunctorSolutionNodeStruct) SetNoBackTracking

func (sn *FunctorSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type FunctorStruct

type FunctorStruct BuiltInPredicateStruct

func Functor

func Functor(arguments ...Unifiable) FunctorStruct

Functor - creates a FunctorStruct, which holds the predicate's name and arguments. Functor requires 2 or 3 arguments Params: arguments (Unifiable) Return: FunctorStruct

func (FunctorStruct) GetSolver

func (fs FunctorStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for Functor predicate.

func (FunctorStruct) RecreateVariables

func (fs FunctorStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (FunctorStruct) ReplaceVariables

func (fs FunctorStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (FunctorStruct) String

func (fs FunctorStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type Goal

type Goal interface {
	Expression

	// GetSolver - gets a solution node for the current goal.
	GetSolver(kb KnowledgeBase, parentSolution SubstitutionSet,
		parentNode SolutionNode) SolutionNode
}

func ParseSubgoal

func ParseSubgoal(subgoal string) (Goal, error)

ParseSubgoal

This function parses all subgoals. It returns a goal object, and an error.

The Not and Time operators are dealt with first, because they enclose subgoals. Eg.

not($X = $Y)
time(qsort)

Params: subgoal as string Return: subgoal as Goal object

error

func RecreateVariablesForOperators

func RecreateVariablesForOperators(op []Goal, vars VarMap) []Goal

RecreateVariables - The scope of a logic variable is the rule in which it is defined. This method satisfies the Expression and Goal interfaces. Refer to comments in expression.go.

func ReplaceVariablesForOperators

func ReplaceVariablesForOperators(op []Goal, ss SubstitutionSet) []Goal

ReplaceVariablesForOperators() Operators must implement ReplaceVariables(), in order to satisfy the Expression and Goal interfaces. Refer to comments in expression.go.

type GreaterThanOrEqualSolutionNodeStruct

type GreaterThanOrEqualSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*GreaterThanOrEqualSolutionNodeStruct) GetParentNode

GetParentNode This function satisfies the SolutionNode interface.

func (*GreaterThanOrEqualSolutionNodeStruct) NextSolution

NextSolution - compares two numbers, float or integer. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*GreaterThanOrEqualSolutionNodeStruct) SetNoBackTracking

func (sn *GreaterThanOrEqualSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type GreaterThanOrEqualStruct

type GreaterThanOrEqualStruct BuiltInPredicateStruct

func GreaterThanOrEqual

func GreaterThanOrEqual(arguments ...Unifiable) GreaterThanOrEqualStruct

GreaterThanOrEqual - creates a comparison predicate, GreaterThanOrEqualStruct, which holds the predicate's name and arguments. GreaterThanOrEqual requires 2 arguments, which can be Integers or Floats. Params: arguments (Unifiable) Return: GreaterThanOrEqualStruct

func ParseGreaterThanOrEqual

func ParseGreaterThanOrEqual(str string) (GreaterThanOrEqualStruct, bool)

ParseGreaterThanOrEqual - creates a GreaterThanOrEqualStruct from a string. If the string does not contain ">=", the function returns with the success flag set to false. If there is an error in parsing one of the terms, the function causes a panic. Params:

string, eg.: $X >= 18

Return:

greater-than-or-equal predicate
success/failure flag

func (GreaterThanOrEqualStruct) GetSolver

func (s GreaterThanOrEqualStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (GreaterThanOrEqualStruct) RecreateVariables

func (gte GreaterThanOrEqualStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (GreaterThanOrEqualStruct) ReplaceVariables

func (gte GreaterThanOrEqualStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (GreaterThanOrEqualStruct) String

func (gte GreaterThanOrEqualStruct) String() string

String - creates a string representation of this comparison. For example: $X >= 8. Returns: string representation

type GreaterThanSolutionNodeStruct

type GreaterThanSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*GreaterThanSolutionNodeStruct) GetParentNode

func (sn *GreaterThanSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode This function satisfies the SolutionNode interface.

func (*GreaterThanSolutionNodeStruct) NextSolution

func (sn *GreaterThanSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - compares two numbers, float or integer. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*GreaterThanSolutionNodeStruct) SetNoBackTracking

func (sn *GreaterThanSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type GreaterThanStruct

type GreaterThanStruct BuiltInPredicateStruct

func GreaterThan

func GreaterThan(arguments ...Unifiable) GreaterThanStruct

GreaterThan - creates a comparison predicate, GreaterThanStruct, which holds the predicate's name and arguments. GreaterThan requires 2 arguments, which can be Integers or Floats. Params: arguments (Unifiable) Return: GreaterThanStruct

func ParseGreaterThan

func ParseGreaterThan(str string) (GreaterThanStruct, bool)

ParseGreaterThan - creates a GreaterThanStruct from a string. If the string does not contain ">", the function returns with the success flag set to false. If there is an error in parsing one of the terms, the function causes a panic. Params:

string, eg.: $X > 18

Return:

greater-than predicate
success/failure flag

func (GreaterThanStruct) GetSolver

func (s GreaterThanStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (GreaterThanStruct) RecreateVariables

func (gte GreaterThanStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (GreaterThanStruct) ReplaceVariables

func (gte GreaterThanStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (GreaterThanStruct) String

func (gte GreaterThanStruct) String() string

String - creates a string representation of this comparison. For example: $X > 8. Returns: string representation

type IncludeSolutionNodeStruct

type IncludeSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*IncludeSolutionNodeStruct) GetParentNode

func (n *IncludeSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*IncludeSolutionNodeStruct) NextSolution

func (sn *IncludeSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*IncludeSolutionNodeStruct) SetNoBackTracking

func (sn *IncludeSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type IncludeStruct

type IncludeStruct BuiltInPredicateStruct

func Include

func Include(arguments ...Unifiable) IncludeStruct

Include - creates an IncludeStruct, which holds the name and arguments. Include requires 3 arguments. Params: arguments (Unifiable) Return: IncludeStruct

func (IncludeStruct) GetSolver

func (is IncludeStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for the Include predicate.

func (IncludeStruct) RecreateVariables

func (is IncludeStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (IncludeStruct) ReplaceVariables

func (is IncludeStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (IncludeStruct) String

func (is IncludeStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type IntStack

type IntStack []int

func (*IntStack) IsEmpty

func (s *IntStack) IsEmpty() bool

IsEmpty - check for empty stack.

func (*IntStack) Peek

func (s *IntStack) Peek() (int, bool)

Peek - returns the top integer on the stack. Does not pop integer. Return: top integer

success flag - true if the stack was not empty

func (*IntStack) Pop

func (s *IntStack) Pop() (int, bool)

Pop - pops an integer from the top of the stack. Return: popped integer

success flag - true if integer popped

func (*IntStack) Push

func (s *IntStack) Push(i int)

Push - pushes an integer onto the stack.

type Integer

type Integer int64

-------------------------------------------- Define a 64 bit integer.

func (Integer) RecreateVariables

func (i Integer) RecreateVariables(m VarMap) Expression

RecreateVariables - creates unique variables every time the inference engine fetches a rule from the knowledge base. A constant is not a variable, so this function simply returns the constant. This function satisfies the Expression interface.

func (Integer) ReplaceVariables

func (i Integer) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables() is called after a solution has been found. It replaces logic variables with the constants which they are bound to, in order to display results. For constants, ReplaceVariables() simply returns the constant. This function satisfies the Expression interface.

func (Integer) String

func (i Integer) String() string

String - return this term as a string.

func (Integer) TermType

func (i Integer) TermType() int

TermType - returns an integer constant which identifies this type.

func (Integer) Unify

func (i Integer) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - unifies an Integer with another term. If both terms are Integers, and equal, then Unify succeeds. If they are not equal, Unify fails. If one of the terms is an unbound Variable, then Unify binds the Variable to the Integer, records the binding in the substitution set, and returns with success. If the Variable is already bound to a different Integer, Unify will fail.

type JoinStruct

type JoinStruct BuiltInPredicateStruct

func Join

func Join(arguments ...Unifiable) JoinStruct

Join - creates a JoinStruct, which holds the function's name and arguments. Join requires at least 2 arguments. Params: arguments (Unifiable) Return: JoinStruct

func (JoinStruct) RecreateVariables

func (js JoinStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (JoinStruct) ReplaceVariables

func (js JoinStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (JoinStruct) String

func (js JoinStruct) String() string

String - creates a string representation. Returns: function_name(arg1, arg2, arg3)

func (JoinStruct) TermType

func (f JoinStruct) TermType() int

TermType - returns a constant which identifies this type. This function satisfies the Unifiable interface.

func (JoinStruct) Unify

func (js JoinStruct) Unify(other Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

Unify - unifies the result of a function with another term (usually a variable). Params:

other unifiable term
substitution set

Returns:

updated substitution set
success/failure flag

type KnowledgeBase

type KnowledgeBase map[string][]RuleStruct

A knowledge base is a dictionary indexed by a key. Each indexed item is a slice of rules and/or facts.

func (KnowledgeBase) Add

func (kb KnowledgeBase) Add(rules ...RuleStruct)

Add - adds facts and rules to the knowledge base. Eg. knowledgebase.Add(fact1, fact2, rule1, rule2)

func (KnowledgeBase) FormatKB

func (kb KnowledgeBase) FormatKB() string

FormatKB - formats the knowledge base facts and rules for display. This method is useful for diagnostics. The keys are sorted.

func (KnowledgeBase) GetRule

func (kb KnowledgeBase) GetRule(goal Goal, i int) RuleStruct

GetRule - fetches a rule (or fact) from the knowledge base. Rules are indexed by functor/arity (eg. sister/2) and by index number. The variables of the retrieved rule must be made unique, by calling recreateVariables().

type LessThanOrEqualSolutionNodeStruct

type LessThanOrEqualSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*LessThanOrEqualSolutionNodeStruct) GetParentNode

func (sn *LessThanOrEqualSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode This function satisfies the SolutionNode interface.

func (*LessThanOrEqualSolutionNodeStruct) NextSolution

NextSolution - compares two numbers, float or integer. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*LessThanOrEqualSolutionNodeStruct) SetNoBackTracking

func (sn *LessThanOrEqualSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type LessThanOrEqualStruct

type LessThanOrEqualStruct BuiltInPredicateStruct

func LessThanOrEqual

func LessThanOrEqual(arguments ...Unifiable) LessThanOrEqualStruct

LessThanOrEqual - creates a comparison predicate, LessThanOrEqualStruct, which holds the predicate's name and arguments. LessThanOrEqual requires 2 arguments, which can be Integers or Floats. Params: arguments (Unifiable) Return: LessThanOrEqualStruct

func ParseLessThanOrEqual

func ParseLessThanOrEqual(str string) (LessThanOrEqualStruct, bool)

ParseLessThanOrEqual - creates a LessThanOrEqualStruct from a string. If the string does not contain "<=", the function returns with the success flag set to false. If there is an error in parsing one of the terms, the function causes a panic. Params:

string, eg.: $X <= 18

Return:

less-than-or-equal predicate
success/failure flag

func (LessThanOrEqualStruct) GetSolver

func (s LessThanOrEqualStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (LessThanOrEqualStruct) RecreateVariables

func (lte LessThanOrEqualStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (LessThanOrEqualStruct) ReplaceVariables

func (lte LessThanOrEqualStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (LessThanOrEqualStruct) String

func (lte LessThanOrEqualStruct) String() string

String - creates a string representation of this comparison. For example: $X <= 8. Returns: string representation

type LessThanSolutionNodeStruct

type LessThanSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*LessThanSolutionNodeStruct) GetParentNode

func (sn *LessThanSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode This function satisfies the SolutionNode interface.

func (*LessThanSolutionNodeStruct) NextSolution

func (sn *LessThanSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - compares two numbers, float or integer. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*LessThanSolutionNodeStruct) SetNoBackTracking

func (sn *LessThanSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type LessThanStruct

type LessThanStruct BuiltInPredicateStruct

func LessThan

func LessThan(arguments ...Unifiable) LessThanStruct

LessThan - creates a comparison predicate, LessThanStruct, which holds the predicate's name and arguments. LessThan requires 2 arguments, which can be Integers or Floats. Params: arguments (Unifiable) Return: LessThanStruct

func ParseLessThan

func ParseLessThan(str string) (LessThanStruct, bool)

ParseLessThan - creates a LessThanStruct from a string. If the string does not contain "<", the function returns with the success flag set to false. If there is an error in parsing one of the terms, the function causes a panic. Params:

string, eg.: $X < 18

Return:

less-than predicate
success/failure flag

func (LessThanStruct) GetSolver

func (s LessThanStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface.

func (LessThanStruct) RecreateVariables

func (lt LessThanStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (LessThanStruct) ReplaceVariables

func (lt LessThanStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (LessThanStruct) String

func (lt LessThanStruct) String() string

String - creates a string representation of this comparison. For example: $X < 8. Returns: string representation

type LinkedListStruct

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

func EmptyList

func EmptyList() LinkedListStruct

EmptyList - returns an empty LinkedList.

func MakeLinkedList

func MakeLinkedList(vbar bool, args ...Unifiable) LinkedListStruct

MakeLinkedList - makes a singly-linked list, such as [a, b, c]

list := MakeLinkedList(false, a, b, c)

The first parameter, vbar, is set true for lists which have a tail variable, such as [a, b, c | $Tail]

list := MakeLinkedList(true, a, b, c, $Tail)

Params: vbar - vertical bar flag

args - list of unifiable arguments

Return: linked list

func ParseLinkedList

func ParseLinkedList(str string) (LinkedListStruct, error)

ParseLinkedList - parses a string to create a linked list. For example,

list, err := ParseLinkedList("[a, b, c | $X]")

Produces an error if the string is invalid.

func (LinkedListStruct) Flatten

func (ll LinkedListStruct) Flatten(numOfTerms int, ss SubstitutionSet) ([]Unifiable, bool)

Flatten - partially flattens this linked list. If the number of terms requested is two, this function will return a slice of the first and second terms, and the tail of the linked list. In other words, the list [a, b, c, d] becomes a slice containing a, b, and the linked list [c, d]. The function returns the resulting slice and a boolean to indicate success or failure.

func (LinkedListStruct) GetCount

func (ll LinkedListStruct) GetCount() int

GetCount - returns the number of items in the list.

func (LinkedListStruct) GetNext

func (ll LinkedListStruct) GetNext() *LinkedListStruct

GetNext - returns pointer to the rest of the list.

func (LinkedListStruct) GetTerm

func (ll LinkedListStruct) GetTerm() Unifiable

GetTerm - returns the top term of this list.

func (LinkedListStruct) RecreateVariables

func (ll LinkedListStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - The scope of a logic variable is the rule or goal in which it is defined. When the algorithm tries to solve a goal, it calls this method to ensure that the variables are unique. See comments in expression.go.

func (LinkedListStruct) ReplaceVariables

func (ll LinkedListStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - replaces a bound variable with its binding. This method is used for displaying final results. Refer to comments in expression.go.

func (LinkedListStruct) String

func (ll LinkedListStruct) String() string

String - returns a string representation of this list.

func (LinkedListStruct) TermType

func (ll LinkedListStruct) TermType() int

TermType - Returns an integer constant which identifies the type. This function satisfies the Unifiable interface.

func (LinkedListStruct) Unify

Unify - unifies this LinkedList with a Variable or another LinkedList. Two lists can unify if they have the same number of items, and each corresponding pair of items can unify. Or, if one of the lists ends in a tail Variable (eg. [a, b, | $X]), the tail Variable can unify with the remainder of the other list. The method returns an updated substitution set and a boolean flag which indicates success or failure. Please refer to unifiable.go.

type MultiplyStruct

type MultiplyStruct BuiltInPredicateStruct

func Multiply

func Multiply(arguments ...Unifiable) MultiplyStruct

Multiply - creates a MultiplyStruct, which holds the function's name and arguments. Multiply requires at least 2 arguments. Params: arguments (Unifiable) Return: MultiplyStruct

func (MultiplyStruct) RecreateVariables

func (ms MultiplyStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (MultiplyStruct) ReplaceVariables

func (ms MultiplyStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (MultiplyStruct) String

func (ms MultiplyStruct) String() string

String - creates a string representation. Returns: function_name(arg1, arg2, arg3)

func (MultiplyStruct) TermType

func (as MultiplyStruct) TermType() int

TermType - returns a constant which identifies this type.

func (MultiplyStruct) Unify

Unify - unifies the result of a function with another term (usually a variable).

Params:

other unifiable term
substitution set

Returns:

updated substitution set
success/failure flag

type NewLineSolutionNodeStruct

type NewLineSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*NewLineSolutionNodeStruct) GetParentNode

func (n *NewLineSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*NewLineSolutionNodeStruct) NextSolution

func (nlsn *NewLineSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - simply prints out a new line character. This function satisfies the SolutionNode interface.

func (*NewLineSolutionNodeStruct) SetNoBackTracking

func (nlsn *NewLineSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type NewLineStruct

type NewLineStruct BuiltInPredicateStruct

func NL

func NL() NewLineStruct

NL - creates a predicate which outputs a new-line code. No arguments are needed.

func (NewLineStruct) GetSolver

func (nls NewLineStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for new line predicates.

func (NewLineStruct) RecreateVariables

func (nls NewLineStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (NewLineStruct) ReplaceVariables

func (nls NewLineStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (NewLineStruct) String

func (nls NewLineStruct) String() string

String - creates a string representation.

type NotOp

type NotOp Operator

func Not

func Not(operands ...Goal) NotOp

Not - creates an NotOp type, which holds the operator's operand. Params: operands (Goal) Return: NotOp

func (NotOp) GetSolver

func (n NotOp) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for the Not operator.

func (NotOp) RecreateVariables

func (n NotOp) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (NotOp) ReplaceVariables

func (n NotOp) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (NotOp) String

func (n NotOp) String() string

String - Creates a string for debugging purposes.

type NotSolutionNodeStruct

type NotSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*NotSolutionNodeStruct) GetParentNode

func (n *NotSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*NotSolutionNodeStruct) HasNextRule

func (n *NotSolutionNodeStruct) HasNextRule() bool

HasNextRule - returns true if the knowledge base contains untried rules for this node's goal. False otherwise.

func (*NotSolutionNodeStruct) NextRule

func (n *NotSolutionNodeStruct) NextRule() RuleStruct

NextRule - fetches the next rule from the database, according to ruleNumber. The method HasNextRule must be called to ensure that a rule can be fetched from the knowledge base. If GetRule is called with invalid parameters, the knowledge base will panic.

func (*NotSolutionNodeStruct) NextSolution

func (n *NotSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls NextSolution() on the operand (which is a Goal). If there is a solution, the function will set the success flag to false. If there is no solution, the function will set the success flag to true. ('Not' means 'not unifiable'.) Returns: substitution set

success/failure flag

func (*NotSolutionNodeStruct) SetNoBackTracking

func (n *NotSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - rset the NoBackTracking flag. This flag is used to implement Cuts.

type Operator

type Operator []Goal

An operator consists of a list of operands (= Goals).

type OrOp

type OrOp Operator

func Or

func Or(operands ...Goal) OrOp

Or - creates a logical Or operator. Params: operands

func (OrOp) GetSolver

func (o OrOp) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for Or operator.

func (OrOp) RecreateVariables

func (o OrOp) RecreateVariables(vars VarMap) Expression

RecreateVariables - Operators must implement RecreateVariables(), in order to satisfy the Expression and Goal interfaces. Refer to comments in expression.go.

func (OrOp) ReplaceVariables

func (o OrOp) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Operators must implement ReplaceVariables(), in order to satisfy the Expression and Goal interfaces. Refer to comments in expression.go.

func (OrOp) String

func (o OrOp) String() string

String - Creates a string for debugging purposes.

type OrSolutionNodeStruct

type OrSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*OrSolutionNodeStruct) GetParentNode

func (n *OrSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*OrSolutionNodeStruct) HasNextRule

func (o *OrSolutionNodeStruct) HasNextRule() bool

HasNextRule - returns true if the knowledge base contains untried rules for this node's goal. False otherwise.

func (*OrSolutionNodeStruct) NextRule

func (o *OrSolutionNodeStruct) NextRule() RuleStruct

NextRule - fetches the next rule from the database, according to ruleNumber. The method HasNextRule must be called to ensure that a rule can be fetched from the knowledge base. If GetRule is called with invalid parameters, the knowledge base will panic.

func (*OrSolutionNodeStruct) NextSolution

func (o *OrSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NexSolution - recursively calls NextSolution on all subgoals. If the search succeeds, the boolean return value is true, and the substitution set is updated. If the search fails, the boolean value is false.

func (*OrSolutionNodeStruct) SetNoBackTracking

func (o *OrSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag. This flag is used to implement Cuts.

type PrintListSolutionNodeStruct

type PrintListSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*PrintListSolutionNodeStruct) GetParentNode

func (n *PrintListSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*PrintListSolutionNodeStruct) NextSolution

func (sn *PrintListSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*PrintListSolutionNodeStruct) SetNoBackTracking

func (sn *PrintListSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type PrintListStruct

type PrintListStruct BuiltInPredicateStruct

func PrintList

func PrintList(arguments ...Unifiable) PrintListStruct

PrintList - creates a print_list predicate.

func (PrintListStruct) GetSolver

func (pls PrintListStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for PrintList predicate.

func (PrintListStruct) RecreateVariables

func (pls PrintListStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (PrintListStruct) ReplaceVariables

func (pls PrintListStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (PrintListStruct) String

func (pls PrintListStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type PrintSolutionNodeStruct

type PrintSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*PrintSolutionNodeStruct) GetParentNode

func (n *PrintSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*PrintSolutionNodeStruct) NextSolution

func (sn *PrintSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls a function to evaluate the current goal, based on its arguments and the substitution set. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*PrintSolutionNodeStruct) SetNoBackTracking

func (sn *PrintSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type PrintStruct

type PrintStruct BuiltInPredicateStruct

func Print

func Print(arguments ...Unifiable) PrintStruct

Print - creates a print predicate. This function is variadic.

func (PrintStruct) GetSolver

func (ps PrintStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets solution node for Print predicate.

func (PrintStruct) RecreateVariables

func (ps PrintStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (PrintStruct) ReplaceVariables

func (ps PrintStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (PrintStruct) String

func (ps PrintStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1, arg2, arg3)

type RuleStruct

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

head :- body.

func Fact

func Fact(head Complex) RuleStruct

Fact - Factory function to create a Fact.

func ParseRule

func ParseRule(str string) (RuleStruct, error)

ParseRule - create a fact or rule from a string representation. Examples of usage:

c := ParseRule("male(Harold).")
c := ParseRule("father($X, $Y) :- parent($X, $Y), male($X).")

func Rule

func Rule(head Complex, body Goal) RuleStruct

Rule - Factory function to create a Rule.

func (RuleStruct) GetBody

func (r RuleStruct) GetBody() Goal

GetBody - returns the body of this rule, which is Goal type.

func (RuleStruct) GetHead

func (r RuleStruct) GetHead() Complex

GetHead - returns the head of this rule, which is Complex type.

func (RuleStruct) Key

func (r RuleStruct) Key() string

Key - generates a key from the head term. Eg. loves(Chandler, Monica) --> loves/2

func (RuleStruct) RecreateVariables

func (r RuleStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - In Prolog, and in this inference engine, the scope of a logic variable is the rule or goal in which it is defined. When the algorithm tries to solve a goal, it calls this method to ensure that the variables are unique. See comments in expression.go.

func (RuleStruct) ReplaceVariables

func (r RuleStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - replaces a bound variable with its binding. This method is used for displaying final results. Refer to comments in expression.go.

func (RuleStruct) String

func (r RuleStruct) String() string

String - displays a rule in its source form, eg. father(Anakin, Luke).

type SolutionNode

type SolutionNode interface {
	NextSolution() (SubstitutionSet, bool)
	SetNoBackTracking()
	GetParentNode() SolutionNode
}

func MakeComplexSolutionNode

func MakeComplexSolutionNode(g Complex, kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

func MakeCutSolutionNode

func MakeCutSolutionNode(c CutOp, kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

func MakeFailSolutionNode

func MakeFailSolutionNode(f FailOp, kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

func MakeTimeSolutionNode

func MakeTimeSolutionNode(goal Goal, kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

type SolutionNodeStruct

type SolutionNodeStruct struct {
	KnowledgeBase  KnowledgeBase
	ParentSolution SubstitutionSet
	ParentNode     SolutionNode
	NoBackTracking bool

	Goal Goal // goal being solved
	// contains filtered or unexported fields
}

func MakeSolutionNode

func MakeSolutionNode(goal Goal, kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNodeStruct

MakeSolutionNode - makes a solution node with the given arguments: Params:

goal
knowledgebase
parent solution (substitution set)
solution node of parent

Return:

a solution node struct

type SubstitutionSet

type SubstitutionSet []*Unifiable

func (SubstitutionSet) CastAtom

func (ss SubstitutionSet) CastAtom(term Unifiable) (Atom, bool)

CastAtom - if the given Unifiable term is an Atom, cast it as an Atom and return it. If it is a Variable, get the ground term. If that term is an Atom, cast it as an Atom and return it. Otherwise fail.

Params: Unifiable term Return: Atom

success/failure flag

func (SubstitutionSet) CastComplex

func (ss SubstitutionSet) CastComplex(term Unifiable) (Complex, bool)

CastComplex - if the given Unifiable term is a Complex term, cast it as Complex and return it. If the given term is a Variable, get the ground term. If the ground term is a Complex term, cast it and return it. Otherwise fail. Params: Unifiable term Return: Complex term

success/failure flag

func (SubstitutionSet) CastLinkedList

func (ss SubstitutionSet) CastLinkedList(term Unifiable) (LinkedListStruct, bool)

CastLinkedList - if the given Unifiable term is a linked list, cast it as a LinkedList and return it. If it is a Variable, get the ground term. If that term is a linked list, cast it as a LinkedList and return it. Otherwise fail.

Params: Unifiable term Return: linked list

success/failure flag

func (SubstitutionSet) GetBinding

func (ss SubstitutionSet) GetBinding(v VariableStruct) (*Unifiable, error)

GetBinding() - Returns the binding of a logic variable. If there is no binding, return an error. Params: logic variable Return: bound term

error

func (SubstitutionSet) GetGroundTerm

func (ss SubstitutionSet) GetGroundTerm(u Unifiable) (Unifiable, bool)

GetGroundTerm - if the given term is a ground term, return it. If it's a variable, try to get its ground term. If the variable is bound to a ground term, return the term and set the success flag to true. Otherwise, return the variable and set the success flag to false. Params: term Return: ground term

success/failure flag

func (SubstitutionSet) IsBound

func (ss SubstitutionSet) IsBound(v VariableStruct) bool

IsBound() - A logic variable is bound if there exists an entry for it in the substitution set. Params: logic variable Return: true/false

func (SubstitutionSet) IsGroundVariable

func (ss SubstitutionSet) IsGroundVariable(v VariableStruct) bool

IsGroundVariable - A variable is 'ground' if it is ultimately bound to something other than a variable. Params: logic variable Return: true/false

func (SubstitutionSet) String

func (ss SubstitutionSet) String() string

String - creates a string representation of the substitution set, for debugging purposes.

type SubtractStruct

type SubtractStruct BuiltInPredicateStruct

func Subtract

func Subtract(arguments ...Unifiable) SubtractStruct

Subtract - creates a SubtractStruct, which holds the function's name and arguments. Subtract requires at least 2 arguments. Params: arguments (Unifiable) Return: SubtractStruct

func (SubtractStruct) RecreateVariables

func (sbs SubtractStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (SubtractStruct) ReplaceVariables

func (sbs SubtractStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (SubtractStruct) String

func (sbs SubtractStruct) String() string

String - creates a string representation. Returns: function_name(arg1, arg2, arg3)

func (SubtractStruct) TermType

func (as SubtractStruct) TermType() int

TermType - returns a constant which identifies this type.

func (SubtractStruct) Unify

Unify - unifies the result of a function with another term (usually a variable).

Params:

other unifiable term
substitution set

Returns:

updated substitution set
success/failure flag

type TimeSolutionNodeStruct

type TimeSolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*TimeSolutionNodeStruct) GetParentNode

func (n *TimeSolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*TimeSolutionNodeStruct) HasNextRule

func (n *TimeSolutionNodeStruct) HasNextRule() bool

HasNextRule - returns true if the knowledge base contains untried rules for this node's goal. False otherwise.

func (*TimeSolutionNodeStruct) NextRule

func (n *TimeSolutionNodeStruct) NextRule() RuleStruct

NextRule - fetches the next rule from the database, according to ruleNumber. The method HasNextRule must be called to ensure that a rule can be fetched from the knowledge base. If GetRule is called with invalid parameters, the knowledge base will panic.

func (*TimeSolutionNodeStruct) NextSolution

func (n *TimeSolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution If the search fails, the boolean value is false.

func (*TimeSolutionNodeStruct) SetNoBackTracking

func (n *TimeSolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - rset the NoBackTracking flag. This flag is used to implement Cuts.

type TimeStruct

type TimeStruct BuiltInPredicateStruct

func Time

func Time(arguments ...Unifiable) TimeStruct

Time - creates a TimeStruct, which holds the predicate's name and arguments. It accepts only one argument, which must be a complex term. Params: 1 complex term Return: TimeStruct

func (TimeStruct) GetSolver

func (ts TimeStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for the Time predicate.

func (TimeStruct) RecreateVariables

func (ts TimeStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (TimeStruct) ReplaceVariables

func (ts TimeStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (TimeStruct) String

func (ts TimeStruct) String() string

String - creates a string representation. Returns: predicate_name(arg1)

type TokenStruct

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

TokenStruct is a node in a token tree.

func TokenBranch

func TokenBranch(theType int, children []TokenStruct) TokenStruct

TokenBranch - produces a branch node (TokenStruct) with the given child nodes. Valid branch node types are: GROUP, AND, OR. Param: type of node

child nodes

Return: a branch (parent) node

func TokenLeaf

func TokenLeaf(str string) TokenStruct

TokenLeaf - produces a leaf node (TokenStruct) for the given string. Valid leaf node types are: COMMA, SEMICOLON, LPAREN, RPAREN, SUBGOAL.

Param: symbol or subgoal (string) Return: token (TokenStruct)

func Tokenize

func Tokenize(str string) ([]TokenStruct, error)

Tokenize - Divides the given string into a series of tokens.

Note: Parentheses can be part of a complex term: likes(Charles, Gina) or used to group terms: (father($_, $X); mother($_, $X))

Params: string to parse Return: tokens

error

func (TokenStruct) String

func (ts TokenStruct) String() string

String - produces a string representation of the node for debugging. Eg.

SUBGOAL > sister(Janelle, Amanda)

Return: printable string

type Unifiable

type Unifiable interface {
	Expression

	// Unify - unifies two terms, if possible. If a variable is
	// unified (bound) to another term, the binding is recorded
	// in the substitution set.
	//
	// Parameters:
	//     other unifiable term
	//     substitution set
	// Returns:
	//     new substitution set
	//     flag, true = success, false = failure
	Unify(u Unifiable, ss SubstitutionSet) (SubstitutionSet, bool)

	// TermType - returns an integer which defines the type of expression.
	// Eg. ATOM, INTEGER, etc.
	TermType() int
}

type UnifySolutionNodeStruct

type UnifySolutionNodeStruct struct {
	SolutionNodeStruct
	// contains filtered or unexported fields
}

func (*UnifySolutionNodeStruct) GetParentNode

func (n *UnifySolutionNodeStruct) GetParentNode() SolutionNode

GetParentNode

func (*UnifySolutionNodeStruct) NextSolution

func (sn *UnifySolutionNodeStruct) NextSolution() (SubstitutionSet, bool)

NextSolution - calls Unify to attempt to unify two terms. Returns:

updated substitution set
success/failure flag

This function satisfies the SolutionNode interface.

func (*UnifySolutionNodeStruct) SetNoBackTracking

func (sn *UnifySolutionNodeStruct) SetNoBackTracking()

SetNoBackTracking - set the NoBackTracking flag, which is used to implement Cuts. This function satisfies the SolutionNode interface.

type UnifyStruct

type UnifyStruct BuiltInPredicateStruct

func ParseUnify

func ParseUnify(str string) (UnifyStruct, bool)

ParseUnify - creates a logical Unify predicate from a string. If the string does not contain "=", the function returns with the success flag set to false. If there is an error in parsing one of the terms, the function throws a panic. Params:

string, eg.: $X = verb

Return:

unify predicate
success/failure flag

func Unify

func Unify(arguments ...Unifiable) UnifyStruct

Unify - creates a unification predicate (UnifyStruct).

func (UnifyStruct) GetSolver

func (s UnifyStruct) GetSolver(kb KnowledgeBase,
	parentSolution SubstitutionSet,
	parentNode SolutionNode) SolutionNode

GetSolver - gets a solution node for this predicate. This function satisfies the Goal interface. Params: knowledge base

parentSolution
parentNode

Return: solutionNode

func (UnifyStruct) RecreateVariables

func (us UnifyStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - Refer to comments in expression.go.

func (UnifyStruct) ReplaceVariables

func (us UnifyStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - Refer to comments in expression.go.

func (UnifyStruct) String

func (us UnifyStruct) String() string

String - creates a string representation. Returns: "arg1 = arg2"

type VarMap

type VarMap map[string]VariableStruct

VarMap defines a map which is used for RecreateVariables, to keep track of previously recreated variables.

type VariableStruct

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

func LogicVar

func LogicVar(str string) (VariableStruct, error)

LogicVar - Factory function to create a logic Variable from a string. The variable must begin with a dollar sign and a letter. Eg. $X If it does not, a error is produced.

func (VariableStruct) ID

func (v VariableStruct) ID() int

ID - Returns the ID number of the given variable.

func (VariableStruct) RecreateVariables

func (v VariableStruct) RecreateVariables(vars VarMap) Expression

RecreateVariables - The scope of a logic variable is the rule or goal in which it is defined. When the algorithm tries to solve a goal, it calls this method to ensure that the variables are unique. See comments in expression.go. Note: This method creates variables from previously validated variables, so there is no need to validate the variable name by calling LogicVar(). Params: map of previously recreated variables Return: new variable (as Expression)

func (VariableStruct) ReplaceVariables

func (v VariableStruct) ReplaceVariables(ss SubstitutionSet) Expression

ReplaceVariables - replaces a bound variable with its binding. This method is used for displaying final results. Refer to comments in expression.go.

func (VariableStruct) String

func (v VariableStruct) String() string

String - return this term as a string.

func (VariableStruct) TermType

func (v VariableStruct) TermType() int

TermType - Returns an integer constant which identifies the type.

func (VariableStruct) Unify

Unify - Unifies this variable with another unifiable expression (if this variable is not already bound). Please refer to unifiable.go.

Jump to

Keyboard shortcuts

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