Exemplo n.º 1
0
 /// <summary>
 /// Adds the specified statement.
 /// </summary>
 /// <param name="statement">The statement.</param>
 public override void Add(Statement statement)
 {
     if (statement.Meta == Statement.DefaultMeta)
         AddStatement(statement);
     else
         AddFormulaStatement(statement);
 }
 public static Literal SelectLiteral(this SelectableSource src, Statement tpl)
 {
     var st = SelectSingle(src, tpl);
     if (st.HasValue && st.Value.Object is Literal)
         return (Literal)st.Value.Object;
     return null;
 }
Exemplo n.º 3
0
            public bool Add(SemWeb.Statement stmt)
            {
                string predicate = stmt.Predicate.Uri;
                string title     = System.IO.Path.GetFileName(predicate);
                string bg        = InfoDisplay.Color(info.Style.Background(Gtk.StateType.Active));
                string fg        = InfoDisplay.Color(info.Style.Foreground(Gtk.StateType.Active));

                //if (MetadataStore.Namespaces.GetPrefix (path) != null) {
                if (stmt.Object is Literal)
                {
                    stream.Write("<tr><td valign=top align=right bgcolor=\"" + bg + "\"><font color=\"" + fg + "\">");
                    stream.Write(title);
                    stream.Write("</font></td><td>");

                    string s = ((SemWeb.Literal)(stmt.Object)).Value;

                    /*
                     * else {
                     *      MemoryStore store = source.Select (stmt.Invert (), new SelectPartialFilter (true, false, false, false));
                     *      s = "";
                     *      foreach (Statement sub in store) {
                     *              s += sub.Object.ToString () + "/n";
                     *      }
                     * }
                     */
                    if (s != null && s != String.Empty)
                    {
                        stream.Write(s);
                    }
                    stream.Write("</td><tr>");
                }
                return(true);
            }
Exemplo n.º 4
0
		public bool Matches(Statement statement) {
			if (Subject != null && Subject != statement.Subject && statement.Subject != null) return false;
			if (Predicate != null && Predicate != statement.Predicate && statement.Predicate != null) return false;
			if (Object != null && Object != statement.Object && statement.Object != null) return false;
			if (Meta != null && Meta != statement.Meta && statement.Meta != null) return false;
			return true;
		}
Exemplo n.º 5
0
		public override MetaQueryResult MetaQuery(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource source) {
			SemWeb.Query.MetaQueryResult ret = new SemWeb.Query.MetaQueryResult();
			
			ret.QuerySupported = true;
			
			ret.NoData = new bool[graph.Length];
			for (int i = 0; i < graph.Length; i++) {
				// Take this statement and replace variables by nulls
				// to make it a statement template.
				Statement st = graph[i];
				for (int j = 0; j < 4; j++) {
					if (st.GetComponent(j) is Variable)
						st.SetComponent(j, null);
				}
				
				// See if the store contains this template.
				if (st != Statement.All && !source.Contains(st)) {
					ret.NoData[i] = true;
					continue;
				}
			
				// Process it further in case we have variables
				// with known values, in which case if none of the
				// known values is in the store, we also know this
				// statement is unanswerable.
				for (int j = 0; j < 4; j++) {
					Resource r = graph[i].GetComponent(j);
					
					// No need to check the following given the check above.
					//if (r != null && !(r is Variable) && !source.Contains(r))
					//	ret.NoData[i] = true;
					
					if (r != null && r is Variable && options.VariableKnownValues != null && 
					#if !DOTNET2
					options.VariableKnownValues.Contains((Variable)r)
					#else
					options.VariableKnownValues.ContainsKey((Variable)r)
					#endif
					) {
						bool found = false;
						#if !DOTNET2
						foreach (Resource s in (ICollection)options.VariableKnownValues[(Variable)r]) {
						#else
						foreach (Resource s in (ICollection<Resource>)options.VariableKnownValues[(Variable)r]) {
						#endif
							if (source.Contains(s)) {
								found = true;
								break;
							}
						}
						if (!found) {
							ret.NoData[i] = true;
						}
					}
				}
			}
			
			return ret;
		}
Exemplo n.º 6
0
 public override SemWeb.Query.MetaQueryResult MetaQuery(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource targetModel)
 {
     QueryCheckArg(graph);
     SemWeb.Query.MetaQueryResult ret = new SemWeb.Query.MetaQueryResult();
     ret.QuerySupported = true;
     // TODO: Best to check also whether variables in the query are even known to us.
     return ret;
 }
Exemplo n.º 7
0
            public bool Add(SemWeb.Statement stmt)
            {
                if (stmt.Predicate == MetadataStore.Namespaces.Resolve("tiff:ImageWidth"))
                {
                    if (width == null)
                    {
                        width = ((SemWeb.Literal)stmt.Object).Value;
                    }
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("tiff:ImageLength"))
                {
                    if (height == null)
                    {
                        height = ((SemWeb.Literal)stmt.Object).Value;
                    }
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("exif:PixelXDimension"))
                {
                    width = ((SemWeb.Literal)stmt.Object).Value;
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("exif:PixelYDimension"))
                {
                    height = ((SemWeb.Literal)stmt.Object).Value;
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("exif:ExposureTime"))
                {
                    exposure = ((SemWeb.Literal)stmt.Object).Value;
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("exif:ApertureValue"))
                {
                    aperture = ((SemWeb.Literal)stmt.Object).Value;
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("exif:FNumber"))
                {
                    fnumber = ((SemWeb.Literal)stmt.Object).Value;
                }
                else if (stmt.Predicate == MetadataStore.Namespaces.Resolve("exif:ISOSpeedRatings"))
                {
                    iso_anon = stmt.Object;
                }
                else if (stmt.Subject == iso_anon && stmt.Predicate == MetadataStore.Namespaces.Resolve("rdf:li"))
                {
                    iso_speed = ((SemWeb.Literal)stmt.Object).Value;
                }
                else if (add && stmt.Subject.Uri == null)
                {
                    store.Add(stmt);
                }

                if (width == null || height == null || exposure == null || aperture == null || iso_speed == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Exemplo n.º 8
0
		private void QueryCheckArg(Statement[] graph) {
			if (graph == null) throw new ArgumentNullException("graph");
			foreach (Statement s in graph) {
				if (s.Subject == null || s.Predicate == null || s.Object == null || s.Meta == null)
					throw new ArgumentNullException("Graph statements cannot contain a null subject, predicate, or object. Use a Variable instance instead.");
				if (s.Meta != Statement.DefaultMeta && !(s.Meta is Variable))
					throw new NotSupportedException("Graph statements' meta fields must be Statement.DefaultMeta. Other values of meta are not currently supported.");
			}
		}
Exemplo n.º 9
0
		void Add(Statement schemastatement) {
			if (schemastatement.Predicate == subClassOf && schemastatement.Object is Entity)
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, superclasses, subclasses, true);
			if (schemastatement.Predicate == subPropertyOf && schemastatement.Object is Entity)
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, superprops, subprops, true);
			if (schemastatement.Predicate == domain && schemastatement.Object is Entity)
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, domains, domainof, false);
			if (schemastatement.Predicate == range && schemastatement.Object is Entity)
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, ranges, rangeof, false);
		}
Exemplo n.º 10
0
 public bool Add(Statement st)
 {
     int s = indexes.ContainsKey(st.Subject) ? (int)indexes[st.Subject] : -1;
     int p = indexes.ContainsKey(st.Predicate) ? (int)indexes[st.Predicate] : -1;
     int o = indexes.ContainsKey(st.Object) ? (int)indexes[st.Object] : -1;
     if (s != -1 && p != -1) { connectivity[s,p]=true; connectivity[p,s]=true; }
     if (s != -1 && o != -1) { connectivity[s,o]=true; connectivity[o,s]=true; }
     if (p != -1 && o != -1) { connectivity[p,o]=true; connectivity[o,p]=true; }
     return true;
 }
Exemplo n.º 11
0
		public override void Add(Statement statement) {
			if (statement.AnyNull) throw new ArgumentNullException();
			if (checkForDuplicates && Contains(statement)) return;
			statements.Add(statement);
			if (isIndexed) {
				GetIndexArray(statementsAboutSubject, statement.Subject).Add(statement);
				GetIndexArray(statementsAboutObject, statement.Object).Add(statement);
			}
			if (!checkForDuplicates) distinct = false;
		}
		bool Select(Statement template, StatementSink sink, bool ask) {
			return Select(
				template.Subject == null ? null : new Entity[] { template.Subject },
				template.Predicate == null ? null : new Entity[] { template.Predicate },
				template.Object == null ? null : new Resource[] { template.Object },
				template.Meta == null ? null : new Entity[] { template.Meta },
				null,
				0,
				sink,
				ask
				);
		}
