コード例 #1
0
 protected IQueryConditionNode ComposeLiteralCondition(FieldDescriptor fld, LiteralFilter filter)
 {
     if (filter is StringCompareFilter)
     {
         var f = (StringCompareFilter)filter;
         return(new QueryConditionNode((QField)fld.FieldName, GetQueryCondition(f.Type), new QConst(f.Pattern)));
     }
     else if (filter is StringContainsFilter)
     {
         var f = (StringContainsFilter)filter;
         return(new QueryConditionNode((QField)fld.FieldName, Conditions.Like, new QConst(String.Format("%{0}%", f.Pattern))));
     }
     else if (filter is StringStartsWithFilter)
     {
         var f = (StringStartsWithFilter)filter;
         return(new QueryConditionNode((QField)fld.FieldName, Conditions.Like, new QConst(String.Format("{0}%", f.Pattern))));
     }
     else if (filter is StringEndsWithFilter)
     {
         var f = (StringEndsWithFilter)filter;
         return(new QueryConditionNode((QField)fld.FieldName, Conditions.Like, new QConst(String.Format("%{0}", f.Pattern))));
     }
     else if (filter is NumericCompareFilter)
     {
         var f = (NumericCompareFilter)filter;
         if (fld.FieldType == null || !IsNumericType(fld.FieldType))
         {
             return(null);                    // avoid SQL 'cannot compare' error
         }
         // should we check for fld type?
         return(new QueryConditionNode((QField)fld.FieldName, GetQueryCondition(f.Type), new QConst(f.Number)));
     }
     return(null);
 }
コード例 #2
0
        public void AddValueFilter(Entity entity, LiteralFilter filter)
        {
            SetupValueFilter d = new SetupValueFilter();

            d.a = entity;
            d.b = filter;
            setupValueFilters.Add(d);
        }
コード例 #3
0
 public bool Add(Statement s)
 {
     if (filters != null && filters.Length > 0 &&
         !LiteralFilter.MatchesFilters(s.Object, filters, model))
     {
         return(true);
     }
     return(sink.Add(s));
 }
コード例 #4
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)));
            }
コード例 #5
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);
                    }
                }
            }
        }
コード例 #6
0
ファイル: Query.cs プロジェクト: hnafar/semweb-dotnet
        public void AddLiteralFilter(Variable variable, LiteralFilter filter)
        {
            if (VariableLiteralFilters == null)
            {
                VariableLiteralFilters = new LitFilterMap();
            }
            LitFilterList list = null;

                        #if DOTNET2
            if (VariableLiteralFilters.ContainsKey(variable))
                        #endif
            list = (LitFilterList)VariableLiteralFilters[variable];
            if (list == null)
            {
                list = new LitFilterList();
                VariableLiteralFilters[variable] = list;
            }
            list.Add(filter);
        }
コード例 #7
0
ファイル: SQLStore2.cs プロジェクト: hnafar/semweb-dotnet
        private string FilterToSQL(LiteralFilter filter, string col)
        {
            if (filter is SemWeb.Filters.StringCompareFilter)
            {
                SemWeb.Filters.StringCompareFilter f = (SemWeb.Filters.StringCompareFilter)filter;
                return(col + FilterOpToSQL(f.Type) + Escape(f.Pattern, true));
            }

            /*if (filter is SemWeb.Filters.StringContainsFilter) {
             *      SemWeb.Filters.StringContainsFilter f = (SemWeb.Filters.StringContainsFilter)filter;
             *      return CreateLikeTest(col, f.Pattern, 1); // 1=contains
             * }*/
            if (filter is SemWeb.Filters.StringStartsWithFilter)
            {
                SemWeb.Filters.StringStartsWithFilter f = (SemWeb.Filters.StringStartsWithFilter)filter;
                return(CreatePrefixTest(col, f.Pattern));                // 0=starts-with
            }
            if (filter is SemWeb.Filters.NumericCompareFilter)
            {
                SemWeb.Filters.NumericCompareFilter f = (SemWeb.Filters.NumericCompareFilter)filter;
                return(col + FilterOpToSQL(f.Type) + f.Number);
            }
            return(null);
        }
