Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 12 additions & 54 deletions bql/grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func BQL() *Grammar {
NewTokenType(lexer.ItemNode),
NewSymbol("CONSTRUCT_PREDICATE"),
NewSymbol("CONSTRUCT_OBJECT"),
NewSymbol("REIFICATION_CLAUSE"),
NewSymbol("MORE_CONSTRUCT_PREDICATE_OBJECT_PAIRS"),
NewSymbol("MORE_CONSTRUCT_TRIPLES"),
},
},
Expand All @@ -806,7 +806,7 @@ func BQL() *Grammar {
NewTokenType(lexer.ItemBlankNode),
NewSymbol("CONSTRUCT_PREDICATE"),
NewSymbol("CONSTRUCT_OBJECT"),
NewSymbol("REIFICATION_CLAUSE"),
NewSymbol("MORE_CONSTRUCT_PREDICATE_OBJECT_PAIRS"),
NewSymbol("MORE_CONSTRUCT_TRIPLES"),
},
},
Expand All @@ -815,7 +815,7 @@ func BQL() *Grammar {
NewTokenType(lexer.ItemBinding),
NewSymbol("CONSTRUCT_PREDICATE"),
NewSymbol("CONSTRUCT_OBJECT"),
NewSymbol("REIFICATION_CLAUSE"),
NewSymbol("MORE_CONSTRUCT_PREDICATE_OBJECT_PAIRS"),
NewSymbol("MORE_CONSTRUCT_TRIPLES"),
},
},
Expand Down Expand Up @@ -859,56 +859,17 @@ func BQL() *Grammar {
},
},
},
"REIFICATION_CLAUSE": []*Clause{
"MORE_CONSTRUCT_PREDICATE_OBJECT_PAIRS": []*Clause{
{
Elements: []Element{
NewTokenType(lexer.ItemSemicolon),
NewSymbol("REIFICATION_PREDICATE"),
NewSymbol("REIFICATION_OBJECT"),
NewSymbol("REIFICATION_CLAUSE"),
NewSymbol("CONSTRUCT_PREDICATE"),
NewSymbol("CONSTRUCT_OBJECT"),
NewSymbol("MORE_CONSTRUCT_PREDICATE_OBJECT_PAIRS"),
},
},
{},
},
"REIFICATION_PREDICATE": []*Clause{
{
Elements: []Element{
NewTokenType(lexer.ItemPredicate),
},
},
{
Elements: []Element{
NewTokenType(lexer.ItemBinding),
},
},
},
"REIFICATION_OBJECT": []*Clause{
{
Elements: []Element{
NewTokenType(lexer.ItemNode),
},
},
{
Elements: []Element{
NewTokenType(lexer.ItemBlankNode),
},
},
{
Elements: []Element{
NewTokenType(lexer.ItemPredicate),
},
},
{
Elements: []Element{
NewTokenType(lexer.ItemLiteral),
},
},
{
Elements: []Element{
NewTokenType(lexer.ItemBinding),
},
},
},
"MORE_CONSTRUCT_TRIPLES": []*Clause{
{
Elements: []Element{
Expand Down Expand Up @@ -1042,17 +1003,14 @@ func SemanticBQL() *Grammar {

// CONSTRUCT clause semantic hooks.
setClauseHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_FACTS"}, semantic.InitWorkingConstructClauseHook(), semantic.TypeBindingClauseHook(semantic.Construct))

constructTriplesSymbols := []semantic.Symbol{"CONSTRUCT_TRIPLES", "MORE_CONSTRUCT_TRIPLES"}
setClauseHook(semanticBQL, constructTriplesSymbols, semantic.NextWorkingConstructClauseHook(), semantic.NextWorkingConstructClauseHook())
setClauseHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_PREDICATE"}, semantic.NextWorkingConstructPredicateObjectPairClauseHook(), nil)
setClauseHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_OBJECT"}, nil, semantic.NextWorkingConstructPredicateObjectPairClauseHook())

setElementHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_TRIPLES"}, semantic.ConstructSubjectClauseHook(), nil)
setElementHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_PREDICATE"}, semantic.ConstructPredicateClauseHook(), nil)
setElementHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_OBJECT"}, semantic.ConstructObjectClauseHook(), nil)

setClauseHook(semanticBQL, []semantic.Symbol{"REIFICATION_CLAUSE"}, semantic.NextWorkingReificationClauseHook(), semantic.NextWorkingReificationClauseHook())
setElementHook(semanticBQL, []semantic.Symbol{"REIFICATION_PREDICATE"}, semantic.ReificationPredicateClauseHook(), nil)
setElementHook(semanticBQL, []semantic.Symbol{"REIFICATION_OBJECT"}, semantic.ReificationObjectClauseHook(), nil)
setElementHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_TRIPLES"}, semantic.ConstructSubjectHook(), nil)
setElementHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_PREDICATE"}, semantic.ConstructPredicateHook(), nil)
setElementHook(semanticBQL, []semantic.Symbol{"CONSTRUCT_OBJECT"}, semantic.ConstructObjectHook(), nil)

return semanticBQL
}
209 changes: 126 additions & 83 deletions bql/grammar/grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,34 @@ func TestAcceptByParse(t *testing.T) {
// Test Construct clause.
`construct {?s "new_predicate"@[] ?o} into ?a from ?b where {?s "old_predicate"@[,] ?o} having ?s = ?o;`,
`construct {?s "new_predicate"@[] ?o} into ?a from ?b where {?s "old_predicate"@[,] ?o};`,
`construct {?s ?p ?o} into ?a from ?b where {?n "_subject"@[] ?s.
?n "_predicate"@[] ?p.
?n "_object"@[] ?o};`,
`construct {?s ?p ?o}
into ?a
from ?b
where {?n "_subject"@[] ?s.
?n "_predicate"@[] ?p.
?n "_object"@[] ?o};`,
`construct {?s ?p ?o.
_:v "_subject"@[] ?s.
_:v "_predicate"@[] ?p.
_:v "_object"@[] ?o} into ?a from ?b where {?n "_subject"@[] ?s.
?n "_predicate"@[] ?p.
?n "_object"@[] ?o};`,
_:v "_object"@[] ?o}
into ?a
from ?b
where {?n "_subject"@[] ?s.
?n "_predicate"@[] ?p.
?n "_object"@[] ?o};`,
`construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2};`,

"predicate_2"@[] ?o2}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2};`,
`construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2.
?s "predicate_3"@[] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
?s "predicate_3"@[] ?o3}
into ?a
from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
}
p, err := NewParser(BQL())
if err != nil {
Expand Down Expand Up @@ -235,15 +245,23 @@ func TestRejectByParse(t *testing.T) {
`construct {?s "foo"@[,] ?o} from ?b where{?s "foo"@[,] ?o} having ?s = ?o;`,
// Construct clause with badly formed blank node.
`construct {?s ?p ?o.
_v "some_pred"@[] ?k } into ?a from ?b where {?s "foo"@[,] ?o};`,
_v "some_pred"@[] ?k}
into ?a
from ?b
where {?s "foo"@[,] ?o};`,
// Construct clause with badly formed triple.
`construct {?s ?p ?o.
_:v "some_pred"@[]} into ?a from ?b where {?s "foo"@[,] ?o};`,
// Construct clause with badly formed reification clause.
_:v "some_pred"@[]}
into ?a
from ?b
where {?s "foo"@[,] ?o};`,
// Construct clause with badly formed predicate-object pair.
`construct {?s "predicate_1"@[] ?o1;
?s "predicate_2"@[] ?o2} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2};`,

?s "predicate_2"@[] ?o2}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2};`,
}
p, err := NewParser(BQL())
if err != nil {
Expand Down Expand Up @@ -294,16 +312,21 @@ func TestAcceptGraphOpsByParseAndSemantic(t *testing.T) {

// Construct data. Graphs can be input or output graphs.
{`construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
empty, []string{"?b"}, []string{"?a"}, 0},
"predicate_2"@[] ?o2}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`, empty, []string{"?b"}, []string{"?a"}, 0},

// construct data into multiple output graphs from multple input graphs.
{`construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2} into ?a, ?b from ?c, ?d where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
empty, []string{"?c", "?d"}, []string{"?a", "?b"}, 0},
"predicate_2"@[] ?o2}
into ?a, ?b
from ?c, ?d
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`, empty, []string{"?c", "?d"}, []string{"?a", "?b"}, 0},
}
p, err := NewParser(SemanticBQL())
if err != nil {
Expand Down Expand Up @@ -445,18 +468,24 @@ func TestSemanticStatementConstructClausesLengthCorrectness(t *testing.T) {
}{
{
query: `construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want: 1,
"predicate_2"@[] ?o2}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want: 1,
},
{
query: `construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2.
?s "predicate_3"@[] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want: 2,
"predicate_2"@[] ?o2.
?s "predicate_3"@[] ?o3}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want: 2,
},
}
p, err := NewParser(SemanticBQL())
Expand All @@ -474,67 +503,81 @@ func TestSemanticStatementConstructClausesLengthCorrectness(t *testing.T) {
}
}

