Skip to content

Commit

Permalink
Merge branch 'master' of github.com:daniel86/knowrob
Browse files Browse the repository at this point in the history
  • Loading branch information
sasjonge committed Dec 11, 2020
2 parents 7eaca9f + 8c89876 commit 7fee6aa
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 82 deletions.
202 changes: 145 additions & 57 deletions src/db/mongo/tripledb/triples.pl
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@
option(graph(Graph), Options, user),
triple_db(DB,Coll),
%%
read_triple_(FirstTriple,S,P,Operator,Value,Unit),
read_triple_(FirstTriple,S,P,Operator,Value,Unit,none),
read_vars_(S,P,Value,FirstTripleVars),
read_vars_2(FirstTripleVars,FirstVars),
read_vars_2(none,FirstTripleVars,FirstVars),
%%
triple_aggregate1(Coll,Xs,QScope,Graph,FirstVars,Doc_inner,AllVars),
%%
Expand Down Expand Up @@ -317,74 +317,66 @@

%%
triple_aggregate1(_,[],_,_,Vars,[],Vars) :- !.
triple_aggregate1(Coll,[Triple|Xs],QScope,Graph,Vars0,
[Lookup0,Unwind0,Scopes0,Project0|Doc_inner],
Vars_n) :-
read_triple_(Triple,S,P,Operator,Value,Unit),
triple_aggregate1(Coll, [Triple|Xs], QScope, Graph, Vars0, TripleDoc, Vars_n) :-
read_triple_(Triple,S,P,Operator,Value,Unit,Modifier),
read_vars_(S,P,Value,TripleVars),
% find all joins with input documents
findall([Field_j,Value_j],
( member([Field_j,_,Value_j],TripleVars),
member([Field_j,_],Vars0)
),
Joins
),
%%%%%% recursion
read_vars_2(TripleVars,Vars1),
read_vars_2(Modifier,TripleVars,Vars1),
append(Vars0,Vars1,Vars_new),
list_to_set(Vars_new,Vars2),
triple_aggregate1(Coll,Xs,QScope,Graph,Vars2,Doc_inner,Vars_n),
%%%%%% $lookup
%%%%%%
triple_query_document_(S,P,
Operator,Value,Unit,
QScope,Graph,QueryDoc),
aggregate_match_(Joins,MatchDoc),
findall([Let_key,string(Let_val)],
( member([Let_key,_],Joins),
atom_concat('$',Let_key,Let_val)
),
LetDoc
),
Lookup0=['$lookup', [
['from',string(Coll)],
% create a field "next" with all matching documents
['as',string('next')],
% make fields from input document accessible in pipeline
['let',LetDoc],
% get matching documents
['pipeline', array([['$match', [
['$expr', ['$and', array(MatchDoc)]] |
QueryDoc
]]])]
]],
Operator,Value,Unit,
QScope,Graph,QueryDoc),
triple_aggregate2(Modifier,
Coll,QueryDoc,
Vars0,TripleVars,
Doc_inner,
TripleDoc).

