Skip to content

Commit

Permalink
Add ttl.Document equal.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Oct 25, 2023
1 parent b7218d4 commit 6e78aec
Show file tree
Hide file tree
Showing 16 changed files with 1,540 additions and 896 deletions.
2 changes: 1 addition & 1 deletion internal/project/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (r *Report) AddTest(name string, outcome testsuite.OutcomeValue) {
Result: testsuite.TestResult{
Date: ttl.StringLiteral{
Value: time.Now().In(time.UTC).Format("2006-01-02-0700"),
DatatypeIRI: "xsd:date",
DatatypeIRI: &ttl.IRI{Prefixed: true, Value: "xsd:date"},
},
Outcome: outcome,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/testsuite/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (p Project) line() ttl.Statement {
},
{
Verb: &ttl.IRI{Prefixed: true, Value: "doap:created"},
ObjectList: []ttl.Object{&ttl.StringLiteral{Value: p.Created.Format("2006-01-02-0700"), DatatypeIRI: "xsd:date"}},
ObjectList: []ttl.Object{&ttl.StringLiteral{Value: p.Created.Format("2006-01-02-0700"), DatatypeIRI: &ttl.IRI{Prefixed: true, Value: "xsd:date"}}},
},
{
Verb: &ttl.IRI{Prefixed: true, Value: "doap:programming-language"},
Expand Down
2 changes: 1 addition & 1 deletion internal/testsuite/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ExampleNewReport() {
Result: testsuite.TestResult{
Date: ttl.StringLiteral{
Value: "2023-09-09+00:00",
DatatypeIRI: "xsd:date",
DatatypeIRI: &ttl.IRI{Prefixed: true, Value: "xsd:date"},
},
Outcome: testsuite.Passed,
},
Expand Down
62 changes: 56 additions & 6 deletions nquads/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
nt "github.com/0x51-dev/rdf/ntriples"
"github.com/0x51-dev/upeg/parser"
"github.com/0x51-dev/upeg/parser/op"
"slices"
"sort"
"strings"
)

Expand Down Expand Up @@ -47,15 +49,16 @@ func parseDocument(n *parser.Node) (Document, error) {
if n.Name != "Document" {
return nil, fmt.Errorf("document: unknown %s", n.Name)
}
var quads []Quad
var document Document
for _, n := range n.Children() {
quad, err := ParseQuad(n)
if err != nil {
return nil, err
}
quads = append(quads, *quad)
document = append(document, *quad)
}
return quads, nil
sort.Sort(document)
return document, nil
}

func (d Document) Equal(other Document) bool {
Expand All @@ -75,14 +78,57 @@ func (d Document) Equal(other Document) bool {
return true
}

func (d Document) Graphs() map[nt.Subject]nt.Document {
g := make(map[nt.Subject]nt.Document)
func (d Document) Graphs() map[string]nt.Document {
g := make(map[string]nt.Document)
for _, q := range d {
g[q.GraphLabel] = append(g[q.GraphLabel], q.Triple)
var graphLabel string
if q.GraphLabel != nil {
graphLabel = q.GraphLabel.String()
}
g[graphLabel] = append(g[graphLabel], q.Triple)
}
return g
}

func (d Document) Len() int {
return len(d)
}

func (d Document) Less(i, j int) bool {
if d[i].GraphLabel == nil && d[j].GraphLabel != nil {
return true
} else if d[i].GraphLabel != nil && d[j].GraphLabel == nil {
return false
}
return d[i].String() < d[j].String()
}

func (d Document) NormalizeBlankNodes() Document {
var keys []nt.Subject
for _, v := range d {
if !slices.Contains(keys, v.GraphLabel) {
keys = append(keys, v.GraphLabel)
}
}

g := d.Graphs()
var document Document
for _, k := range keys {
var key string
if k != nil {
key = k.String()
}
v := g[key].NormalizeBlankNodes()
for _, t := range v {
document = append(document, Quad{
Triple: t,
GraphLabel: k,
})
}
}
return document
}

func (d Document) String() string {
var s string
for _, q := range d {
Expand All @@ -91,6 +137,10 @@ func (d Document) String() string {
return s
}

func (d Document) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}

type Quad struct {
nt.Triple
GraphLabel nt.Subject
Expand Down
14 changes: 7 additions & 7 deletions nquads/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestDocument_Equal(t *testing.T) {
nt.Triple{
Subject: a,
Predicate: b,
Object: nt.BlankNode("_:b1"),
Object: nt.BlankNode("b1"),
}, nil,
),
}
Expand All @@ -59,7 +59,7 @@ func TestDocument_Equal(t *testing.T) {
nt.Triple{
Subject: a,
Predicate: b,
Object: nt.BlankNode("_:b2"),
Object: nt.BlankNode("b2"),
}, nil,
),
}) {
Expand All @@ -70,7 +70,7 @@ func TestDocument_Equal(t *testing.T) {
nt.Triple{
Subject: a,
Predicate: b,
Object: nt.BlankNode("_:b3"),
Object: nt.BlankNode("b1"),
}, nt.IRIReference("https://example.com/g"), // Different graph.
),
}) {
Expand All @@ -96,12 +96,12 @@ func TestExamples(t *testing.T) {
}

{ // fmt.Stringer
doc, err := nq.ParseDocument(doc.String())
doc2, err := nq.ParseDocument(doc.String())
if err != nil {
t.Fatal(err)
}
if len(doc) != test.quads {
t.Error(len(doc))
if !doc.Equal(doc2) {
t.Error(doc, doc2)
}
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestSuite(t *testing.T) {
report.AddTest(e.Name, testsuite.Failed)
t.Fatal(err)
}
if len(doc) != len(doc2) {
if !doc.Equal(doc2) {
report.AddTest(e.Name, testsuite.Failed)
t.Fatal(len(doc), len(doc2))
}
Expand Down
Loading

0 comments on commit 6e78aec

Please sign in to comment.