Type.jssrc/ | |
|---|---|
| |
VIE.TypesWithin VIE, we provide special capabilities of handling types of entites. This helps for example to query easily for certain entities (e.g., you only need to query for Persons and not for all subtypes). | if (VIE.prototype.Type) {
throw new Error("ERROR: VIE.Type is already defined. Please check your installation!");
}
if (VIE.prototype.Types) {
throw new Error("ERROR: VIE.Types is already defined. Please check your installation!");
} |
VIE.Type(id, attrs)This is the constructor of a VIE.Type.
| VIE.prototype.Type = function (id, attrs) {
if (id === undefined || typeof id !== 'string') {
throw "The type constructor needs an 'id' of type string! E.g., 'Person'";
} |
idThis field stores the id of the type's instance.
| this.id = this.vie.namespaces.isUri(id) ? id : this.vie.namespaces.uri(id); |
checks whether such a type is already defined. | if (this.vie.types.get(this.id)) {
throw new Error("The type " + this.id + " is already defined!");
}
|
supertypesThis field stores all parent types of the type's instance. This
is set if the current type inherits from another type.
| this.supertypes = new this.vie.Types(); |
subtypesThis field stores all children types of the type's instance. This
will be set if another type inherits from the current type.
| this.subtypes = new this.vie.Types();
|
attributesThis field stores all attributes of the type's instance as
a proper
| this.attributes = new this.vie.Attributes(this, (attrs)? attrs : []); |
isof(type)This method checks whether the current type is a child of the given type.
| this.isof = function (type) {
type = this.vie.types.get(type);
if (type) {
return type.subsumes(this.id);
} else {
throw new Error("No valid type given");
}
}; |
subsumes(type)This method checks whether the current type is a parent of the given type.
| this.subsumes = function (type) {
type = this.vie.types.get(type);
if (type) {
if (this.id === type.id) {
return true;
}
var subtypes = this.subtypes.list();
for (var c = 0; c < subtypes.length; c++) {
var childObj = subtypes[c];
if (childObj) {
if (childObj.id === type.id || childObj.subsumes(type)) {
return true;
}
}
}
return false;
} else {
throw new Error("No valid type given");
}
};
|
inherit(supertype)This method invokes inheritance throught the types. This adds the current type to the
subtypes of the supertype and vice versa.
| this.inherit = function (supertype) {
if (typeof supertype === "string") {
this.inherit(this.vie.types.get(supertype));
}
else if (supertype instanceof this.vie.Type) {
supertype.subtypes.addOrOverwrite(this);
this.supertypes.addOrOverwrite(supertype);
try { |
only for validation of attribute-inheritance! | this.attributes.list();
} catch (e) {
supertype.subtypes.remove(this);
this.supertypes.remove(supertype);
throw e;
}
} else if (jQuery.isArray(supertype)) {
for (var i = 0, slen = supertype.length; i < slen; i++) {
this.inherit(supertype[i]);
}
} else {
throw new Error("Wrong argument in VIE.Type.inherit()");
}
return this;
};
|
hierarchy()This method serializes the hierarchy of child types into an object.
| this.hierarchy = function () {
var obj = {id : this.id, subtypes: []};
var list = this.subtypes.list();
for (var c = 0, llen = list.length; c < llen; c++) {
var childObj = this.vie.types.get(list[c]);
obj.subtypes.push(childObj.hierarchy());
}
return obj;
};
|
instance()This method creates a
| this.instance = function (attrs, opts) {
attrs = (attrs)? attrs : {};
opts = (opts)? opts : {};
|
turn type/attribute checking on by default! | if (opts.typeChecking !== false) {
for (var a in attrs) {
if (a.indexOf('@') !== 0 && !this.attributes.get(a)) {
throw new Error("Cannot create an instance of " + this.id + " as the type does not allow an attribute '" + a + "'!");
}
}
}
if (attrs['@type']) {
attrs['@type'].push(this.id);
} else {
attrs['@type'] = this.id;
}
return new this.vie.Entity(attrs, opts);
}; |
toString()This method returns the id of the type.
| this.toString = function () {
return this.id;
};
}; |
VIE.Types()This is the constructor of a VIE.Types. This is a convenience class
to store
| VIE.prototype.Types = function () {
this._types = {};
|
add(id, attrs)This method adds a
| this.add = function (id, attrs) {
if (this.get(id)) {
throw new Error("Type '" + id + "' already registered.");
}
else {
if (typeof id === "string") {
var t = new this.vie.Type(id, attrs);
this._types[t.id] = t;
return t;
} else if (id instanceof this.vie.Type) {
this._types[id.id] = id;
return id;
} else {
throw new Error("Wrong argument to VIE.Types.add()!");
}
}
return this;
};
|
addOrOverwrite(id, attrs)This method adds or overwrites a
| this.addOrOverwrite = function(id, attrs){
if (this.get(id)) {
this.remove(id);
}
return this.add(id, attrs);
};
|
get(id)This method retrieves a
| this.get = function (id) {
if (!id) {
return undefined;
}
if (typeof id === 'string') {
var lid = this.vie.namespaces.isUri(id) ? id : this.vie.namespaces.uri(id);
return this._types[lid];
} else if (id instanceof this.vie.Type) {
return this.get(id.id);
}
return undefined;
};
|
remove(id)This method removes a type of given id from the type. This also
removes all children if their only parent were this
type. Furthermore, this removes the link from the
super- and subtypes.
| this.remove = function (id) {
var t = this.get(id); |
test whether the type actually exists in VIE | if (!t) {
return this;
}
if (!t || t.subsumes("owl:Thing")) {
console.warn("You are not allowed to remove 'owl:Thing'.");
return this;
}
delete this._types[t.id];
var subtypes = t.subtypes.list();
for (var c = 0; c < subtypes.length; c++) {
var childObj = subtypes[c];
if (childObj.supertypes.list().length === 1) { |
recursively remove all children | this.remove(childObj);
} else {
childObj.supertypes.remove(t.id);
}
}
return t;
};
|
toArray() === list()This method returns an array of all types.
| this.toArray = this.list = function () {
var ret = [];
for (var i in this._types) {
ret.push(this._types[i]);
}
return ret;
}; |
sort(types, desc)This method sorts an array of types in their order, given by the
inheritance. This returns a copy and leaves the original array untouched.
| this.sort = function (types, desc) {
var self = this;
types = (jQuery.isArray(types))? types : [ types ];
desc = (desc)? true : false;
if (types.length === 0) return [];
var copy = [ types[0] ];
for (var x = 1, tlen = types.length; x < tlen; x++) {
var insert = types[x];
var insType = self.get(insert);
if (insType) {
for (var y = 0; y < copy.length; y++) {
if (insType.subsumes(copy[y])) {
copy.splice(y,0,insert);
break;
} else if (y === copy.length - 1) {
copy.push(insert);
}
}
}
}
if (!desc) {
copy.reverse();
}
return copy;
};
};
|