Skip to content

Commit e950b59

Browse files
committed
feat: support for ontology generation
fix: blank EntityId decoder
1 parent f72bb26 commit e950b59

File tree

9 files changed

+1001
-4
lines changed

9 files changed

+1001
-4
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,56 @@ Every JsonLD object can be turn to Json using it's `toJson` method.
181181
## Detailed examples
182182

183183
**For More examples, please see src/examples/scala/examples**
184+
185+
## Ontology
186+
187+
JsonLD4s library allows defining and generating ontologies.
188+
189+
### Defining ontology
190+
191+
Defining ontology of a specific type should be done using the `io.renku.jsonld.ontology.Type` class. An example definition can look like follows:
192+
193+
```scala
194+
import io.renku.jsonld.ontology._
195+
import io.renku.jsonld.Schema
196+
197+
val prov: Schema = Schema.from("http://www.w3.org/ns/prov", separator = "#")
198+
val renku: Schema = Schema.from("https://swissdatasciencecenter.github.io/renku-ontology", separator = "#")
199+
val schema: Schema = Schema.from("http://schema.org")
200+
201+
val subtypeOntology: Type = Type.Def(
202+
Class(schema / "Thing"),
203+
DataProperty(schema / "name", xsd / "string")
204+
)
205+
206+
val rootOntology: Type = Type.Def(
207+
Class(prov / "Activity"),
208+
ObjectProperties(
209+
ObjectProperty(renku / "parameter", subtypeOntology)
210+
),
211+
DataProperties(DataProperty(prov / "startedAtTime", xsd / "dateTime"),
212+
DataProperty(prov / "endedAtTime", xsd / "dateTime")
213+
)
214+
)
215+
```
216+
217+
Type's class needs to be defined with the `Class` type, properties linking other types with a collection of `ObjectProperty` objects and simple value properties with a collection of `DataProperty` objects. The library calculates properties' ranges and domains automatically during ontology generation.
218+
219+
### Generating ontology
220+
221+
Generating ontology is a trivial task which can be done using the `io.renku.jsonld.ontology.generateOntology` method. The method takes a `Type` definition and a `Schema`.
222+
223+
```scala
224+
import io.renku.jsonld.ontology._
225+
import io.renku.jsonld.Schema
226+
227+
val renku: Schema = Schema.from("https://swissdatasciencecenter.github.io/renku-ontology", separator = "#")
228+
val schema: Schema = Schema.from("http://schema.org")
229+
230+
val ontology: Type = Type.Def(
231+
Class(schema / "Thing"),
232+
DataProperty(schema / "name", xsd / "string")
233+
)
234+
235+
generateOntology(ontology, renku)
236+
```

src/main/scala/io/renku/jsonld/EntityId.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ object EntityId {
5656

5757
implicit val entityIdJsonDecoder: Decoder[EntityId] = Decoder.instance {
5858
_.as[String].map {
59-
case s if s.startsWith("_:") => EntityId.blank
60-
case s => EntityId.of(s)
59+
case s"_:${uuid}" => EntityId.BlankNodeEntityId(UUID.fromString(uuid))
60+
case s => EntityId.of(s)
6161
}
6262
}
6363

6464
implicit val stringToEntityId: String => EntityId = v => StandardEntityId(URIref.encode(v))
6565
implicit val propertyToEntityId: Property => EntityId = p => StandardEntityId(URIref.encode(p.url))
66+
implicit val schemaToEntityId: Schema => EntityId = s => StandardEntityId(URIref.encode(s.url))
6667

6768
implicit val show: Show[EntityId] = Show[EntityId](entityId => entityId.valueShow.show(entityId.value))
6869
}

src/main/scala/io/renku/jsonld/JsonLDEncoder.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ object JsonLDEncoder {
6161
final implicit val encodeLong: JsonLDEncoder[Long] = (a: Long) => JsonLD.fromLong(a)
6262
final implicit val encodeInstant: JsonLDEncoder[Instant] = (a: Instant) => JsonLD.fromInstant(a)
6363
final implicit val encodeLocalDate: JsonLDEncoder[LocalDate] = (a: LocalDate) => JsonLD.fromLocalDate(a)
64+
final implicit val encodeSchema: JsonLDEncoder[Schema] = (a: Schema) => JsonLD.fromEntityId(a.toString)
6465
final implicit val encodeEntityId: JsonLDEncoder[EntityId] = JsonLD.fromEntityId
6566
final implicit val encodeJsonLD: JsonLDEncoder[JsonLD] = identity
6667
final implicit val encodeBoolean: JsonLDEncoder[Boolean] = (a: Boolean) => JsonLD.fromBoolean(a)

src/main/scala/io/renku/jsonld/Schema.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package io.renku.jsonld
2020

2121
import cats.Show
2222

23-
abstract class Schema(url: String, separator: String) extends Product with Serializable {
23+
abstract class Schema(val url: String, val separator: String) extends Product with Serializable {
2424
def /(name: String): Property = Property(s"$url$separator$name")
2525
def /(name: Number): Property = Property(s"$url$separator$name")
2626
def asPrefix(name: String): String = s"PREFIX $name: <$url$separator>"
@@ -42,6 +42,6 @@ object Schema {
4242
def from(baseUrl: String, separator: String): Schema = CustomSeparatorSchema(baseUrl, separator)
4343

4444
private[jsonld] final case class SlashSeparatorSchema(value: String) extends Schema(value, separator = "/")
45-
private[jsonld] final case class CustomSeparatorSchema(value: String, separator: String)
45+
private[jsonld] final case class CustomSeparatorSchema(value: String, override val separator: String)
4646
extends Schema(value, separator)
4747
}

0 commit comments

Comments
 (0)