コード例 #8
0
ファイル: SQLStore.cs プロジェクト: shariqatariq/profiles-rns
		private string FilterToSQL(LiteralFilter filter, string col) {
			if (filter is SemWeb.Filters.StringCompareFilter) {
				SemWeb.Filters.StringCompareFilter f = (SemWeb.Filters.StringCompareFilter)filter;
				return col + FilterOpToSQL(f.Type) + Escape(f.Pattern);
			}
			if (filter is SemWeb.Filters.StringContainsFilter) {
				SemWeb.Filters.StringContainsFilter f = (SemWeb.Filters.StringContainsFilter)filter;
				return CreateLikeTest(col, f.Pattern, 1); // 1=contains
			}
			if (filter is SemWeb.Filters.StringStartsWithFilter) {
				SemWeb.Filters.StringStartsWithFilter f = (SemWeb.Filters.StringStartsWithFilter)filter;
				return CreateLikeTest(col, f.Pattern, 0); // 0=starts-with
			}
			if (filter is SemWeb.Filters.StringEndsWithFilter) {
				SemWeb.Filters.StringEndsWithFilter f = (SemWeb.Filters.StringEndsWithFilter)filter;
				return CreateLikeTest(col, f.Pattern, 2); // 2==ends-with
			}
			if (filter is SemWeb.Filters.NumericCompareFilter) {
				SemWeb.Filters.NumericCompareFilter f = (SemWeb.Filters.NumericCompareFilter)filter;
				return col + FilterOpToSQL(f.Type) + f.Number;
			}
			return null;
		}
コード例 #9
0
ファイル: SQLStore.cs プロジェクト: shariqatariq/profiles-rns
		private string FilterOpToSQL(LiteralFilter.CompType op) {
			switch (op) {
			case LiteralFilter.CompType.LT: return " < ";
			case LiteralFilter.CompType.LE: return " <= ";
			case LiteralFilter.CompType.NE: return " <> ";
			case LiteralFilter.CompType.EQ: return " = ";
			case LiteralFilter.CompType.GT: return " > ";
			case LiteralFilter.CompType.GE: return " >= ";
			default: throw new ArgumentException(op.ToString());
			}			
		}
コード例 #10
0
		private string FilterToSQL(LiteralFilter filter, string col) {
			if (filter is SemWeb.Filters.StringCompareFilter) {
				SemWeb.Filters.StringCompareFilter f = (SemWeb.Filters.StringCompareFilter)filter;
				return col + FilterOpToSQL(f.Type) + Escape(f.Pattern, true);
			}
			if (filter is SemWeb.Filters.StringContainsFilter) {
				SemWeb.Filters.StringContainsFilter f = (SemWeb.Filters.StringContainsFilter)filter;
				return col + " LIKE " + quote + "%" + Escape(f.Pattern, false).Replace("%", "\\%") + "%" + quote;
			}
			if (filter is SemWeb.Filters.StringStartsWithFilter) {
				SemWeb.Filters.StringStartsWithFilter f = (SemWeb.Filters.StringStartsWithFilter)filter;
				return col + " LIKE " + quote + Escape(f.Pattern, false).Replace("%", "\\%") + "%" + quote;
			}
			if (filter is SemWeb.Filters.NumericCompareFilter) {
				SemWeb.Filters.NumericCompareFilter f = (SemWeb.Filters.NumericCompareFilter)filter;
				return col + FilterOpToSQL(f.Type) + f.Number;
			}
			return null;
		}
