Concept:State/Java

From BITPlan cr Wiki
Revision as of 18:53, 25 July 2020 by Wf (talk | contribs) (created by WikiTask 2020-07-25T17:53:48Z)
(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
 /**
  * Event
  * a meeting of researchers at a specific time and place
  */
  public static class Event extends TopicBase {
  
    public String acronym;
    public String homepage;
    public String city;
    public String state;
    public String country;

    public String getAcronym() { return acronym; }
    public void setAcronym(String pAcronym) { acronym=pAcronym; }
    public String getHomepage() { return homepage; }
    public void setHomepage(String pHomepage) { homepage=pHomepage; }
    public String getCity() { return city; }
    public void setCity(String pCity) { city=pCity; }
    public String getState() { return state; }
    public void setState(String pState) { state=pState; }
    public String getCountry() { return country; }
    public void setCountry(String pCountry) { country=pCountry; }
    /**
     * convert this Event to a JSON string
     * @return the JSON representation
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this Event to a WikiSon string
     * @return the WikiSon representation of this Event
     */
    public String toWikiSon() {
      String wikison= "{{Event\n";
      wikison+=toWikiSon("acronym",acronym);
      wikison+=toWikiSon("homepage",homepage);
      wikison+=toWikiSon("city",city);
      wikison+=toWikiSon("state",state);
      wikison+=toWikiSon("country",country);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this Event to a SiDIF string
     * @return the SiDIF representation of this Event
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA Event\n",this.pageid);
      siDIF+=propertySiDIF("acronym",acronym,"text");
      siDIF+=propertySiDIF("homepage",homepage,"url");
      siDIF+=propertySiDIF("city",city,"Page");
      siDIF+=propertySiDIF("state",state,"Page");
      siDIF+=propertySiDIF("country",country,"Page");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

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

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

    /**
     * construct a Event from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Event(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple acronymTriple=query.selectSingle(pageid,"acronym",null);
      if (acronymTriple==null)
        acronymTriple=query.selectSingle(pageid,"Property:Event_acronym",null);
      if (acronymTriple!=null) 
        acronym=acronymTriple.getObject().toString();
      Triple homepageTriple=query.selectSingle(pageid,"homepage",null);
      if (homepageTriple==null)
        homepageTriple=query.selectSingle(pageid,"Property:Event_homepage",null);
      if (homepageTriple!=null) 
        homepage=homepageTriple.getObject().toString();
      Triple cityTriple=query.selectSingle(pageid,"city",null);
      if (cityTriple==null)
        cityTriple=query.selectSingle(pageid,"Property:Event_city",null);
      if (cityTriple!=null) 
        city=cityTriple.getObject().toString();
      Triple stateTriple=query.selectSingle(pageid,"state",null);
      if (stateTriple==null)
        stateTriple=query.selectSingle(pageid,"Property:Event_state",null);
      if (stateTriple!=null) 
        state=stateTriple.getObject().toString();
      Triple countryTriple=query.selectSingle(pageid,"country",null);
      if (countryTriple==null)
        countryTriple=query.selectSingle(pageid,"Property:Event_country",null);
      if (countryTriple!=null) 
        country=countryTriple.getObject().toString();
      init(query);
    } // constructor for Event
    
    // >>>{user defined topic code}{Event}{Event}
    // <<<{user defined topic code}{Event}{Event}
  } // class Event
  /**
   * Manager for Event
   */
  public static class EventManager extends TopicBase {
 
    public String topicName="Event";
    public transient List<Event> mEvents=new ArrayList<Event>();
    public transient Map<String,Event> mEventMap=new LinkedHashMap<String,Event>();

    /**
     * get my Events
     */
    public List<Event> getEvents() {
      List<Event> result=this.mEvents;
      return result;
    }

    /**
     *  add a new Event 
     */
    public Event add(Event pEvent) {
      mEvents.add(pEvent);
      mEventMap.put(pEvent.getPageid(),pEvent);
      return pEvent;
    }

    /**
     *  add a new Event from the given triple
     */
    public Event add(TripleQuery query,Triple pEventTriple) {
      Event lEvent=new Event(query,pEventTriple);
      add(lEvent);
      return lEvent;
    }

    // reinitialize my mEvent map
    public void reinit() {
      mEventMap.clear();
      for (Event lEvent:mEvents) {
        mEventMap.put(lEvent.getPageid(),lEvent);
      }
    }

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

    // default constructor for Event Manager
    public EventManager() {}

    // add Events from the given query
    public void addEvents(TripleQuery pEventQuery,TripleQuery query) {
      if (pEventQuery!=null) {
        for (Triple lEventTriple:pEventQuery.getTriples()) {
          add(query,lEventTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public EventManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lEventQuery=query.query(null,"isA","Event");
      addEvents(lEventQuery,query);
      // then the SMW triplestore
      lEventQuery=query.query(null,"Property:IsA","Event");
      addEvents(lEventQuery,query);
      init(query);
    } // constructor for Event Manager
    
    // >>>{user defined topicmanager code}{Event}{Event}
    // <<<{user defined topicmanager code}{Event}{Event}
  } // class Event Manager
 /**
  * City
  * large permanent human settlement
  */
  public static class City extends TopicBase {
  
    public String name;
    public String wikidataid;

    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getWikidataid() { return wikidataid; }
    public void setWikidataid(String pWikidataid) { wikidataid=pWikidataid; }
    /**
     * convert this City to a JSON string
     * @return the JSON representation
     */
    public String toJson() { return JSON.toJSONString(this); }

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

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

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

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

    /**
     * construct a City from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public City(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:City_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple wikidataidTriple=query.selectSingle(pageid,"wikidataid",null);
      if (wikidataidTriple==null)
        wikidataidTriple=query.selectSingle(pageid,"Property:City_wikidataid",null);
      if (wikidataidTriple!=null) 
        wikidataid=wikidataidTriple.getObject().toString();
      init(query);
    } // constructor for City
    
    // >>>{user defined topic code}{City}{City}
    // <<<{user defined topic code}{City}{City}
  } // class City
  /**
   * Manager for City
   */
  public static class CityManager extends TopicBase {
 
    public String topicName="City";
    public transient List<City> mCitys=new ArrayList<City>();
    public transient Map<String,City> mCityMap=new LinkedHashMap<String,City>();

    /**
     * get my Cities
     */
    public List<City> getCities() {
      List<City> result=this.mCitys;
      return result;
    }

    /**
     *  add a new City 
     */
    public City add(City pCity) {
      mCitys.add(pCity);
      mCityMap.put(pCity.getPageid(),pCity);
      return pCity;
    }

    /**
     *  add a new City from the given triple
     */
    public City add(TripleQuery query,Triple pCityTriple) {
      City lCity=new City(query,pCityTriple);
      add(lCity);
      return lCity;
    }

    // reinitialize my mCity map
    public void reinit() {
      mCityMap.clear();
      for (City lCity:mCitys) {
        mCityMap.put(lCity.getPageid(),lCity);
      }
    }

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

    // default constructor for City Manager
    public CityManager() {}

    // add Cities from the given query
    public void addCities(TripleQuery pCityQuery,TripleQuery query) {
      if (pCityQuery!=null) {
        for (Triple lCityTriple:pCityQuery.getTriples()) {
          add(query,lCityTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public CityManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lCityQuery=query.query(null,"isA","City");
      addCities(lCityQuery,query);
      // then the SMW triplestore
      lCityQuery=query.query(null,"Property:IsA","City");
      addCities(lCityQuery,query);
      init(query);
    } // constructor for City Manager
    
    // >>>{user defined topicmanager code}{City}{City}
    // <<<{user defined topicmanager code}{City}{City}
  } // class City Manager
 /**
  * Country
  * distinct region in geography; a broad term that can include political divisions or regions associated with distinct political characteristics
  */
  public static class Country extends TopicBase {
  
    public String name;
    public String wikidataid;

    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getWikidataid() { return wikidataid; }
    public void setWikidataid(String pWikidataid) { wikidataid=pWikidataid; }
    /**
     * convert this Country to a JSON string
     * @return the JSON representation
     */
    public String toJson() { return JSON.toJSONString(this); }

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

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

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

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

    /**
     * construct a Country from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Country(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:Country_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple wikidataidTriple=query.selectSingle(pageid,"wikidataid",null);
      if (wikidataidTriple==null)
        wikidataidTriple=query.selectSingle(pageid,"Property:Country_wikidataid",null);
      if (wikidataidTriple!=null) 
        wikidataid=wikidataidTriple.getObject().toString();
      init(query);
    } // constructor for Country
    
    // >>>{user defined topic code}{Country}{Country}
    // <<<{user defined topic code}{Country}{Country}
  } // class Country
  /**
   * Manager for Country
   */
  public static class CountryManager extends TopicBase {
 
    public String topicName="Country";
    public transient List<Country> mCountrys=new ArrayList<Country>();
    public transient Map<String,Country> mCountryMap=new LinkedHashMap<String,Country>();

    /**
     * get my Countries
     */
    public List<Country> getCountries() {
      List<Country> result=this.mCountrys;
      return result;
    }

    /**
     *  add a new Country 
     */
    public Country add(Country pCountry) {
      mCountrys.add(pCountry);
      mCountryMap.put(pCountry.getPageid(),pCountry);
      return pCountry;
    }

    /**
     *  add a new Country from the given triple
     */
    public Country add(TripleQuery query,Triple pCountryTriple) {
      Country lCountry=new Country(query,pCountryTriple);
      add(lCountry);
      return lCountry;
    }

    // reinitialize my mCountry map
    public void reinit() {
      mCountryMap.clear();
      for (Country lCountry:mCountrys) {
        mCountryMap.put(lCountry.getPageid(),lCountry);
      }
    }

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

    // default constructor for Country Manager
    public CountryManager() {}

    // add Countries from the given query
    public void addCountries(TripleQuery pCountryQuery,TripleQuery query) {
      if (pCountryQuery!=null) {
        for (Triple lCountryTriple:pCountryQuery.getTriples()) {
          add(query,lCountryTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public CountryManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lCountryQuery=query.query(null,"isA","Country");
      addCountries(lCountryQuery,query);
      // then the SMW triplestore
      lCountryQuery=query.query(null,"Property:IsA","Country");
      addCountries(lCountryQuery,query);
      init(query);
    } // constructor for Country Manager
    
    // >>>{user defined topicmanager code}{Country}{Country}
    // <<<{user defined topicmanager code}{Country}{Country}
  } // class Country Manager
 /**
  * State
  * territorial and constitutional community forming part of a federal union
  */
  public static class State extends TopicBase {
  
    public String name;
    public String wikidataid;

    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getWikidataid() { return wikidataid; }
    public void setWikidataid(String pWikidataid) { wikidataid=pWikidataid; }
    /**
     * convert this State to a JSON string
     * @return the JSON representation
     */
    public String toJson() { return JSON.toJSONString(this); }

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

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

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

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

    /**
     * construct a State from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public State(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:State_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple wikidataidTriple=query.selectSingle(pageid,"wikidataid",null);
      if (wikidataidTriple==null)
        wikidataidTriple=query.selectSingle(pageid,"Property:State_wikidataid",null);
      if (wikidataidTriple!=null) 
        wikidataid=wikidataidTriple.getObject().toString();
      init(query);
    } // constructor for State
    
    // >>>{user defined topic code}{State}{State}
    // <<<{user defined topic code}{State}{State}
  } // class State
  /**
   * Manager for State
   */
  public static class StateManager extends TopicBase {
 
    public String topicName="State";
    public transient List<State> mStates=new ArrayList<State>();
    public transient Map<String,State> mStateMap=new LinkedHashMap<String,State>();

    /**
     * get my States
     */
    public List<State> getStates() {
      List<State> result=this.mStates;
      return result;
    }

    /**
     *  add a new State 
     */
    public State add(State pState) {
      mStates.add(pState);
      mStateMap.put(pState.getPageid(),pState);
      return pState;
    }

    /**
     *  add a new State from the given triple
     */
    public State add(TripleQuery query,Triple pStateTriple) {
      State lState=new State(query,pStateTriple);
      add(lState);
      return lState;
    }

    // reinitialize my mState map
    public void reinit() {
      mStateMap.clear();
      for (State lState:mStates) {
        mStateMap.put(lState.getPageid(),lState);
      }
    }

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

    // default constructor for State Manager
    public StateManager() {}

    // add States from the given query
    public void addStates(TripleQuery pStateQuery,TripleQuery query) {
      if (pStateQuery!=null) {
        for (Triple lStateTriple:pStateQuery.getTriples()) {
          add(query,lStateTriple);
        }
      }
    }

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

}
Showing below 0 pages.

Retrieved from "https://cr.bitplan.com/index.php?title=Concept:State/Java&oldid=395#smw-result"