diff --git a/x/logic/predicate/atom.go b/x/logic/predicate/atom.go index e4b06a9e..88048e0e 100644 --- a/x/logic/predicate/atom.go +++ b/x/logic/predicate/atom.go @@ -7,3 +7,35 @@ import ( // AtomPair are terms with principal functor (-)/2. // For example, the term -(A, B) denotes the pair of elements A and B. var AtomPair = engine.NewAtom("-") + +// AtomJSON are terms with principal functor json/1. +// It is used to represent json objects. +var AtomJSON = engine.NewAtom("json") + +// AtomAt are terms with principal functor (@)/1. +// It is used to represent special values in json objects. +var AtomAt = engine.NewAtom("@") + +// AtomTrue is the term true. +var AtomTrue = engine.NewAtom("true") + +// AtomFalse is the term false. +var AtomFalse = engine.NewAtom("false") + +// AtomNull is the term null. +var AtomNull = engine.NewAtom("null") + +// MakeNull returns the compound term @(null). +// It is used to represent the null value in json objects. +func MakeNull() engine.Term { + return AtomAt.Apply(AtomNull) +} + +// MakeBool returns the compound term @(true) if b is true, otherwise @(false). +func MakeBool(b bool) engine.Term { + if b { + return AtomAt.Apply(AtomTrue) + } + + return AtomAt.Apply(AtomFalse) +} diff --git a/x/logic/predicate/json.go b/x/logic/predicate/json.go index aeb615d6..19e1e94a 100644 --- a/x/logic/predicate/json.go +++ b/x/logic/predicate/json.go @@ -17,9 +17,6 @@ import ( "github.com/okp4/okp4d/x/logic/util" ) -// AtomJSON is a term which represents a json as a compound term `json([Pair])`. -var AtomJSON = engine.NewAtom("json") - // JSONProlog is a predicate that will unify a JSON string into prolog terms and vice versa. // // json_prolog(?Json, ?Term) is det @@ -125,11 +122,11 @@ func termsToJSON(term engine.Term, env *engine.Env) ([]byte, error) { } switch { - case AtomBool(true).Compare(t, env) == 0: + case MakeBool(true).Compare(t, env) == 0: return json.Marshal(true) - case AtomBool(false).Compare(t, env) == 0: + case MakeBool(false).Compare(t, env) == 0: return json.Marshal(false) - case AtomNull.Compare(t, env) == 0: + case MakeNull().Compare(t, env) == 0: return json.Marshal(nil) } @@ -153,9 +150,9 @@ func jsonToTerms(value any) (engine.Term, error) { } return engine.Integer(r.Int64()), nil case bool: - return AtomBool(v), nil + return MakeBool(v), nil case nil: - return AtomNull, nil + return MakeNull(), nil case map[string]any: keys := lo.Keys(v) sort.Strings(keys) diff --git a/x/logic/predicate/util.go b/x/logic/predicate/util.go index 73a2e9dd..4718f3ee 100644 --- a/x/logic/predicate/util.go +++ b/x/logic/predicate/util.go @@ -78,18 +78,6 @@ func ListToBytes(terms engine.ListIterator, env *engine.Env) ([]byte, error) { return bt, nil } -func AtomBool(b bool) engine.Term { - var r engine.Atom - if b { - r = engine.NewAtom("true") - } else { - r = engine.NewAtom("false") - } - return engine.NewAtom("@").Apply(r) -} - -var AtomNull = engine.NewAtom("@").Apply(engine.NewAtom("null")) - // ExtractJSONTerm is an utility function that would extract all attribute of a JSON object // that is represented in prolog with the `json` atom. //