Esempio n. 1
0
 public bool Add(Statement s)
 {
     if (filters != null && filters.Length > 0 &&
         !LiteralFilter.MatchesFilters(s.Object, filters, model))
     {
         return(true);
     }
     return(sink.Add(s));
 }
Esempio n. 2
0
            public override bool Add(VariableBindings result)
            {
                Resource subj = si == -1 ? d.Subject : result.Values[si];
                Resource pred = pi == -1 ? d.Predicate : result.Values[pi];
                Resource obj  = oi == -1 ? d.Object : result.Values[oi];

                if (!(subj is Entity) || !(pred is Entity))
                {
                    return(true);
                }
                if (litFilters != null && !LiteralFilter.MatchesFilters(obj, litFilters, null))
                {
                    return(true);
                }
                return(sink.Add(new Statement((Entity)subj, (Entity)pred, obj)));
            }
Esempio n. 3
0
        }                                                               // not sure...

        public override void Select(SelectFilter filter, SelectableSource targetModel, StatementSink sink)
        {
            if (filter.Subjects == null)
            {
                filter.Subjects = new Entity[] { new Variable("subject") }
            }
            ;
            if (filter.Predicates == null)
            {
                filter.Predicates = new Entity[] { new Variable("predicate") }
            }
            ;
            if (filter.Objects == null)
            {
                filter.Objects = new Entity[] { new Variable("object") }
            }
            ;

            if (filter.Metas == null)
            {
                filter.Metas = new Entity[] { Statement.DefaultMeta }
            }
            ;

            foreach (Statement s in filter)               // until we can operate on filter directly
            {
                ArrayList evidence = prove(rules, targetModel, new Statement[] { s }, -1);
                if (evidence == null)
                {
                    continue;                     // not provable (in max number of steps, if that were given)
                }
                foreach (EvidenceItem ei in evidence)
                {
                    foreach (Statement h in ei.head)                       // better be just one statement
                    {
                        if (filter.LiteralFilters != null &&
                            !LiteralFilter.MatchesFilters(h.Object, filter.LiteralFilters, targetModel))
                        {
                            continue;
                        }

                        sink.Add(h);
                    }
                }
            }
        }
Esempio n. 4
0
        bool Select(Entity[] subjects, Entity[] predicates, Resource[] objects, Entity[] metas, LiteralFilter[] litFilters, int limit, StatementSink sink, bool ask)
        {
            // TODO: Change meta into named graphs.  Anything but a null or DefaultMeta
            // meta returns no statements immediately.
            if (metas != null && (metas.Length != 1 || metas[0] != Statement.DefaultMeta))
            {
                return(false);
            }

            string query;
            bool   nonull = false;

            if (subjects != null && subjects.Length == 1 &&
                predicates != null && predicates.Length == 1 &&
                objects != null && objects.Length == 1)
            {
                query  = "ASK WHERE { " + S(subjects[0], null) + " " + S(predicates[0], null) + " " + S(objects[0], null) + "}";
                nonull = true;
            }
            else
            {
                if (ask)
                {
                    query = "ASK";
                }
                else
                {
                    query = "SELECT *";
                }
                query += " WHERE { ";
                query += S(subjects, "subject");
                query += " ";
                query += S(predicates, "predicate");
                query += " ";
                query += S(objects, "object");
                query += " . ";
                query += SL(subjects, "subject");
                query += SL(predicates, "predicate");
                query += SL(objects, "object");
                query += " }";

                // TODO: Pass literal filters to server.
            }

            if (limit >= 1)
            {
                query += " LIMIT " + limit;
            }

            XmlDocument result = Load(query);

            if (ask || nonull)
            {
                foreach (XmlElement boolean in result.DocumentElement)
                {
                    if (boolean.Name != "boolean")
                    {
                        continue;
                    }
                    bool ret = boolean.InnerText == "true";
                    if (ask)
                    {
                        return(ret);
                    }
                    else if (ret)
                    {
                        sink.Add(new Statement(subjects[0], predicates[0], objects[0]));
                    }
                    return(false);
                }
                throw new ApplicationException("Invalid server response: No boolean node.");
            }

            XmlElement bindings = null;

            foreach (XmlElement e in result.DocumentElement)
            {
                if (e.Name == "results")
                {
                    bindings = e;
                }
            }
            if (bindings == null)
            {
                throw new ApplicationException("Invalid server response: No result node.");
            }

            MemoryStore distinctCheck = null;

            if (bindings.GetAttribute("distinct") != "true")
            {
                distinctCheck = new MemoryStore();
            }

            Hashtable bnodes = new Hashtable();

            foreach (XmlNode bindingnode in bindings)
            {
                if (!(bindingnode is XmlElement))
                {
                    continue;
                }
                XmlElement binding = (XmlElement)bindingnode;
                Resource   subj    = GetBinding(binding, "subject", subjects, bnodes);
                Resource   pred    = GetBinding(binding, "predicate", predicates, bnodes);
                Resource   obj     = GetBinding(binding, "object", objects, bnodes);
                if (!(subj is Entity) || !(pred is Entity))
                {
                    continue;
                }
                Statement s = new Statement((Entity)subj, (Entity)pred, obj);
                if (distinctCheck != null && distinctCheck.Contains(s))
                {
                    continue;
                }
                if (litFilters != null && !LiteralFilter.MatchesFilters(s.Object, litFilters, this))
                {
                    continue;
                }
                if (!sink.Add(s))
                {
                    return(true);
                }
                if (distinctCheck != null)
                {
                    distinctCheck.Add(s);
                }
            }

            return(true);
        }
