Skip to content
Snippets Groups Projects
Unverified Commit 6b621d34 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

get embedded rules.

parent f29ea787
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package rules package rules
import ( import (
"embed"
"errors" "errors"
"fmt" "fmt"
...@@ -13,67 +14,8 @@ type Engine struct { ...@@ -13,67 +14,8 @@ type Engine struct {
p *prolog.Interpreter p *prolog.Interpreter
} }
var ( //go:embed source/*
prologKinds = ` var src embed.FS
unit_kind(f, temperature).
unit_kind(k, temperature).
unit_kind(c, temperature).
unit_kind(m2, area).
unit_kind(cm2, area).
unit_kind(mm, distance).
unit_kind(cm, distance).
unit_kind(m, distance).
unit_kind(km, distance).
unit_kind(feet, distance).
unit_kind(mg, mass).
unit_kind(g, mass).
unit_kind(kg, mass).
unit_kind(lb, mass).
unit_kind(cup, volume).
unit_kind(litre, volume).
unit_kind(butter, density).
unit_kind(water, density).
`
prologConvert = `
rule(GivenQuant, f, TargetQuant, c) :-
TargetQuant is (GivenQuant - 32) * 5 / 9.
rule(GivenQuant, c, TargetQuant, f) :-
TargetQuant is (GivenQuant / 5 * 9) + 32.
rule(GivenQuant, c, TargetQuant, k) :-
TargetQuant is GivenQuant + 273.15.
rule(GivenQuant, k, TargetQuant, c) :-
TargetQuant is GivenQuant - 273.15.
rule(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
rule(GivenQuant, GivenUnit, X, Y),
rule(X, Y, TargetQuant, TargetUnit).
ratio(m, km, 0.001).
ratio(m, cm, 100).
ratio(m, mm, 1000).
ratio(feet, m, 0.3048).
ratio(feet, inches, 12).
ratio(g, mg, 1000).
ratio(g, kg, 0.001).
ratio(X, Y, Ratio) :- ratio(Y, X, Z), Ratio is 1/Z, !.
ratio(X, Y, Ratio) :-
ratio(X, Step, R1),
ratio(Step, Y, R2),
Step =\= X,
Step =\= Y,
Ratio is R1 * R2, !.
convert(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
unit_kind(GivenUnit, temperature),
unit_kind(TargetUnit, temperature),
rule(GivenQuant, GivenUnit, TargetQuant, TargetUnit).
convert(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
ratio(GivenUnit, TargetUnit, Ratio),
TargetQuant is GivenQuant * Ratio, !.
`
)
// New creates a new rules engine. // New creates a new rules engine.
func New() (*Engine, error) { func New() (*Engine, error) {
...@@ -81,18 +23,17 @@ func New() (*Engine, error) { ...@@ -81,18 +23,17 @@ func New() (*Engine, error) {
p: prolog.New(nil, nil), p: prolog.New(nil, nil),
} }
// Treat a string argument as an atom. // Treat a string argument as an atom.
if err := ngn.p.Exec(`:- set_prolog_flag(double_quotes, atom).`); err != nil { err := ngn.p.Exec(`:- set_prolog_flag(double_quotes, atom).`)
return nil, err if err != nil {
}
if err := ngn.p.Exec(`:- set_prolog_flag(debug, on).`); err != nil {
return nil, err return nil, err
} }
if err := ngn.p.Exec(prologKinds); err != nil { rules, err := src.ReadFile("source/rules.pdb")
if err != nil {
return nil, err return nil, err
} }
err = ngn.p.Exec(string(rules))
if err := ngn.p.Exec(prologConvert); err != nil { if err != nil {
return nil, err return nil, err
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment