-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCForget.java
174 lines (129 loc) · 7.3 KB
/
RCForget.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package test;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.io.OWLXMLOntologyFormat;
import org.semanticweb.owlapi.model.*;
import forgetting.Forgetter;
public class RCForget {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
public static void forgetRC(String corpus, Double percent, String ontoInPath, String flag, String ontoOutPath, String conceptsInPath, String rolesInPath,String dataLogPath,String status)
throws OWLOntologyCreationException, IOException, CloneNotSupportedException, OWLOntologyStorageException {
ArrayList<String> ontoFilePaths = new ArrayList<String>();
try {
//readfile("C:/Users/DELL/Documents/test_data/BioPortal_Ontologies");
ontoFilePaths = ReadFiles.readfile(ontoInPath + "\\" + corpus+"\\" + status);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
System.out.println("Ontology Files Founded in" + ontoInPath);
System.out.println("-----" + flag + "_" + Double.toString(percent) + "_" + corpus);
Integer ontoNum = ontoFilePaths.size();
//log
String logPath=dataLogPath+"\\" + corpus + "_" + flag + "_" + String.valueOf(percent)+".csv";
ReadFiles.clearInfoForFile(logPath);
//output
String outputDir=ontoOutPath + "\\" + corpus + "\\" + Double.toString(percent)
+ "\\" + flag ;//;
ReadFiles.mkDirectory(outputDir);
ReadFiles.deleteDir(outputDir);
for (int i = 0; i < ontoNum; i++) {
{
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(logPath, true)));
} catch (Exception e) {
e.printStackTrace();
}
OWLOntologyManager managerCurr = OWLManager.createOWLOntologyManager();
//get the name of each onto
String filePathCurr = ontoFilePaths.get(i);
String[] tmps = filePathCurr.split("/");
String ontoName = tmps[tmps.length - 1];
System.out.println(ontoName);
IRI iri = IRI.create(filePathCurr);
OWLOntology o = managerCurr.loadOntologyFromOntologyDocument(new IRIDocumentSource(iri),
new OWLOntologyLoaderConfiguration().setLoadAnnotationAxioms(true));
Integer ontoSize = o.getTBoxAxioms(true).size();
Integer cSigSize = o.getClassesInSignature().size();//1
Integer rSigSize = o.getObjectPropertiesInSignature().size();//2
Integer iSigSize = o.getIndividualsInSignature().size();
File cFile;
File rFile;
String line_;
BufferedReader creader;
BufferedReader rreader;
// String cFileName = conceptsInPath + "\\" + corpus + "_" + flag + "_" + String.valueOf(percent) + ontoName + ".concepts";
// String rFileName = rolesInPath + "\\" + corpus + "_" + flag + "_" + String.valueOf(percent) + ontoName + ".roles";
String cFileName= conceptsInPath+"\\"+String.valueOf(percent)+"_"+flag+"_"+ontoName+".concepts";
String rFileName= conceptsInPath+"\\"+String.valueOf(percent)+"_"+flag+"_"+ontoName+".roles";
OWLDataFactory factory = managerCurr.getOWLDataFactory();
cFile = new File(cFileName);
rFile = new File(rFileName);
Set<OWLClass> sig_c = new HashSet<OWLClass>();
Set<OWLObjectProperty> sig_r = new HashSet<OWLObjectProperty>();
if (cFile.exists()) {
creader = new BufferedReader(new FileReader(cFile));
List<OWLClass> concepts_lst = new ArrayList<OWLClass>(o.getClassesInSignature());
line_ = creader.readLine();
while (line_ != null) {
sig_c.add(factory.getOWLClass(IRI.create(line_)));
// System.out.println("concept:"+concepts_lst.get(Integer.parseInt(line_.trim())));
line_ = creader.readLine();
}
}
if (rFile.exists()) {
rreader = new BufferedReader(new FileReader(rFile));
List<OWLObjectProperty> roles_lst = new ArrayList<OWLObjectProperty>(o.getObjectPropertiesInSignature());
line_ = rreader.readLine();
while (line_ != null) {
sig_r.add(factory.getOWLObjectProperty(IRI.create(line_)));
// System.out.println("roles:"+roles_lst.get(Integer.parseInt(line_.trim())));
line_ = rreader.readLine();
}
}
Forgetter f = new Forgetter();
OutputStream os = new FileOutputStream(
new File(outputDir+ "\\" + ontoName));
Runtime r = Runtime.getRuntime();
r.gc();
long startMem=r.freeMemory();
long begin= System.currentTimeMillis();
OWLOntology o_ = f.Forgetting(sig_c, sig_r, o);
managerCurr.saveOntology(o_, new OWLXMLOntologyFormat(), os);
//Date date1= new Date(System.currentTimeMillis());
//String endTime = sdf.format(date);
long endMem=r.freeMemory();
long end=System.currentTimeMillis();
String memCost = String.valueOf((startMem-endMem)/1024);
long executionTime =end-begin;
String content=ontoName+","+ontoSize+","+cSigSize+","+rSigSize+","+corpus+","+flag+","+percent.toString()+","+ executionTime+","+memCost;
out.write(content + "\n");
out.close();
}
}
}
public static void main(String[] args) throws Exception {
String[] corpusL = {"corpus1"};//, "corpus2", "corpus3"};
Double[] percentL = {0.1,0.3};//, 0.3};
String[] flagL={"1"};
String ontoInputPath = "E:\\dyw\\Test2\\Result\\OntoSelected";
String conceptsInputPath = "E:\\dyw\\Test2\\Result\\CRselected\\Concepts";
String rolesInputPath = "E:\\dyw\\Test2\\Result\\CRselected\\Roles";
String ontoOutPath="E:\\dyw\\Test2\\Result\\OntoForgetted";
String dataLogPath="E:\\dyw\\Data\\Result\\OntoData\\forgetLog ";
for(String cps:corpusL)
for(Double pct:percentL)
for(String flag:flagL)
forgetRC(cps,pct,ontoInputPath,flag,ontoOutPath,conceptsInputPath,rolesInputPath,dataLogPath,"waiting");
}
}