-
Notifications
You must be signed in to change notification settings - Fork 0
/
knowledge_base.go
122 lines (105 loc) · 3.2 KB
/
knowledge_base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2019 Melvin Davis<hi@melvindavis.me>. All rights reserved.
// Use of this source code is governed by a Melvin Davis<hi@melvindavis.me>
// license that can be found in the LICENSE file.
package interpreter
import "encoding/json"
/*
* This file contains the definition of knowledge base type node
*/
//KnowledgeBaseType indicates the type of the knowledgebase
type KnowledgeBaseType uint
const (
//SystemKB stands for the knowledgebase of the system
SystemKB KnowledgeBaseType = 1
//UserKB stands for the knowledgebase of the user
UserKB KnowledgeBaseType = 2
)
//KnowledgeBaseNode can store info about any node. It doesn't have a parent node
type KnowledgeBaseNode struct {
//UID is the unique id of the node
UID string
//Word is the word with which the knowledge base node has to be matched
Word []rune
//Name is the name of the node
Name string
//Children are the children node of the node
Children []Node
//Resolved indicates that the node is resolved
Resolved bool
//Description of the node
Description string
//KBType indicates the type of the knowledgebase
KBType KnowledgeBaseType
}
type knowledgebaseNode struct {
UID string `json:"uid,omitempty"`
Word string `json:"word,omitempty"`
Name string `json:"name,omitempty"`
Children []Node `json:"children,omitempty"`
Resolved bool `json:"resolved,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description"`
KBType KnowledgeBaseType `json:"kb_type"`
}
//Copy will return a copy of the node
func (k *KnowledgeBaseNode) Copy() Node {
return &KnowledgeBaseNode{
UID: k.UID,
Word: k.Word,
Name: k.Name,
Children: k.Children,
Resolved: k.Resolved,
Description: k.Description,
KBType: k.KBType,
}
}
//ID returns the unique id of the node
func (k *KnowledgeBaseNode) ID() string {
return k.UID
}
//Type returns Table Type
func (k *KnowledgeBaseNode) Type() Type {
return KnowledgeBase
}
//TokenWord returns the word property of the node
func (k *KnowledgeBaseNode) TokenWord() []rune {
return k.Word
}
//PID returns the PUID if the node
func (k *KnowledgeBaseNode) PID() string {
return ""
}
//Parent returns the PN of the node
func (k *KnowledgeBaseNode) Parent() Node {
return nil
}
//MarshalJSON encodes the node into a serializable json
func (k *KnowledgeBaseNode) MarshalJSON() ([]byte, error) {
return json.Marshal(&knowledgebaseNode{
k.UID, string(k.Word), k.Name, k.Children, k.Resolved, "KnowledgeBase", k.Description, k.KBType,
})
}
//UnmarshalJSON decodes the node from a json
func (k *KnowledgeBaseNode) UnmarshalJSON(data []byte) error {
m := &knowledgebaseNode{}
err := json.Unmarshal(data, m)
if err != nil {
return err
}
k.UID = m.UID
k.Word = []rune(m.Word)
k.Name = m.Name
k.Children = m.Children
k.Resolved = m.Resolved
k.Description = m.Description
k.KBType = m.KBType
return nil
}
//IsResolved will return true if the node is resolved
func (k *KnowledgeBaseNode) IsResolved() bool {
return k.Resolved
}
//SetResolved will set the resolved state of the node
func (k *KnowledgeBaseNode) SetResolved(state bool) {
k.Resolved = state
}