Show VisualKnowledge.java syntax highlighted
package org.integratedmodelling.thinkcap.visualelements;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import org.integratedmodelling.thinkcap.ThinkcapManager;
import org.integratedmodelling.thinklab.KnowledgeManager;
import org.integratedmodelling.thinklab.exception.ThinklabException;
import org.integratedmodelling.thinklab.exception.ThinklabMalformedSemanticTypeException;
import org.integratedmodelling.thinklab.exception.ThinklabNoKMException;
import org.integratedmodelling.thinklab.interfaces.IConcept;
import org.integratedmodelling.thinklab.interfaces.IOntology;
import org.integratedmodelling.thinklab.interfaces.IProperty;
import org.integratedmodelling.thinklab.interfaces.IResource;
public abstract class VisualKnowledge {
private static Hashtable<String, String> typeIcons = new Hashtable<String, String>();
public static String getTypeIcon(IConcept concept, boolean isLarge) {
/* FIXME this should recurse type hierarchy and return the closest available */
String ret = isLarge ? "type_icons/owl_large.png" : "type_icons/owl_large.png";
/* try setting to concept space icon first */
String icon =
concept.getConceptSpace() +
"_" +
(isLarge ? "large" : "small");
if (typeIcons.containsKey(icon))
ret = typeIcons.get(icon);
icon =
concept.getConceptSpace() +
"_" +
concept.getLocalName() +
"_" +
(isLarge ? "large" : "small");
if (typeIcons.containsKey(icon))
ret = typeIcons.get(icon);
return ret;
}
public static void addTypeIcon(String newf) {
String tname = newf.toString();
if (tname.contains("/"))
tname = tname.substring(tname.lastIndexOf("/") + 1);
if (tname.contains("."))
tname = tname.substring(0, tname.lastIndexOf("."));
typeIcons.put(tname, newf);
}
/**
* Utility for use inside templates
* @param concept
* @return
* @throws IMANoKMException
* @throws IMAMalformedSemanticTypeException
*/
public IConcept getConcept(String concept) throws ThinklabMalformedSemanticTypeException, ThinklabNoKMException {
return KnowledgeManager.KM().retrieveConcept(concept);
}
/**
* Utility for use inside templates
* @param concept
* @return
* @throws IMANoKMException
* @throws IMAMalformedSemanticTypeException
*/
public IProperty getProperty(String property) throws ThinklabMalformedSemanticTypeException, ThinklabNoKMException {
return KnowledgeManager.KM().retrieveProperty(property);
}
public Collection<IProperty> getObjectProperties(String concept) throws ThinklabException {
ArrayList<IProperty> ret = new ArrayList<IProperty>();
IConcept c = getConcept(concept);
for (IProperty p : c.getProperties()) {
if (p.isObjectProperty())
ret.add(p);
}
return ret;
}
public Collection<IProperty> getClassificationProperties(String concept) throws ThinklabException {
ArrayList<IProperty> ret = new ArrayList<IProperty>();
IConcept c = getConcept(concept);
for (IProperty p : c.getProperties()) {
if (p.isClassification())
ret.add(p);
}
return ret;
}
public Collection<IProperty> getLiteralProperties(String concept) throws ThinklabException {
ArrayList<IProperty> ret = new ArrayList<IProperty>();
IConcept c = getConcept(concept);
for (IProperty p : c.getProperties()) {
if (p.isLiteralProperty())
ret.add(p);
}
return ret;
}
public String getLabelForOntology(String ontology) {
String ret = "";
IOntology ont =
ThinkcapManager.getThinkcapManager().getKnowledgeRepository().retrieveOntology(ontology);
if (ont != null) {
ret = ont.getLabel();
}
if (ret == null || ret.equals(""))
ret = "Concept space: " + ontology;
return ret;
}
public String getLabelForConcept(String concept) throws ThinklabException {
IConcept c = getConcept(concept);
String ret = c.getLabel();
if (ret == null || ret.equals("")) {
ret = c.getLocalName();
/* reparse camel into sentence */
ret = checkUpperCamel(ret);
}
return ret;
}
public String getLabelForProperty(IProperty property) throws ThinklabException {
String ret = property.getLabel();
if (ret == null || ret.equals("")) {
ret = property.getLocalName();
/* reparse camel into sentence */
ret = checkLowerCamel(ret);
}
return ret;
}
public String getDescription(IResource property) {
String ret = property.getDescription();
if (ret == null || ret.equals(""))
ret = "No description given";
return ret;
}
public String getPropertyRangeLinks(IProperty property) throws ThinklabException {
String ret = "";
Collection<IConcept> range = property.getRange();
if (range == null)
return "object";
int ncs = range.size();
int idc = 0;
for (IConcept c : range) {
if (idc > 0) {
ret += idc < (ncs - 1) ? ", " : " or ";
}
ret +=
"<a href=\"Dictionary.cmd?concept=" +
c +
"\">" +
getLabelForConcept(c.toString()) +
"</a>";
}
return ret.equals("") ? "object" : ret;
}
public String getPropertyCardinalityDescription(IProperty property) {
String ret = "any number of";
// FIXME need to adapt to final shape of cardinality methods
// if (property.getMinimumCardinality() == 1 &&
// property.getMaximumCardinality() == 1)
// ret = "one";
// else if (property.getMinimumCardinality() == 1 &&
// property.getMaximumCardinality() == 0)
// ret = "one or more";
// else if (property.getMinimumCardinality() == 0 &&
// property.getMaximumCardinality() == 0)
// ret = "zero or more";
return "ask me later";
}
/**
* Return the resource ID of an image that can be used to illustrate the i-th result. Cache
* images. Use plug-ins to produce images. May return null, meaning there's no image.
* @param i
* @return
*/
public String getImage(IConcept c, boolean isLarge) {
return getTypeIcon(c, isLarge);
}
/* quite idiotic */
public static String checkUpperCamel(String ret) {
if (ret.length() > 2 && Character.isUpperCase(ret.charAt(0)) && Character.isLowerCase(ret.charAt(1))) {
ret = ret.replaceAll("([A-Z])", " $1");
}
return ret;
}
/* quite idiotic */
public static String checkLowerCamel(String ret) {
/* FIXME
* check should be: starts with a lowercase and contains uppercase in
* positions different from last.
*/
if (ret.length() > 2 && Character.isLowerCase(ret.charAt(0)) && Character.isLowerCase(ret.charAt(1))) {
ret = ret.replaceAll("([A-Z])", " $1");
}
return ret;
}
}
See more files for this project here