Concept:PID/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
 /**
  * CANInfo
  * OBD-II Diagnostic CAN Bus Information item
  */
  public static class CANInfo extends TopicBase {
  
    public String name;
    public String description;
    public String unit;
    public String pid;

    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getDescription() { return description; }
    public void setDescription(String pDescription) { description=pDescription; }
    public String getUnit() { return unit; }
    public void setUnit(String pUnit) { unit=pUnit; }
    public String getPid() { return pid; }
    public void setPid(String pPid) { pid=pPid; }
    /**
     * convert this CANInfo to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

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

    /**
     * convert this CANInfo to a SiDIF string
     * @return the SiDIF representation of this CANInfo
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA CANInfo\n",this.pageid);
      siDIF+=propertySiDIF("name",name,"Text");
      siDIF+=propertySiDIF("description",description,"Text");
      siDIF+=propertySiDIF("unit",unit,"Text");
      siDIF+=propertySiDIF("pid",pid,"Page");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a CANInfo from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public CANInfo(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:CANInfo_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple descriptionTriple=query.selectSingle(pageid,"description",null);
      if (descriptionTriple==null)
        descriptionTriple=query.selectSingle(pageid,"Property:CANInfo_description",null);
      if (descriptionTriple!=null) 
        description=descriptionTriple.getObject().toString();
      Triple unitTriple=query.selectSingle(pageid,"unit",null);
      if (unitTriple==null)
        unitTriple=query.selectSingle(pageid,"Property:CANInfo_unit",null);
      if (unitTriple!=null) 
        unit=unitTriple.getObject().toString();
      Triple pidTriple=query.selectSingle(pageid,"pid",null);
      if (pidTriple==null)
        pidTriple=query.selectSingle(pageid,"Property:CANInfo_pid",null);
      if (pidTriple!=null) 
        pid=pidTriple.getObject().toString();
      init(query);
    } // constructor for CANInfo
    
    // >>>{user defined topic code}{CANInfo}{CANInfo}
    // <<<{user defined topic code}{CANInfo}{CANInfo}
  } // class CANInfo
  /**
   * Manager for CANInfo
   */
  public static class CANInfoManager extends TopicBase {
 
    public String topicName="CANInfo";
    public transient List<CANInfo> mCANInfos=new ArrayList<CANInfo>();
    public transient Map<String,CANInfo> mCANInfoMap=new LinkedHashMap<String,CANInfo>();

    /**
     * get my CANInfos
     */
    public List<CANInfo> getCANInfos() {
      List<CANInfo> result=this.mCANInfos;
      return result;
    }

    /**
     *  add a new CANInfo 
     */
    public CANInfo add(CANInfo pCANInfo) {
      mCANInfos.add(pCANInfo);
      mCANInfoMap.put(pCANInfo.getPageid(),pCANInfo);
      return pCANInfo;
    }

    /**
     *  add a new CANInfo from the given triple
     */
    public CANInfo add(TripleQuery query,Triple pCANInfoTriple) {
      CANInfo lCANInfo=new CANInfo(query,pCANInfoTriple);
      add(lCANInfo);
      return lCANInfo;
    }

    // reinitialize my mCANInfo map
    public void reinit() {
      mCANInfoMap.clear();
      for (CANInfo lCANInfo:mCANInfos) {
        mCANInfoMap.put(lCANInfo.getPageid(),lCANInfo);
      }
    }

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

    // default constructor for CANInfo Manager
    public CANInfoManager() {}

    // add CANInfos from the given query
    public void addCANInfos(TripleQuery pCANInfoQuery,TripleQuery query) {
      if (pCANInfoQuery!=null) {
        for (Triple lCANInfoTriple:pCANInfoQuery.getTriples()) {
          add(query,lCANInfoTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public CANInfoManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lCANInfoQuery=query.query(null,"isA","CANInfo");
      addCANInfos(lCANInfoQuery,query);
      // then the SMW triplestore
      lCANInfoQuery=query.query(null,"Property:IsA","CANInfo");
      addCANInfos(lCANInfoQuery,query);
      init(query);
    } // constructor for CANInfo Manager
    
    // >>>{user defined topicmanager code}{CANInfo}{CANInfo}
    // <<<{user defined topicmanager code}{CANInfo}{CANInfo}
  } // class CANInfo Manager
 /**
  * PID
  * OBD-II Diagnostic Parameter
  */
  public static class PID extends TopicBase {
  
    public String id;
    public String name;
    public String description;
    public String links;
    public String examples;
    public String frequency;

    public String getId() { return id; }
    public void setId(String pId) { id=pId; }
    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getDescription() { return description; }
    public void setDescription(String pDescription) { description=pDescription; }
    public String getLinks() { return links; }
    public void setLinks(String pLinks) { links=pLinks; }
    public String getExamples() { return examples; }
    public void setExamples(String pExamples) { examples=pExamples; }
    public String getFrequency() { return frequency; }
    public void setFrequency(String pFrequency) { frequency=pFrequency; }
    /**
     * convert this PID to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this PID to a WikiSon string
     * @return the WikiSon representation of this PID
     */
    public String toWikiSon() {
      String wikison= "{{PID\n";
      wikison+=toWikiSon("id",id);
      wikison+=toWikiSon("name",name);
      wikison+=toWikiSon("description",description);
      wikison+=toWikiSon("links",links);
      wikison+=toWikiSon("examples",examples);
      wikison+=toWikiSon("frequency",frequency);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this PID to a SiDIF string
     * @return the SiDIF representation of this PID
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA PID\n",this.pageid);
      siDIF+=propertySiDIF("id",id,"Text");
      siDIF+=propertySiDIF("name",name,"Text");
      siDIF+=propertySiDIF("description",description,"Text");
      siDIF+=propertySiDIF("links",links,"Text");
      siDIF+=propertySiDIF("examples",examples,"Text");
      siDIF+=propertySiDIF("frequency",frequency,"Text");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a PID from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public PID(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple idTriple=query.selectSingle(pageid,"id",null);
      if (idTriple==null)
        idTriple=query.selectSingle(pageid,"Property:PID_id",null);
      if (idTriple!=null) 
        id=idTriple.getObject().toString();
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:PID_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple descriptionTriple=query.selectSingle(pageid,"description",null);
      if (descriptionTriple==null)
        descriptionTriple=query.selectSingle(pageid,"Property:PID_description",null);
      if (descriptionTriple!=null) 
        description=descriptionTriple.getObject().toString();
      Triple linksTriple=query.selectSingle(pageid,"links",null);
      if (linksTriple==null)
        linksTriple=query.selectSingle(pageid,"Property:PID_links",null);
      if (linksTriple!=null) 
        links=linksTriple.getObject().toString();
      Triple examplesTriple=query.selectSingle(pageid,"examples",null);
      if (examplesTriple==null)
        examplesTriple=query.selectSingle(pageid,"Property:PID_examples",null);
      if (examplesTriple!=null) 
        examples=examplesTriple.getObject().toString();
      Triple frequencyTriple=query.selectSingle(pageid,"frequency",null);
      if (frequencyTriple==null)
        frequencyTriple=query.selectSingle(pageid,"Property:PID_frequency",null);
      if (frequencyTriple!=null) 
        frequency=frequencyTriple.getObject().toString();
      init(query);
    } // constructor for PID
    
    // >>>{user defined topic code}{PID}{PID}
    // <<<{user defined topic code}{PID}{PID}
  } // class PID
  /**
   * Manager for PID
   */
  public static class PIDManager extends TopicBase {
 
    public String topicName="PID";
    public transient List<PID> mPIDs=new ArrayList<PID>();
    public transient Map<String,PID> mPIDMap=new LinkedHashMap<String,PID>();

    /**
     * get my PIDs
     */
    public List<PID> getPIDs() {
      List<PID> result=this.mPIDs;
      return result;
    }

    /**
     *  add a new PID 
     */
    public PID add(PID pPID) {
      mPIDs.add(pPID);
      mPIDMap.put(pPID.getPageid(),pPID);
      return pPID;
    }

    /**
     *  add a new PID from the given triple
     */
    public PID add(TripleQuery query,Triple pPIDTriple) {
      PID lPID=new PID(query,pPIDTriple);
      add(lPID);
      return lPID;
    }

    // reinitialize my mPID map
    public void reinit() {
      mPIDMap.clear();
      for (PID lPID:mPIDs) {
        mPIDMap.put(lPID.getPageid(),lPID);
      }
    }

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

    // default constructor for PID Manager
    public PIDManager() {}

    // add PIDs from the given query
    public void addPIDs(TripleQuery pPIDQuery,TripleQuery query) {
      if (pPIDQuery!=null) {
        for (Triple lPIDTriple:pPIDQuery.getTriples()) {
          add(query,lPIDTriple);
        }
      }
    }

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

}
Showing below 0 pages.

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