private void ProcessSelectExpandItem(SelectExpandClause clause, EdmNode edmNode, CustomNavigationProperty parentCustomNavProp, NavigationPropertySegment parentSegment) { if (edmNode == null) { var entityType = parentSegment.EdmType.AsElementType() as EdmEntityType; var RdfNode = new VariablePattern($"{parentSegment.NavigationProperty.Name}"); edmNode = new EdmNode() { Name = parentSegment.NavigationProperty.Name, ItemEdmType = entityType, RdfNode = RdfNode, //StructProperties = entityType.StructuralProperties().ToList(), NavProperties = new List <CustomNavigationProperty>() }; parentCustomNavProp.NestedEdmNodes.Add(edmNode); } List <IEdmStructuralProperty> structPropList = new List <IEdmStructuralProperty>(); foreach (var item in clause.SelectedItems) { var selectItem = item as PathSelectItem; if (selectItem != null && selectItem.SelectedPath != null) { var segment = selectItem.SelectedPath.FirstSegment as PropertySegment; if (segment != null) { structPropList.Add(segment.Property); } } var expandItem = item as ExpandedNavigationSelectItem; if (expandItem != null) { var segment = expandItem.PathToNavigationProperty.FirstSegment as NavigationPropertySegment; if (segment != null && expandItem.SelectAndExpand != null) { CustomNavigationProperty customNavProp = new CustomNavigationProperty(); edmNode.NavProperties.Add(customNavProp); customNavProp.NestedEdmNodes = new List <EdmNode>(); customNavProp.NavigationProperty = segment.NavigationProperty; customNavProp.Filters = expandItem.FilterOption != null ? expandItem.FilterOption.Expression : null; customNavProp.Top = expandItem.TopOption.GetValueOrDefault(); customNavProp.Skip = expandItem.SkipOption.GetValueOrDefault(); customNavProp.OrderBy = expandItem.OrderByOption; ProcessSelectExpandItem(expandItem.SelectAndExpand, null, customNavProp, segment); } } } if (structPropList.Count > 0) { //edmNode.StructProperties = structPropList; if (parentCustomNavProp != null) { parentCustomNavProp.StructProperties = structPropList; } } }
private string ConstructSparql() { NodeFactory nodeFactory = new NodeFactory(); List <ITriplePattern> constructList = new List <ITriplePattern>(); List <ITriplePattern> whereList = new List <ITriplePattern>(); List <ITriplePattern> optionList = new List <ITriplePattern>(); List <ITriplePattern> optSubQueryTriplePatterns = new List <ITriplePattern>(); EdmNode previousEdmNode = null; EdmNode endEdmNode = EdmNodeList.Last(); foreach (var edmNode in EdmNodeList) { var isaTriple = new TriplePattern(edmNode.RdfNode, new NodeMatchPattern(nodeFactory.CreateUriNode(new Uri(RdfSpecsHelper.RdfType))), new NodeMatchPattern(nodeFactory.CreateUriNode(GetUri(edmNode.ItemEdmType)))); if (edmNode == endEdmNode) { constructList.Add(isaTriple); } whereList.Add(isaTriple); if (previousEdmNode != null) { Dictionary <string, Tuple <Type, Uri> > preProperties = GetAllProperties(previousEdmNode.ItemEdmType); var relationTriple = new TriplePattern(previousEdmNode.RdfNode, new NodeMatchPattern(nodeFactory.CreateUriNode(new Uri(preProperties[edmNode.Name].Item2.AbsoluteUri))), edmNode.RdfNode); if (edmNode == endEdmNode) { constructList.Add(relationTriple); } whereList.Add(relationTriple); } previousEdmNode = edmNode; if (edmNode == endEdmNode) { Dictionary <string, Tuple <Type, Uri> > properties = GetAllProperties(edmNode.ItemEdmType); foreach (var prop in edmNode.StructProperties.Where(p => p.Name != "LocalId")) { var propTriple = new TriplePattern(edmNode.RdfNode, new NodeMatchPattern(nodeFactory.CreateUriNode(new Uri(properties[prop.Name].Item2.AbsoluteUri))), new VariablePattern($"{prop.Name}")); constructList.Add(propTriple); optionList.Add(propTriple); } ConstructFromNavProperties(edmNode, constructList, optSubQueryTriplePatterns); } } IQueryBuilder queryBuilder = QueryBuilder.Construct(q => q.Where(constructList.ToArray())) .Where(whereList.Concat(SubQueryTriplePatterns).ToArray()); foreach (var tp in optionList) { queryBuilder.Optional(gp => gp.Where(tp)); } foreach (var tp in optSubQueryTriplePatterns) { queryBuilder.Optional(gp => gp.Where(tp)); } if (FilterExp != null) { queryBuilder.Filter(FilterExp); } ///*OrderBy options*/ //if (QueryOptions.OrderBy != null && QueryOptions.OrderBy.OrderByClause != null) //{ // var edmNode = EdmNodeList[EdmNodeList.Count - 1]; // foreach (var node in QueryOptions.OrderBy.OrderByNodes) // { // var typedNode = node as OrderByPropertyNode; // var ordName = typedNode.Property.Name; // if (ordName.ToLower() == "localid") // ordName = edmNode.Name; // if (typedNode.OrderByClause.Direction == OrderByDirection.Ascending) // queryBuilder.OrderBy(ordName); // else // queryBuilder.OrderByDescending(ordName); // } //} return(queryBuilder.BuildQuery().ToString()); }
private void ConstructFromNavProperties(EdmNode edmNode, List <ITriplePattern> constructList, List <ITriplePattern> optSubQueryTriplePatterns) { NodeFactory nodeFactory = new NodeFactory(); Dictionary <string, Tuple <Type, Uri> > properties = GetAllProperties(edmNode.ItemEdmType); if (edmNode.NavProperties.Count > 0) { foreach (var expProp in edmNode.NavProperties) { var expEntityType = expProp.NavigationProperty.Type.Definition.AsElementType() as EdmEntityType; List <ITriplePattern> tripleList = new List <ITriplePattern>(); tripleList.Add(new TriplePattern(new VariablePattern($"?{expProp.NavigationProperty.Name}"), new NodeMatchPattern(nodeFactory.CreateUriNode(new Uri(RdfSpecsHelper.RdfType))), new NodeMatchPattern(nodeFactory.CreateUriNode(GetUri(expEntityType))))); tripleList.Add(new TriplePattern(edmNode.RdfNode, new NodeMatchPattern(nodeFactory.CreateUriNode(new Uri(properties[expProp.NavigationProperty.Name].Item2.AbsoluteUri))), new VariablePattern($"?{expProp.NavigationProperty.Name}"))); constructList.AddRange(tripleList); List <ITriplePattern> predTripleList = new List <ITriplePattern>(); Dictionary <string, Tuple <Type, Uri> > expandProperties = GetAllProperties(expEntityType); List <IEdmStructuralProperty> structProps = expProp.StructProperties; if (structProps == null) { structProps = expEntityType.StructuralProperties().ToList(); } foreach (var prop in structProps.Where(p => p.Name != "LocalId")) { var expPropTriple = new TriplePattern(new VariablePattern($"?{expProp.NavigationProperty.Name}"), new NodeMatchPattern(nodeFactory.CreateUriNode(new Uri(expandProperties[prop.Name].Item2.AbsoluteUri))), new VariablePattern($"?{prop.Name}Expand")); predTripleList.Add(expPropTriple); constructList.Add(expPropTriple); } IQueryBuilder subqueryBuilder = null; var variableList = structProps.Where(p => p.Name != "LocalId") .Select(p => $"?{p.Name}Expand").ToList(); variableList.Add(expProp.NavigationProperty.Name); if (edmNode.RdfNode is VariablePattern) { variableList.Add(edmNode.Name); } subqueryBuilder = QueryBuilder.Select(variableList.ToArray()).Where(tripleList.ToArray()); foreach (var tp in predTripleList) { subqueryBuilder.Optional(gp => gp.Where(tp)); } if (expProp.Filters != null) { ISparqlExpression FilterExp = BuildSparqlFilter(expProp.Filters, "Expand"); subqueryBuilder.Filter(FilterExp); } if (expProp.Top != null & expProp.Top != 0) { subqueryBuilder.Limit(Convert.ToInt32(expProp.Top)); } if (expProp.Skip != null & expProp.Skip != 0) { subqueryBuilder.Offset(Convert.ToInt32(expProp.Skip)); } if (expProp.OrderBy != null) { foreach (var node in expProp.OrderBy.AsEnumerable()) { //var typedNode = node as OrderByPropertyNode; var ordName = (node.Expression as SingleValuePropertyAccessNode).Property.Name; if (ordName.ToLower() == "localid") { ordName = expProp.NavigationProperty.Name; } else { ordName = $"?{ordName}Expand"; } if (node.Direction == OrderByDirection.Ascending) { subqueryBuilder.OrderBy(ordName); } else { subqueryBuilder.OrderByDescending(ordName); } } } optSubQueryTriplePatterns.Add(new SubQueryPattern(subqueryBuilder.BuildQuery())); if (expProp.NestedEdmNodes != null && expProp.NestedEdmNodes.Count > 0) { foreach (var nestedEdmNode in expProp.NestedEdmNodes) { ConstructFromNavProperties(nestedEdmNode, constructList, optSubQueryTriplePatterns); } } } } }