Public
Edited
May 16, 2022
1 fork
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/**
3.1 Triples @link https://www.w3.org/TR/rdf11-concepts/#section-triples
An RDF triple consists of three components:
*/

class Triple {
constructor(_subject, _predicate, _object) {
//the subject, which is an IRI or a blank node
this.subject = new RDF_Subject(_subject)
//the predicate, which is an IRI
this.predicate = new RDF_Predicate(_predicate)
//the object, which is an IRI, a literal or a blank node
this.object = new RDF_Object(_object)
}

edges = () => [[this.subject.name, this.object.name]]
//edges = () => [[this.subject.name, this.predicate.name], [this.predicate.name, this.object.name]]
}
Insert cell
Insert cell
Insert cell
Insert cell
class Resource {}
Insert cell
/**
3.2 IRIs @link https://www.w3.org/TR/rdf11-concepts/#section-IRIs
An IRI (Internationalized Resource Identifier)
*/

class IRI extends Resource {}
Insert cell
### Онтология
Insert cell
class RDF_Ontology {
#subjects = new Set
addSubject(subject) {
const obSubject = new RDF_Subject(subject)
this.#subjects.add(obSubject)
return obSubject
}
}
Insert cell
### Субъект
Insert cell
class RDF_Subject {
#subject
#predicates = new Set
constructor({id, type}) {
console.log('~~~~', {id, type})
this.#subject = new RDF_Resource({id, type})
}

addPredicate = ({predicate, range}) => {
const obPredicate = new RDF_Predicate(predicate)
this.#predicates.add(obPredicate)
return obPredicate
}

export() {
return this.#subject.export()
}
}
Insert cell
Insert cell
class RDF_Predicate {
#predicate
#objects = new Set
constructor(predicate){
this.#predicate = new RDF_Resource({type: predicate})
}

addObject = (objct) => {
const obObject = new RDF_Object(objct)
this.#objects.add(obObject)
return obObject
}
}
Insert cell
Insert cell
class RDF_Object {
#object
constructor(objct){
this.#object = new RDF_Resource({literal: objct})
}
}
Insert cell
Insert cell
class RDF_Resource {
_id
_type
_literal

constructor({id, type, literal}) {
this._id = id
this._type = type
this._literal = literal
}

export() {
return {
id: this._id,
type: this._type,
literal: this._literal
}
}
}
Insert cell
Insert cell
=

|Simple|OWL|Comment
|-|-|-|
|:животное .|:животное a rdfs:Class .|
|#Стрелка .|
|:животное#Стрелка.|<#Стрелка> rdf:type :животное .|
|:кошка:животное#Мурка.|<#Мурка> rdf:type :кошка .<br>:кошка rdfs:subClassOf :животное .|Default subtype for subject is rdfs:subClassOf
|#Теремок;:содержит:животное,#Мурка.|:содержит rdf:type owl:ObjectProperty ;<br>rdfs:domain :зоопарк ;<br>rdfs:range :животное .|Default subtype for predicate is owl:ObjectProperty



