Concept:Volume/Java

From BITPlan ceur-ws Wiki
Revision as of 10:16, 20 July 2020 by Wf (talk | contribs) (created by WikiTask 2020-07-20T08:16:09Z)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 my SiDIF representation
     */
    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
 /**
  * Volume
  * A Volume is a a collection of papers mostly documenting the results of an academic event
  */
  public static class Volume extends TopicBase {
  
    public String number;

    public String getNumber() { return number; }
    public void setNumber(String pNumber) { number=pNumber; }
    /**
     * convert this Volume to a JSON string
     * @return the JSON representation
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this Volume to a WikiSon string
     * @return the WikiSon representation of this Volume
     */
    public String toWikiSon() {
      String wikison= "{{Volume\n";
      wikison+=toWikiSon("number",number);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this Volume to a SiDIF string
     * @return the SiDIF representation of this Volume
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA Volume\n",this.pageid);
      siDIF+=propertySiDIF("number",number,"Number");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a Volume from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Volume(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple numberTriple=query.selectSingle(pageid,"number",null);
      if (numberTriple==null)
        numberTriple=query.selectSingle(pageid,"Property:Volume_number",null);
      if (numberTriple!=null) 
        number=numberTriple.getObject().toString();
      init(query);
    } // constructor for Volume
    
    // >>>{user defined topic code}{Volume}{Volume}
    // <<<{user defined topic code}{Volume}{Volume}
  } // class Volume
  /**
   * Manager for Volume
   */
  public static class VolumeManager extends TopicBase {
 
    public String topicName="Volume";
    public transient List<Volume> mVolumes=new ArrayList<Volume>();
    public transient Map<String,Volume> mVolumeMap=new LinkedHashMap<String,Volume>();

    /**
     * get my Volumes
     */
    public List<Volume> getVolumes() {
      List<Volume> result=this.mVolumes;
      return result;
    }

    /**
     *  add a new Volume 
     */
    public Volume add(Volume pVolume) {
      mVolumes.add(pVolume);
      mVolumeMap.put(pVolume.getPageid(),pVolume);
      return pVolume;
    }

    /**
     *  add a new Volume from the given triple
     */
    public Volume add(TripleQuery query,Triple pVolumeTriple) {
      Volume lVolume=new Volume(query,pVolumeTriple);
      add(lVolume);
      return lVolume;
    }

    // reinitialize my mVolume map
    public void reinit() {
      mVolumeMap.clear();
      for (Volume lVolume:mVolumes) {
        mVolumeMap.put(lVolume.getPageid(),lVolume);
      }
    }

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

    // default constructor for Volume Manager
    public VolumeManager() {}

    // add Volumes from the given query
    public void addVolumes(TripleQuery pVolumeQuery,TripleQuery query) {
      if (pVolumeQuery!=null) {
        for (Triple lVolumeTriple:pVolumeQuery.getTriples()) {
          add(query,lVolumeTriple);
        }
      }
    }

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

}
Showing below 0 pages.

Retrieved from "http://ceur-ws.bitplan.com/index.php?title=Concept:Volume/Java&oldid=241#smw-result"