public boolean performFinish() {
boolean finished = true;
File modelFile = worldPage.getModelFile();
String savePath = worldPage.getSaveFilePath();
String fileName = worldPage.getFileName();
ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
ontModel.read(modelFile.getPath());
ontModel.setStrictMode(false);
localLabel = ontModel.getAnnotationProperty(METAG + "localLabel");
AnnotationProperty annotationPropertyCifString = ontModel.createAnnotationProperty(METAG + "cifString");
AnnotationProperty initialLocation = ontModel.getAnnotationProperty(METAG + "initialLocation");
// TODO 获取Graph
OntClass graph = ontModel.getOntClass(METAG + "sarva_diagram1");
generateMetaGraph(modelFile.getPath());
generateMetaObject(modelFile.getPath());
for (Iterator il2 = graph.listInstances(); il2.hasNext();) {
Individual karmaModel = (Individual) il2.next();
String karmaModelName = karmaModel.getLocalName();
Model model = new Model(graphMap.get(graph.getLocalName()));
model.setId(karmaModelName);
model.setLocalLabel(karmaModel.getPropertyValue(localLabel).toString());
if (karmaModel.hasProperty(annotationPropertyCifString)) {
model.setCifString(karmaModel.getPropertyValue(annotationPropertyCifString).toString().replace("\\\"", "\""));
}
modelMap.put(karmaModelName, model);
for (Iterator ig = karmaModel.listProperties(); ig.hasNext();) {
Statement igs = (Statement) ig.next();
// 添加对象实例
if (igs.getPredicate().getLocalName().equals("graphIncludingObject")) {
obj++;
// 找到对象实例
List<Map<String, String>> listInfo = new ArrayList<>();
Individual graph_object_Individual = ontModel.getIndividual(igs.getResource().getURI());
// locallabel
String objName = graph_object_Individual.getPropertyValue(localLabel).toString();
Map<String,String> nameMap = new HashMap<>();
nameMap.put("对象LocalLabel:", objName);
listInfo.add(nameMap);
//id
String objId = graph_object_Individual.getLocalName();
Map<String,String> idMap = new HashMap<>();
idMap.put("对象id:", objId);
listInfo.add(idMap);
// 对象实例属性
for (Iterator igp = graph_object_Individual.listProperties(); igp.hasNext();) {
Statement igps = (Statement) igp.next();
if (igps.getPredicate().getLocalName().equals("hasProperty")) {
// 找到对象实例属性
Individual object_property_Individual = ontModel.getIndividual(igps.getResource().getURI());
// 找到属性值
String object_property_Individual_value = "";
for (Iterator igp2 = object_property_Individual.listProperties(); igp2.hasNext();) {
Statement igps2 = (Statement) igp2.next();
if (igps2.getPredicate().getLocalName().equals("value")) {
Map<String,String> shuxingNameMap = new HashMap<>();
Map<String,String> shuxingValueMap = new HashMap<>();
object_property_Individual_value = igps2.getString();
String name = object_property_Individual.getPropertyValue(localLabel).toString();
shuxingNameMap.put("属性:", name);
shuxingValueMap.put("属性值:", object_property_Individual_value);
listInfo.add(shuxingNameMap);
listInfo.add(shuxingValueMap);
}
}
}
}
saveInfo.put(objId, listInfo);
System.out.println(saveInfo);
}
}
}
generateWord(savePath, fileName, "测试");
return finished;
}
public void generateWord(String savePath,String fileName, String parameter) {
try {
System.out.println(saveInfo);
System.out.println(obj);
XWPFDocument document = new XWPFDocument();
// 添加一个段落
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("对象总数: " + obj);
int i = 1;
for (Map.Entry<String, List<Map<String, String>>> data : saveInfo.entrySet()) {
run.addCarriageReturn(); // 在新行开始
run.setText(String.valueOf(i++));
// run.setText("对象id: " + data.getKey());
for (Map<String, String> dataList : data.getValue()) {
for (Map.Entry<String, String> entry : dataList.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
run.addCarriageReturn(); // 在新行开始
run.setText(key + value);
}
}
}
// 保存文件
try (FileOutputStream out = new FileOutputStream(savePath + "/" + fileName + ".docx")) {
document.write(out);
}
System.out.println("Word文件生成成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
标签:java,String,graph,object,生成,Individual,ontModel,new,word
From: https://blog.51cto.com/u_16340211/8876530