SemWeb : SemWeb Namespace
Store Class

The store class is used to group data sources and to provide inferencing capabilities over data sources.

public class Store : ModifiableSource, QueryableSource, StaticSource, IDisposable


Remarks

The SemWeb.MemoryStore is a special subclass of Store which maintains an in-memory array of statements. Although the MemoryStore inherits from Store, it is illegal to call AddSource on a MemoryStore.

Members

See Also: Inherited members from object.

Constructors

Store ()
Creates an empty store with no data storage backing.
Creates a store that wraps the given SelectableSource.
Creates a Store that loads the statements from the StatementSource into memory.

Properties

DataSources [read-only]
System.Collections.Generic.IList<SemWeb.SelectableSource> . To be added.
Distinct [read-only]
bool . Gets whether the store returns only distinct statments from Select calls.
StatementCount [read-only]
int . Gets the number of statements in the store.

Methods

Add (Statement)
Adds a statement to the store.
AddReasoner (SemWeb.Inference.Reasoner)
Adds inferencing capabilities to the Select and Query methods of this Store.
AddSource (SelectableSource)
Adds a data source to this store.
AddSource (SelectableSource, string)
Adds a named data source to this store.
Clear ()
Clears the contents of the store.
Contains (Resource) : bool
Tests whether the store contains any statements that mention the given resource.
Contains (Statement) : bool
Returns whether the store contains a statement, or any statement that matches the template.
static Create (string) : Store
Creates a Store in the factory design pattern.
static CreateForInput (string) : StatementSource
Creates a StatementSource in the factory design pattern from which statements will be read.
static CreateForOutput (string) : StatementSink
Creates a SemWeb.StatementSink into which statements will be added.
static DefaultContains (SelectableSource, Statement) : bool
A default implementation of the Contains operation for implementors of SelectableSource.
static DefaultReplace (ModifiableSource, Entity, Entity)
To be added.
static DefaultReplace (ModifiableSource, Statement, Statement)
To be added.
static DefaultSelect (SelectableSource, SelectFilter, StatementSink)
This method provides a default implementation of the Select method that takes a SelectFilter argument.
Dispose ()
Releases any external resources associated with the Store.
GetBNodeFromPersistentId (string) : BNode
To be added.
GetEntities () : Entity[]
Returns an array of all entities mentioned in the store.
GetEntitiesOfType (Entity) : Entity[]
Returns an array of all entities in the store whose type is known to be the given type.
GetMetas () : Entity[]
Returns an array of all entities used in the Meta field of any statement in the store.
GetPersistentBNodeId (BNode) : string
To be added.
GetPredicates () : Entity[]
Returns an array of all predicates mentioned in the store.
Import (StatementSource)
Batch copies the contents of a StatementSource into the ModifyableSource contained in the store.
MetaQuery (Statement[], SemWeb.Query.QueryOptions) : SemWeb.Query.MetaQueryResult
Tests the querying capabilities of the store.
Query (Statement[]) : System.Collections.Generic.ICollection<SemWeb.Query.VariableBindings>
Queries the data sources in the Store and returns the resulting variable bindings.
Query (Statement[], SemWeb.Query.QueryOptions, SemWeb.Query.QueryResultSink)
Queries the store with a simple graph match query.
Remove (Statement)
Removes statements from the store.
RemoveAll (Statement[])
Removes all statements matching an array of templates.
Replace (Entity, Entity)
Replaces all occurences of one Entity with another Entity.
Replace (Statement, Statement)
Replaces a single statement with another statement.
Select (SelectFilter) : SelectResult
Retuns a SemWeb.SelectResult object that represents the result of the corresponding select call with a StatementSink.
Select (Statement) : SelectResult
Retuns a SemWeb.SelectResult object that represents the result of matching a statement template against the store.
Select (StatementSink)
Streams all statements in this store into a StatementSink.
Select (SelectFilter, StatementSink)
Queries the story for matching statements, with advanced options, and writes the statements to a SemWeb.StatementSink.
Select (Statement, StatementSink)
Queries the story for matching statements, and writes the statements to a SemWeb.StatementSink.
SelectObjects (Entity, Entity) : Resource[]
Finds all objects in statements with the given subject and predicate.
SelectSubjects (Entity, Resource) : Entity[]
Finds all subjects in statements with the given predicate and object.
Write (System.IO.TextWriter)
Writes the contents of the store to a stream in N3 format.

Member Details

Clear Method

public void Clear ()

Clears the contents of the store.

Remarks

An InvalidOperationException is thrown if more than one data source has been added to the Store with AddSource (in which case SemWeb would not want to clear them all) or if the data source added is not a SemWeb.ModifiableSource. The underlying data source is cleared with SemWeb.ModifiableSource.Clear().

GetEntitiesOfType Method

public Entity[] GetEntitiesOfType (Entity type)

Returns an array of all entities in the store whose type is known to be the given type.

Parameters

type
The type of entities to return.

Returns

An array of all entities in the store whose type is known to be the given type.

Remarks

Types are indicated using the rdf:type predicate.

Add Method

public void Add (Statement statement)

Adds a statement to the store.

Parameters

statement
The statement to add.

Remarks

The Subject, Predicate, Object, and Meta fields of the statement must not be null or an ArgumentNullException is thrown.

An InvalidOperationException is thrown if more than one data source has been added to the Store with AddSource (in which case SemWeb would not know which data source to add the statement to) or if the data source added is not a SemWeb.ModifiableSource.


Remove Method

public void Remove (Statement template)

Removes statements from the store.

Parameters

template
A statement or statement template to remove from the store.

Remarks

Every matching statement from all of the data sources included in this store is removed from the store that contains it. An InvalidOperationException is thrown if any data source added to the Store with AddSource is not a SemWeb.ModifiableSource.

Import Method

public void Import (StatementSource source)

Batch copies the contents of a StatementSource into the ModifyableSource contained in the store.

Parameters

source
The source whose statements will be copied into the store.

Remarks

An InvalidOperationException is thrown if more than one data source has been added to the Store with AddSource or if the data source added is not a SemWeb.ModifiableSource.

See SemWeb.ModifiableSource.Import(SemWeb.StatementSource).


Contains Method

public bool Contains (Statement template)

Returns whether the store contains a statement, or any statement that matches the template.

Parameters

template
The statement to search for, or a statement template.

Returns

true if the store contains the statement or any statement matching the template.

Remarks


Select Method

public void Select (Statement template, StatementSink result)

Queries the story for matching statements, and writes the statements to a SemWeb.StatementSink.

Parameters

template
A statement template. Use SemWeb.Statement.All to select all statements in the store, or a statement with null fields to select statements that match the non-null parts of the statement.
result
A StatementSink to which each matching statement will be added.

Remarks


SelectObjects Method

public Resource[] SelectObjects (Entity subject, Entity predicate)

Finds all objects in statements with the given subject and predicate.

Parameters

subject
The subject of the statements to query.
predicate
The predicate of the statements to query.

Returns

An array of the entities found as the object of the matching statements, excluding literal values. An entity occurs in the array at most once.

Remarks

Note:

This method is not recommended when the store can contain a large number of statements, as every matching entity must be loaded into memory before this method returns.


SelectSubjects Method

public Entity[] SelectSubjects (Entity predicate, Resource object)

Finds all subjects in statements with the given predicate and object.

Parameters

predicate
The predicate of the statements to query.
object
The object of the statements to query.

Returns

An array of the entities found as the subject of the matching statements. An entity occurs in the array at most once.

Remarks

Note:

This method is not recommended when the store can contain a large number of statements, as every matching entity must be loaded into memory before this method returns.


StatementCount Property

public int StatementCount { get; }

Gets the number of statements in the store.

Value

The number of statements in the store.

Remarks

An InvalidOperationException is thrown if any data source added to the Store with AddSource is not a SemWeb.StaticSource.

Write Method

public void Write (System.IO.TextWriter writer)

Writes the contents of the store to a stream in N3 format.

Parameters

writer
The stream to which statements are sent.

Remarks

None.

GetEntities Method

public Entity[] GetEntities ()

Returns an array of all entities mentioned in the store.

Returns

An array of all entities mentioned in the store.

Remarks

None.

GetPredicates Method

public Entity[] GetPredicates ()

Returns an array of all predicates mentioned in the store.

Returns

An array of all predicates mentioned in the store.

Remarks

None.

Replace Method

public void Replace (Entity find, Entity replacement)

Replaces all occurences of one Entity with another Entity.

Parameters

find
The Entity to search for in the store.
replacement
The Entity to replace a.

Remarks

All occurences of find in statements in the Store are replaced with references to replacement.

An InvalidOperationException is thrown if any data source added to the Store with AddSource does not implement SemWeb.ModifiableSource.


Store Constructor

public Store ()

Creates an empty store with no data storage backing.

Remarks


CreateForInput Method

public static StatementSource CreateForInput (string spec)

Creates a StatementSource in the factory design pattern from which statements will be read.

Parameters

spec
A specifier string, whose format is given below.

Returns

A StatementSource from which statements can be read. You may cast the value to SemWeb.RdfReader or SemWeb.SelectableSource when appropriate.

Remarks

The specifier string is in one of the following formats. When a filename is expected, a dash can be used to indicate standard input.

spec Meaning
mem A new SemWeb.MemoryStore.
xml:[filename] A SemWeb.RdfXmlReader to read the RDF/XML file. Example: "xml:mydata.rdf"

n3:[filename]

turtle:[filename]

nt:[filename]

ntriples:[filename]

A SemWeb.N3Reader to read the N3/Turtle/NTriples file. Example: "n3:mydata.n3"
sqlite:[table]:[connection string]

A SemWeb.Stores.SQLStore connected to the SQLite database given in the connection string, using the tables prefixed by the table parameter. The returned store can be both read from and written to, despite the name of this method. Example: "sqlite:rdf:Uri=file:mydatabase.sqlite;Version=3"

mysql:[table]:[connection string] A SemWeb.Stores.SQLStore connected to the MySQL database given in the connection string, using the tables prefixed by the table parameter. The returned store can be both read from and written to, despite the name of this method. Example: "mysql:rdf:Database=databasename;Server=localhost;User Id=username"
postgresql:[table]:[connection string] A SemWeb.Stores.SQLStore connected to the PostgreSQL database given in the connection string, using the tables prefixed by the table parameter. The returned store can be both read from and written to, despite the name of this method. Example: "postgresql:rdf:Server=localhost;Port=5432;Database=SemanticWeb;User Id=user"
sparql-http:[URL] A SemWeb.Remote.SparqlHttpSource, which accesses a remote data source using the SPARQL Protocol. Example: "sparql-http:http://www.govtrack.us/sparql"

The SQL stores can be both read from and written to, despite the name of this method.


CreateForOutput Method

public static StatementSink CreateForOutput (string spec)

Creates a SemWeb.StatementSink into which statements will be added.

Parameters

spec
A specification string whose format is given below.

Returns

A StatementSink. You may cast the value to SemWeb.RdfWriter or SemWeb.ModifiableSource when appropriate.

Remarks

The specifier string is in one of the following formats. When a filename is expected, a dash can be used to indicate standard output.

spec Meaning
mem A new SemWeb.MemoryStore.
xml:[filename] A SemWeb.RdfXmlWriter opened on the given file. Example: "xml:mydata.rdf"

n3:[filename]

turtle:[filename]

nt:[filename]

ntriples:[filename]

A SemWeb.N3Writer opened on the given file, writing in the indicated format. Example: "turtle:mydata.turtle"
SQL-based stores For Sqlite, MySQL, and PostgreSQL-based stores, use the spec string described in SemWeb.Store.CreateForInput(string). These stores can be both read from and written to, despite the name of this method.

Select Method

public SelectResult Select (Statement template)

Retuns a SemWeb.SelectResult object that represents the result of matching a statement template against the store.

Parameters

template
A statement template. Use SemWeb.Statement.All to select all statements in the store, or a statement with null fields to select statements that match the non-null parts of the statement.

Returns

A SemWeb.SelectResult representing the matching statements.

Remarks

On calling this method, the data in this store is not immediately accessed. The matching statements are loaded the first time they are requested through the returned SemWeb.SelectResult object. Those statements are accessed in one of three ways:

SemWeb.SelectResult implements IEnumerable, so the result object can be used in foreach loops, shown in this example:

C# Example
foreach (Statement s in mystore.Select(Statement.All)) {
    Console.WriteLine(s.ToString());
}

The drawback of this method is that all of the matching statements are loaded into memory before the loop begins, and so this method is not recommended when the number of matching statements may be very large.

The SemWeb.SelectResult.Load() method loads all of the matching statements into a SemWeb.MemoryStore. The MemoryStore can then be used to perform further Selects, or to get the matching statements as an array. This method is not advised when the number of matching statements may be very large.

SemWeb.SelectResult implements SemWeb.StatementSource, and the SemWeb.StatementSource.Select(SemWeb.StatementSink) method on the returned object will stream the matching statements directly from this store to the provided SemWeb.StatementSink without keeping the statements in memory.

template is a statement template, which means any field in template may be null, and those fields are excluded from the statement filter. For example, setting the Subject and Predicate fields but leaving the Object and Meta fields null will match all statements in the store that have the given Subject and Predicate, and anything in their Object and Meta fields.


Select Method

public void Select (StatementSink result)

Streams all statements in this store into a StatementSink.

Parameters

result
The destination for the statements in this store.

Remarks

The SemWeb.StatementSink.Add(SemWeb.Statement) method is called on result for each statement in this store.

Replace Method

public void Replace (Statement find, Statement replacement)

Replaces a single statement with another statement.

Parameters

find
The statement to find. This parameter must be a complete statement (with subject, predicate, and object non-null), not a template.
replacement
The statement that will replace the first statement.

Remarks

Store implementations may choose to optimize this method over a call to Remove followed by Add.

An InvalidOperationException is thrown if any data source added to the Store with AddSource does not implement SemWeb.ModifiableSource.


GetMetas Method

public Entity[] GetMetas ()

Returns an array of all entities used in the Meta field of any statement in the store.

Returns

An array of entities used in the Meta field of statements in the store.

Remarks

None.

RemoveAll Method

public void RemoveAll (Statement[] templates)

Removes all statements matching an array of templates.

Parameters

templates
An array of statement templates. Statement templates may contain a null subject, predicate, object, and/or meta.

Remarks

An InvalidOperationException is thrown if any data source added to the Store with AddSource does not implement SemWeb.ModifiableSource.

Dispose Method

public void Dispose ()

Releases any external resources associated with the Store.

Remarks

Calls System.IDisposable.Dispose() on any data sources added to the Store with the AddSource method that implement IDisposable. Stores may release external resources or close remote connections through Dispose, so users of the Store class should Dispose stores when they are finished being used.

DefaultContains Method

public static bool DefaultContains (SelectableSource source, Statement template)

A default implementation of the Contains operation for implementors of SelectableSource.

Parameters

source
A data source.
template
A statement template to match against the source.

Returns

Whether any statements in the source match the template.

Remarks

This method is intended to be used by implementors of SelectableSource.

Distinct Property

public bool Distinct { get; }

Gets whether the store returns only distinct statments from Select calls.

Value

True if any call to Select and its overloads yields only distinct statements (i.e. no duplicates).

Remarks

When this property returns false, callers of Select may want to ensure the resulting statements have no duplicates. It this property returns true, such a check is unnecessary.

Select Method

public void Select (SelectFilter filter, StatementSink result)

Queries the story for matching statements, with advanced options, and writes the statements to a SemWeb.StatementSink.

Parameters

filter
A filter specifying what statements to return.
result
A StatementSink to which each matching statement will be added.

Remarks


Select Method

public SelectResult Select (SelectFilter filter)

Retuns a SemWeb.SelectResult object that represents the result of the corresponding select call with a StatementSink.

Parameters

filter
A filter specifying what statements to return.

Returns

A SemWeb.SelectResult representing the matching statements.

Remarks


DefaultSelect Method

public static void DefaultSelect (SelectableSource source, SelectFilter filter, StatementSink sink)

This method provides a default implementation of the Select method that takes a SelectFilter argument.

Parameters

source
The source to query.
filter
The select filter to query with.
sink
The result sink to send matching statements to.

Remarks

This method is intended to be used by subclasses of Store that do not want to provide their own implementation of Select with a SelectFilter.

Contains Method

public bool Contains (Resource resource)

Tests whether the store contains any statements that mention the given resource.

Parameters

resource
A Resource (entity, literal, or bnode).

Returns

A boolean value indicating whether the Store contains any statements mentioning the resource.

Remarks

None.

MetaQuery Method

Tests the querying capabilities of the store.

Parameters

graph
The graph to be matched in a call to Query.
options
The options to be passed to the call to Query.

Returns

A SemWeb.Query.MetaQueryResult indicating how the query will be executed.

Remarks

See SemWeb.QueryableSource for more information. The default implementation of this method returns a SemWeb.Query.MetaQueryResult structure with the SemWeb.Query.MetaQueryResult.QuerySupported flag set to true. In addition, the SemWeb.Query.MetaQueryResult.NoData field is initialized by checking that the resources mentioned in the query are present in the store (with SemWeb.Store.Contains(SemWeb.Resource)).

Query Method

Queries the store with a simple graph match query.

Parameters

graph
The graph to match.
options
Query options.
sink
The output sink.

Remarks

See SemWeb.QueryableSource for more information. The default implementation of this method uses the SemWeb.Query.GraphMatch class to answer the query.

Store Constructor

public Store (SelectableSource source)

Creates a store that wraps the given SelectableSource.

Parameters