Exemplo n.º 13
0
			public override void Add (Statement stmt) 
			{
				string predicate = stmt.Predicate.Uri;
				string prefix;
				string localname;

				// Fill in the namespaces with nice prefixes
				if (MetadataStore.Namespaces.Normalize (predicate, out prefix, out localname)) {
					if (prefix != null)
						Namespaces.AddNamespace (predicate.Remove (predicate.Length - localname.Length, localname.Length), prefix);
				}
				base.Add (stmt);
			}
Exemplo n.º 14
0
		void AddAxiom(Statement schemastatement) {
			if (schemastatement.Predicate == subClassOf && schemastatement.Object is Entity) {
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, superclasses, subclasses);
				AddRelation(schemastatement.Subject, rdfsresource, superclasses, subclasses);
				AddRelation((Entity)schemastatement.Object, rdfsresource, superclasses, subclasses);
			}
			if (schemastatement.Predicate == subPropertyOf && schemastatement.Object is Entity)
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, superprops, subprops);
			if (schemastatement.Predicate == domain && schemastatement.Object is Entity) {
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, domains, domainof);
				AddRelation((Entity)schemastatement.Object, rdfsresource, superclasses, subclasses);
			}
			if (schemastatement.Predicate == range && schemastatement.Object is Entity) {
				AddRelation(schemastatement.Subject, (Entity)schemastatement.Object, ranges, rangeof);
				AddRelation((Entity)schemastatement.Object, rdfsresource, superclasses, subclasses);
			}
		}
Exemplo n.º 15
0
        /// <summary>
        /// Converts a dotNetRDF Triple to a SemWeb Statement
        /// </summary>
        /// <param name="t">Triple</param>
        /// <param name="mapping">Mapping of Blank Nodes</param>
        /// <returns></returns>
        public static Statement ToSemWeb(Triple t, SemWebMapping mapping)
        {
            Entity s, p;
            Resource o;
            Statement stmt;
            if (t.IsGroundTriple)
            {
                //Easy to map across without need for BNode mapping
                stmt = new Statement(ToSemWebEntity(t.Subject), ToSemWebEntity(t.Predicate), ToSemWeb(t.Object));
            }
            else
            {
                s = ToSemWebEntity(t.Subject, mapping);
                p = ToSemWebEntity(t.Predicate, mapping);
                o = ToSemWeb(t.Object, mapping);

                stmt = new Statement(s, p, o);
            }
            return stmt;
        }
Exemplo n.º 16
0
 public void Extract(Assembly assembly, Store rdfStore)
 {
     var types = assembly.GetExportedTypes();
     foreach (var t in types) {
         // store class hierarchy
         var typeEntity = GetEntity(t);
         if (t.IsInterface) {
             var stType = new Statement(typeEntity, NS.Rdf.typeEntity, NS.CSO.interfaceEntity);
             if (!rdfStore.Contains(stType)) {
                 rdfStore.Add(stType);
                 rdfStore.AddLabel(typeEntity, t.FullName);
             }
         } else if (t.IsClass) {
             var stType = new Statement(typeEntity, NS.Rdf.typeEntity, NS.CSO.classEntity);
             if (!rdfStore.Contains(stType)) {
                 rdfStore.Add(stType);
                 rdfStore.AddLabel(typeEntity, t.FullName);
             }
         } else
             continue;
         if (t.BaseType != null)
             rdfStore.Add(new Statement(typeEntity, NS.Rdfs.subClassOfEntity, GetEntity(t.BaseType) ));
         // store info about interfaces
         var ifaces = t.GetInterfaces();
         foreach (var iType in ifaces) {
             rdfStore.Add(new Statement(typeEntity, NS.CSO.Implements, GetEntity(iType)));
             rdfStore.Add(new Statement(typeEntity, NS.Rdfs.subClassOfEntity, GetEntity(iType)));
         }
         // store info about properties
         var props = t.GetProperties(BindingFlags.Public|BindingFlags.SetProperty|BindingFlags.DeclaredOnly|BindingFlags.Instance);
         foreach (var p in props) {
             var propEntity = NS.DotNet.GetPropertyEntity(p.Name);
             rdfStore.Add(new Statement(propEntity, NS.Rdf.typeEntity, NS.Rdf.PropertyEntity));
             rdfStore.AddLabel(propEntity, p.Name);
             rdfStore.Add(new Statement(propEntity, NS.Rdfs.domainEntity, typeEntity));
         }
     }
 }
Exemplo n.º 17
0
		public abstract void Query(Statement[] graph, QueryOptions options, SelectableSource targetModel, QueryResultSink result);
Exemplo n.º 18
0
		public abstract MetaQueryResult MetaQuery(Statement[] graph, QueryOptions options, SelectableSource targetModel);
