Commit e3389610 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Support inverse links

parent 160cd48802f2
......@@ -145,11 +145,33 @@ export class RdfEntity {
}
public getValuesForE(property: RdfEntity): $rdf.Node[] {
let results = this.doGetValues(this, property);
let inverses = this.manager.inverseRelation(property);
inverses.forEach((inverse: RdfEntity) => {
results = results.concat(this.doGetSubjects(inverse, this));
});
return results;
}
public getValuesForS(property: string): $rdf.Node[] {
return this.getValuesForE(this.manager.entityForUri(property));
}
public getValuesForN(property: $rdf.NamedNode): $rdf.Node[] {
return this.getValuesForE(this.manager.entityForUri(property.uri));
}
/**
* Gets all the RDF node
* @param subject The subject
* @param property The property
*/
private doGetValues(subject: RdfEntity, property: RdfEntity): $rdf.Node[] {
let results: $rdf.Node[] = [];
for (var i = 0; i != this.aliases.length; i++) {
for (var i = 0; i != subject.aliases.length; i++) {
for (var j = 0; j != property.aliases.length; j++) {
let values = this.manager.store.each(
this.aliases[i],
let values = subject.manager.store.each(
subject.aliases[i],
property.aliases[j],
undefined,
undefined
......@@ -162,12 +184,27 @@ export class RdfEntity {
return results;
}
public getValuesForS(property: string): $rdf.Node[] {
return this.getValuesForE(this.manager.entityForUri(property));
}
public getValuesForN(property: $rdf.NamedNode): $rdf.Node[] {
return this.getValuesForE(this.manager.entityForUri(property.uri));
/**
* Gets all the RDF node
* @param subject The subject
* @param property The property
*/
private doGetSubjects(property: RdfEntity, object: RdfEntity): $rdf.Node[] {
let results: $rdf.Node[] = [];
for (var i = 0; i != object.aliases.length; i++) {
for (var j = 0; j != property.aliases.length; j++) {
let values = object.manager.store.each(
undefined,
property.aliases[j],
object.aliases[i],
undefined
);
values.forEach((value: $rdf.Node) => {
if (results.indexOf(value) < 0) results.push(value);
});
}
}
return results;
}
}
......@@ -191,6 +228,10 @@ export class RdfEntityStore {
* The map of known aliases
*/
private aliases: { [uri: string]: string[] };
/**
* The inverse relations
*/
private inverses: { [uri: string]: string };
/**
* Initializes this store
......@@ -202,6 +243,7 @@ export class RdfEntityStore {
this.nodes = {};
this.entities = {};
this.aliases = {};
this.inverses = {};
if (aliases != null && aliases != undefined) {
for (var i = 0; i != aliases.length; i++) {
let series = aliases[i];
......@@ -247,6 +289,23 @@ export class RdfEntityStore {
return result;
}
/**
* Gets the reverse relations
* @param relation A relation
*/
public inverseRelation(relation: RdfEntity): RdfEntity[] {
let result: RdfEntity[] = [];
let self = this;
relation.uris.forEach((uri: string) => {
let inverse = self.inverses[uri];
if (inverse != null && inverse != undefined) {
let inverseEntity = self.entityForUri(inverse);
if (result.indexOf(inverseEntity) < 0) result.push(inverseEntity);
}
});
return result;
}
/**
* Declares that some URIs are equivalent
* @param uri An URI
......@@ -261,6 +320,16 @@ export class RdfEntityStore {
}
}
/**
* Declares that two URIs are inverse relations
* @param uri1 An URI for a relation
* @param uri2 The URI of the inverse relation
*/
public inverse(uri1: string, uri2: string): void {
this.inverses[uri1] = uri2;
this.inverses[uri2] = uri1;
}
/**
* Resolves all the aliases for an entity
* @param entity The entity
......
......@@ -197,6 +197,10 @@ function loadPerson(
target: string
): FoaFPerson {
let entityStore = new RdfEntityStore(context.store, ALIAS);
entityStore.inverse(
"http://dbpedia.org/ontology/influenced",
"http://dbpedia.org/ontology/influencedBy"
);
let entityTarget = entityStore.entityForUri(target);
let entityFocus = entityTarget.getEntityForS(
"http://xmlns.com/foaf/0.1/focus"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment