Commit 0eff3a99 authored by piotr.gawron's avatar piotr.gawron
Browse files

temp commit - species alias is abstract and there are specific alias

classes for all species
parent 3a1b80c5
package lcsb.mapviewer.cdplugin.copypaste;
import java.util.HashSet;
import java.util.Set;
import jp.sbi.celldesigner.MainWindow;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.model.celldesigner.annotation.XmlAnnotationParser;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamRelationType;
import lcsb.mapviewer.model.map.MiriamType;
import org.apache.log4j.Logger;
/**
* This abstract class defines common function for {@link CopyAction} and
* {@link PasteAction}.
*
* @author Piotr Gawron
*
*/
public class CopyPasteAbstractAction extends XmlParser {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private final Logger logger = Logger.getLogger(CopyPasteAbstractAction.class);
/**
* Prefix used for serialization/deserialization of {@link MiriamData} to
* string stored in system clipboard.
*/
private static final String PREFIX = "[MIRIAM]";
/**
* Plugin object which ran this action. This object is used to update
* information about species
*/
private CopyPastePlugin plug;
/**
* Object of CellDesigner main form.
*/
private MainWindow window;
/**
* Serializes {@link MiriamData} into string that will be stored into system
* clipboard.
*
* @param md
* object to serialize
* @return string representation of {@link MiriamData}
*/
protected String serialize(MiriamData md) {
return PREFIX + "\t" + md.getRelationType().getStringRepresentation() + "\t" + md.getDataType().getUris().get(0) + "\t" + md.getResource();
}
/**
* Deserialize {@link MiriamData} from string creted by
* {@link #serialize(MiriamData)} method.
*
* @param string
* string representation of {@link MiriamData}
* @return {@link MiriamData} obtained from input string
*/
protected MiriamData deserialize(String string) {
if (string == null) {
return null;
}
String[] tmp = string.split("\t");
// CHECKSTYLE:OFF
if (tmp.length == 4) {
if (PREFIX.equals(tmp[0])) {
String relation = tmp[1];
String uri = tmp[2];
String resource = tmp[3];
MiriamType mt = MiriamType.getTypeByUri(uri);
MiriamRelationType mrt = MiriamRelationType.getTypeByStringRepresentation(relation);
if (mt == null || mrt == null) {
return null;
}
return new MiriamData(mrt, mt, resource);
}
}
// CHECKSTYLE:ON
return null;
}
/**
* Returns serialized string of annotations and notes.
*
* @param annotationString
* xml string representing annotations in CellDesigner
* @param notesString
* flat notes String
* @return serialized string
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
protected String getCopyString(String annotationString, String notesString) throws InvalidXmlSchemaException {
XmlAnnotationParser xap = new XmlAnnotationParser();
Set<MiriamData> set = xap.parse(annotationString);
StringBuilder result = new StringBuilder();
for (MiriamData md : set) {
result.append(serialize(md) + "\n");
}
result.append(notesString);
return result.toString();
}
/**
* Deserialize string created by {@link #getCopyString(String, String)}
* method. Used to create {@link MiriamData} set and notes from clipboard.
*
* @param value
* string for deserialization
*
* @return {@link Pair} of {@link MiriamData} set and notes string
*/
protected Pair<Set<MiriamData>, String> getAnnotationDataFromClipboardString(String value) {
if (value == null) {
return new Pair<Set<MiriamData>, String>(null, null);
}
String[] rows = value.split("\n");
StringBuilder sb = new StringBuilder("");
boolean miriam = true;
Set<MiriamData> set = new HashSet<MiriamData>();
for (int i = 0; i < rows.length; i++) {
String row = rows[i];
if (miriam) {
MiriamData md = deserialize(row);
if (md != null) {
set.add(md);
} else {
miriam = false;
}
}
if (!miriam) {
sb.append(row);
sb.append("\n");
}
}
String notes = sb.toString().trim();
return new Pair<Set<MiriamData>, String>(set, notes);
}
/**
* @return the plug
* @see #plug
*/
protected CopyPastePlugin getPlug() {
return plug;
}
/**
* @param plug
* the plug to set
* @see #plug
*/
protected void setPlug(CopyPastePlugin plug) {
this.plug = plug;
}
/**
* @return the window
* @see #window
*/
protected MainWindow getWindow() {
return window;
}
/**
* @param window
* the window to set
* @see #window
*/
protected void setWindow(MainWindow window) {
this.window = window;
}
package lcsb.mapviewer.cdplugin.copypaste;
import java.util.HashSet;
import java.util.Set;
import jp.sbi.celldesigner.MainWindow;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.model.celldesigner.annotation.XmlAnnotationParser;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamRelationType;
import lcsb.mapviewer.model.map.MiriamType;
import org.apache.log4j.Logger;
/**
* This abstract class defines common function for {@link CopyAction} and
* {@link PasteAction}.
*
* @author Piotr Gawron
*
*/
public class CopyPasteAbstractAction extends XmlParser {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private final Logger logger = Logger.getLogger(CopyPasteAbstractAction.class);
/**
* Prefix used for serialization/deserialization of {@link MiriamData} to
* string stored in system clipboard.
*/
private static final String PREFIX = "[MIRIAM]";
/**
* Plugin object which ran this action. This object is used to update
* information about species
*/
private CopyPastePlugin plug;
/**
* Object of CellDesigner main form.
*/
private MainWindow window;
/**
* Serializes {@link MiriamData} into string that will be stored into system
* clipboard.
*
* @param md
* object to serialize
* @return string representation of {@link MiriamData}
*/
protected String serialize(MiriamData md) {
return PREFIX + "\t" + md.getRelationType().getStringRepresentation() + "\t" + md.getDataType().getUris().get(0) + "\t" + md.getResource();
}
/**
* Deserialize {@link MiriamData} from string creted by
* {@link #serialize(MiriamData)} method.
*
* @param string
* string representation of {@link MiriamData}
* @return {@link MiriamData} obtained from input string
*/
protected MiriamData deserialize(String string) {
if (string == null) {
return null;
}
String[] tmp = string.split("\t");
// CHECKSTYLE:OFF
if (tmp.length == 4) {
if (PREFIX.equals(tmp[0])) {
String relation = tmp[1];
String uri = tmp[2];
String resource = tmp[3];
MiriamType mt = MiriamType.getTypeByUri(uri);
MiriamRelationType mrt = MiriamRelationType.getTypeByStringRepresentation(relation);
if (mt == null || mrt == null) {
return null;
}
return new MiriamData(mrt, mt, resource);
}
}
// CHECKSTYLE:ON
return null;
}
/**
* Returns serialized string of annotations and notes.
*
* @param annotationString
* xml string representing annotations in CellDesigner
* @param notesString
* flat notes String
* @return serialized string
* @throws InvalidXmlSchemaException
* thrown when xmlString is invalid
*/
protected String getCopyString(String annotationString, String notesString) throws InvalidXmlSchemaException {
XmlAnnotationParser xap = new XmlAnnotationParser();
Set<MiriamData> set = xap.parse(annotationString);
StringBuilder result = new StringBuilder();
for (MiriamData md : set) {
result.append(serialize(md) + "\n");
}
result.append(notesString);
return result.toString();
}
/**
* Deserialize string created by {@link #getCopyString(String, String)}
* method. Used to create {@link MiriamData} set and notes from clipboard.
*
* @param value
* string for deserialization
*
* @return {@link Pair} of {@link MiriamData} set and notes string
*/
protected Pair<Set<MiriamData>, String> getAnnotationDataFromClipboardString(String value) {
if (value == null) {
return new Pair<Set<MiriamData>, String>(null, null);
}
String[] rows = value.split("\n");
StringBuilder sb = new StringBuilder("");
boolean miriam = true;
Set<MiriamData> set = new HashSet<MiriamData>();
for (int i = 0; i < rows.length; i++) {
String row = rows[i];
if (miriam) {
MiriamData md = deserialize(row);
if (md != null) {
set.add(md);
} else {
miriam = false;
}
}
if (!miriam) {
sb.append(row);
sb.append("\n");
}
}
String notes = sb.toString().trim();
return new Pair<Set<MiriamData>, String>(set, notes);
}
/**
* @return the plug
* @see #plug
*/
protected CopyPastePlugin getPlug() {
return plug;
}
/**
* @param plug
* the plug to set
* @see #plug
*/
protected void setPlug(CopyPastePlugin plug) {
this.plug = plug;
}
/**
* @return the window
* @see #window
*/
protected MainWindow getWindow() {
return window;
}
/**
* @param window
* the window to set
* @see #window
*/
protected void setWindow(MainWindow window) {
this.window = window;
}
}
\ No newline at end of file
package lcsb.mapviewer.annotation.cache;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.w3c.dom.Node;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.model.cache.CacheType;
/**
* Implementation of cache that stores everything in application memory. Data is
* lost when application restarts. It implements singleton pattern. All method
* are synchronized (so there are no issues when few thread are trying to access
* data at the same time),
*
* @author Piotr Gawron
*
*/
public final class ApplicationLevelCache extends XmlParser implements QueryCacheInterface {
/**
* How many values should be stored in cache before we even try to consider
* releasing object due to huge memory usage.
*/
static final int MIN_CACHED_VALUES_BEFORE_CLEAN = 100;
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(ApplicationLevelCache.class);
/**
* Cached nodes stored locally and identified by string.
*/
private static Map<String, Node> cachedQueryNodes = new HashMap<String, Node>();
/**
* Cached strings stored locally and identified by string.
*/
private static Map<String, String> cachedQueryString = new HashMap<String, String>();
/**
* Global instance of cache.
*/
private static ApplicationLevelCache cache = null;
/**
* Returns single instance of global application cache (singleton pattern).
*
* @return single instance of global application cache
*/
public static ApplicationLevelCache getInstance() {
if (Configuration.isApplicationCacheOn()) {
if (cache == null) {
cache = new ApplicationLevelCache();
}
return cache;
} else {
return null;
}
}
/**
* Default constructor. Prevents instatiation.
*/
private ApplicationLevelCache() {
}
@Override
public synchronized Node getXmlNodeByQuery(String query, CacheType type) {
Node result = null;
result = cachedQueryNodes.get(type.getId() + "\n" + query);
return result;
}
@Override
public synchronized String getStringByQuery(String query, CacheType type) {
String result = null;
result = cachedQueryString.get(type.getId() + "\n" + query);
return result;
}
@Override
public synchronized void setCachedQuery(String query, CacheType type, Object object) {
performMemoryBalance();
if (object instanceof String) {
cachedQueryString.put(type.getId() + "\n" + query, (String) object);
} else if (object instanceof Node) {
cachedQueryNodes.put(type.getId() + "\n" + query, (Node) object);
} else if (object == null) {
cachedQueryString.put(type.getId() + "\n" + query, (String) object);
} else {
throw new CacheException("Unknown object type: " + object.getClass());
}
}
/**
* Method that clean cache if memory usage is too high.
*/
synchronized void performMemoryBalance() {
long cacheCount = 0;
cacheCount += cachedQueryString.size();
cacheCount += cachedQueryNodes.size();
// check memory usage only if we have some data to clear
if (cacheCount > MIN_CACHED_VALUES_BEFORE_CLEAN) {
Runtime runtime = Runtime.getRuntime();
long maxMem = runtime.maxMemory();
long useMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
// if free memory is less than 10% then we clear cache
if (useMem > maxMem * Configuration.getMemorySaturationRatioTriggerClean()) {
logger.info("Cache will be cleared");
logger.info("Memory usage: " + useMem + " out of " + maxMem + " is used. Exceeded: " + maxMem * Configuration.getMemorySaturationRatioTriggerClean());
logger.info("Elements in cache: " + cacheCount);
clearCache();
System.gc();
}
}
}
@Override
public synchronized void removeByQuery(String query, CacheType type) {
cachedQueryString.remove(type.getId() + "\n" + query);
cachedQueryNodes.remove(type.getId() + "\n" + query);
}
@Override
public synchronized void clearCache() {
logger.info("Clearing application cache");
cachedQueryNodes.clear();
cachedQueryString.clear();
}
@Override
public void invalidateByQuery(String query, CacheType type) {
removeByQuery(query, type);
}
}
package lcsb.mapviewer.annotation.cache;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.w3c.dom.Node;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.XmlParser;
import lcsb.mapviewer.model.cache.CacheType;
/**
* Implementation of cache that stores everything in application memory. Data is
* lost when application restarts. It implements singleton pattern. All method
* are synchronized (so there are no issues when few thread are trying to access
* data at the same time),
*
* @author Piotr Gawron
*
*/
public final class ApplicationLevelCache extends XmlParser implements QueryCacheInterface {
/**
* How many values should be stored in cache before we even try to consider
* releasing object due to huge memory usage.
*/
static final int MIN_CACHED_VALUES_BEFORE_CLEAN = 100;
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(ApplicationLevelCache.class);
/**
* Cached nodes stored locally and identified by string.
*/
private static Map<String, Node> cachedQueryNodes = new HashMap<String, Node>();
/**
* Cached strings stored locally and identified by string.
*/
private static Map<String, String> cachedQueryString = new HashMap<String, String>();
/**
* Global instance of cache.
*/
private static ApplicationLevelCache cache = null;