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
75ef492a
Unverified
Commit
75ef492a
authored
Jun 15, 2024
by
Kevin Lyda
Browse files
Options
Downloads
Patches
Plain Diff
Conversion rules are not cooperating.
parent
aaa6f3f6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
rules/rules.go
+63
-26
63 additions, 26 deletions
rules/rules.go
rules/rules_test.go
+15
-13
15 additions, 13 deletions
rules/rules_test.go
with
78 additions
and
39 deletions
rules/rules.go
+
63
−
26
View file @
75ef492a
...
@@ -20,10 +20,12 @@ var (
...
@@ -20,10 +20,12 @@ var (
unit_kind(c, temperature).
unit_kind(c, temperature).
unit_kind(m2, area).
unit_kind(m2, area).
unit_kind(cm2, area).
unit_kind(cm2, area).
unit_kind(mm, distance).
unit_kind(cm, distance).
unit_kind(cm, distance).
unit_kind(m, distance).
unit_kind(m, distance).
unit_kind(km, distance).
unit_kind(km, distance).
unit_kind(feet, distance).
unit_kind(feet, distance).
unit_kind(mg, mass).
unit_kind(g, mass).
unit_kind(g, mass).
unit_kind(kg, mass).
unit_kind(kg, mass).
unit_kind(lb, mass).
unit_kind(lb, mass).
...
@@ -41,13 +43,36 @@ rule(GivenQuant, c, TargetQuant, k) :-
...
@@ -41,13 +43,36 @@ rule(GivenQuant, c, TargetQuant, k) :-
TargetQuant is GivenQuant + 273.15.
TargetQuant is GivenQuant + 273.15.
rule(GivenQuant, k, TargetQuant, c) :-
rule(GivenQuant, k, TargetQuant, c) :-
TargetQuant is GivenQuant - 273.15.
TargetQuant is GivenQuant - 273.15.
rule(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
rule(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
rule(GivenQuant, GivenUnit, X, Y),
rule(GivenQuant, GivenUnit, X, Y),
rule(X, Y, TargetQuant, TargetUnit).
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(Y, Step, R1),
ratio(Step, Z, R2),
Step =\= Y,
Step =\= Z,
Ratio is R1 * R2, !.
convert(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
convert(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
unit_kind(GivenUnit, temperature),
unit_kind(GivenUnit, temperature),
unit_kind(TargetUnit, temperature),
unit_kind(TargetUnit, temperature),
rule(GivenQuant, GivenUnit, TargetQuant, TargetUnit).
rule(GivenQuant, GivenUnit, TargetQuant, TargetUnit).
convert(GivenQuant, GivenUnit, TargetQuant, TargetUnit) :-
write("foo"), nl,
ratio(GivenUnit, TargetUnit, Ratio),
TargetQuant is GivenQuant * Ratio, !.
`
`
)
)
...
@@ -60,6 +85,9 @@ func New() (*Engine, error) {
...
@@ -60,6 +85,9 @@ func New() (*Engine, error) {
if
err
:=
ngn
.
p
.
Exec
(
`:- set_prolog_flag(double_quotes, atom).`
);
err
!=
nil
{
if
err
:=
ngn
.
p
.
Exec
(
`:- set_prolog_flag(double_quotes, atom).`
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
if
err
:=
ngn
.
p
.
Exec
(
`:- set_prolog_flag(debug, on).`
);
err
!=
nil
{
return
nil
,
err
}
if
err
:=
ngn
.
p
.
Exec
(
prologKinds
);
err
!=
nil
{
if
err
:=
ngn
.
p
.
Exec
(
prologKinds
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -98,22 +126,31 @@ func (ngn *Engine) Kind(unit string) (string, error) {
...
@@ -98,22 +126,31 @@ func (ngn *Engine) Kind(unit string) (string, error) {
// Convert converts units.
// Convert converts units.
func
(
ngn
*
Engine
)
Convert
(
qty
float64
,
givenUnit
,
targetUnit
string
)
(
float64
,
error
)
{
func
(
ngn
*
Engine
)
Convert
(
qty
float64
,
givenUnit
,
targetUnit
string
)
(
float64
,
error
)
{
sols
,
err
:=
ngn
.
p
.
Query
(
"convert(?, ?, Qty, ?)."
,
qty
,
givenUnit
,
targetUnit
)
sol
:=
ngn
.
p
.
QuerySolution
(
"convert(?, ?, Qty, ?)."
,
qty
,
givenUnit
,
targetUnit
)
if
sol
==
nil
{
fmt
.
Println
(
"sol == nil"
)
return
0
,
errors
.
New
(
"Query failed"
)
}
err
:=
sol
.
Err
()
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"
Huh?
"
)
fmt
.
Println
(
"
sol.Err()
"
)
return
0
,
err
return
0
,
err
}
}
defer
sols
.
Close
()
for
sols
.
Next
()
{
var
quantity
struct
{
var
quantity
struct
{
Qty
float64
Qty
float64
}
}
if
err
:=
sols
.
Scan
(
&
quantity
);
err
!=
nil
{
err
=
sol
.
Scan
(
&
quantity
)
fmt
.
Println
(
"Scan?"
)
if
err
==
nil
{
return
0
,
err
fmt
.
Println
(
"got qty"
)
}
return
quantity
.
Qty
,
err
return
quantity
.
Qty
,
err
}
}
fmt
.
Println
(
"qty scan failed"
)
var
dbg
[]
byte
if
err
:=
sol
.
Scan
(
&
dbg
);
err
!=
nil
{
fmt
.
Println
(
"on dbg scan"
)
return
0
,
err
}
fmt
.
Printf
(
"dbg: '%s'
\n
"
,
dbg
)
return
0
,
err
return
0
,
err
}
}
This diff is collapsed.
Click to expand it.
rules/rules_test.go
+
15
−
13
View file @
75ef492a
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
)
func
TestKinds
(
t
*
testing
.
T
)
{
func
TestKinds
(
t
*
testing
.
T
)
{
...
@@ -20,11 +21,11 @@ func TestKinds(t *testing.T) {
...
@@ -20,11 +21,11 @@ func TestKinds(t *testing.T) {
}
}
ngn
,
err
:=
New
()
ngn
,
err
:=
New
()
assert
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
for
_
,
tc
:=
range
cases
{
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
result
,
err
:=
ngn
.
Kind
(
tc
.
query
)
result
,
err
:=
ngn
.
Kind
(
tc
.
query
)
assert
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
tc
.
result
,
result
)
assert
.
Equal
(
t
,
tc
.
result
,
result
)
})
})
...
@@ -36,31 +37,32 @@ func TestConverts(t *testing.T) {
...
@@ -36,31 +37,32 @@ func TestConverts(t *testing.T) {
name
string
name
string
qty
float64
qty
float64
given
string
given
string
target
string
result
float64
result
float64
target
string
}{
}{
{
{
"c to f"
,
"c to f"
,
22.0
,
22.0
,
"c"
,
"c"
,
71.6
,
"f"
,
"f"
,
71.6
,
},
},
{
{
"k to f"
,
"k to f"
,
22.0
,
22.0
,
"k"
,
"k"
,
-
420.07
,
"f"
,
"f"
,
},
-
420.07
,
{
"inches to cm"
,
2.0
,
"inches"
,
5.08
,
"cm"
,
},
},
}
}
ngn
,
err
:=
New
()
ngn
,
err
:=
New
()
assert
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
for
_
,
tc
:=
range
cases
{
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
result
,
err
:=
ngn
.
Convert
(
tc
.
qty
,
tc
.
given
,
tc
.
target
)
result
,
err
:=
ngn
.
Convert
(
tc
.
qty
,
tc
.
given
,
tc
.
target
)
assert
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
assert
.
InDelta
(
t
,
tc
.
result
,
result
,
0.1
)
assert
.
InDelta
(
t
,
tc
.
result
,
result
,
0.1
)
})
})
...
...
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