コード例 #11
0
ファイル: SQLStore.cs プロジェクト: shariqatariq/profiles-rns
		private void Select(Resource templateSubject, Resource templatePredicate, Resource templateObject, Resource templateMeta, LiteralFilter[] litFilters, StatementSink result, int limit) {
			if (result == null) throw new ArgumentNullException();
	
			lock (syncroot) {
			
			Init();
			RunAddBuffer();
			
			// Don't select on columns that we already know from the template.
			// But grab the URIs and literal values for MultiRes selection.
			SelectColumnFilter columns = new SelectColumnFilter();
			columns.SubjectId = (templateSubject == null) || templateSubject is MultiRes;
			columns.PredicateId = (templatePredicate == null) || templatePredicate is MultiRes;
			columns.ObjectId = (templateObject == null) || templateObject is MultiRes;
			columns.MetaId = (templateMeta == null) || templateMeta is MultiRes;
			columns.SubjectUri = templateSubject == null;
			columns.PredicateUri = templatePredicate == null;
			columns.ObjectData = templateObject == null || (templateObject is MultiRes && ((MultiRes)templateObject).ContainsLiterals());
			columns.MetaUri = templateMeta == null;
			
			if (isOrContains(templatePredicate, rdfs_member)) {
				columns.PredicateId = true;
				columns.PredicateUri = true;
			}
			
			// Meta URIs tend to be repeated a lot, so we don't
			// want to ever select them from the database.
			// This preloads them, although it makes the first
			// select quite slow.
			/*if (templateMeta == null && SupportsSubquery) {
				LoadMetaEntities();
				columns.MetaUri = false;
			}*/
			
			// Have to select something
			bool fakeSubjectIdSelect = false;
			if (!columns.SubjectId && !columns.PredicateId && !columns.ObjectId && !columns.MetaId) {
				columns.SubjectId = true;
				fakeSubjectIdSelect = true;
			}
				
			// Pre-cache the IDs of resources in a MultiRes. TODO: Pool these into one array.
			foreach (Resource r in new Resource[] { templateSubject, templatePredicate, templateObject, templateMeta }) {
				MultiRes mr = r as MultiRes;
				if (mr == null) continue;
				PrefetchResourceIds(mr.items);
				CleanMultiRes(mr);
				if (mr.items.Length == 0) // no possible values
					return;
			}
				
			// SQLite has a problem with LEFT JOIN: When a condition is made on the
			// first table in the ON clause (q.objecttype=0/1), when it fails,
			// it excludes the row from the first table, whereas it should only
			// exclude the results of the join.
						
			System.Text.StringBuilder cmd = new System.Text.StringBuilder("SELECT ");
			if (!SupportsLimitClause && limit >= 1) {
				cmd.Append("TOP ");
				cmd.Append(limit);
				cmd.Append(' ');
			}
			if (!HasUniqueStatementsConstraint)
				cmd.Append("DISTINCT ");
			SelectFilterColumns(columns, cmd);
			cmd.Append(" FROM ");
			cmd.Append(table);
			cmd.Append("_statements AS q");
			SelectFilterTables(columns, cmd);
			cmd.Append(' ');
			
			bool wroteWhere;
			if (!WhereClause(templateSubject, templatePredicate, templateObject, templateMeta, cmd, out wroteWhere)) return;
			
			// Transform literal filters into SQL.
			if (litFilters != null) {
				foreach (LiteralFilter f in litFilters) {
					string s = FilterToSQL(f, "lit.value");
					if (s != null) {
						if (!wroteWhere) { cmd.Append(" WHERE "); wroteWhere = true; }
						else { cmd.Append(" AND "); }
						cmd.Append(' ');
						cmd.Append(s);
					}
				}
			}
			
			if (SupportsLimitClause && limit >= 1) {
				cmd.Append(" LIMIT ");
				cmd.Append(limit);
			}

			cmd.Append(';');
			
			if (Debug) {
				string cmd2 = cmd.ToString();
				//if (cmd2.Length > 80) cmd2 = cmd2.Substring(0, 80);
				Console.Error.WriteLine(cmd2);
			}
			
			Hashtable entMap = new Hashtable();
			
			// Be sure if a MultiRes is involved we hash the
			// ids of the entities so we can return them
			// without creating new ones.
			CacheMultiObjects(entMap, templateSubject);
			CacheMultiObjects(entMap, templatePredicate);
			CacheMultiObjects(entMap, templateObject);
			CacheMultiObjects(entMap, templateMeta);
			
			using (IDataReader reader = RunReader(cmd.ToString())) {
				while (reader.Read()) {
					int col = 0;
                    Int64 sid = -1, pid = -1, ot = -1, oid = -1, mid = -1;
					string suri = null, puri = null, ouri = null, muri = null;
					string lv = null, ll = null, ld = null;
					
					if (columns.SubjectId) { sid = reader.GetInt64(col++); }
					if (columns.PredicateId) { pid = reader.GetInt64(col++); }
					if (columns.ObjectId) { oid = reader.GetInt64(col++); }
					if (columns.MetaId) { mid = reader.GetInt64(col++); }
					
					if (columns.SubjectUri) { suri = AsString(reader[col++]); }
					if (columns.PredicateUri) { puri = AsString(reader[col++]); }
					if (columns.ObjectData) { ot = reader.GetInt64(col++); ouri = AsString(reader[col++]); lv = AsString(reader[col++]); ll = AsString(reader[col++]); ld = AsString(reader[col++]);}
					if (columns.MetaUri) { muri = AsString(reader[col++]); }
					
					Entity subject = GetSelectedEntity(sid, suri, templateSubject, columns.SubjectId && !fakeSubjectIdSelect, columns.SubjectUri, entMap);
					Entity predicate = GetSelectedEntity(pid, puri, templatePredicate, columns.PredicateId, columns.PredicateUri, entMap);
					Resource objec = GetSelectedResource(oid, ot, ouri, lv, ll, ld, templateObject, columns.ObjectId, columns.ObjectData, entMap);
					Entity meta = GetSelectedEntity(mid, muri, templateMeta, columns.MetaId, columns.MetaUri, templateMeta != null ? entMap : null);

					if (litFilters != null && !LiteralFilter.MatchesFilters(objec, litFilters, this))
						continue;
						
					bool ret = result.Add(new Statement(subject, predicate, objec, meta));
					if (!ret) break;
				}
			}
			
			} // lock
		}