Exemplo n.º 19
0
		public Proof(Statement[] proved, ProofStep[] steps) {
			Proved = proved;
			Steps = steps;
		}
Exemplo n.º 20
0
		public abstract bool Distinct { get; } // assume targetModel.Distinct is true, *then* would Select be distinct?

		public void Select(Statement template, SelectableSource targetModel, StatementSink sink) {
			Select(new SelectFilter(template), targetModel, sink);
		}
Exemplo n.º 21
0
		public Rule(Statement[] antecedent, Statement[] consequent) {
			Antecedent = antecedent;
			Consequent = consequent;
		}
Exemplo n.º 22
0
		public override void Add(Statement statement) {
			if (statement.AnyNull) throw new ArgumentNullException();
			WriteStatement2(URI(statement.Subject), URI(statement.Predicate),
				statement.Object is Literal ? ((Literal)statement.Object).ToString() : URI((Entity)statement.Object));
		}
Exemplo n.º 23
0
        /// <summary>
        /// Executes a Graph Pattern style query against the Source
        /// </summary>
        /// <param name="graph">Graph Pattern</param>
        /// <param name="options">Query Options</param>
        /// <param name="sink">Results Sink</param>
        /// <remarks>
        /// <para>
        /// This is implemented by transforming the Graph Pattern which is a set of SemWeb Statement templates into a SPARQL Algebra BGP.  The resulting algebra is then executed using the Leviathan engine and the results converted into VariableBindings for SemWeb
        /// </para>
        /// <para>
        /// The only Query Option that is supported is the Limit option
        /// </para>
        /// </remarks>
        public void Query(Statement[] graph, SW.Query.QueryOptions options, SW.Query.QueryResultSink sink)
        {
            ISparqlAlgebra algebra = this.ToAlgebra(graph);

            SparqlEvaluationContext context = new SparqlEvaluationContext(null, new InMemoryDataset(this._store));
            BaseMultiset results = context.Evaluate(algebra);//algebra.Evaluate(context);

            sink.Init(results.Variables.Select(v => new Variable(v)).ToArray());
            if (results.Count > 0)
            {
                int c = 0;
                foreach (Set s in results.Sets)
                {
                    //Apply Limit if applicable
                    if (options.Limit > 0 && c >= options.Limit)
                    {
                        sink.Finished();
                        return;
                    }

                    //Convert the Set to VariableBindings for SemWeb
                    Variable[] vars = s.Variables.Select(v => new Variable(v)).ToArray();
                    Resource[] resources = s.Variables.Select(v => SemWebConverter.ToSemWeb(s[v], this.GetMapping(s[v].Graph))).ToArray();
                    SW.Query.VariableBindings bindings = new SW.Query.VariableBindings(vars, resources);

                    //Keep adding results until the sink tells us to stop
                    if (!sink.Add(bindings))
                    {
                        sink.Finished();
                        return;
                    }
                    c++;
                }
                sink.Finished();
            }
            else
            {
                sink.Finished();
            }
        }
Exemplo n.º 24
0
 /// <summary>
 /// Returns a Meta Result that says that the Query is supported
 /// </summary>
 /// <param name="graph">Graph Pattern</param>
 /// <param name="options">Query Options</param>
 /// <returns></returns>
 /// <remarks>
 /// <para>
 /// The method does not actually do any analysis of the query, it assumes that all Graph Patterns expressible in SemWeb statement templates can be transformed to a SPARQL Algebra BGP (which generally they can)
 /// </para>
 /// </remarks>
 public SW.Query.MetaQueryResult MetaQuery(Statement[] graph, SW.Query.QueryOptions options)
 {
     SW.Query.MetaQueryResult metaResult = new SW.Query.MetaQueryResult();
     metaResult.QuerySupported = true;
     return metaResult;
 }
Exemplo n.º 25
0
 public abstract void Add(Statement statement);
Exemplo n.º 26
0
 /// <summary>
 /// Writes the statement to the output stream.
 /// </summary>
 /// <param name="statement">The statement.</param>
 protected virtual void WriteStatement(Statement statement)
 {
     WriteEntity(statement.Subject);
     Write(' ');
     WriteStatementBody(statement);
 }
