PersonJava

From BITPlan royal-family Wiki
Revision as of 06:08, 7 October 2017 by Wf (talk | contribs) (→‎Code)
Jump to navigation Jump to search

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 propertySidif of the given property
     * 
     * @param name
     * @param value
     * @return
     */
    public static String propertySiDIF(String name, String value, String type) {
      // default is an empty string - no property line for emtpy values
      String result = "";
      // 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
        result += quote + value + quote + " is " + name + " of it\n";
      }
      // 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
 /**
  * Family
  * In most societies, the family is the principal institution for the socialization of children.
  */
  public static class Family extends TopicBase {
  
    public String name;
    public String weddingDate;
    public String weddingPlace;
    public String yearMarried;
    public String monthMarried;
    public String divorced;
    public String parentOf;
    public String husbandOf;
    public String wifeOf;

    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getWeddingDate() { return weddingDate; }
    public void setWeddingDate(String pWeddingDate) { weddingDate=pWeddingDate; }
    public String getWeddingPlace() { return weddingPlace; }
    public void setWeddingPlace(String pWeddingPlace) { weddingPlace=pWeddingPlace; }
    public String getYearMarried() { return yearMarried; }
    public void setYearMarried(String pYearMarried) { yearMarried=pYearMarried; }
    public String getMonthMarried() { return monthMarried; }
    public void setMonthMarried(String pMonthMarried) { monthMarried=pMonthMarried; }
    public String getDivorced() { return divorced; }
    public void setDivorced(String pDivorced) { divorced=pDivorced; }
    public String getParentOf() { return parentOf; }
    public void setParentOf(String pParentOf) { parentOf=pParentOf; }
    public String getHusbandOf() { return husbandOf; }
    public void setHusbandOf(String pHusbandOf) { husbandOf=pHusbandOf; }
    public String getWifeOf() { return wifeOf; }
    public void setWifeOf(String pWifeOf) { wifeOf=pWifeOf; }
    /**
     * convert this Family to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this Family to a WikiSon string
     */
    public String toWikiSon() {
      String wikison= "{{Family\n";
      wikison+=toWikiSon("name",name);
      wikison+=toWikiSon("weddingDate",weddingDate);
      wikison+=toWikiSon("weddingPlace",weddingPlace);
      wikison+=toWikiSon("yearMarried",yearMarried);
      wikison+=toWikiSon("monthMarried",monthMarried);
      wikison+=toWikiSon("divorced",divorced);
      wikison+=toWikiSon("parentOf",parentOf);
      wikison+=toWikiSon("husbandOf",husbandOf);
      wikison+=toWikiSon("wifeOf",wifeOf);
      wikison+="}}\n";
      return wikison;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a Family from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Family(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:Family_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple weddingDateTriple=query.selectSingle(pageid,"weddingDate",null);
      if (weddingDateTriple==null)
        weddingDateTriple=query.selectSingle(pageid,"Property:Family_weddingDate",null);
      if (weddingDateTriple!=null) 
        weddingDate=weddingDateTriple.getObject().toString();
      Triple weddingPlaceTriple=query.selectSingle(pageid,"weddingPlace",null);
      if (weddingPlaceTriple==null)
        weddingPlaceTriple=query.selectSingle(pageid,"Property:Family_weddingPlace",null);
      if (weddingPlaceTriple!=null) 
        weddingPlace=weddingPlaceTriple.getObject().toString();
      Triple yearMarriedTriple=query.selectSingle(pageid,"yearMarried",null);
      if (yearMarriedTriple==null)
        yearMarriedTriple=query.selectSingle(pageid,"Property:Family_yearMarried",null);
      if (yearMarriedTriple!=null) 
        yearMarried=yearMarriedTriple.getObject().toString();
      Triple monthMarriedTriple=query.selectSingle(pageid,"monthMarried",null);
      if (monthMarriedTriple==null)
        monthMarriedTriple=query.selectSingle(pageid,"Property:Family_monthMarried",null);
      if (monthMarriedTriple!=null) 
        monthMarried=monthMarriedTriple.getObject().toString();
      Triple divorcedTriple=query.selectSingle(pageid,"divorced",null);
      if (divorcedTriple==null)
        divorcedTriple=query.selectSingle(pageid,"Property:Family_divorced",null);
      if (divorcedTriple!=null) 
        divorced=divorcedTriple.getObject().toString();
      Triple parentOfTriple=query.selectSingle(pageid,"parentOf",null);
      if (parentOfTriple==null)
        parentOfTriple=query.selectSingle(pageid,"Property:Family_parentOf",null);
      if (parentOfTriple!=null) 
        parentOf=parentOfTriple.getObject().toString();
      Triple husbandOfTriple=query.selectSingle(pageid,"husbandOf",null);
      if (husbandOfTriple==null)
        husbandOfTriple=query.selectSingle(pageid,"Property:Family_husbandOf",null);
      if (husbandOfTriple!=null) 
        husbandOf=husbandOfTriple.getObject().toString();
      Triple wifeOfTriple=query.selectSingle(pageid,"wifeOf",null);
      if (wifeOfTriple==null)
        wifeOfTriple=query.selectSingle(pageid,"Property:Family_wifeOf",null);
      if (wifeOfTriple!=null) 
        wifeOf=wifeOfTriple.getObject().toString();
      init(query);
    } // constructor for Family
    
    // >>>{user defined topic code}{Family}{Family}
    // <<<{user defined topic code}{Family}{Family}
  } // class Family
  /**
   * Manager for Family
   */
  public static class FamilyManager extends TopicBase {
 
    public String topicName="Family";
    public transient List<Family> mFamilys=new ArrayList<Family>();
    public transient Map<String,Family> mFamilyMap=new LinkedHashMap<String,Family>();

    /**
     * get my Families
     */
    public List<Family> getFamilies() {
      List<Family> result=this.mFamilys;
      return result;
    }

    /**
     *  add a new Family 
     */
    public Family add(Family pFamily) {
      mFamilys.add(pFamily);
      mFamilyMap.put(pFamily.getPageid(),pFamily);
      return pFamily;
    }

    /**
     *  add a new Family from the given triple
     */
    public Family add(TripleQuery query,Triple pFamilyTriple) {
      Family lFamily=new Family(query,pFamilyTriple);
      add(lFamily);
      return lFamily;
    }

    // reinitialize my mFamily map
    public void reinit() {
      mFamilyMap.clear();
      for (Family lFamily:mFamilys) {
        mFamilyMap.put(lFamily.getPageid(),lFamily);
      }
    }

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

    // default constructor for Family Manager
    public FamilyManager() {}

    // add Families from the given query
    public void addFamilies(TripleQuery pFamilyQuery,TripleQuery query) {
      if (pFamilyQuery!=null) {
        for (Triple lFamilyTriple:pFamilyQuery.getTriples()) {
          add(query,lFamilyTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public FamilyManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lFamilyQuery=query.query(null,"isA","Family");
      addFamilies(lFamilyQuery,query);
      // then the SMW triplestore
      lFamilyQuery=query.query(null,"Property:IsA","Family");
      addFamilies(lFamilyQuery,query);
      init(query);
    } // constructor for Family Manager
    
    // >>>{user defined topicmanager code}{Family}{Family}
    // <<<{user defined topicmanager code}{Family}{Family}
  } // class Family Manager
 /**
  * Person
  * A Person is a human being
  */
  public static class Person extends TopicBase {
  
    public String id;
    public String name;
    public String nobleTitle;
    public String picture;
    public String sex;
    public String born;
    public String yearBorn;
    public String monthBorn;
    public String birthPlace;
    public String died;
    public String diedAt;
    public String yearDied;
    public String monthDied;
    public String noInLine;
    public String wikiPedia;
    public String childOf;

    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 getNobleTitle() { return nobleTitle; }
    public void setNobleTitle(String pNobleTitle) { nobleTitle=pNobleTitle; }
    public String getPicture() { return picture; }
    public void setPicture(String pPicture) { picture=pPicture; }
    public String getSex() { return sex; }
    public void setSex(String pSex) { sex=pSex; }
    public String getBorn() { return born; }
    public void setBorn(String pBorn) { born=pBorn; }
    public String getYearBorn() { return yearBorn; }
    public void setYearBorn(String pYearBorn) { yearBorn=pYearBorn; }
    public String getMonthBorn() { return monthBorn; }
    public void setMonthBorn(String pMonthBorn) { monthBorn=pMonthBorn; }
    public String getBirthPlace() { return birthPlace; }
    public void setBirthPlace(String pBirthPlace) { birthPlace=pBirthPlace; }
    public String getDied() { return died; }
    public void setDied(String pDied) { died=pDied; }
    public String getDiedAt() { return diedAt; }
    public void setDiedAt(String pDiedAt) { diedAt=pDiedAt; }
    public String getYearDied() { return yearDied; }
    public void setYearDied(String pYearDied) { yearDied=pYearDied; }
    public String getMonthDied() { return monthDied; }
    public void setMonthDied(String pMonthDied) { monthDied=pMonthDied; }
    public String getNoInLine() { return noInLine; }
    public void setNoInLine(String pNoInLine) { noInLine=pNoInLine; }
    public String getWikiPedia() { return wikiPedia; }
    public void setWikiPedia(String pWikiPedia) { wikiPedia=pWikiPedia; }
    public String getChildOf() { return childOf; }
    public void setChildOf(String pChildOf) { childOf=pChildOf; }
    /**
     * convert this Person to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this Person to a WikiSon string
     */
    public String toWikiSon() {
      String wikison= "{{Person\n";
      wikison+=toWikiSon("id",id);
      wikison+=toWikiSon("name",name);
      wikison+=toWikiSon("nobleTitle",nobleTitle);
      wikison+=toWikiSon("picture",picture);
      wikison+=toWikiSon("sex",sex);
      wikison+=toWikiSon("born",born);
      wikison+=toWikiSon("yearBorn",yearBorn);
      wikison+=toWikiSon("monthBorn",monthBorn);
      wikison+=toWikiSon("birthPlace",birthPlace);
      wikison+=toWikiSon("died",died);
      wikison+=toWikiSon("diedAt",diedAt);
      wikison+=toWikiSon("yearDied",yearDied);
      wikison+=toWikiSon("monthDied",monthDied);
      wikison+=toWikiSon("noInLine",noInLine);
      wikison+=toWikiSon("wikiPedia",wikiPedia);
      wikison+=toWikiSon("childOf",childOf);
      wikison+="}}\n";
      return wikison;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a Person from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Person(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple idTriple=query.selectSingle(pageid,"id",null);
      if (idTriple==null)
        idTriple=query.selectSingle(pageid,"Property:Person_id",null);
      if (idTriple!=null) 
        id=idTriple.getObject().toString();
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:Person_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple nobleTitleTriple=query.selectSingle(pageid,"nobleTitle",null);
      if (nobleTitleTriple==null)
        nobleTitleTriple=query.selectSingle(pageid,"Property:Person_nobleTitle",null);
      if (nobleTitleTriple!=null) 
        nobleTitle=nobleTitleTriple.getObject().toString();
      Triple pictureTriple=query.selectSingle(pageid,"picture",null);
      if (pictureTriple==null)
        pictureTriple=query.selectSingle(pageid,"Property:Person_picture",null);
      if (pictureTriple!=null) 
        picture=pictureTriple.getObject().toString();
      Triple sexTriple=query.selectSingle(pageid,"sex",null);
      if (sexTriple==null)
        sexTriple=query.selectSingle(pageid,"Property:Person_sex",null);
      if (sexTriple!=null) 
        sex=sexTriple.getObject().toString();
      Triple bornTriple=query.selectSingle(pageid,"born",null);
      if (bornTriple==null)
        bornTriple=query.selectSingle(pageid,"Property:Person_born",null);
      if (bornTriple!=null) 
        born=bornTriple.getObject().toString();
      Triple yearBornTriple=query.selectSingle(pageid,"yearBorn",null);
      if (yearBornTriple==null)
        yearBornTriple=query.selectSingle(pageid,"Property:Person_yearBorn",null);
      if (yearBornTriple!=null) 
        yearBorn=yearBornTriple.getObject().toString();
      Triple monthBornTriple=query.selectSingle(pageid,"monthBorn",null);
      if (monthBornTriple==null)
        monthBornTriple=query.selectSingle(pageid,"Property:Person_monthBorn",null);
      if (monthBornTriple!=null) 
        monthBorn=monthBornTriple.getObject().toString();
      Triple birthPlaceTriple=query.selectSingle(pageid,"birthPlace",null);
      if (birthPlaceTriple==null)
        birthPlaceTriple=query.selectSingle(pageid,"Property:Person_birthPlace",null);
      if (birthPlaceTriple!=null) 
        birthPlace=birthPlaceTriple.getObject().toString();
      Triple diedTriple=query.selectSingle(pageid,"died",null);
      if (diedTriple==null)
        diedTriple=query.selectSingle(pageid,"Property:Person_died",null);
      if (diedTriple!=null) 
        died=diedTriple.getObject().toString();
      Triple diedAtTriple=query.selectSingle(pageid,"diedAt",null);
      if (diedAtTriple==null)
        diedAtTriple=query.selectSingle(pageid,"Property:Person_diedAt",null);
      if (diedAtTriple!=null) 
        diedAt=diedAtTriple.getObject().toString();
      Triple yearDiedTriple=query.selectSingle(pageid,"yearDied",null);
      if (yearDiedTriple==null)
        yearDiedTriple=query.selectSingle(pageid,"Property:Person_yearDied",null);
      if (yearDiedTriple!=null) 
        yearDied=yearDiedTriple.getObject().toString();
      Triple monthDiedTriple=query.selectSingle(pageid,"monthDied",null);
      if (monthDiedTriple==null)
        monthDiedTriple=query.selectSingle(pageid,"Property:Person_monthDied",null);
      if (monthDiedTriple!=null) 
        monthDied=monthDiedTriple.getObject().toString();
      Triple noInLineTriple=query.selectSingle(pageid,"noInLine",null);
      if (noInLineTriple==null)
        noInLineTriple=query.selectSingle(pageid,"Property:Person_noInLine",null);
      if (noInLineTriple!=null) 
        noInLine=noInLineTriple.getObject().toString();
      Triple wikiPediaTriple=query.selectSingle(pageid,"wikiPedia",null);
      if (wikiPediaTriple==null)
        wikiPediaTriple=query.selectSingle(pageid,"Property:Person_wikiPedia",null);
      if (wikiPediaTriple!=null) 
        wikiPedia=wikiPediaTriple.getObject().toString();
      Triple childOfTriple=query.selectSingle(pageid,"childOf",null);
      if (childOfTriple==null)
        childOfTriple=query.selectSingle(pageid,"Property:Person_childOf",null);
      if (childOfTriple!=null) 
        childOf=childOfTriple.getObject().toString();
      init(query);
    } // constructor for Person
    
    // >>>{user defined topic code}{Person}{Person}
    // <<<{user defined topic code}{Person}{Person}
  } // class Person
  /**
   * Manager for Person
   */
  public static class PersonManager extends TopicBase {
 
    public String topicName="Person";
    public transient List<Person> mPersons=new ArrayList<Person>();
    public transient Map<String,Person> mPersonMap=new LinkedHashMap<String,Person>();

    /**
     * get my Persons
     */
    public List<Person> getPersons() {
      List<Person> result=this.mPersons;
      return result;
    }

    /**
     *  add a new Person 
     */
    public Person add(Person pPerson) {
      mPersons.add(pPerson);
      mPersonMap.put(pPerson.getPageid(),pPerson);
      return pPerson;
    }

    /**
     *  add a new Person from the given triple
     */
    public Person add(TripleQuery query,Triple pPersonTriple) {
      Person lPerson=new Person(query,pPersonTriple);
      add(lPerson);
      return lPerson;
    }

    // reinitialize my mPerson map
    public void reinit() {
      mPersonMap.clear();
      for (Person lPerson:mPersons) {
        mPersonMap.put(lPerson.getPageid(),lPerson);
      }
    }

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

    // default constructor for Person Manager
    public PersonManager() {}

    // add Persons from the given query
    public void addPersons(TripleQuery pPersonQuery,TripleQuery query) {
      if (pPersonQuery!=null) {
        for (Triple lPersonTriple:pPersonQuery.getTriples()) {
          add(query,lPersonTriple);
        }
      }
    }

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

}