close

  
<p> //returns a chunk of HTML to display for the id
function reflection(id){
  output = "";
  
  //FOR METASCHEMA: change "reflect" to "metareflect" in this next line
  var reflectquery = acre.require("albums").query;
  
  //the metareflect query is almost exactly the same as the reflect query.
  //the one small change lets us see the property as a metaschema relationship
  
  reflectquery = acre.freebase.extend_query(reflectquery, {"id":id});
  
  //we can also extend the query structure ourselves
  reflectquery["type"][0]["properties"][0]["links"][0]["source"]["id"] = id;
  reflectquery["type"][0]["properties"][0]["master_property"]["links"][0]["target"]["id"] = id;
  
  var rez = acre.freebase.mqlread(reflectquery).result;
  console.log(rez); //for console debug mode
  
  //iterate through the result structure
  for(typeindex in rez["type"]){
    for(propindex in rez["type"][typeindex]["properties"]){
      var propstruc = rez["type"][typeindex]["properties"][propindex];
      
      //what property is this
      var theprop = propstruc["id"];
      
      //The metaschema version of this query has one more structure to inspect.
      //Using propstruc["!/base/fbontology/predicate_path/properties"][0] is a bit of a cheat!
      //This version will only get you single property path relations
      //to do this fully we need to change the metareflect query to dig a little deeper
      //and look at the chain in properties in the property path
      
      if(propstruc["!/base/fbontology/predicate_path/properties"] != undefined){
        theprop = propstruc["!/base/fbontology/predicate_path/properties"][0]["predicate"];
      }
    
      console.log(theprop);
      
      //look at all the outbound links
      for(olinkindex in propstruc["links"]){
        
        //get the "value" of this property (it might be an object or primitive value)
        if(propstruc["links"][olinkindex]["target_value"] == null){
          var propval = propstruc["links"][olinkindex]["target"]["id"]
        }else{
          var propval = propstruc["links"][olinkindex]["target_value"]
        }
        
        //call the appropriate function for this property
        var snippet = dispatcher(theprop, propval);
        
        if(snippet != null){
          output += snippet + "<br>";
        }
      }//end olink loop
      
      //look at all the inbound links
      if(propstruc["master_property"] != null){
        for(ilinkindex in propstruc["master_property"]["links"]){
          
          //get the "value" of this property (inlinks can only be objects)
          var propval = propstruc["master_property"]["links"][ilinkindex]["source"]["id"]
              
              //call the appropriate function for this property
              var snippet = dispatcher(theprop, propval);
          
          if(snippet != null){
            output += snippet + "<br>";
          }
        }//end ilink loop
      }//if master-links
      
    }//for prop
  }//for type

  return output; 
}

//the magic dispatcher
//if a function with the property name exists, call it with the value of the prop
function dispatcher(prop, val){
  var targfuncname = prop2funcname(prop);
  var targfunc = acre.require("prop_actions")[targfuncname];
  console.log(targfuncname);
  if(targfunc != undefined){
    return targfunc.call(this, val);
  }
  
  return null;
}

//functions can't have slash names, so replace with underscore...and drop the first slash
function prop2funcname(id){
  if(id.charAt(0) == "/"){
   return id.substr(1).replace(/\//g,"_"); 
  }
  return id; //didn't start with slash, then it was a metaschema predicate...good to go.
}


 </p>

Comments

Hide