typejson rename
author:
contributor:
published:
updated:
source uri:
Summary
/*
* Helper functions
*/
// Different expected types mean different properties we need to look...
* Helper functions
*/
// Different expected types mean different properties we need to look...
Content
/*
* Helper functions
*/
// Different expected types mean different properties we need to look for on the other end
function subPropQuery(p){
switch (p.expected_type.id) {
// Value types
case "/type/int":
case "/type/float":
case "/type/boolean":
case "/type/rawstring":
case "/type/uri":
case "/type/text":
case "/type/datetime":
case "/type/bytestring":
case "/type/id":
case "/type/key":
case "/type/value":
return [{"value":null, "optional":true, "index":null, "sort":"index"}];
// Localized strings
// TODO: Ability to request other languages
case "/type/text":
return [{"id":null,"name":null,"lang":"lang/en","optional":true, "index":null, "sort":"index"}];
// TODO: These types need special handling too
case "/common/document":
case "/common/image":
case "/common/webpage":
// Standard object
default:
return [{"id":null, "name":null, "optional":true, "index":null, "sort":"index"}];
}
};
function propQuery(p){
// Test whether property is a CVT
if (p.expected_type['/freebase/type_hints/mediator']) {
//If so, add sub queries for the CVTs disambiguating properties
var q = [{"id":null,"optional":true}]
for (sp in p.expected_type.properties) {
//Check if property is reciprocating
if ((p.expected_type.properties[sp].id == p.master_property.id) || (p.expected_type.properties[sp].id == p.master_property.reverse_property)) {
//If also not unique, it is a symmetrical CVT and we need to filter out original topic
if(p.expected_type.properties[sp].unique != true) {
q[0][p.expected_type.properties[sp].id] = subPropQuery(p.expected_type.properties[sp]);
q[0][p.expected_type.properties[sp].id][0]['id!='] = topicid
}
//Otherwise, don't bother with the property at all since it will just be the topic itself
} else {
q[0][p.expected_type.properties[sp].id] = subPropQuery(p.expected_type.properties[sp]);
}
}
// If property is not reciprocating, just get the values or objects on the other end
} else {
var q = subPropQuery(p);
}
return q;
};
function constructQuery(schema){
// Start with parameterized base query
var q = {"id":topicid,"type":typeid};
// Then add in the optimized query for each property found in the schema
for (p in schema.properties) {
q[schema.properties[p].id] = propQuery(schema.properties[p]);
}
return (q);
}
/*
* Main body
*/
// TODO: fail without topic id
var topicid = acre.environ.params.id;
// Default to topic if no type given
var typeid = acre.environ.params.type ? acre.environ.params.type : "/common/topic";
// First need to find out all there is to know about
// the properties of the type we're interested in
var schemaIntrospection = acre.mqlread({
"id" : typeid,
"type" : "/type/type",
"properties" : [{
"id" : null,
"name" : null,
"index" : null,
"sort" : "index",
"master_property" : {
"id" : null,
"reverse_property" : null,
"optional" : true
},
"expected_type" : {
"id" : null,
"name" : null,
"/freebase/type_hints/mediator" : null,
"properties" : [{
"id" : null,
"name" : null,
"unique" : null,
"expected_type" : {
"id":null
},
"/freebase/property_hints/disambiguator" : true,
"/freebase/property_hints/display_none" : {
"optional" : "forbidden",
"value" : true
},
"index" : null,
"sort" : "index",
"optional" : true
}],
"/freebase/property_hints/display_none" : {
"optional" : "forbidden",
"value" : true
}
}
}],
});
var data = acre.mqlread(constructQuery(schemaIntrospection));
acre.start_response(200, {
'content-type': 'application/json; charset="UTF-8"'
});
acre.write(JSON.stringify(data));
* Helper functions
*/
// Different expected types mean different properties we need to look for on the other end
function subPropQuery(p){
switch (p.expected_type.id) {
// Value types
case "/type/int":
case "/type/float":
case "/type/boolean":
case "/type/rawstring":
case "/type/uri":
case "/type/text":
case "/type/datetime":
case "/type/bytestring":
case "/type/id":
case "/type/key":
case "/type/value":
return [{"value":null, "optional":true, "index":null, "sort":"index"}];
// Localized strings
// TODO: Ability to request other languages
case "/type/text":
return [{"id":null,"name":null,"lang":"lang/en","optional":true, "index":null, "sort":"index"}];
// TODO: These types need special handling too
case "/common/document":
case "/common/image":
case "/common/webpage":
// Standard object
default:
return [{"id":null, "name":null, "optional":true, "index":null, "sort":"index"}];
}
};
function propQuery(p){
// Test whether property is a CVT
if (p.expected_type['/freebase/type_hints/mediator']) {
//If so, add sub queries for the CVTs disambiguating properties
var q = [{"id":null,"optional":true}]
for (sp in p.expected_type.properties) {
//Check if property is reciprocating
if ((p.expected_type.properties[sp].id == p.master_property.id) || (p.expected_type.properties[sp].id == p.master_property.reverse_property)) {
//If also not unique, it is a symmetrical CVT and we need to filter out original topic
if(p.expected_type.properties[sp].unique != true) {
q[0][p.expected_type.properties[sp].id] = subPropQuery(p.expected_type.properties[sp]);
q[0][p.expected_type.properties[sp].id][0]['id!='] = topicid
}
//Otherwise, don't bother with the property at all since it will just be the topic itself
} else {
q[0][p.expected_type.properties[sp].id] = subPropQuery(p.expected_type.properties[sp]);
}
}
// If property is not reciprocating, just get the values or objects on the other end
} else {
var q = subPropQuery(p);
}
return q;
};
function constructQuery(schema){
// Start with parameterized base query
var q = {"id":topicid,"type":typeid};
// Then add in the optimized query for each property found in the schema
for (p in schema.properties) {
q[schema.properties[p].id] = propQuery(schema.properties[p]);
}
return (q);
}
/*
* Main body
*/
// TODO: fail without topic id
var topicid = acre.environ.params.id;
// Default to topic if no type given
var typeid = acre.environ.params.type ? acre.environ.params.type : "/common/topic";
// First need to find out all there is to know about
// the properties of the type we're interested in
var schemaIntrospection = acre.mqlread({
"id" : typeid,
"type" : "/type/type",
"properties" : [{
"id" : null,
"name" : null,
"index" : null,
"sort" : "index",
"master_property" : {
"id" : null,
"reverse_property" : null,
"optional" : true
},
"expected_type" : {
"id" : null,
"name" : null,
"/freebase/type_hints/mediator" : null,
"properties" : [{
"id" : null,
"name" : null,
"unique" : null,
"expected_type" : {
"id":null
},
"/freebase/property_hints/disambiguator" : true,
"/freebase/property_hints/display_none" : {
"optional" : "forbidden",
"value" : true
},
"index" : null,
"sort" : "index",
"optional" : true
}],
"/freebase/property_hints/display_none" : {
"optional" : "forbidden",
"value" : true
}
}
}],
});
var data = acre.mqlread(constructQuery(schemaIntrospection));
acre.start_response(200, {
'content-type': 'application/json; charset="UTF-8"'
});
acre.write(JSON.stringify(data));
Created by:
jdouglas
Mar 10, 2008
Last edited by:
Constant Gardener Process
Aug 18, 2009
Recent Discussions about typejson
There is no discussion about this document.
Start the Discussion »