コード例 #12
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();
                }
            }
        }
コード例 #13
0
        public void SelectByTemplateTest()
        {
            var store = new DalcRdfStore();

            store.Dalc = PrepareDalc();
            string ns_foaf_name = "http://xmlns.com/foaf/0.1/name";
            string ns_accounts  = "urn:test:accounts";
            string ns_number    = "urn:test:number";
            string ns_id        = "urn:test:id";
            string ns_aff_id    = "urn:test:aff_id";

            store.Sources = new[] {
                new DalcRdfStore.SourceDescriptor()
                {
                    Ns          = ns_accounts,
                    IdFieldName = "id",
                    RdfType     = NS.Rdfs.Class,
                    SourceName  = "accounts",
                    Fields      = new [] {
                        new DalcRdfStore.FieldDescriptor()
                        {
                            FieldName = "name",
                            Ns        = ns_foaf_name,
                            RdfType   = NS.Rdf.Property,
                            FieldType = typeof(string)
                        },
                        new DalcRdfStore.FieldDescriptor()
                        {
                            FieldName = "age",
                            FieldType = typeof(int),
                            Ns        = "urn:test:age",
                            RdfType   = NS.Rdf.Property
                        },
                        new DalcRdfStore.FieldDescriptor()
                        {
                            FieldName = "number",
                            FieldType = typeof(int),
                            Ns        = ns_number,
                            RdfType   = NS.Rdf.Property
                        },
                        new DalcRdfStore.FieldDescriptor()
                        {
                            FieldType = typeof(int),
                            FieldName = "id",
                            Ns        = ns_id,
                            RdfType   = NS.Rdf.Property
                        },
                        new DalcRdfStore.FieldDescriptor()
                        {
                            FieldType    = typeof(int),
                            FieldName    = "aff_id",
                            Ns           = ns_aff_id,
                            RdfType      = NS.Rdf.Property,
                            FkSourceName = "accounts"
                        }
                    }
                }
            };
            store.Init();
            //System.Diagnostics.Debugger.Break();
            var ns_account1 = "urn:test:accounts#1";
            var ns_account2 = "urn:test:accounts#2";
            var ns_account3 = "urn:test:accounts#3";
            var ns_age      = "urn:test:age";

            Assert.IsFalse(store.Contains(new Statement(ns_account1, ns_age, new Literal("2"))));
            Assert.IsTrue(store.Contains(new Statement(ns_account2, ns_age, new Literal("2"))));
            Assert.IsTrue(store.Contains(new Statement(null, ns_age, new Literal("2"))));

            var sinkMock = GetSinkMock();

            // test case: no subject
            store.Select(new Statement(null, ns_age, new Literal("2")), sinkMock.Object);
            sinkMock.Verify(a => a.Add(new Statement(ns_account2, ns_age, new Literal("2"))), Times.Exactly(1));

            var sinkMock2 = GetSinkMock();

            // test case: only subject
            store.Select(new Statement(ns_account1, null, null), sinkMock2.Object);
            sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_age, new Literal("26"))), Times.Exactly(1));
            sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_number, new Literal("2"))), Times.Exactly(1));
            sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_foaf_name, new Literal("Vitalik"))), Times.Exactly(1));
            sinkMock2.Verify(a => a.Add(new Statement(ns_account1, NS.Rdf.type, (Entity)ns_accounts)), Times.Exactly(1));
            sinkMock2.Verify(a => a.Add(new Statement(ns_account1, ns_aff_id, (Entity)ns_account3)), Times.Exactly(1));

            // test case: select filter
            var sinkMock3 = GetSinkMock();

            store.Select(
                new SelectFilter(null, new[] { (Entity)ns_age, (Entity)ns_number }, new[] { new Literal("2") }, null), sinkMock3.Object);
            sinkMock3.Verify(a => a.Add(new Statement(ns_account1, ns_number, new Literal("2"))), Times.Exactly(1));
            sinkMock3.Verify(a => a.Add(new Statement(ns_account2, ns_age, new Literal("2"))), Times.Exactly(1));

            var sinkMock4 = GetSinkMock();

            store.Select(new Statement(null, ns_aff_id, (Entity)ns_account1), sinkMock4.Object);
            sinkMock4.Verify(a => a.Add(new Statement(ns_account2, ns_aff_id, (Entity)ns_account1)), Times.Exactly(1));

            // test case: schema selects
            Assert.IsTrue(store.Contains(new Statement(ns_aff_id, NS.Rdf.type, NS.Rdf.PropertyEntity)));
            Assert.IsTrue(store.Contains(new Statement(ns_aff_id, NS.Rdfs.domainEntity, (Entity)ns_accounts)));

            //store.Select(new N3Writer(Console.Out));

            int schemaStatements = 0;
            var sinkMock5        = GetSinkMock();

            sinkMock5.Setup(a => a.Add(It.IsAny <Statement>())).Callback(() => schemaStatements++).Returns(true);
            store.Select(new Statement(null, NS.Rdf.typeEntity, NS.Rdfs.ClassEntity), sinkMock5.Object);
            Assert.AreEqual(1, schemaStatements);
            sinkMock5.Verify(a => a.Add(new Statement(ns_accounts, NS.Rdf.typeEntity, NS.Rdfs.ClassEntity)), Times.Exactly(1));

            store.Select(new Statement(null, NS.Rdfs.domainEntity, (Entity)ns_accounts), sinkMock5.Object);
            Assert.AreEqual(1 + 5, schemaStatements);
            sinkMock5.Verify(a => a.Add(new Statement(ns_foaf_name, NS.Rdfs.domainEntity, (Entity)ns_accounts)), Times.Exactly(1));
            sinkMock5.Verify(a => a.Add(new Statement(ns_age, NS.Rdfs.domainEntity, (Entity)ns_accounts)), Times.Exactly(1));

            // test case: literal filter
            int litFilterStatements = 0;
            var sinkMock6           = GetSinkMock();

            sinkMock6.Setup(a => a.Add(It.IsAny <Statement>())).Callback(() => litFilterStatements++).Returns(true);
            store.Select(
                new SelectFilter(new[] { (Entity)ns_account3 }, null, null, null)
            {
                LiteralFilters = new [] { LiteralFilter.Create(LiteralFilter.CompType.GT, 20) }
            },
                sinkMock6.Object);
            Assert.AreEqual(2, litFilterStatements);
            sinkMock6.Verify(a => a.Add(new Statement(ns_account3, ns_age, new Literal("52"))), Times.Exactly(1));
            sinkMock6.Verify(a => a.Add(new Statement(ns_account3, ns_number, new Literal("100"))), Times.Exactly(1));

            // test case: select by type
            int typeStatementsCount = 0;
            var sinkMock7           = GetSinkMock();

            sinkMock7.Setup(a => a.Add(It.IsAny <Statement>())).Callback(() => typeStatementsCount++).Returns(true);
            store.Select(new Statement(null, NS.Rdf.typeEntity, (Entity)ns_accounts), sinkMock7.Object);
            Assert.AreEqual(3, typeStatementsCount);

            typeStatementsCount = 0;
            store.Select(new Statement(null, NS.Rdf.typeEntity, null), sinkMock7.Object);
            Assert.AreEqual(9, typeStatementsCount);
        }
