/// <summary> /// Exectues a certain List of InnerQuery objects /// </summary> /// <param name="input">the list of innerquery to be queried</param> /// <returns>a list of resultSet one for each innerquery.queryText</returns> public static List<ResSetToJSON.innerResult> ExecuteQueryWithInnerQuery(SPARQLQueryBuilder.InnerQuery input,string obj1,string obj2) { //list to hold the results List<ResSetToJSON.innerResult> resultsList = new List<ResSetToJSON.innerResult>(); try { //temp result holder ResSetToJSON.innerResult temp; //fetching results and passing to the list temp = new ResSetToJSON.innerResult(); // //temp.firstObj = obj1; //temp.lastObj = obj2; temp = setOriginalObjects(temp,input.queryText); temp.connectState = (int)input.connectState; temp.resultSets = ExecuteQueryWithString(input.queryText); //if there's any results add it if (temp.resultSets.Count > 0) resultsList.Add(temp); } catch { } return resultsList; }
/// <summary> /// Builds and returns a set of queries to find relations between two object1 and object2. /// </summary> /// <param name="object1">object1</param> /// <param name="object2">object2</param> /// <param name="maxDistance">MaxiumDistance between the two objects</param> /// <param name="limit">Limit of results</param> /// <param name="ignoredObjects">List of strings of names of objects be ignored in the Queries</param> /// <param name="ignoredProperties">List of strings of names of properties to be ignored in the Queries</param> /// <param name="avoidCycles">Integer value which indicates whether we want to suppress cycles , 0 = no cycle avoidance , 1 = no intermediate object can be object1 or object2 , 2 = like 1 + an object can not occur more than once in a connection</param> /// <returns>false means an error happened, true means it's ok</returns> public bool generateQueries(string object1, string object2, int maxDistance=3, int limit=50, List<string> ignoredObjects = null, List<string> ignoredProperties = null, int avoidCycles = 1) { //here we reset everything because this method is only used upon new comparison //resetting the bool isEndOfResults = false; //resetting the list of queries generatedQueriesList = new List<SPARQLQueryBuilder.InnerQuery>(); //resetting the resultlist curResultSet2 = new List<string>(); //resetting the uniqueList of JsonObjects uniqueJsonObjects = new List<string>(); //to make other methods see the two objects obj1 = object1; obj2 = object2; SPARQLQueryBuilder builder = new SPARQLQueryBuilder(); //hardcoded objects to ignore (For testing purposes) ignoredObjects = new List<string>(); ignoredObjects.Add("http://dbpedia.org/ontology/wikiPageWikiLink"); ignoredObjects.Add("http://dbpedia.org/ontology/wikiPageRedirects"); ignoredObjects.Add("http://www.w3.org/2002/07/owl#Thing"); ignoredObjects.Add("http://www.opengis.net/gml/_Feature"); generatedQueriesList = builder.buildQueries(object1, object2, maxDistance, limit, ignoredObjects, ignoredObjects, avoidCycles); //generatedQueriesList=builder.buildQueries(object1, object2, maxDistance, limit, ignoredObjects, ignoredProperties, avoidCycles); //if an error happened if (generatedQueriesList.Count < 1) return false; return true; }
/// <summary> /// Builds and returns a set of queries to find relations between two object1 and object2. /// </summary> /// <param name="object1">object1</param> /// <param name="object2">object2</param> /// <param name="maxDistance">MaxiumDistance between the two objects</param> /// <param name="limit">Limit of results</param> /// <param name="ignoredObjects">List of strings of names of objects be ignored in the Queries</param> /// <param name="ignoredProperties">List of strings of names of properties to be ignored in the Queries</param> /// <param name="avoidCycles">Integer value which indicates whether we want to suppress cycles , 0 = no cycle avoidance , 1 = no intermediate object can be object1 or object2 , 2 = like 1 + an object can not occur more than once in a connection</param> /// <returns>false means an error happened, true means it's ok</returns> public bool generateQueries(string object1, string object2, int maxDistance = 3, int limit = 50, List <string> ignoredObjects = null, List <string> ignoredProperties = null, int avoidCycles = 1) { //here we reset everything because this method is only used upon new comparison //resetting the bool isEndOfResults = false; //resetting the list of queries generatedQueriesList = new List <SPARQLQueryBuilder.InnerQuery>(); //resetting the resultlist curResultSet2 = new List <string>(); //resetting the uniqueList of JsonObjects uniqueJsonObjects = new List <string>(); //to make other methods see the two objects obj1 = object1; obj2 = object2; SPARQLQueryBuilder builder = new SPARQLQueryBuilder(); //hardcoded objects to ignore (For testing purposes) ignoredObjects = new List <string>(); ignoredObjects.Add("http://dbpedia.org/ontology/wikiPageWikiLink"); ignoredObjects.Add("http://dbpedia.org/ontology/wikiPageRedirects"); ignoredObjects.Add("http://www.w3.org/2002/07/owl#Thing"); ignoredObjects.Add("http://www.opengis.net/gml/_Feature"); generatedQueriesList = builder.buildQueries(object1, object2, maxDistance, limit, ignoredObjects, ignoredObjects, avoidCycles); //generatedQueriesList=builder.buildQueries(object1, object2, maxDistance, limit, ignoredObjects, ignoredProperties, avoidCycles); //if an error happened if (generatedQueriesList.Count < 1) { return(false); } return(true); }
/// <summary> /// gets the relations between a list of uris the relations is in form of predicates and objects /// </summary> /// <param name="uri"> is the list of URI to fnd relation between</param> /// <param name="Distance">find the relations that lies at this distance ie : if distance = 3 there are 3 predicates between two URIs</param> /// <param name="Limit">the limit of relations to find between each two URIs</param> /// <returns>list of containing lists , each lists contains strings in order to discribe the relation /// Egypt >> type >> populated place << type<< syria /// </returns> public static List <List <string> > getRelations(List <string> uri, int Distance, int Limit = 50) { List <string> ignoredPredicates = Regex.Split(File.ReadAllText("ignoredPredicates.txt"), "\r\n|\n").ToList(); List <string> ignoredObjects = Regex.Split(File.ReadAllText("ignoredObjects.txt"), "\r\n|\n").ToList(); //Making combinations of URIs to get relations between List <KeyValuePair <string, string> > URIPairs = getCombinations(uri); SPARQLQueryBuilder QueryBuilder = new SPARQLQueryBuilder(); List <SPARQLQueryBuilder.InnerQuery> Queries = new List <SPARQLQueryBuilder.InnerQuery>(); //Building Queries foreach (KeyValuePair <string, string> pair in URIPairs) { List <SPARQLQueryBuilder.InnerQuery> tmpinnerQueries = QueryBuilder.buildQueries(pair.Key, pair.Value, Distance, Limit, ignoredObjects, ignoredPredicates, 1); Queries.AddRange(tmpinnerQueries); } //executing Queries List <List <string> > URIS = new List <List <string> >(); foreach (SPARQLQueryBuilder.InnerQuery Q in Queries) { //SparqlResultSet resultSet = QueryHandler.ExecuteQueryWithString(Q.queryText); SparqlResultSet resultSet = Request.RequestWithHTTP(Q.queryText); if (Q.connectState == SPARQLQueryBuilder.connectionType.connectedDirectly || Q.connectState == SPARQLQueryBuilder.connectionType.connectedDirectlyInverted) { foreach (SparqlResult result in resultSet) { List <string> toAdd = new List <string>(); toAdd.Add(Q.object1); /// foreach (KeyValuePair<string, INode> row in result.ToList()); for (int i = 1; i <= Distance; i++) { if (result.HasValue("pf" + i)) { toAdd.Add(result.Value("pf" + i).ToString()); } if (result.HasValue("of" + i)) { toAdd.Add(result.Value("of" + i).ToString()); } } toAdd.Add(Q.object2); URIS.Add(toAdd); } } else if (Q.connectState == SPARQLQueryBuilder.connectionType.connectedViaMiddle || Q.connectState == SPARQLQueryBuilder.connectionType.connectedViaMiddleInverted) { foreach (SparqlResult result in resultSet) { List <string> toAdd = new List <string>(); toAdd.Add(Q.object1); bool lastAdded = false; // false for literal . true for predicate for (int i = 1; i < Distance; i++) { if (result.HasValue("pf" + i) || result.HasValue("of" + i)) { if (!lastAdded) { if (result.HasValue("pf" + i)) { toAdd.Add(result.Value("pf" + i).ToString()); lastAdded = true; } if (result.HasValue("of" + i)) { toAdd.Add(result.Value("of" + i).ToString()); lastAdded = false; } } else { if (result.HasValue("of" + i)) { toAdd.Add(result.Value("of" + i).ToString()); lastAdded = false; } if (result.HasValue("pf" + i)) { toAdd.Add(result.Value("pf" + i).ToString()); lastAdded = true; } } } } toAdd.Add(result.Value("middle").ToString()); lastAdded = false; // false for literal . true for predicate for (int i = Distance; i >= 1; i--) { if (result.HasValue("ps" + i) || result.HasValue("os" + i)) { if (!lastAdded) { if (result.HasValue("ps" + i)) { toAdd.Add(result.Value("ps" + i).ToString()); lastAdded = true; } if (result.HasValue("os" + i)) { toAdd.Add(result.Value("os" + i).ToString()); lastAdded = false; } } else { if (result.HasValue("os" + i)) { toAdd.Add(result.Value("os" + i).ToString()); lastAdded = false; } if (result.HasValue("ps" + i)) { toAdd.Add(result.Value("ps" + i).ToString()); lastAdded = true; } } } } toAdd.Add(Q.object2); URIS.Add(toAdd); } } } //relationsComparer c = new relationsComparer(); return(URIS);//.Distinct(c).ToList(); }