Concept:SimpleGraphModule/Java

From BITPlan Wiki
Jump to navigation Jump to search

java code

@// This is a rythm template
@// the args are the standard wikiTask arguments
@import org.sidif.triple.TripleQuery
@import org.sidif.triple.Triple
@import com.alibaba.fastjson.JSON
@args() {
  String title 
  String logo
  org.sidif.wiki.WikiTask wikiTask
  org.sidif.triple.TripleStore tripleStore
}


@def static {

  /**
   * Base class
   */
  static abstract class TopicBase {
    // each Topic has a pageid - for non subobject thats the pagename
    public String pageid;

    /**
     * get a WikiSon version of the given name value
     * 
     * @param name
     * @param value
     * @return - the string representation
     */
    public String toWikiSon(String name, String value) {
      String result = "<!-- " + name + " is null-->\n";
      if (value != null)
        result = "|" + name + "=" + value + "\n";
      return result;
    }

    /**
     * get the SiDIF representation of the given property
     * 
     * @param name - the name of the property
     * @param value - the value of the property
     * @param type - the type of the property
     * @return - the SiDIF Sting representation of the property
     */
    public static String propertySiDIF(String name, String value, String type) {
      // default is a comment line which can be filled by uncommenting
      String result = String.format("# is is %s of it\n",name);;
      // if the value is not empty
      if ((value != null) && (!("".equals(value.trim())))) {
        // do we need to quote the result?
        String quote = "";
        // this depends on the Type
        if ("Text".equals(type)) {
          quote = "\"";
        }
        // create a SIDIF Property line like
        // "John" is lastname of it
        // convert double quotes to single quotes - FIXME - should we escape instead?
        value=value.replace("\"","'");
        result = String.format("%s%s%s is %s of it\n",quote,value,quote,name);
      }
      // return the SiDIF property line
      return result;
    }

    /**
     * get me as a String
     * 
     * @param name
     * @param value
     * @return
     */
    public static String propertySiDIF(String name, String value) {
      String result = propertySiDIF(name, value, "Text");
      return result;
    }

    /**
     * check if the given boolean String value is true
     * 
     * @param value
     * @return true if the value is not null and has true/TRUE as it's string
     *         content
     */
    public boolean isTrue(String value) {
      boolean result = false;
      if (value != null && value.toLowerCase().equals("true")) {
        result = true;
      }
      return result;
    }

    /**
     * initialize
     */
    public void init(TripleQuery query) {
    }
  } // TopicBase
 /**
  * SimpleGraphModule
  * A SimpleGraphModule is an Apache/TinkerPop API wrapper for a system
  */
  public static class SimpleGraphModule extends TopicBase {
  
    public String name;
    public String logo;
    public String modulename;
    public String systemname;
    public String url;
    public String apiname;
    public String apiurl;
    public String issue;
    public String documentation;

    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getLogo() { return logo; }
    public void setLogo(String pLogo) { logo=pLogo; }
    public String getModulename() { return modulename; }
    public void setModulename(String pModulename) { modulename=pModulename; }
    public String getSystemname() { return systemname; }
    public void setSystemname(String pSystemname) { systemname=pSystemname; }
    public String getUrl() { return url; }
    public void setUrl(String pUrl) { url=pUrl; }
    public String getApiname() { return apiname; }
    public void setApiname(String pApiname) { apiname=pApiname; }
    public String getApiurl() { return apiurl; }
    public void setApiurl(String pApiurl) { apiurl=pApiurl; }
    public String getIssue() { return issue; }
    public void setIssue(String pIssue) { issue=pIssue; }
    public String getDocumentation() { return documentation; }
    public void setDocumentation(String pDocumentation) { documentation=pDocumentation; }
    /**
     * convert this SimpleGraphModule to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this SimpleGraphModule to a WikiSon string
     * @return the WikiSon representation of this SimpleGraphModule
     */
    public String toWikiSon() {
      String wikison= "{{SimpleGraphModule\n";
      wikison+=toWikiSon("name",name);
      wikison+=toWikiSon("logo",logo);
      wikison+=toWikiSon("modulename",modulename);
      wikison+=toWikiSon("systemname",systemname);
      wikison+=toWikiSon("url",url);
      wikison+=toWikiSon("apiname",apiname);
      wikison+=toWikiSon("apiurl",apiurl);
      wikison+=toWikiSon("issue",issue);
      wikison+=toWikiSon("documentation",documentation);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this SimpleGraphModule to a SiDIF string
     * @return the SiDIF representation of this SimpleGraphModule
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA SimpleGraphModule\n",this.pageid);
      siDIF+=propertySiDIF("name",name,"Text");
      siDIF+=propertySiDIF("logo",logo,"Page");
      siDIF+=propertySiDIF("modulename",modulename,"Text");
      siDIF+=propertySiDIF("systemname",systemname,"Text");
      siDIF+=propertySiDIF("url",url,"URI");
      siDIF+=propertySiDIF("apiname",apiname,"Text");
      siDIF+=propertySiDIF("apiurl",apiurl,"URI");
      siDIF+=propertySiDIF("issue",issue,"ExternalIdentifier");
      siDIF+=propertySiDIF("documentation",documentation,"Text");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

    /**
     * default constructor for SimpleGraphModule
     */
    public SimpleGraphModule() {}

    /**
     * construct a SimpleGraphModule from the given Triple
     * @param query - the TripleQuery to get the triples from
     * @param pSimpleGraphModuleTriple - the triple to construct me from
     */
    public SimpleGraphModule(TripleQuery query,Triple pSimpleGraphModuleTriple) {
      this(query,pSimpleGraphModuleTriple.getSubject().toString());
    } // constructor

    /**
     * construct a SimpleGraphModule from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public SimpleGraphModule(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple logoTriple=query.selectSingle(pageid,"logo",null);
      if (logoTriple==null)
        logoTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_logo",null);
      if (logoTriple!=null) 
        logo=logoTriple.getObject().toString();
      Triple modulenameTriple=query.selectSingle(pageid,"modulename",null);
      if (modulenameTriple==null)
        modulenameTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_modulename",null);
      if (modulenameTriple!=null) 
        modulename=modulenameTriple.getObject().toString();
      Triple systemnameTriple=query.selectSingle(pageid,"systemname",null);
      if (systemnameTriple==null)
        systemnameTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_systemname",null);
      if (systemnameTriple!=null) 
        systemname=systemnameTriple.getObject().toString();
      Triple urlTriple=query.selectSingle(pageid,"url",null);
      if (urlTriple==null)
        urlTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_url",null);
      if (urlTriple!=null) 
        url=urlTriple.getObject().toString();
      Triple apinameTriple=query.selectSingle(pageid,"apiname",null);
      if (apinameTriple==null)
        apinameTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_apiname",null);
      if (apinameTriple!=null) 
        apiname=apinameTriple.getObject().toString();
      Triple apiurlTriple=query.selectSingle(pageid,"apiurl",null);
      if (apiurlTriple==null)
        apiurlTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_apiurl",null);
      if (apiurlTriple!=null) 
        apiurl=apiurlTriple.getObject().toString();
      Triple issueTriple=query.selectSingle(pageid,"issue",null);
      if (issueTriple==null)
        issueTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_issue",null);
      if (issueTriple!=null) 
        issue=issueTriple.getObject().toString();
      Triple documentationTriple=query.selectSingle(pageid,"documentation",null);
      if (documentationTriple==null)
        documentationTriple=query.selectSingle(pageid,"Property:SimpleGraphModule_documentation",null);
      if (documentationTriple!=null) 
        documentation=documentationTriple.getObject().toString();
      init(query);
    } // constructor for SimpleGraphModule
    
    // >>>{user defined topic code}{SimpleGraphModule}{SimpleGraphModule}
    // <<<{user defined topic code}{SimpleGraphModule}{SimpleGraphModule}
  } // class SimpleGraphModule
  /**
   * Manager for SimpleGraphModule
   */
  public static class SimpleGraphModuleManager extends TopicBase {
 
    public String topicName="SimpleGraphModule";
    public transient List<SimpleGraphModule> mSimpleGraphModules=new ArrayList<SimpleGraphModule>();
    public transient Map<String,SimpleGraphModule> mSimpleGraphModuleMap=new LinkedHashMap<String,SimpleGraphModule>();

    /**
     * get my SimpleGraphModules
     */
    public List<SimpleGraphModule> getSimpleGraphModules() {
      List<SimpleGraphModule> result=this.mSimpleGraphModules;
      return result;
    }

    /**
     *  add a new SimpleGraphModule 
     */
    public SimpleGraphModule add(SimpleGraphModule pSimpleGraphModule) {
      mSimpleGraphModules.add(pSimpleGraphModule);
      mSimpleGraphModuleMap.put(pSimpleGraphModule.getPageid(),pSimpleGraphModule);
      return pSimpleGraphModule;
    }

    /**
     *  add a new SimpleGraphModule from the given triple
     */
    public SimpleGraphModule add(TripleQuery query,Triple pSimpleGraphModuleTriple) {
      SimpleGraphModule lSimpleGraphModule=new SimpleGraphModule(query,pSimpleGraphModuleTriple);
      add(lSimpleGraphModule);
      return lSimpleGraphModule;
    }

    // reinitialize my mSimpleGraphModule map
    public void reinit() {
      mSimpleGraphModuleMap.clear();
      for (SimpleGraphModule lSimpleGraphModule:mSimpleGraphModules) {
        mSimpleGraphModuleMap.put(lSimpleGraphModule.getPageid(),lSimpleGraphModule);
      }
    }

    // convert this manager to json format 
    public String toJson() { return JSON.toJSONString(this); }
    
    // get a new manager from the given json string
    public static SimpleGraphModuleManager fromJson(String json) {
      SimpleGraphModuleManager result=JSON.parseObject(json, SimpleGraphModuleManager.class);
      result.reinit();
      return result;
    }

    // default constructor for SimpleGraphModule Manager
    public SimpleGraphModuleManager() {}

    // add SimpleGraphModules from the given query
    public void addSimpleGraphModules(TripleQuery pSimpleGraphModuleQuery,TripleQuery query) {
      if (pSimpleGraphModuleQuery!=null) {
        for (Triple lSimpleGraphModuleTriple:pSimpleGraphModuleQuery.getTriples()) {
          add(query,lSimpleGraphModuleTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public SimpleGraphModuleManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lSimpleGraphModuleQuery=query.query(null,"isA","SimpleGraphModule");
      addSimpleGraphModules(lSimpleGraphModuleQuery,query);
      // then the SMW triplestore
      lSimpleGraphModuleQuery=query.query(null,"Property:IsA","SimpleGraphModule");
      addSimpleGraphModules(lSimpleGraphModuleQuery,query);
      init(query);
    } // constructor for SimpleGraphModule Manager
    
    // >>>{user defined topicmanager code}{SimpleGraphModule}{SimpleGraphModule}
    // <<<{user defined topicmanager code}{SimpleGraphModule}{SimpleGraphModule}
  } // class SimpleGraphModule Manager

}
Showing below 0 pages.

Retrieved from "https://wiki.bitplan.com/index.php?title=Concept:SimpleGraphModule/Java&oldid=6334#smw-result"