Concept:Event/Java

From BITPlan cr Wiki
Revision as of 14:23, 22 July 2020 by Wf (talk | contribs) (created by WikiTask 2020-07-22T12:23:02Z)
(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 getAcronym() { return acronym; }
    public void setAcronym(String pAcronym) { acronym=pAcronym; }
    /**
     * 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+="}}\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,"acronym");
      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();
      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

}
Showing below 0 pages.

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