%%
triple_aggregate2(Modifier,
Coll,QueryDoc,
Vars0,TripleVars,
Doc_inner, TripleDoc) :-
%%%%%% $lookup
aggregate_lookup_(Coll,
QueryDoc,Vars0,TripleVars,
Lookup0),
%%%%%% $unwind the next field
%% TODO
% Unwind0=['$unwind',[
% ['path', string('$next')],
% ['preserveNullAndEmptyArrays',bool(true)]
% ]],
Unwind0=['$unwind',string('$next')],
aggregate_unwind_(Modifier,Unwind0),
%%%%%% $set scopes, $setUnion is used to avoid duplicate scopes
Scopes0=['$set',['v_scope',['$setUnion',
array([string('$v_scope'), array([string('$next.scope')])])
]]],
aggregate_scopes_(Modifier,Scopes0),
%%%%%% $project
findall([Pr_Key,string(Pr_Value)],(
findall([Pr_Key,Pr_Value],(
%
( Pr_Key='v_scope',
atom_concat('$',Pr_Key,Pr_Value)
atom_concat('$',Pr_Key,Pr_Value0),
Pr_Value=string(Pr_Value0)
)
% copy value of var e.g. { 'S': '$S' }
; ( member([Pr_Key,_],Vars0),
atom_concat('$',Pr_Key,Pr_Value)
atom_concat('$',Pr_Key,Pr_Value0),
Pr_Value=string(Pr_Value0)
)
% set new value of var e.g. { 'S': '$next.s' }
; ( member([Pr_Key,_,Field],TripleVars),
\+ member([Pr_Key,_],Vars0),
atom_concat('$next.',Field,Pr_Value)
aggregate_project_(Modifier,Field,Pr_Value)
)
% findall special case
; ( Modifier=findall(List),
read_vars_1([[List,'next']],[[Pr_Key,List,Field]]),
aggregate_project_(Modifier,Field,Pr_Value)
)
), ProjectDoc),
Project0=['$project', ProjectDoc].
Project0=['$project', ProjectDoc],
%%%%%%
( Unwind0=[]
-> TripleDoc=[Lookup0,Scopes0,Project0|Doc_inner]
; TripleDoc=[Lookup0,Unwind0,Scopes0,Project0|Doc_inner]
).

%%
triple_aggregate_unify_(_,[]) :- !.
Expand All @@ -400,28 +392,121 @@
Var=Value.

%%
aggregate_match_(Joins,MatchDoc) :-
aggregate_lookup_(Coll,QueryDoc,Vars0,TripleVars,Lookup) :-
% find all joins with input documents
findall([Field_j,Value_j],
( member([Field_j,_,Value_j],TripleVars),
member([Field_j,_],Vars0)
),
Joins
),
% pass input document value to lookup
findall([Let_key,string(Let_val)],
( member([Let_key,_],Joins),
atom_concat('$',Let_key,Let_val)
),
LetDoc
),
% perform the join operation (equals the input document value)
findall(['$eq', array([string(Match_key),string(Match_val)])],
% { $eq: [ "$s", "$$R" ] },
( member([Join_var,Join_field],Joins),
atom_concat('$',Join_field,Match_key),
atom_concat('$$',Join_var,Match_val)
),
MatchDoc
).
),
% finally compose the lookup document
Lookup=['$lookup', [
['from',string(Coll)],
% create a field "next" with all matching documents
['as',string('next')],
% make fields from input document accessible in pipeline
['let',LetDoc],
% get matching documents
['pipeline', array([['$match', [
['$expr', ['$and', array(MatchDoc)]] |
QueryDoc
]]])]
]].

%%
aggregate_unwind_(none,
['$unwind',string('$next')]) :- !.

aggregate_unwind_(findall(_), []) :- !.

aggregate_unwind_(ignore,
['$unwind',[
['path', string('$next')],
['preserveNullAndEmptyArrays',bool(true)]
]]) :- !.

%%
aggregate_scopes_(none,
['$set',['v_scope',['$setUnion',
array([string('$v_scope'), array([string('$next.scope')])])
]]]) :- !.

aggregate_scopes_(findall(_),
['$set',['v_scope',string('$v_scope')]]) :-
% TODO: handle scope in findall
!.

aggregate_scopes_(ignore,
['$set',['v_scope',['$setUnion', array([
string('$v_scope'),
['$ifNull', array([
string('$next.scope'),
array([])
])]
])]]]) :- !.

%%
aggregate_project_(none,Field,string(Pr_Value)) :-
!,
atom_concat('$next.',Field,Pr_Value).

aggregate_project_(findall(_),Field,['$map',[
['input',string('$next')],
['in',[
['s',string('$$this.s')],
['p',string('$$this.p')],
['o',string('$$this.o')]
]]
]]) :-
!,
Field='next'.

aggregate_project_(ignore,Field,['$cond',array([
['$not', array([string(Pr_Value)]) ],
string('$$REMOVE'),
string(Pr_Value)
])]) :-
atom_concat('$next.',Field,Pr_Value).

%%
read_triple_(
triple(QSubject,QProperty,QValue),
TripleTerm,
Subject,
Property,
MngOperator,MngValue,Unit) :-
MngOperator,MngValue,Unit,
Modifier) :-
read_triple_1_(TripleTerm,
triple(QSubject,QProperty,QValue),
Modifier
),
strip_variable(QSubject,Subject),
strip_variable(QProperty,Property),
strip_variable(QValue,ValueQuery),
mng_query_value_(ValueQuery,MngOperator,MngValue,Unit).

read_triple_1_(ignore(X),X,ignore) :- !.
read_triple_1_(findall(X,Y),X,findall(Y)) :- !.
read_triple_1_(X,X,none).

%%

read_vars_(S,P,Value,Vars) :-
strip_type_(S,_,S0),
strip_type_(P,_,P0),
Expand All @@ -439,9 +524,12 @@
read_vars_1(Xs,Ys).

%%
read_vars_2([],[]) :- !.
read_vars_2([[X0,X1,_]|Xs],[[X0,X1]|Ys]) :-
read_vars_2(Xs,Ys).
read_vars_2(findall(List),_,[[Key,List]]) :-
!,
read_vars_1([[List,'next']],[[Key,List,_]]).
read_vars_2(_,[],[]) :- !.
read_vars_2(Modifier,[[X0,X1,_]|Xs],[[X0,X1]|Ys]) :-
read_vars_2(Modifier,Xs,Ys).

%%
%
Expand Down
41 changes: 16 additions & 25 deletions src/model/SOMA/OBJ.pl
Original file line number Diff line number Diff line change
Expand Up @@ -195,40 +195,31 @@

object_shape_new(Obj,ShapeTerm,[Frame,Pos,Rot],MaterialTerm) :-
once((var(Obj);Obj0=Obj)),
findall([Obj0,Shape,ShapeRegion,ShapeP,ShapeV],
findall([Obj0,Shape,ShapeRegion,ShapeAttributes0],
ask(aggregate([
triple(Obj0,soma:hasShape,Shape),
triple(Shape,dul:hasRegion,ShapeRegion),
% optional: get origin, fallback to unit transform
%optional(triple(ShapeRegion,'http://knowrob.org/kb/urdf.owl#hasOrigin',Origin)),
%optional(triple(ground(Origin), soma:hasPositionVector, term(Pos))),
%optional(triple(ground(Origin), soma:hasOrientationVector, term(Rot))),
% TODO: it would be possible to get all properties of an individual,
% but then no join is possible.
% maybe exception with once concept.
%all(triple(ShapeRegion,_,_),ShapeAttributes)
triple(ShapeRegion,ShapeP,ShapeV)
%,optional([
% triple(ShapeRegion,'http://knowrob.org/kb/urdf.owl#hasOrigin',Origin),
% triple(Origin, soma:hasPositionVector, term(Pos)),
% triple(Origin, soma:hasOrientationVector, term(Rot))
%])
%ignore(triple(Obj,soma:hasColor,Color)),
%ignore(triple(Color,dul:hasRegion,Region)),
%ignore(triple(ShapeRegion,'http://knowrob.org/kb/urdf.owl#hasOrigin',Origin)),
%ignore(triple(Origin, soma:hasPositionVector, term(Pos))),
%ignore(triple(Origin, soma:hasOrientationVector, term(Rot))),
% get all attributes of ShapeRegion
findall(triple(ShapeRegion,_,_), ShapeAttributes0)
])),
ObjectShapes
),

%% can we do this better?
findall(X, member([X|_],ObjectShapes),Objects),
list_to_set(Objects,Objects0),
member(Obj,Objects0),

findall([Po,Vo],
member([Obj,_,_,Po,Vo],ObjectShapes),
ShapeAttributes),

%%
member([Obj,_,_,ShapeAttributes1],ObjectShapes),
findall([P1,O1], (
member(Doc1, ShapeAttributes1),
member(p-string(P1), Doc1),
member(o-Ox, Doc1),
Ox=..[_,O1]
), ShapeAttributes),

rdf_split_url(_,Frame,Obj),

shape_data2(ShapeAttributes,ShapeTerm),

%% TODO
Expand Down

0 comments on commit 7fee6aa

Please sign in to comment.