Concept:Brainteaser/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
 /**
  * Brainteaser
  * BITPlan Newsletter
  */
  public static class Brainteaser extends TopicBase {
  
    public String issue;
    public String date;
    public String problem;
    public String solution;
    public String finishDate;
    public String pdf;
    public String page;

    public String getIssue() { return issue; }
    public void setIssue(String pIssue) { issue=pIssue; }
    public String getDate() { return date; }
    public void setDate(String pDate) { date=pDate; }
    public String getProblem() { return problem; }
    public void setProblem(String pProblem) { problem=pProblem; }
    public String getSolution() { return solution; }
    public void setSolution(String pSolution) { solution=pSolution; }
    public String getFinishDate() { return finishDate; }
    public void setFinishDate(String pFinishDate) { finishDate=pFinishDate; }
    public String getPdf() { return pdf; }
    public void setPdf(String pPdf) { pdf=pPdf; }
    public String getPage() { return page; }
    public void setPage(String pPage) { page=pPage; }
    /**
     * convert this Brainteaser to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this Brainteaser to a WikiSon string
     * @return the WikiSon representation of this Brainteaser
     */
    public String toWikiSon() {
      String wikison= "{{Brainteaser\n";
      wikison+=toWikiSon("issue",issue);
      wikison+=toWikiSon("date",date);
      wikison+=toWikiSon("problem",problem);
      wikison+=toWikiSon("solution",solution);
      wikison+=toWikiSon("finishDate",finishDate);
      wikison+=toWikiSon("pdf",pdf);
      wikison+=toWikiSon("page",page);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this Brainteaser to a SiDIF string
     * @return the SiDIF representation of this Brainteaser
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA Brainteaser\n",this.pageid);
      siDIF+=propertySiDIF("issue",issue,"Text");
      siDIF+=propertySiDIF("date",date,"Date");
      siDIF+=propertySiDIF("problem",problem,"Text");
      siDIF+=propertySiDIF("solution",solution,"Text");
      siDIF+=propertySiDIF("finishDate",finishDate,"Date");
      siDIF+=propertySiDIF("pdf",pdf,"Page");
      siDIF+=propertySiDIF("page",page,"Page");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a Brainteaser from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Brainteaser(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple issueTriple=query.selectSingle(pageid,"issue",null);
      if (issueTriple==null)
        issueTriple=query.selectSingle(pageid,"Property:Brainteaser_issue",null);
      if (issueTriple!=null) 
        issue=issueTriple.getObject().toString();
      Triple dateTriple=query.selectSingle(pageid,"date",null);
      if (dateTriple==null)
        dateTriple=query.selectSingle(pageid,"Property:Brainteaser_date",null);
      if (dateTriple!=null) 
        date=dateTriple.getObject().toString();
      Triple problemTriple=query.selectSingle(pageid,"problem",null);
      if (problemTriple==null)
        problemTriple=query.selectSingle(pageid,"Property:Brainteaser_problem",null);
      if (problemTriple!=null) 
        problem=problemTriple.getObject().toString();
      Triple solutionTriple=query.selectSingle(pageid,"solution",null);
      if (solutionTriple==null)
        solutionTriple=query.selectSingle(pageid,"Property:Brainteaser_solution",null);
      if (solutionTriple!=null) 
        solution=solutionTriple.getObject().toString();
      Triple finishDateTriple=query.selectSingle(pageid,"finishDate",null);
      if (finishDateTriple==null)
        finishDateTriple=query.selectSingle(pageid,"Property:Brainteaser_finishDate",null);
      if (finishDateTriple!=null) 
        finishDate=finishDateTriple.getObject().toString();
      Triple pdfTriple=query.selectSingle(pageid,"pdf",null);
      if (pdfTriple==null)
        pdfTriple=query.selectSingle(pageid,"Property:Brainteaser_pdf",null);
      if (pdfTriple!=null) 
        pdf=pdfTriple.getObject().toString();
      Triple pageTriple=query.selectSingle(pageid,"page",null);
      if (pageTriple==null)
        pageTriple=query.selectSingle(pageid,"Property:Brainteaser_page",null);
      if (pageTriple!=null) 
        page=pageTriple.getObject().toString();
      init(query);
    } // constructor for Brainteaser
    
    // >>>{user defined topic code}{Brainteaser}{Brainteaser}
    // <<<{user defined topic code}{Brainteaser}{Brainteaser}
  } // class Brainteaser
  /**
   * Manager for Brainteaser
   */
  public static class BrainteaserManager extends TopicBase {
 
    public String topicName="Brainteaser";
    public transient List<Brainteaser> mBrainteasers=new ArrayList<Brainteaser>();
    public transient Map<String,Brainteaser> mBrainteaserMap=new LinkedHashMap<String,Brainteaser>();

    /**
     * get my Brainteasers
     */
    public List<Brainteaser> getBrainteasers() {
      List<Brainteaser> result=this.mBrainteasers;
      return result;
    }

    /**
     *  add a new Brainteaser 
     */
    public Brainteaser add(Brainteaser pBrainteaser) {
      mBrainteasers.add(pBrainteaser);
      mBrainteaserMap.put(pBrainteaser.getPageid(),pBrainteaser);
      return pBrainteaser;
    }

    /**
     *  add a new Brainteaser from the given triple
     */
    public Brainteaser add(TripleQuery query,Triple pBrainteaserTriple) {
      Brainteaser lBrainteaser=new Brainteaser(query,pBrainteaserTriple);
      add(lBrainteaser);
      return lBrainteaser;
    }

    // reinitialize my mBrainteaser map
    public void reinit() {
      mBrainteaserMap.clear();
      for (Brainteaser lBrainteaser:mBrainteasers) {
        mBrainteaserMap.put(lBrainteaser.getPageid(),lBrainteaser);
      }
    }

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

    // default constructor for Brainteaser Manager
    public BrainteaserManager() {}

    // add Brainteasers from the given query
    public void addBrainteasers(TripleQuery pBrainteaserQuery,TripleQuery query) {
      if (pBrainteaserQuery!=null) {
        for (Triple lBrainteaserTriple:pBrainteaserQuery.getTriples()) {
          add(query,lBrainteaserTriple);
        }
      }
    }

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

}
Showing below 0 pages.

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