/// <summary> /// Find items based on a template pattern /// </summary> /// <param name="query"></param> /// <param name="stopAfterFirst"></param> /// <returns>Returns matching items if found. Returns empty (non-null) array if none found.</returns> public JToken FindItems(JObject query, bool stopAfterFirst = true) { var results = new JArray(); foreach (var source in this.Items) { if (source.Value == null) { continue; } foreach (var candidateItem in source.Value) { var joItem = (JObject)candidateItem.DeepClone(); var u = Unification.Unify(joItem, query); if (u == null) { continue; } joItem.Add("#unification", u); results.Add(joItem); if (stopAfterFirst) { return(results[0]); } } } return(results); }
public JToken Query(JToken query) { if (query.Type == JTokenType.Object) { var joQuery = (JObject)query; var select = joQuery["#select"]; if (select == null) { return(query); } else { joQuery.Remove("#select"); } var result = new JArray(); // {"#item": "?x", "select": "username"} foreach (var querySource in this.Items) { if (querySource.Value.Type == JTokenType.Object) { var joQuerySource = (JObject)querySource.Value; var items = joQuerySource["items"]; if (items == null) { continue; } var jaItems = (JArray)items; foreach (var item in jaItems) { var joItem = (JObject)item.DeepClone(); var unification = Unification.Unify(joItem, joQuery); if (unification == null) { continue; } if (select == null) { joItem.Add("#unification", unification); result.Add(joItem); } else { var sSelect = select.ToString(); if (sSelect == "*") { result.Add(joItem); } else { var value = joItem[select.ToString()]; result.Add(value); } } } } } if (result.Count == 1) { return(result.First); } else { return(result); } } else if (query.Type == JTokenType.Array) { return(query); } else if (query.Type == JTokenType.String) { return(query); } return(null); }