Skip to content

Commit

Permalink
Generalized Reification Clauses into Predicate-Object Pairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
apr94 committed Jul 10, 2017
1 parent abce26b commit 4a523e2
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 842 deletions.
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
}
196 changes: 120 additions & 76 deletions bql/grammar/grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,35 @@ 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 "_subject"@[] ?s.
_:v "_predicate"@[] ?p.
_: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};`,
"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};`,
}
p, err := NewParser(BQL())
if err != nil {
Expand Down Expand Up @@ -235,15 +246,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 +313,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 +469,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,
?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 +504,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,
?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,
?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,
?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,
?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,
?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 +589,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

0 comments on commit 4a523e2

Please sign in to comment.