Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
units
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kevin Lyda
units
Commits
6b621d34
Unverified
Commit
6b621d34
authored
Jun 16, 2024
by
Kevin Lyda
Browse files
Options
Downloads
Patches
Plain Diff
get embedded rules.
parent
f29ea787
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
rules/rules.go
+9
-68
9 additions, 68 deletions
rules/rules.go
with
9 additions
and
68 deletions
rules/rules.go
+
9
−
68
View file @
6b621d34
...
@@ -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
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment