Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2441b5f
Remove shared pointer, have addEntity return a ref to the entity and …
OliverWoolland Nov 20, 2026
04081bc
Bump tests for new api - addEntity creates entities
OliverWoolland Nov 20, 2026
da922d4
Update docs for removed shared pointer / addEntity creates entities
OliverWoolland Nov 20, 2026
38757f6
Broaden ignore for test output
OliverWoolland Nov 23, 2026
63e00be
Support generation of mixed array contexts
OliverWoolland Nov 23, 2026
cfe1ce1
Fix typo in test case name for JSON-LD output
OliverWoolland Nov 23, 2026
888a8bb
WIP: make getentity safe
OliverWoolland Nov 23, 2026
7813871
Overload addContext method for prefix/no-prefix
OliverWoolland Nov 23, 2026
3e5257f
Pre-init context with ro-crate spec
OliverWoolland Nov 23, 2026
f9bc4f6
Extend context handling for read/write
OliverWoolland Nov 23, 2026
45a18ab
Ensure addContext unit tests roundtrip
OliverWoolland Nov 23, 2026
5812862
Merge branch 'array-context' of github.com:eScienceLab/ro-crate-cpp i…
OliverWoolland Nov 23, 2026
ed51a8e
Merge branch 'array-context' into entity-return-safe
OliverWoolland Nov 23, 2026
4ee5b60
Add hasEntity method
OliverWoolland Nov 23, 2026
bbbc8ec
Allow users to bulk add properties to entities
OliverWoolland Nov 23, 2026
c82b27d
Update docs with new set shape
OliverWoolland Nov 23, 2026
f03f081
Add get properties doc
OliverWoolland Nov 23, 2026
d70fb46
Add getters for entity register and context list
OliverWoolland Nov 23, 2026
1868f8e
Split context get into local and remote and bump tests
OliverWoolland Nov 23, 2026
ba9ff09
Add support for detached crates
OliverWoolland Nov 24, 2026
5bb93c7
Bump autodocs
OliverWoolland Nov 27, 2026
f64c7f6
Rename addEntity to createEntity
OliverWoolland Nov 27, 2026
c38a6b5
Merge pull request #29 from eScienceLab/detached-crates
OliverWoolland Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ vcpkg_installed/
# test output & cache
Testing/
.cache/
*out*
99 changes: 57 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,35 @@ void create_example_ro_crate() {
// this is currently missing

// Get the root metadata entity from the crate and set the description
Entity root = crate.getEntity("ro-crate-metadata.json");
Entity& root = crate.getEntity("ro-crate-metadata.json");
root.set("description", "RO-Crate Metadata File Descriptor (this file)");

// Add name, description to the root data entity (./)
Entity rootData = crate.getEntity("./");
Entity& rootData = crate.getEntity("./");
rootData.set("name", "Example RO-Crate");
rootData.set("description", "The RO-Crate Root Data Entity");

// --------------------------------------------------------------------------
// Create the person, which is a contextual entity, and add it to the crate

Entity alice({"Person"});
Entity& alice = crate.createEntity("#alice", {"Person"});
alice.set("name", "Alice");
alice.set("description", "One of hopefully many Contextual Entities");
crate.addEntity("#alice", alice);

// --------------------------------------------------------------------------
// Create the place, another contextual entity, and add it to the crate
Entity catalinaPark({"Place"});
Entity& catalinaPark = crate.createEntity("http://sws.geonames.org/8152662/", {"Place"});
catalinaPark.set("name", "Catalina Park");
crate.addEntity("http://sws.geonames.org/8152662/", catalinaPark);

// --------------------------------------------------------------------------
// Create two datasets, which are data entities, and add them to the crate.
Entity data1({"File"});
Entity& data1 = crate.createEntity("data1.txt", {"File"});
data1.set("description", "One of hopefully many Data Entities");
data1.set("author", alice);
data1.set("contentLocation", catalinaPark);
crate.addEntity("data1.txt", data1);
rootData.set("hasPart", data1); // Ensure that the root data entity has a hasPart relationship to data1

Entity data2({"File"});
crate.addEntity("data2.txt", data2);
Entity& data2 = crate.createEntity("data2.txt", {"File"});
rootData.set("hasPart", data2);

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -94,49 +90,72 @@ root data entity (./).
ROCrate crate;
```

### Create an Entity
### Create an Entity

To create an entity, instantiate the `Entity` class. You must provide a type,
or can provide a list of types.
Entities are always owned by an RO-Crate: creating one and adding it to the
crate happen in a single step, via the `createEntity` method on `ROCrate`. The
first argument is the entity ID, and the second is a type, or list of types.
`createEntity` constructs the entity and returns a mutable reference to it.

```cpp
ROCrate crate;

// Single type
Entity alice({"Person"});
Entity& alice = crate.createEntity("#alice", {"Person"});

// Multiple types
Entity data1({"File", "Dataset"});
Entity& data1 = crate.createEntity("data1.txt", {"File", "Dataset"});
```

### Add properties to an Entity

To add properties to an entity, use the `set` method. The first argument is the
property name, and the second argument is the property value. The property value
can either be a string, or another entity.
To add properties to an entity, use the `set` method on the reference returned
by `createEntity` (or `getEntity`). The first argument is the property name, and
the second argument is the property value. The property value can either be a
string, or another entity.

If a string property value is provided, it create a straightforward key-value pair
in the entity e.g. `name: "Alice"`. If an entity is provided, it will create a
relationship between the two entities e.g. `author: { "@id": "#alice" }`.

```cpp
ROCrate crate;

// Add a string property
Entity alice({"Person"});
Entity& alice = crate.createEntity("#alice", {"Person"});
alice.set("name", "Alice");

// Add a relationship property
Entity data1({"File"});
Entity& data1 = crate.createEntity("data1.txt", {"File"});
data1.set("author", alice);
```

### Add an Entity to an RO-Crate
You can also bulk add properties to an entity by passing a map of property names
and values to the `set` method.

```cpp
ROCrate crate;
Entity& alice = crate.createEntity("#alice", {"Person"});
std::map<std::string, std::string> properties = {
{"name", "Alice"},
{"description", "One of hopefully many Contextual Entities"}
};
```

### Get properties of an Entity

To add an entity to an RO-Crate, use the `addEntity` method. The first argument is
the entity ID, and the second argument is the entity object.
To get properties of an entity, use the `getProperty` method on the reference returned by `createEntity` (or `getEntity`).

```cpp
ROCrate crate;
Entity& alice = crate.createEntity("#alice", {"Person"});
alice.set("name", "Alice");

Entity alice({"Person"});
crate.addEntity("#alice", alice);
for (const auto& [property, values] : alice.getProperties()) {
for (const auto& value : values) {
std::cout << property << ": " << value.value << "\n";
}
}
```

### Add additional context to an RO-Crate
Expand Down Expand Up @@ -175,25 +194,19 @@ relationships between entities.

### Entity lifecycle

When an entity is created, it is not automatically added to the RO-Crate. The user
must explicitly add the entity to the RO-Crate using the `addEntity` method.

When an entity is added to the RO-Crate, it is stored in a map of entities.

For maximum flexiblity, an already added entity may still be updated on the
original entity object. The RO-Crate shares the same entity object, so any changes
made to the original entity will be reflected in the RO-Crate.
An entity cannot exist independently of an RO-Crate: it is always constructed
and owned by the crate's entity register, via `ROCrate::createEntity`. This method
returns a mutable reference to the entity, which can be used to set further
properties, or passed to `set` on other entities to create relationships.

Worked example:

```cpp
ROCrate crate;

Entity alice({"Person"});
Entity& alice = crate.createEntity("#alice", {"Person"});
alice.set("name", "Alice");

crate.addEntity("#alice", alice);

/* Crate state:
{
"#alice": {
Expand All @@ -203,12 +216,9 @@ crate.addEntity("#alice", alice);
}
*/

// Add an additional property to the crate entity
Entity crateAlice = crate.getEntity("#alice");
crateAlice.set("description", "One of hopefully many Contextual Entities");

// Add an additional property to the original entity object
alice.set("occupation", "Software Engineer");
// Further updates go through the same reference, or a fresh one from getEntity
alice.set("description", "One of hopefully many Contextual Entities");
crate.getEntity("#alice").set("occupation", "Software Engineer");

/* Crate state:
{
Expand All @@ -222,6 +232,11 @@ alice.set("occupation", "Software Engineer");
*/
```

Note that `getEntity` and `createEntity` both return references (`Entity&`).
Copying the result into a plain `Entity` variable (rather than binding a
reference) makes an independent copy that will not reflect further changes,
and will not itself reflect changes made to the crate's entity afterwards.

## Reference

Reference documentation generated by [doxide](doxide.org) is available [here](http://esciencelab.org.uk/ro-crate-cpp/).
51 changes: 49 additions & 2 deletions docs/rocrate/Entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ Represents an entity in the RO-Crate.
An entity can have multiple types and properties, where each property can have multiple values.
Properties can be set as either literal values or references to other entities.

Entities are created and added via ROCrate::createEntity,
which constructs the Entity and returns a mutable reference to it.


## Functions

| Name | Description |
| ---- | ----------- |
| [Entity](#Entity) | Constructs an Entity with the specified types. |
| [set](#set) | Sets a property-value pair for the entity. |
| [set](#set) | Sets a property to reference another entity. |
| [set](#set) | Sets multiple properties for the entity at once. |
| [getProperties](#getProperties) | Retrieves the properties of the entity. |
| [Entity](#Entity) | Constructs an Entity with the specified types. |
| [assignId](#assignId) | Assigns an '@id' to the entity. |
| [validateSetRequest](#validateSetRequest) | Validates a set request for a property-value pair. |

## Function Details

Expand All @@ -28,6 +34,9 @@ Properties can be set as either literal values or references to other entities.

Constructs an Entity with the specified types.

Private since entities are always constructed and owned by an RO-Crate
via ROCrate::createEntity.


:material-location-enter: `types`
: A vector of strings representing the types of the entity.
Expand All @@ -49,8 +58,18 @@ Properties can be set as either literal values or references to other entities.
: std::invalid_argument if the id is empty.


### getProperties<a name="getProperties"></a>
!!! function "Properties getProperties() const"

Retrieves the properties of the entity.


:material-keyboard-return: **Return**
: A copy of the properties map, where each property maps to a vector of PropertyValue objects.


### set<a name="set"></a>
!!! function "void set( Property property, Value value, ValueType type = ValueType::Literal )"
!!! function "void set(Property property, Value value, ValueType type = ValueType::Literal)"

Sets a property-value pair for the entity.

Expand Down Expand Up @@ -86,3 +105,31 @@ Properties can be set as either literal values or references to other entities.
: std::runtime_error if the referenced entity does not have an '@id' property set.


!!! function "void set(std::map&lt;Property, Value&gt; properties)"

Sets multiple properties for the entity at once.


:material-location-enter: `properties`
: A map of property names to their corresponding values.

:material-alert-circle-outline: **Throw**
: std::invalid_argument if any property name or value is empty, or if attempting to set '@id' directly.


### validateSetRequest<a name="validateSetRequest"></a>
!!! function "void validateSetRequest(Property property, Value value)"

Validates a set request for a property-value pair.


:material-location-enter: `property`
: The name of the property to validate.

:material-location-enter: `value`
: The value to validate.

:material-alert-circle-outline: **Throw**
: std::invalid_argument if the property name or value is empty, or if attempting to set '@id' directly.


Loading
Loading