Esempio n. 5
0
        /// <summary>
        /// Selects Statements from the Source based on a Filter
        /// </summary>
        /// <param name="filter">Statement Filter</param>
        /// <param name="sink">Sink to stream results to</param>
        public void Select(SelectFilter filter, StatementSink sink)
        {
            //Don't support filters on Metas for the Graph Source
            if (filter.Metas != null)
            {
                throw new RdfException("The dotNetRDF GraphSource does not support SemWeb filters which use Meta filters");
            }

            //Want to build an IEnumerable based on the Filter
            IEnumerable <Triple> ts = Enumerable.Empty <Triple>();
            INode s, p, o;

            if (filter.Subjects != null)
            {
                if (filter.Predicates != null)
                {
                    //Subject-Predicate filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s = SemWebConverter.FromSemWeb(subj, this._mapping);
                        foreach (Entity pred in filter.Predicates)
                        {
                            p  = SemWebConverter.FromSemWeb(pred, this._mapping);
                            ts = ts.Concat(this._g.GetTriplesWithSubjectPredicate(s, p));
                        }
                    }
                }
                else if (filter.Objects != null)
                {
                    //Subject-Object filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s = SemWebConverter.FromSemWeb(subj, this._mapping);
                        foreach (Resource obj in filter.Objects)
                        {
                            o  = SemWebConverter.FromSemWeb(obj, this._mapping);
                            ts = ts.Concat(this._g.GetTriplesWithSubjectObject(s, o));
                        }
                    }
                }
                else
                {
                    //Subjects filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s  = SemWebConverter.FromSemWeb(subj, this._mapping);
                        ts = ts.Concat(this._g.GetTriplesWithSubject(s));
                    }
                }
            }
            else if (filter.Predicates != null)
            {
                if (filter.Objects != null)
                {
                    //Predicate-Object Filter
                    foreach (Entity pred in filter.Predicates)
                    {
                        p = SemWebConverter.FromSemWeb(pred, this._mapping);
                        foreach (Resource obj in filter.Objects)
                        {
                            o  = SemWebConverter.FromSemWeb(obj, this._mapping);
                            ts = ts.Concat(this._g.GetTriplesWithPredicateObject(p, o));
                        }
                    }
                }
                else
                {
                    //Predicate Filter
                    foreach (Entity pred in filter.Predicates)
                    {
                        p  = SemWebConverter.FromSemWeb(pred, this._mapping);
                        ts = ts.Concat(this._g.GetTriplesWithPredicate(p));
                    }
                }
            }
            else if (filter.Objects != null)
            {
                //Object Filter
                foreach (Resource obj in filter.Objects)
                {
                    o  = SemWebConverter.FromSemWeb(obj, this._mapping);
                    ts = ts.Concat(this._g.GetTriplesWithObject(o));
                }
            }
            else
            {
                //Everything is null so this is a Select All
                ts = this._g.Triples;
            }

            int count = 0;

            foreach (Triple t in ts)
            {
                //Apply limit if applicable
                if (filter.Limit > 0 && count >= filter.Limit)
                {
                    return;
                }

                //Convert to a Statement and apply applicable Literal Filters
                Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
                if (filter.LiteralFilters != null)
                {
                    if (LiteralFilter.MatchesFilters(stmt.Object, filter.LiteralFilters, this))
                    {
                        //If the Object matched the filters then we return the Triple and stop
                        //streaming if the sink tells us to
                        if (!sink.Add(stmt))
                        {
                            return;
                        }
                        count++;
                    }
                    //If it doesn't match the filter it is ignored
                }
                else
                {
                    //Just add the statement and stop if the sink tells us to stop streaming
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                    count++;
                }
            }
        }