コード例 #14
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++;
                }
            }
        }
コード例 #15
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);
        }
コード例 #16
0
ファイル: SQLStore2.cs プロジェクト: hnafar/semweb-dotnet
        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;
                    }
                }
            }
        }
コード例 #17
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;
		}
コード例 #18
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++;
                }
            }
        }
コード例 #19
0
            protected override void extractLiteralFilters(name.levering.ryan.sparql.model.logic.ExpressionLogic node, java.util.Map literalFilters)
            {
                base.extractLiteralFilters(node, literalFilters);

                if (node is BinaryExpressionNode)
                {
                    BinaryExpressionNode b = (BinaryExpressionNode)node;

                    LiteralFilter.CompType comp;
                    if (node is ASTEqualsNode)
                    {
                        comp = LiteralFilter.CompType.EQ;
                    }
                    else if (node is ASTNotEqualsNode)
                    {
                        comp = LiteralFilter.CompType.NE;
                    }
                    else if (node is ASTGreaterThanNode)
                    {
                        comp = LiteralFilter.CompType.GT;
                    }
                    else if (node is ASTGreaterThanEqualsNode)
                    {
                        comp = LiteralFilter.CompType.GE;
                    }
                    else if (node is ASTLessThanNode)
                    {
                        comp = LiteralFilter.CompType.LT;
                    }
                    else if (node is ASTLessThanEqualsNode)
                    {
                        comp = LiteralFilter.CompType.LE;
                    }
                    else
                    {
                        return;
                    }

                    SparqlVariable            var;
                    org.openrdf.model.Literal val;

                    object left  = RemoveCast(b.getLeftExpression());
                    object right = RemoveCast(b.getRightExpression());

                    if (left is ASTVar && right is org.openrdf.model.Literal)
                    {
                        var = (SparqlVariable)left;
                        val = (org.openrdf.model.Literal)right;
                    }
                    else if (right is ASTVar && left is org.openrdf.model.Literal)
                    {
                        var = (SparqlVariable)right;
                        val = (org.openrdf.model.Literal)left;
                        switch (comp)
                        {
                        case LiteralFilter.CompType.LT: comp = LiteralFilter.CompType.GE; break;

                        case LiteralFilter.CompType.LE: comp = LiteralFilter.CompType.GT; break;

                        case LiteralFilter.CompType.GT: comp = LiteralFilter.CompType.LE; break;

                        case LiteralFilter.CompType.GE: comp = LiteralFilter.CompType.LT; break;
                        }
                    }
                    else
                    {
                        return;
                    }

                    object parsedvalue = new Literal(val.getLabel(), null, val.getDatatype() == null ? null : val.getDatatype().ToString()).ParseValue();

                    LiteralFilter filter = LiteralFilter.Create(comp, parsedvalue);
                    addLiteralFilter(var, filter, literalFilters);
                }
            }
コード例 #20
0
		public FilterSink(LiteralFilter[] filters, StatementSink sink, SelectableSource model) {
			this.filters = filters;
			this.sink = sink;
			this.model = model;
		}