The Entity framework is built on top of the Semantics.SDK and providesentity-based transactions on a RDF store such as Semantics.Server. This article will cover some of the basic concepts of the Entity framework.
Entity
An entity consists of two things: (1) A URI that indentifies the entity and (2) and set of RDF statements that describe the entity.
Description Type
A description type defines the statements associated with an entity. In the simple case just the statements with the entity URI as the subject value are considered part of the entity. In real applications an entity description is often much richer than this. A common description type is called a CBD (concise bounded description). A CBD is defined as all statements with the entity URI as the subject value plus any anonymous nodes recursively referenced by any of those statements.
Description types can be defined using rules and/or custom .NET code. The example below shows the CBD definition using rules.
RULEBASE
(
SELECT ?s ?p ?o ?r WHERE {?s ?p ?o. filter(?r=?s)} AS <description>
SELECT ?s ?p ?o ?r WHERE {
?r ?x ?y.
{<description>(?s, ?p, ?o, ?y)}
filter(isblank(?y))
} AS <description>
)
Retrieving an Entity
When an entity is retrieved using the RdfEntity framework via the EntityModel class. An entity model requires a URI for the model and a EntityServiceProvider in its constructor. We will discuss the EntityServiceProvider class later but it can be thought of as an entity store driver.
EntityModel model = new EntityModel(provider, “model:MyModel”);
model.UriResolver.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
model.UriResolver.AddNamespace("foaf", "http://xmlns.com/foaf/0.1/");
model.UriResolver.AddNamespace("model", "http://entitystore/model/model-1/");
model.UriResolver.AddNamespace("store", "http://entitystore/");
Entity derrish = model.GetEntity("model:DerrishRepchick");
derrish.DescriptionType = "model:CBD";
An entity is constructed when EntityModel.GetEntity is called. The entity is bound to the specifed URI and the entity model and therefore the underlying service provider. All calls on the instance of the entity will be routed through the entity model that created it.
Notice how we set the DescriptionType property on the entity. This tells the framework which statements to retrieve for this entity. The statements are not actually retrieved until a graph is accessed for that entity.
string creator = derrish.NamedNode[EntityGraphUri.Metadata]["dc:creator"].FirstOrDefault();
Entity Graphs
An entity model is logically and/or physically partitioned into multiple graphs. The Entity framework has some well-known graphs that are defined as follows. You may choose which, if any, of these graphs to support in your entity model. However, some are required for certain features of the Entity framework.
Ontology Graph
Contains statements about the onotology. This is a required graph.
Fact Graph
Contains explicit facts about entities. This is a required graph.
Description Graph
Contains full entity description (facts + inferences). This is a required read-only graph.
Inference Graph
Contains only inferences about entities. This is an optional read-only graph.
Metadata Graph
Contains metadata about the entities. This is an optional read-only graph.
Provider Graph
Contains metadata about the underlying service provider. This is a required (optional for some providers) read-only graph.
Storing an Entity
A modified entity can be stored by calling the SaveToStore method. You will want to make sure to set the correct description type on the entity before storing. It is often the case that an entity will have asymmetric description types. Meaning often more statements are retrieved then stored. For example you may retrieve facts and inferences but only want to store facts. A similar issue exists when removing an entity from the store.
Service Providers
The Entity framework contains a built in provider for Semantics.Server called SemanticServerProvider. This service provider allows an entity model to be maintained in a Semantics.Server database. The SemanticServerProvider has a convenient helper class for setting up an entity model called ModelSetup.