source
The data source to add to the Store.

Remarks

Other data sources can be added with SemWeb.Store.AddSource(SemWeb.SelectableSource).

Store Constructor

public Store (StatementSource source)

Creates a Store that loads the statements from the StatementSource into memory.

Parameters

source
A source of statements.

Remarks

The store loads the statements into memory.

AddReasoner Method

public virtual void AddReasoner (SemWeb.Inference.Reasoner reasoner)

Adds inferencing capabilities to the Select and Query methods of this Store.

Parameters

reasoner
A reasoning engine, such as SemWeb.Inference.RDFS or SemWeb.Inference.Euler.

Remarks

The Select and Query methods will apply inferencing to the store. The reasoners are applied recursively in the reverse order that they were added with this method. That is, if reasoner A is added before reasoner B, then a call to select will start with reasoner B, but when reasoner B queries the underlying data it will query reasoner A, and reasoner A will actually access any underlying data.

AddSource Method

public virtual void AddSource (SelectableSource source)

Adds a data source to this store.

Parameters

source
A data source.

Remarks

None.

AddSource Method

public virtual void AddSource (SelectableSource source, string uri)

Adds a named data source to this store.

Parameters

source
A data source.
uri
A URI identifying the data source.

Remarks

The data source is added to the store and is identified by uri. An ArgumentException is thrown if uri has already been used in a previous call to this method.

In calls to Contains(Statement), Select, Add, Remove, RemoveAll, and Replace(Statement,Statement), the Meta field of the statement argument is used to choose which data source added with AddSource to query for information. When the supplied Meta field is null or when no named data sources have been added to the Store through a call to this method, all data sources are queried. However, when named data sources have been added with this method and the supplied Meta field is SemWeb.Statement.DefaultMeta, only unnamed data sources added through the other AddSource method (SemWeb.Store.AddSource(SemWeb.SelectableSource)) are queried, and when the supplied SemWeb.Statement.DefaultMeta's URI matches the URI of a named data source added through this method, only that data source is queried.


Create Method

public static Store Create (string spec)

Creates a Store in the factory design pattern.

Parameters

spec
A specification string whose format is given below.

Returns

A Store object to which data sources and reasoning may have been added according to the specification string spec.

Remarks

spec is a newline- or pipe- ("|") delimited string of specification strings to be passed to SemWeb.Store.CreateForInput(string).

For each of those substrings, SemWeb.Store.CreateForInput(string) is called on that string. If the result is a SemWeb.SelectableSource, it is added to the returned Store with AddSource. If the data source is a SemWeb.StatementSource but not a SemWeb.SelectableSource, like an SemWeb.RdfReader, the contents of the data source are first loaded into a SemWeb.MemoryStore and the SemWeb.MemoryStore is then added to the Store to be returned.

In addition, spec can be prefixed with "rdfs+" so that an SemWeb.Inference.RDFS reasoning engine is added to the Store with AddReasoner.

The following are example specification strings:

Example
mysql:rdf:Database=databasename;Server=localhost;User Id=username  (creates a Store around a SQLStore)

rdfs+mysql:rdf:Database=databasename;Server=localhost;User Id=username (creates a Store around an SQLStore and applies RDFS reasoning)

n3:filename.n3 (loads the file into memory and returns a Store containing the data)

n3:file1.n3|n3:file2.n3|...  (loads each of the files into memory and returns a Store containing all of them)

DefaultReplace Method

public static void DefaultReplace (ModifiableSource source, Entity find, Entity replacement)

To be added.

Parameters

source
To be added.
find
To be added.
replacement
To be added.

Remarks

To be added.

DefaultReplace Method

public static void DefaultReplace (ModifiableSource source, Statement find, Statement replacement)

To be added.

Parameters

source
To be added.
find
To be added.
replacement
To be added.

Remarks

To be added.

GetBNodeFromPersistentId Method

public BNode GetBNodeFromPersistentId (string persistentId)

To be added.

Parameters

persistentId
To be added.

Returns

To be added.

Remarks

To be added.

GetPersistentBNodeId Method

public string GetPersistentBNodeId (BNode node)

To be added.

Parameters

node
To be added.

Returns

To be added.

Remarks

To be added.

Query Method

Queries the data sources in the Store and returns the resulting variable bindings.

Parameters

graph
A graph pattern, which is an array of Statements, to match against the store.

Returns

The variable bindings as a collection over SemWeb.Query.VariableBindings instances.

Remarks


DataSources Property

To be added.

Value

To be added.

Remarks

To be added.