func TestSemanticStatementReificationClausesLengthCorrectness(t *testing.T) {
func TestSemanticStatementPredicateObjectPairsLengthCorrectness(t *testing.T) {
table := []struct {
query string
want_one int
want_two int
query string
want_one int
want_two int
}{
{
query: `construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2.
?s "predicate_3"@[] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want_one: 1,
want_two: 0,
"predicate_2"@[] ?o2.
?s "predicate_3"@[] ?o3}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want_one: 2,
want_two: 1,
},
{
query: `construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2;
"predicate_3"@[] ?o3.
?s1 "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2;
"predicate_3"@[] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s1 "old_predicate_3"@[,] ?o3};`,
want_one: 2,
want_two: 2,
"predicate_2"@[] ?o2;
"predicate_3"@[] ?o3.
?s1 "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2;
"predicate_3"@[] ?o3}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s1 "old_predicate_3"@[,] ?o3};`,
want_one: 3,
want_two: 3,
},
{
query: `construct {?s "predicate_1"@[2015-07-19T13:12:04.669618843-07:00] ?o1;
"predicate_2"@[2015-07-19T13:12:04.669618843-07:00] ?o2.
?s "predicate_3"@[2015-07-19T13:12:04.669618843-07:00] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want_one: 1,
want_two: 0,
"predicate_2"@[2015-07-19T13:12:04.669618843-07:00] ?o2.
?s "predicate_3"@[2015-07-19T13:12:04.669618843-07:00] ?o3}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s "old_predicate_3"@[,] ?o3};`,
want_one: 2,
want_two: 1,
},
{
query: `construct {?s "predicate_1"@[2015-07-19T13:12:04.669618843-07:00] ?o1;
"predicate_2"@[2015-07-19T13:12:04.669618843-07:00] ?o2;
"predicate_3"@[2015-07-19T13:12:04.669618843-07:00] ?o3.
?s1 "predicate_1"@[2015-07-19T13:12:04.669618843-07:00] ?o1;
"predicate_2"@[2015-07-19T13:12:04.669618843-07:00] ?o2;
"predicate_3"@[2015-07-19T13:12:04.669618843-07:00] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s1 "old_predicate_3"@[,] ?o3};`,
want_one: 2,
want_two: 2,
"predicate_2"@[2015-07-19T13:12:04.669618843-07:00] ?o2;
"predicate_3"@[2015-07-19T13:12:04.669618843-07:00] ?o3.
?s1 "predicate_1"@[2015-07-19T13:12:04.669618843-07:00] ?o1;
"predicate_2"@[2015-07-19T13:12:04.669618843-07:00] ?o2;
"predicate_3"@[2015-07-19T13:12:04.669618843-07:00] ?o3}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s1 "old_predicate_3"@[,] ?o3};`,
want_one: 3,
want_two: 3,
},
{
query: `construct {?s "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2;
"predicate_3"@[?t] ?o3.
?s1 "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2;
"predicate_3"@[?t] ?o3} into ?a from ?b where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s1 "old_predicate_3"@[,] AT ?t ?o3};`,
want_one: 2,
want_two: 2,
"predicate_2"@[] ?o2;
"predicate_3"@[?t] ?o3.
?s1 "predicate_1"@[] ?o1;
"predicate_2"@[] ?o2;
"predicate_3"@[?t] ?o3}
into ?a
from ?b
where {?s "old_predicate_1"@[,] ?o1.
?s "old_predicate_2"@[,] ?o2.
?s1 "old_predicate_3"@[,] AT ?t ?o3};`,
want_one: 3,
want_two: 3,
},

}
p, err := NewParser(SemanticBQL())
if err != nil {
Expand All @@ -545,11 +588,11 @@ func TestSemanticStatementReificationClausesLengthCorrectness(t *testing.T) {
if err := p.Parse(NewLLk(entry.query, 1), st); err != nil {
t.Errorf("Parser.consume: Failed to accept valid semantic entry %q", entry.query)
}
if got, want := len(st.ConstructClauses()[0].ReificationClauses()), entry.want_one; got != want {
t.Errorf("Invalid number of reification clauses for query %q; got %d, want %d; %v", entry.query, got, want, st.ConstructClauses()[0].ReificationClauses())
if got, want := len(st.ConstructClauses()[0].PredicateObjectPairs()), entry.want_one; got != want {
t.Errorf("Invalid number of predicate-object pairs for query %q; got %d, want %d; %v", entry.query, got, want, st.ConstructClauses()[0].PredicateObjectPairs())
}
if got, want := len(st.ConstructClauses()[1].ReificationClauses()), entry.want_two; got != want {
t.Errorf("Invalid number of reification clauses for query %q; got %d, want %d; %v", entry.query, got, want, st.ConstructClauses()[0].ReificationClauses())
if got, want := len(st.ConstructClauses()[1].PredicateObjectPairs()), entry.want_two; got != want {
t.Errorf("Invalid number of predicate-object pairs for query %q; got %d, want %d; %v", entry.query, got, want, st.ConstructClauses()[0].PredicateObjectPairs())
}
}
}
Loading