Insert cell
Insert cell
//TODO parse to triple set
zooTextBig = `:животное#Стрелка.:кошка#Мурка;:цвет,серый;:цвет глаз,желтый,голубой.:кошка:животное.:зоопарк#Теремок;:содержит:животное,:кошка#Варька,:обезьянка:животное:существо#Чи-чи.:человек:существо#Василий;:сторож:зоопарк,#Тетемок.#Василий;:цвет_глаз 'голубой'.`
Insert cell
//TODO parse to triple set
zooText = `:dog:animal#Strelka.:cat#Murka.:cat:animal:creature.:zoo#Teremok;:host:animal,:cat#Varka,#Murka,#Strelka.`
Insert cell
{
const zoo = new Parser('Teremok')
zoo.loadText(zooTextBig)
return zoo.owl('http://www.zoo.org/')
}
Insert cell
class Parser {
#name
#text
#buffer

#subject
#subjectId
#subjectType

#predicate
#range

#set = new Set
constructor(name) {
this.#name = name
this.#buffer = ''
}

parseTriple = (text) => {
const parts = text.split(';'),
//First element is Subject
subject = parts.shift(),
ontology = new RDF_Ontology,
obSubject = ontology.addSubject(this.parseResource(subject)),
{id, type} = obSubject.export()
console.log('prev==', this.#subjectId, 'sub==', subject, 'id=', id)
if(id) {
this.#subjectId = id
} if(type)
this.#subjectType = type
//Other elements is Predicates with Objects
while(parts.length) { // Predivates iterate
const objects = parts.shift().split(','),
//First element is Predicate
obPredicate = obSubject.addPredicate(this.parsePredicate(objects.shift()))
//Other elements is Objects
const objs = objects.map(o => {
obPredicate.addObject(o)
return o
}).map(this.parseObjects)
.map(i => (i.length && i.includes('#') ? `<${i}>` : `'${i}'`))
.join(',')
this.#buffer += ` .
<${id}> ${this.#predicate} ${objs}`
}
this.#set.add(obSubject)
}

parseResource = (text) => {
let id, type, literal
if(text.includes('#')) {
[type, id] = text.split('#')
id = `#${id}`
if(type.includes(':'))
text = type
}

if(text.includes(':')) {
type = null
const classes = text.split(':').filter(s => s).map(c => `:${c}`)
console.log('classes', classes, type)
if(!type)
type = classes[0]

while(classes.length > 1) {
let subClass = classes.shift()
this.#buffer += ` .
${subClass} rdfs:subClassOf ${classes[0]}`
}
} else {
id = text
}
console.log('return', [id, type])
if(id && type) {
this.#buffer += ` .
<${id}> rdf:type ${type}`
}
return {id, type, literal}
}

parsePredicate = (text) => {
[this.#predicate, this.#range] = text.substr(1).split(':')
this.#predicate = `:${this.#predicate.replace(' ', '_')}`
if(this.#range) {
this.#range = `:${this.#range}`
this.#buffer += ` .
${this.#predicate} rdf:type owl:ObjectProperty ;
rdfs:domain ${this.#subjectType} ;
rdfs:range ${this.#range}`
}

return [this.#predicate, this.#range]
}
parseObjects = (text) => {
return this.parseResource(text)
}

loadText(text) {
this.#text = text
}
parse = () => {
console.clear()
this.#text.split('.').map(this.parseTriple.bind(this))
}

owl(prefix) {
this.parse()
const owl = `@prefix : <${prefix}${this.#name}#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <${prefix}${this.#name}> .

<${prefix}${this.#name}> rdf:type owl:Ontology${this.#buffer} .`
console.log(this.#text)
console.log(this.#set)
console.log(owl)
return owl
}
}
Insert cell
/*
3. RDF Graphs @link https://www.w3.org/TR/rdf11-concepts/#section-rdf-graph
An RDF graph is a set of RDF triples.
*/

// zooGraph = new Set([
// new RDF('Стрелка', 'животное'),
// new RDF('Мурка', 'кошка'),
// new RDF('Варька', 'кошка'),
// new RDFS('кошка', 'животное'),
// new RDF('Теремок', 'зоопарк'),
// new Triple('Теремок', 'зоопарк:содержит', 'Варька'),
// new RDFS('зоопарк:содержит', 'животное', 'range'), //new Triple('зоопарк', 'животное', 'содержит'),
// ])

Insert cell
Insert cell
//zoo = makeEdges(zooGraph)
Insert cell
// dot`digraph {
// ${zoo.map(([i, o]) => `${i} -> ${o}`).join("\n")}
// }`
Insert cell
Insert cell
/*
3. RDF Graphs @link https://www.w3.org/TR/rdf11-concepts/#section-rdf-graph
An RDF graph is a set of RDF triples.
*/

// natureGraph = new Set([
// new Triple('небо', 'цвет', 'голубой'),
// new Triple('трава', 'цвет', 'зеленый'),
// new Triple('птица', 'способность', 'летает'),
// new Triple('птица', 'голос', 'чирик'),
// new RDFS('птица', 'животное'),
// new RDFS('голос', 'способность'),
// ])
Insert cell
Insert cell
// dot`digraph {
// ${makeEdges(natureGraph).map(([i, o]) => `${i} -> ${o}`).join("\n")}
// }`
Insert cell
Insert cell
Insert cell
// class Node {
// constructor(name) {
// this.name = name;
// this.computed = false;
// this.indegree = 0;
// this.inputs = new Set;
// this.outputs = new Set;
// }
// }
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more