Concept:PID/Java
Jump to navigation
Jump to search
java code
<source lang='java' id='javacode'>@// 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 = "\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
/**
* PID
* OBD-II Diagnostic Parameter
*/
public static class PID extends TopicBase {
public String id;
public String name;
public String getId() { return id; }
public void setId(String pId) { id=pId; }
public String getName() { return name; }
public void setName(String pName) { name=pName; }
/**
* convert this PID to a JSON string
*/
public String toJson() { return JSON.toJSONString(this); }
/**
* convert this PID to a WikiSon string
*/
public String toWikiSon() {
String wikison= "{{PID\n";
wikison+=toWikiSon("id",id);
wikison+=toWikiSon("name",name);
wikison+="}}\n";
return wikison;
}
/**
* 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();
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
}