Esempio n. 6
0
        private void Select2(SelectFilter filter, StatementSink result)
        {
            // Don't select on columns that we already know from the template.
            SelectColumnFilter columns = new SelectColumnFilter();

            columns.Subject   = (filter.Subjects == null) || (filter.Subjects.Length > 1);
            columns.Predicate = (filter.Predicates == null) || (filter.Predicates.Length > 1);
            columns.Object    = (filter.Objects == null) || (filter.Objects.Length > 1);
            columns.Meta      = (filter.Metas == null) || (filter.Metas.Length > 1);;

            if (filter.Predicates != null | Array.IndexOf(filter.Predicates, rdfs_member) != 1)
            {
                columns.Predicate = true;
            }

            // Have to select something
            if (!columns.Subject && !columns.Predicate && !columns.Object && !columns.Meta)
            {
                columns.Subject = true;
            }

            System.Text.StringBuilder cmd = new System.Text.StringBuilder("SELECT ");
            if (!connection.AreStatementsUnique)
            {
                cmd.Append("DISTINCT ");
            }

            ArrayList cols = new ArrayList();

            if (columns.Subject)
            {
                cols.Add("sinfo.type"); cols.Add("sinfo.value");
            }
            if (columns.Predicate)
            {
                cols.Add("pinfo.type"); cols.Add("pinfo.value");
            }
            if (columns.Object)
            {
                cols.Add("oinfo.type"); cols.Add("oinfo.value"); cols.Add("oinfo.language"); cols.Add("oinfo.datatype");
            }
            if (columns.Meta)
            {
                cols.Add("minfo.type"); cols.Add("minfo.value");
            }
            cmd.Append(String.Join(", ", (String[])cols.ToArray(typeof(String))));

            cmd.Append(" FROM ");
            cmd.Append(prefix);
            cmd.Append("_statements AS q");

            if (columns.Subject)
            {
                cmd.Append(" LEFT JOIN ");
                cmd.Append(prefix);
                cmd.Append("_values AS sinfo ON q.subject = sinfo.id");
            }
            if (columns.Predicate)
            {
                cmd.Append(" LEFT JOIN ");
                cmd.Append(prefix);
                cmd.Append("_values AS pinfo ON q.predicate = pinfo.id");
            }
            if (columns.Object)
            {
                cmd.Append(" LEFT JOIN ");
                cmd.Append(prefix);
                cmd.Append("_values AS oinfo ON q.object = oinfo.id");
            }
            if (columns.Meta)
            {
                cmd.Append(" LEFT JOIN ");
                cmd.Append(prefix);
                cmd.Append("_values AS minfo ON q.meta = minfo.id");
            }

            cmd.Append(' ');

            bool wroteWhere = WhereClause(filter, cmd);

            // Transform literal filters into SQL.
            if (filter.LiteralFilters != null)
            {
                foreach (LiteralFilter f in filter.LiteralFilters)
                {
                    string s = FilterToSQL(f, "oinfo.value");
                    if (s != null)
                    {
                        if (!wroteWhere)
                        {
                            cmd.Append(" WHERE "); wroteWhere = true;
                        }
                        else
                        {
                            cmd.Append(" AND ");
                        }
                        cmd.Append(' ');
                        cmd.Append(s);
                    }
                }
            }

            if (filter.Limit >= 1)
            {
                cmd.Append(" LIMIT ");
                cmd.Append(filter.Limit);
            }

            cmd.Append(';');

            if (Debug)
            {
                string cmd2 = cmd.ToString();
                //if (cmd2.Length > 80) cmd2 = cmd2.Substring(0, 80);
                Console.Error.WriteLine(cmd2);
            }

            using (IDataReader reader = connection.RunReader(cmd.ToString())) {
                while (reader.Read())
                {
                    Entity   s = columns.Subject ? null : filter.Subjects[0];
                    Entity   p = columns.Predicate ? null : filter.Predicates[0];
                    Resource o = columns.Object ? null : filter.Objects[0];
                    Entity   m = columns.Meta ? null : filter.Metas[0];

                    int col = 0;
                    if (columns.Subject)
                    {
                        s = SelectEntity(reader.GetInt32(col++), reader.GetString(col++));
                    }
                    if (columns.Predicate)
                    {
                        p = SelectEntity(reader.GetInt32(col++), reader.GetString(col++));
                    }
                    if (columns.Object)
                    {
                        o = SelectResource(reader.GetInt32(col++), reader.GetString(col++), reader.GetString(col++), reader.GetString(col++));
                    }
                    if (columns.Meta)
                    {
                        m = SelectEntity(reader.GetInt32(col++), reader.GetString(col++));
                    }

                    if (filter.LiteralFilters != null && !LiteralFilter.MatchesFilters(o, filter.LiteralFilters, this))
                    {
                        continue;
                    }

                    bool ret = result.Add(new Statement(s, p, o, m));
                    if (!ret)
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Selects Statements from the Source based on a Filter
        /// </summary>
        /// <param name="filter">Statement Filter</param>
        /// <param name="sink">Sink to stream results to</param>
        public void Select(SelectFilter filter, StatementSink sink)
        {
            IEnumerable <Triple> ts = Enumerable.Empty <Triple>();

            if (filter.Metas != null)
            {
                //This applies over some Graphs
                foreach (Entity meta in filter.Metas)
                {
                    if (meta.Uri != null)
                    {
                        Uri graphUri = new Uri(meta.Uri);
                        if (this._store.HasGraph(graphUri))
                        {
                            ts = ts.Concat(this.FilterToEnumerable(filter, this._store.Graph(graphUri)));
                        }
                    }
                }
            }
            else
            {
                //This applies over all Graphs
                foreach (IGraph g in this._store.Graphs)
                {
                    ts = ts.Concat(this.FilterToEnumerable(filter, g));
                }
            }

            int count = 0;

            foreach (Triple t in ts)
            {
                //Apply limit if applicable
                if (filter.Limit > 0 && count >= filter.Limit)
                {
                    return;
                }

                Statement stmt = SemWebConverter.ToSemWeb(t, this.GetMapping(t.Graph));
                stmt.Meta = new Entity(t.GraphUri.ToString());

                if (filter.LiteralFilters != null)
                {
                    if (LiteralFilter.MatchesFilters(stmt.Object, filter.LiteralFilters, this))
                    {
                        //If the Object matched the filters then we return the Triple and stop
                        //streaming if the sink tells us to
                        if (!sink.Add(stmt))
                        {
                            return;
                        }
                        count++;
                    }
                }
                else
                {
                    //Just add the statement and stop if the sink tells us to stop streaming
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                    count++;
                }
            }
        }
Esempio n. 8
0
        protected void LoadToSink(SourceDescriptor sourceDescr, IList <object> ids, IList <FieldDescriptor> predFlds, Resource[] vals, StatementSink sink, SelectFilter flt)
        {
            // todo: more effective impl using IDbDalc datareader
            var ds       = new DataSet();
            var q        = new Query(sourceDescr.SourceName);
            var flds     = predFlds ?? sourceDescr.Fields;
            var loadType = flt.Predicates == null || flt.Predicates.Contains(NS.Rdf.typeEntity);

            q.Fields    = new string[flds.Count + 1];
            q.Fields[0] = sourceDescr.IdFieldName;
            for (int i = 0; i < flds.Count; i++)
            {
                q.Fields[i + 1] = flds[i].FieldName;
            }
            // compose query condition
            var condition = new QueryGroupNode(GroupType.And);

            if (ids != null)
            {
                condition.Nodes.Add(ComposeCondition(sourceDescr.IdFieldName, ids.ToArray()));
            }
            if (vals != null && !loadType)
            {
                var orGrp = new QueryGroupNode(GroupType.Or);
                for (int i = 0; i < flds.Count; i++)
                {
                    var valCnd = ComposeCondition(flds[i], vals);
                    if (valCnd != null)
                    {
                        orGrp.Nodes.Add(valCnd);
                    }
                }
                if (orGrp.Nodes.Count == 0)
                {
                    return;                     //values are not for this source
                }
                condition.Nodes.Add(orGrp);
            }
            if (flt.LiteralFilters != null)
            {
                var literalFltCondition = ComposeLiteralFilter(flds, flt.LiteralFilters);
                if (literalFltCondition != null)
                {
                    condition.Nodes.Add(literalFltCondition);
                }
            }
            q.Root = condition;
            if (flt.Limit > 0)
            {
                q.RecordCount = flt.Limit;
            }
            // log
            log.Write(LogEvent.Debug, q);
            Console.WriteLine(q.ToString());
            // query result handler
            Action <IDataReader> loadToSinkAction = delegate(IDataReader dataReader) {
                int recIndex = 0;
                while (dataReader.Read() && (recIndex < q.RecordCount))
                {
                    recIndex++;
                    var itemEntity = GetSourceItemEntity(sourceDescr, dataReader[sourceDescr.IdFieldName]);
                    for (int j = 0; j < flds.Count; j++)
                    {
                        var f   = flds[j];
                        var obj = PrepareResource(f, dataReader[f.FieldName]);
                        if (vals == null || vals.Contains(obj))
                        {
                            // literals post-filter
                            if (flt.LiteralFilters != null && !LiteralFilter.MatchesFilters(obj, flt.LiteralFilters, this))
                            {
                                continue;
                            }
                            if (!sink.Add(new Statement(itemEntity, EntityFieldHash[f], obj)))
                            {
                                return;
                            }
                        }
                    }
                    // type predicate
                    if (loadType && flt.LiteralFilters == null)
                    {
                        if (vals == null || vals.Contains(EntitySourceHash[sourceDescr]))
                        {
                            if (!sink.Add(
                                    new Statement(itemEntity, NS.Rdf.typeEntity, EntitySourceHash[sourceDescr])))
                            {
                                return;
                            }
                        }
                    }
                }
            };

            // DB DALC datareader optimization
            if (Dalc is IDbDalc)
            {
                var  dbDalc    = (IDbDalc)Dalc;
                bool closeConn = false;
                try {
                    if (dbDalc.Connection.State != ConnectionState.Open)
                    {
                        dbDalc.Connection.Open();
                        closeConn = true;
                    }
                    IDataReader rdr = dbDalc.LoadReader(q);
                    try {
                        loadToSinkAction(rdr);
                    } finally {
                        rdr.Close();
                    }
                } finally {
                    if (closeConn)
                    {
                        dbDalc.Connection.Close();
                    }
                }
            }
            else
            {
                Dalc.Load(ds, q);
                var tblRdr = ds.Tables[q.SourceName].CreateDataReader();
                try {
                    loadToSinkAction(tblRdr);
                } finally {
                    tblRdr.Close();
                }
            }
        }