下面列出了怎么用weka.core.SerializedObject的API类实例代码及写法,或者点击链接到github查看源代码。
DelValueAction(int nTargetNode, String sValue) {
try {
m_nTargetNode = nTargetNode;
m_sValue = sValue;
m_att = m_Instances.attribute(nTargetNode);
SerializedObject so = new SerializedObject(m_Distributions[nTargetNode]);
m_CPT = (Estimator[]) so.getObject();
;
m_children = new FastVector();
for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {
if (m_ParentSets[iNode].contains(nTargetNode)) {
m_children.addElement(iNode);
}
}
m_childAtts = new Estimator[m_children.size()][];
for (int iChild = 0; iChild < m_children.size(); iChild++) {
int nChild = (Integer) m_children.elementAt(iChild);
m_childAtts[iChild] = m_Distributions[nChild];
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates a given number of deep or shallow (if the kernel implements Copyable)
* copies of the given kernel using serialization.
*
* @param model the kernel to copy
* @param num the number of kernel copies to create.
* @return an array of kernels.
* @throws Exception if an error occurs
*/
public static Kernel[] makeCopies(Kernel model, int num) throws Exception {
if (model == null)
throw new Exception("No model kernel set");
Kernel[] kernels = new Kernel[num];
if (model instanceof Copyable) {
for (int i = 0; i < kernels.length; i++) {
kernels[i] = (Kernel) ((Copyable) model).copy();
}
} else {
SerializedObject so = new SerializedObject(model);
for (int i = 0; i < kernels.length; i++)
kernels[i] = (Kernel) so.getObject();
}
return kernels;
}
/**
* Creates copies of the current clusterer. Note that this method
* now uses Serialization to perform a deep copy, so the Clusterer
* object must be fully Serializable. Any currently built model will
* now be copied as well.
*
* @param model an example clusterer to copy
* @param num the number of clusterer copies to create.
* @return an array of clusterers.
* @exception Exception if an error occurs
*/
public static Clusterer [] makeCopies(Clusterer model,
int num) throws Exception {
if (model == null) {
throw new Exception("No model clusterer set");
}
Clusterer [] clusterers = new Clusterer [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < clusterers.length; i++) {
clusterers[i] = (Clusterer) so.getObject();
}
return clusterers;
}
/**
* Creates copies of the current clusterer. Note that this method
* now uses Serialization to perform a deep copy, so the Clusterer
* object must be fully Serializable. Any currently built model will
* now be copied as well.
*
* @param model an example clusterer to copy
* @param num the number of clusterer copies to create.
* @return an array of clusterers.
* @exception Exception if an error occurs
*/
public static DensityBasedClusterer [] makeCopies(DensityBasedClusterer model,
int num) throws Exception {
if (model == null) {
throw new Exception("No model clusterer set");
}
DensityBasedClusterer [] clusterers = new DensityBasedClusterer [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < clusterers.length; i++) {
clusterers[i] = (DensityBasedClusterer) so.getObject();
}
return clusterers;
}
/**
* Creates copies of the current associator. Note that this method
* now uses Serialization to perform a deep copy, so the Associator
* object must be fully Serializable. Any currently built model will
* now be copied as well.
*
* @param model an example associator to copy
* @param num the number of associators copies to create.
* @return an array of associators.
* @exception Exception if an error occurs
*/
public static Associator[] makeCopies(Associator model,
int num) throws Exception {
if (model == null) {
throw new Exception("No model associator set");
}
Associator [] associators = new Associator [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < associators.length; i++) {
associators[i] = (Associator) so.getObject();
}
return associators;
}
/**
* Creates copies of the current evaluator. Note that this method
* now uses Serialization to perform a deep copy, so the evaluator
* object must be fully Serializable. Any currently built model will
* now be copied as well.
*
* @param model an example evaluator to copy
* @param num the number of evaluator copies to create.
* @return an array of evaluators.
* @exception Exception if an error occurs
*/
public static ASEvaluation [] makeCopies(ASEvaluation model,
int num) throws Exception {
if (model == null) {
throw new Exception("No model evaluator set");
}
ASEvaluation [] evaluators = new ASEvaluation [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < evaluators.length; i++) {
evaluators[i] = (ASEvaluation) so.getObject();
}
return evaluators;
}
/**
* Creates copies of the current search scheme. Note that this method
* now uses Serialization to perform a deep copy, so the search
* object must be fully Serializable. Any currently built model will
* now be copied as well.
*
* @param model an example search scheme to copy
* @param num the number of search scheme copies to create.
* @return an array of search schemes.
* @throws Exception if an error occurs
*/
public static ASSearch[] makeCopies(ASSearch model, int num) throws Exception {
if (model == null)
throw new Exception("No model search scheme set");
ASSearch[] result = new ASSearch[num];
SerializedObject so = new SerializedObject(model);
for (int i = 0; i < result.length; i++)
result[i] = (ASSearch) so.getObject();
return result;
}
/**
* returns deep copies of the given object
*
* @param obj the object to copy
* @param num the number of copies
* @return the deep copies
* @throws Exception if copying fails
*/
protected Object[] makeCopies(Object obj, int num) throws Exception {
if (obj == null)
throw new Exception("No object set");
Object[] objs = new Object[num];
SerializedObject so = new SerializedObject(obj);
for(int i = 0; i < objs.length; i++) {
objs[i] = so.getObject();
}
return objs;
}
AddArcAction(int nParent, int nChild) {
try {
m_nParent = nParent;
m_children = new FastVector();
m_children.addElement(nChild);
//m_nChild = nChild;
SerializedObject so = new SerializedObject(m_Distributions[nChild]);
m_CPT = new Estimator[1][];
m_CPT[0] = (Estimator[]) so.getObject();
;
} catch (Exception e) {
e.printStackTrace();
}
}
AddArcAction(int nParent, FastVector children) {
try {
m_nParent = nParent;
m_children = new FastVector();
m_CPT = new Estimator[children.size()][];
for (int iChild = 0; iChild < children.size(); iChild++) {
int nChild = (Integer) children.elementAt(iChild);
m_children.addElement(nChild);
SerializedObject so = new SerializedObject(m_Distributions[nChild]);
m_CPT[iChild] = (Estimator[]) so.getObject();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void undo() {
try {
for (int iChild = 0; iChild < m_children.size(); iChild++) {
int nChild = (Integer) m_children.elementAt(iChild);
deleteArc(m_nParent, nChild);
SerializedObject so = new SerializedObject(m_CPT[iChild]);
m_Distributions[nChild] = (Estimator[]) so.getObject();
}
} catch (Exception e) {
e.printStackTrace();
}
}
DeleteArcAction(int nParent, int nChild) {
try {
m_nChild = nChild;
m_nParent = nParent;
m_nParents = new int[getNrOfParents(nChild)];
for (int iParent = 0; iParent < m_nParents.length; iParent++) {
m_nParents[iParent] = getParent(nChild, iParent);
}
SerializedObject so = new SerializedObject(m_Distributions[nChild]);
m_CPT = (Estimator[]) so.getObject();
} catch (Exception e) {
e.printStackTrace();
}
}
public void undo() {
try {
SerializedObject so = new SerializedObject(m_CPT);
m_Distributions[m_nChild] = (Estimator[]) so.getObject();
ParentSet parentSet = new ParentSet();
for (int iParent = 0; iParent < m_nParents.length; iParent++) {
parentSet.addParent(m_nParents[iParent], m_Instances);
}
m_ParentSets[m_nChild] = parentSet;
} catch (Exception e) {
e.printStackTrace();
}
}
SetDistributionAction(int nTargetNode, double[][] P) {
try {
m_nTargetNode = nTargetNode;
SerializedObject so = new SerializedObject(m_Distributions[nTargetNode]);
m_CPT = (Estimator[]) so.getObject();
;
m_P = P;
} catch (Exception e) {
e.printStackTrace();
}
}
public void undo() {
try {
SerializedObject so = new SerializedObject(m_CPT);
m_Distributions[m_nTargetNode] = (Estimator[]) so.getObject();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates a given number of deep copies of the given classifier using serialization.
*
* @param model the classifier to copy
* @param num the number of classifier copies to create.
* @return an array of classifiers.
* @exception Exception if an error occurs
*/
public static Classifier [] makeCopies(Classifier model, int num) throws Exception {
if (model == null) {
throw new Exception("No model classifier set");
}
Classifier [] classifiers = new Classifier [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < classifiers.length; i++) {
classifiers[i] = (Classifier) so.getObject();
}
return classifiers;
}
/**
* Creates a given number of deep copies of the given filter using
* serialization.
*
* @param model the filter to copy
* @param num the number of filter copies to create.
* @return an array of filters.
* @throws Exception if an error occurs
*/
public static Filter[] makeCopies(Filter model, int num) throws Exception {
if (model == null) {
throw new Exception("No model filter set");
}
Filter[] filters = new Filter[num];
SerializedObject so = new SerializedObject(model);
for (int i = 0; i < filters.length; i++) {
filters[i] = (Filter) so.getObject();
}
return filters;
}
/**
* Creates a given number of deep copies of the given estimator using serialization.
*
* @param model the estimator to copy
* @param num the number of estimator copies to create.
* @return an array of estimators.
* @exception Exception if an error occurs
*/
public static Estimator [] makeCopies(Estimator model,
int num) throws Exception {
if (model == null) {
throw new Exception("No model estimator set");
}
Estimator [] estimators = new Estimator [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < estimators.length; i++) {
estimators[i] = (Estimator) so.getObject();
}
return estimators;
}
@SuppressWarnings("unchecked")
public static <A> A deepCopy(A value) throws Exception {
if(value instanceof Serializable) {
return (A) new SerializedObject(value).getObject();
} else {
String str = StrUtils.toOptionValue(value);
Object object = StrUtils.fromOptionValue(str);
return (A) object;
}
}
/**
* Creates a given number of deep copies of the given classifier using serialization.
*
* @param model the classifier to copy
* @param num the number of classifier copies to create.
* @return an array of classifiers.
* @exception Exception if an error occurs
*/
public static Classifier [] makeCopies(Classifier model,
int num) throws Exception {
if (model == null) {
throw new Exception("No model classifier set");
}
Classifier [] classifiers = new Classifier [num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < classifiers.length; i++) {
classifiers[i] = (Classifier) so.getObject();
}
return classifiers;
}
/**
* Returns the default search parameters.
*
* @return the parameters
*/
protected AbstractParameter[] defaultSearchParameters() {
AbstractParameter[] result;
MathParameter param;
result = new AbstractParameter[1];
param = new MathParameter();
param.setProperty("K");
param.setMin(1);
param.setMax(3);
param.setStep(1);
param.setBase(10);
param.setExpression("I");
result[0] = param;
try {
result = (AbstractParameter[]) new SerializedObject(result).getObject();
}
catch (Exception e) {
result = new AbstractParameter[0];
System.err.println("Failed to create copy of default parameters!");
e.printStackTrace();
}
return result;
}
/**
* Returns the default search parameters.
*
* @return the parameters
*/
protected AbstractParameter[] defaultSearchParameters() {
AbstractParameter[] result;
MathParameter param;
result = new AbstractParameter[2];
param = new MathParameter();
param.setProperty("M");
param.setMin(5);
param.setMax(15);
param.setStep(5);
param.setBase(10);
param.setExpression("I");
result[0] = param;
param = new MathParameter();
param.setProperty("K");
param.setMin(1);
param.setMax(3);
param.setStep(1);
param.setBase(10);
param.setExpression("I");
result[1] = param;
try {
result = (AbstractParameter[]) new SerializedObject(result).getObject();
}
catch (Exception e) {
result = new AbstractParameter[0];
System.err.println("Failed to create copy of default parameters!");
e.printStackTrace();
}
return result;
}
/**
* Creates a given number of deep copies of the given multi-label classifier using serialization.
*
* @param model the classifier to copy
* @param num the number of classifier copies to create.
* @return an array of classifiers.
* @exception Exception if an error occurs
*/
public static MultiLabelClassifier[] makeCopies(MultiLabelClassifier model, int num) throws Exception {
if (model == null) {
throw new Exception("No model classifier set");
}
MultiLabelClassifier classifiers[] = new MultiLabelClassifier[num];
SerializedObject so = new SerializedObject(model);
for(int i = 0; i < classifiers.length; i++) {
classifiers[i] = (MultiLabelClassifier) so.getObject();
}
return classifiers;
}
static <A> A deepCopy(A object) throws
Exception {
return (A) new SerializedObject(object).getObject();
}
/**
* Creates a shallow copy of the kernel (if it implements Copyable)
* otherwise a deep copy using serialization.
*
* @param kernel the kernel to copy
* @return a shallow or deep copy of the kernel
* @throws Exception if an error occurs
*/
public static Kernel makeCopy(Kernel kernel) throws Exception {
if (kernel instanceof Copyable) {
return (Kernel) ((Copyable) kernel).copy();
}
return (Kernel) new SerializedObject(kernel).getObject();
}
/**
* Creates a deep copy of the given clusterer using serialization.
*
* @param model the clusterer to copy
* @return a deep copy of the clusterer
* @exception Exception if an error occurs
*/
public static Clusterer makeCopy(Clusterer model) throws Exception {
return (Clusterer) new SerializedObject(model).getObject();
}
/**
* Creates a deep copy of the given associator using serialization.
*
* @param model the associator to copy
* @return a deep copy of the associator
* @exception Exception if an error occurs
*/
public static Associator makeCopy(Associator model) throws Exception {
return (Associator) new SerializedObject(model).getObject();
}
/**
* Creates a deep copy of the given classifier using serialization.
*
* @param model the classifier to copy
* @return a deep copy of the classifier
* @exception Exception if an error occurs
*/
public static Classifier makeCopy(Classifier model) throws Exception {
return (Classifier)new SerializedObject(model).getObject();
}
/**
* Creates a deep copy of the given filter using serialization.
*
* @param model the filter to copy
* @return a deep copy of the filter
* @throws Exception if an error occurs
*/
public static Filter makeCopy(Filter model) throws Exception {
return (Filter)new SerializedObject(model).getObject();
}
/**
* Creates a deep copy of the given estimator using serialization.
*
* @param model the estimator to copy
* @return a deep copy of the estimator
* @exception Exception if an error occurs
*/
public static Estimator makeCopy(Estimator model) throws Exception {
return (Estimator)new SerializedObject(model).getObject();
}