static void PopulateNodesToSqlMap(SparqlGraph g) { List <Node> terminalNodes = g.GetAllNodes(typeof(TerminalNode)); foreach (TerminalNode node in terminalNodes) { //try to match each node to sql expressions from mapping string key = $"{node.Pred} {node.Obj}"; if (mapping.mappings.ContainsKey(key)) { string sql = mapping.mappings[key][0].SQL; nodesToSqlMap.Add(node, sql); } else { key = $"{node.Pred}"; if (mapping.mappings.ContainsKey(key)) { string sql = mapping.mappings[key].Find(mapNode => mapNode.Object == node.Obj).SQL; nodesToSqlMap.Add(node, sql); } else //there is no mapping for this node { throw new Exception($"No mapping for node {node.Subj} {node.Pred} {node.Obj} was provided!"); } } } }
static string TranslateToSql(SparqlGraph g, Dictionary <string, List <MapNode> > map) { /*S = List of nodes in Q in a bottom-up topological order * sqlM = a map from nodes to SQL expressions*/ List <Node> S = g.TopologicalSort(); foreach (Node n in S) { if (n is TerminalNode) //translating leaves { //skip (this step was done in PopulateNodesToSqlMap) continue; } else { if (n is JoinNode) { Node n1 = n.Children[0]; Node n2 = n.Children[1]; nodesToSqlMap[n] = InnerJoin(nodesToSqlMap[n1], nodesToSqlMap[n2]); } else if (n is OptionalNode) { Node n1 = n.Children[0]; Node n2 = n.Parent; string e = (n as OptionalNode).JoinCondition; nodesToSqlMap[n] = LeftJoin(nodesToSqlMap[n1], nodesToSqlMap[n2], e); } else if (n is UnionNode) { Node n1 = n.Children[0]; Node n2 = n.Children[1]; nodesToSqlMap[n] = Union(nodesToSqlMap[n1], nodesToSqlMap[n2]); } else if (n is FilterNode) { Node n1 = n.Children[0]; string e = (n as FilterNode).FilterExpression; nodesToSqlMap[n] = Filter(nodesToSqlMap[n1], e); } else if (n is ProjectNode) { Node n1 = n.Children[0]; List <string> pv = (n as ProjectNode).ProjectionVariables; nodesToSqlMap[n] = Project(nodesToSqlMap[n1], pv); } } } return(nodesToSqlMap[S.Last()]); }