Exemplo n.º 27
0
		public override void Query(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource targetModel, QueryResultSink result) {
			SemWeb.Query.GraphMatch q = new SemWeb.Query.GraphMatch();
			foreach (Statement s in graph)
				q.AddGraphStatement(s);
				
			q.ReturnLimit = options.Limit;
			
			if (options.VariableKnownValues != null) {
			#if !DOTNET2
			foreach (DictionaryEntry ent in options.VariableKnownValues)
				q.SetVariableRange((Variable)ent.Key, (ICollection)ent.Value);
			#else
			foreach (KeyValuePair<Variable,ICollection<Resource>> ent in options.VariableKnownValues)
				q.SetVariableRange(ent.Key, ent.Value);
			#endif
			}

			if (options.VariableLiteralFilters != null) {			
			#if !DOTNET2
			foreach (DictionaryEntry ent in options.VariableLiteralFilters)
				foreach (LiteralFilter filter in (ICollection)ent.Value)
					q.AddLiteralFilter((Variable)ent.Key, filter);
			#else
			foreach (KeyValuePair<Variable,ICollection<LiteralFilter>> ent in options.VariableLiteralFilters)
				foreach (LiteralFilter filter in ent.Value)
					q.AddLiteralFilter(ent.Key, filter);
			#endif
			}
			
			if (options.DistinguishedVariables != null) {
				foreach (Variable v in options.DistinguishedVariables)
					q.SetDistinguishedVariable(v);
			}

			q.Run(targetModel, result);
		}
Exemplo n.º 28
0
        /// <summary>
        /// Removes Triples that match the template from the underlying Store
        /// </summary>
        /// <param name="template">Template</param>
        public void Remove(Statement template)
        {
            //Get the Graphs over which the Remove will operate
            IEnumerable<IGraph> gs;
            if (template.Meta == null)
            {
                gs = this._store.Graphs;
            }
            else
            {
                Uri graphUri = new Uri(template.Meta.Uri);
                if (this._store.HasGraph(graphUri))
                {
                    gs = this._store.Graph(graphUri).AsEnumerable();
                }
                else
                {
                    gs = Enumerable.Empty<IGraph>();
                }
            }

            //Retract the Triples which match the Template in each affected Graph
            foreach (IGraph g in gs)
            {
                IEnumerable<Triple> ts = this.TemplateToEnumerable(template, g);
                g.Retract(ts);
            }
        }
Exemplo n.º 29
0
 /// <summary>
 /// Adds the statement.
 /// </summary>
 /// <param name="statement">The statement (not a formula).</param>
 protected virtual void AddStatement(Statement statement)
 {
     WriteStatement(statement);
     FlushWriter();
 }
Exemplo n.º 30
0
 /// <summary>
 /// Removes Triples which match any of the templates from the underlying Store
 /// </summary>
 /// <param name="templates">Templates</param>
 public void RemoveAll(Statement[] templates)
 {
     foreach (Statement template in templates)
     {
         this.Remove(template);
     }
 }
Exemplo n.º 31
0
 /// <summary>
 /// Writes the statement body to the output writer.
 /// </summary>
 /// <param name="statement">The statement.</param>
 protected virtual void WriteStatementBody(Statement statement)
 {
     WritePredicate(statement.Predicate);
     Write(' ');
     WriteResource(statement.Object);
     Write('.');
     WriteLine();
 }
Exemplo n.º 32
0
 /// <summary>
 /// Throws an error since the Replace operation is not valid in the dotNetRDF API model
 /// </summary>
 /// <param name="find">Find Template</param>
 /// <param name="replacement">Replace Template</param>
 public void Replace(Statement find, Statement replacement)
 {
     throw new RdfException("The SemWeb Replace operation is not valid for dotNetRDF Data Sources");
 }
Exemplo n.º 33
0
        /// <summary>
        /// Adds the statement of a formula.
        /// </summary>
        /// <param name="statement">The statement.</param>
        protected virtual void AddFormulaStatement(Statement statement)
        {
            Entity formulaId = statement.Meta;
            Entity statementId = new BNode();

            if (m_LastFormulaId != formulaId)
            {
                m_LastFormulaId = formulaId;
                AddStatement(new Statement(formulaId, Predicate.RdfType, Identifier.LogFormula));
            }

            AddStatement(new Statement(formulaId, Predicate.LogIncludes, statementId));
            AddStatement(new Statement(statementId, Predicate.RdfType, Identifier.RdfStatement));
            AddStatement(new Statement(statementId, Predicate.RdfSubject, statement.Subject));
            AddStatement(new Statement(statementId, Predicate.RdfPredicate, statement.Predicate));
            AddStatement(new Statement(statementId, Predicate.RdfObject, statement.Object));
        }
Exemplo n.º 34
0
 bool StatementSink.Add(Statement statement)
 {
     Add(statement);
     return(true);
 }