📄 contextmodel.java
字号:
package contextsModelManager;
import java.util.*;
import java.io.*;
import contextsPoolManager.ContextList;
/**
* 处理上下文的全局类,这个类中含有一些静态方法用于获得有关上下文模型的信息
* @author carlven
* @since 2007/10/15
* @version 1.0
*
*/
public class ContextModel {
// 读取上下文的ContextModelReader 对象列表,每个对象读取并解析一个上下文模型的.xml 描述文件
// 因为通常一个模型读取并解析一次之后就不用再重复读,因此这里保存这种对象的实例,以方便重用
// 注意,我们假设上下文模型 .xml 在程序运行过程中不会被用户在文件系统这一级进行改变
private static LinkedList<ContextModelReader> readers = new LinkedList<ContextModelReader>();
/**
* 装入文件名 modelName 的上下文模型,并且解析之。注意,我们后面都使用 modelName 来标识不同的模型
* @throws ContextModelException 如果模型有错,可能会抛出这种异常
*/
public static void loadModel(String modelName) throws ContextModelException {
if (findModel(modelName) != null) return ;
ContextModelReader reader = new ContextModelReader(modelName);
try {
reader.read();
} catch (Exception exc) {
throw new ContextModelException("Read context model error: " + exc.getMessage());
}
readers.add(reader);
}
/**
* 获取模型 modelName 中所描述的有关算法组的信息
* @throws ContextModelException 如果模型有错,可能会抛出异常
*/
public static AlgorithmGroup getAlgorithmGroup(String modelName) throws ContextModelException {
ContextModelReader reader = findModel(modelName);
if (reader == null) throw new ContextModelException("Has not load model: " + modelName);
return reader.getAlgorithmGroup();
}
/**
* 获取模型 modelName 中所描述的有关事件的信息
* @throws ContextModelException 如果模型有错,可能会抛出异常
*/
public static EventList getEventList(String modelName) throws ContextModelException {
ContextModelReader reader = findModel(modelName);
if (reader == null) throw new ContextModelException("Has not load model: " + modelName);
return reader.getEventList();
}
/**
* 获取模型 modelName 中所描述的有关上下文的信息
* @throws ContextModelException 如果模型有错,可能会抛出异常
*/
public static ContextList getMetaContextList(String modelName) throws ContextModelException {
ContextModelReader reader = findModel(modelName);
if (reader == null) throw new ContextModelException("Has not load model: " + modelName);
return reader.getMetaContextList();
}
/**
* 生成获取模型 modelName 中所描述的算法组的上下文的 sensor,也即生成名为 算法组名 + Sensor 的方面(aspect)
* 来获取该模型所描述的算法组所可能产生的上下文
* @throws ContextModelException 如果模型有错,可能会抛出异常
*/
public static void generateContextSensor(String modelName) throws ContextModelException, IOException {
ContextModelReader reader = findModel(modelName);
if (reader == null) throw new ContextModelException("Has not load model: " + modelName);
reader.generateContextSensor();
}
/**
* 在已有的 ContextModelReader 对象列表中查找是否已经存在该模型的 ContextModelReader 对象
* @param modelName
* @return
*/
private static ContextModelReader findModel(String modelName) {
Iterator iterator = readers.iterator();
while (iterator.hasNext()) {
ContextModelReader reader = (ContextModelReader)iterator.next();
if (reader.getModelFileName().equals(modelName)) return reader;
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -