Exemple #1
0
        /// <summary>
        /// takes the list of Predicates and add tothem the domain and Range data needed  in two steps
        /// 1st : searching for direct Domain and ranges
        /// 2nd : searching for predicates who don't have domain and ranges and select the all predicates and objects
        /// get their types and add them to the domain and range field
        /// </summary>
        /// <param name="predicateList">the predicate list without the domain and range data</param>
        /// <returns>the predicate list with the predicates have domain and ranges</returns>
        private List <LexiconPredicate> addDomainAndRange(List <LexiconPredicate> predicateList)
        {
            //now interating over the final predicate list and fill the rest of it's details <Domain , Range>
            // step 1 : the Direct Way
            foreach (LexiconPredicate x in predicateList)
            {
                string Query = "Select distinct ?domain ?range where { {" +

                               "<" + x.URI + ">" + "<http://www.w3.org/2000/01/rdf-schema#domain> ?domain.}" +
                               "union { <" + x.URI + ">" + " <http://www.w3.org/2000/01/rdf-schema#range> ?range ." +
                               "}}";

                //QueryHandler.startConnection();
                //SparqlResultSet resulSet = QueryHandler.ExecuteQueryWithString(Query);
                //QueryHandler.closeConnection();
                SparqlResultSet resulSet = Request.RequestWithHTTP(Query);

                if (resulSet.Count() != 0)
                {
                    foreach (SparqlResult result in resulSet)
                    {
                        // check that the domain field is not empty  // and check that this domain wasn't added before
                        if (result.Value("domain") != null)
                        {
                            if (!x.domains.Contains(result.Value("domain").ToString()))
                            {
                                x.domains.Add(result.Value("domain").ToString());
                            }
                        }

                        // check that the range field is not empty  // and check that this range wasn't added before
                        if (result.Value("range") != null)
                        {
                            if (!x.ranges.Contains(result.Value("range").ToString()) && result.Value("range") != null)
                            {
                                x.ranges.Add(result.Value("range").ToString());
                            }
                        }
                    }
                }
            }

            List <LexiconPredicate> toRemove = new List <LexiconPredicate>();

            //step2 : the inDirect Way -- for predicates that didn't have a Domain or range selected before
            foreach (LexiconPredicate x in predicateList)
            {
                bool hasDomain = (x.domains.Count == 0) ? false : true;
                bool hasRange  = (x.ranges.Count == 0) ? false : true;


                if (!hasDomain && !hasRange)
                {
                    toRemove.Add(x);
                    continue;
                }

                string Query = "Select distinct ";
                Query += (hasDomain) ? "" : "?domain";
                Query += (hasRange) ? "" : "?range";
                Query += " where {{";

                if (!hasDomain)
                {
                    Query += "{ ?X <" + x.URI + "> ?Y ." +
                             "?X a ?domain . " +
                             "filter(!REGEX(STR(?domain) ,'http://www.w3.org/2002/07/owl#Thing','i'))" +
                             "}";
                }

                Query += "}";

                if (!hasRange)
                {
                    Query += "union { ?X <" + x.URI + "> ?Y ." +
                             "?Y a ?range . " +
                             "filter(!REGEX(STR(?range) ,'http://www.w3.org/2002/07/owl#Thing','i'))" +
                             "}";
                }

                Query += "}";

                Query += "limit 20 ";



                //QueryHandler.startConnection();
                //SparqlResultSet resulSet = QueryHandler.ExecuteQueryWithString(Query);
                //QueryHandler.closeConnection();
                SparqlResultSet resulSet = Request.RequestWithHTTP(Query);

                if (resulSet.Count() != 0)
                {
                    foreach (SparqlResult result in resulSet)
                    {
                        // check that the domain field is not empty  // and check that this domain wasn't added before
                        if (!hasDomain)
                        {
                            if (result.Value("domain") != null)
                            {
                                if (!x.domains.Contains(result.Value("domain").ToString()))
                                {
                                    x.domains.Add(result.Value("domain").ToString());
                                }
                            }
                        }
                        // check that the range field is not empty  // and check that this range wasn't added before
                        if (!hasRange)
                        {
                            if (result.Value("range") != null)
                            {
                                if (!x.ranges.Contains(result.Value("range").ToString()) && result.Value("range") != null)
                                {
                                    x.ranges.Add(result.Value("range").ToString());
                                }
                            }
                        }
                    }
                }
            }
            foreach (LexiconPredicate x in toRemove)
            {
                predicateList.Remove(x);
            }

            return(predicateList);
        }