-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
109 lines (93 loc) · 2.49 KB
/
time.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
// 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"
"github.com/cuttle-ai/octopus/datetime"
)
/*
* This file contains the defnition of time type node
*/
//TimeNode is the node storing the information about a time.
type TimeNode struct {
//UID is the unique id of the time node
UID string
//Word is the word with which the time node has to be matched
Word []rune
//PUID is the UID of time's parent node
PUID string
//PN is the parent node of the time. It will be a KnowledgeBase
PN Node
//Value is the value of the timenode
Value datetime.Value
//Resolved indicates that the node is resolved
Resolved bool
}
type timeNode struct {
UID string `json:"uid,omitempty"`
Word string `json:"word,omitempty"`
PUID string `json:"puid,omitempty"`
Resolved bool `json:"resolved,omitempty"`
Value datetime.Value `json:"value,omitempty"`
Type string `json:"type,omitempty"`
}
//Copy will return a copy of the node
func (t *TimeNode) Copy() Node {
return &TimeNode{
UID: t.UID,
Word: t.Word,
PN: t.PN,
PUID: t.PUID,
Resolved: t.Resolved,
Value: t.Value,
}
}
//ID returns the unique id of the node
func (t *TimeNode) ID() string {
return t.UID
}
//Type returns Unknown Type
func (t *TimeNode) Type() Type {
return Time
}
//TokenWord returns the word property of the node
func (t *TimeNode) TokenWord() []rune {
return t.Word
}
//PID returns the PUID if the node
func (t *TimeNode) PID() string {
return t.PUID
}
//Parent returns the PN of the node
func (t *TimeNode) Parent() Node {
return t.PN
}
//MarshalJSON encodes the node into a serializable json
func (t *TimeNode) MarshalJSON() ([]byte, error) {
return json.Marshal(&timeNode{
t.UID, string(t.Word), t.PUID, t.Resolved, t.Value, "Time",
})
}
//UnmarshalJSON decodes the node from a json
func (t *TimeNode) UnmarshalJSON(data []byte) error {
m := &timeNode{}
err := json.Unmarshal(data, m)
if err != nil {
return err
}
t.UID = m.UID
t.Word = []rune(m.Word)
t.PUID = m.PUID
t.Resolved = m.Resolved
t.Value = m.Value
return nil
}
//IsResolved will return true if the node is resolved
func (t *TimeNode) IsResolved() bool {
return t.Resolved
}
//SetResolved will set the resolved state of the node
func (t *TimeNode) SetResolved(state bool) {
t.Resolved = state
}