use of org.apache.jena.ontology.OntClass in project jena by apache.
the class ConcurrencyTest method doTestConcurrency.
private void doTestConcurrency(final OntModel model) throws InterruptedException {
// initialize the model
final String NS = PrintUtil.egNS;
model.enterCriticalSection(Lock.WRITE);
final OntClass Top = model.createClass(NS + "Top");
for (int i = 0; i < MODEL_SIZE; i++) {
OntClass C = model.createClass(NS + "C" + i);
Top.addSubClass(C);
model.createIndividual(NS + "i" + i, C);
}
model.leaveCriticalSection();
class QueryExecutingRunnable implements Runnable {
@Override
@SuppressWarnings("unchecked")
public void run() {
// Keep this thread running until the specified duration has expired
long runStartedAt = System.currentTimeMillis();
while (System.currentTimeMillis() - runStartedAt < TEST_LENGTH) {
Thread.yield();
model.enterCriticalSection(Lock.READ);
try {
// Iterate over all statements
StmtIterator it = model.listStatements();
while (it.hasNext()) it.nextStatement();
it.close();
// Check number of instances of Top class
int count = 0;
ExtendedIterator<OntResource> ei = (ExtendedIterator<OntResource>) Top.listInstances();
while (ei.hasNext()) {
ei.next();
count++;
}
ei.close();
if (MODEL_SIZE != count) {
if (FULL_TEST)
System.err.println("Failure - found " + count + " instance, expected " + MODEL_SIZE);
throw new JenaException("Failure - found " + count + " instance, expected " + MODEL_SIZE);
}
} finally {
model.leaveCriticalSection();
}
}
}
}
// Start the threads
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; ++i) {
executorService.submit(new QueryExecutingRunnable());
}
// Wait for threads to finish
// this will *not* terminate any threads currently running
executorService.shutdown();
Thread.sleep(TEST_LENGTH + 50);
// Possibly in deadlock, wait a little longer to be sure
for (int i = 0; i < 50 && !executorService.isTerminated(); i++) {
Thread.sleep(20);
}
if (!executorService.isTerminated()) {
/* uncomment this block to perform deadlock checking, only on java 1.6 */
// Check for deadlock
ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
long[] ids = tmx.findDeadlockedThreads();
if (ids != null) {
ThreadInfo[] infos = tmx.getThreadInfo(ids, true, true);
System.err.println("*** Deadlocked threads");
for (ThreadInfo ti : infos) {
System.err.println("Thread \"" + ti.getThreadName() + "\" id=" + ti.getThreadId() + " " + ti.getThreadState().toString());
System.err.println("Lock name: " + ti.getLockName() + " owned by \"" + ti.getLockOwnerName() + "\" id=" + ti.getLockOwnerId());
System.err.println("\nStack trace:");
for (StackTraceElement st : ti.getStackTrace()) System.err.println(" " + st.getClassName() + "." + st.getMethodName() + " (" + st.getFileName() + ":" + st.getLineNumber() + ")");
System.err.println();
}
}
Assert.assertTrue("Deadlock detected!", false);
/* end deadlock block */
assertTrue("Failed to terminate execution", false);
}
}
use of org.apache.jena.ontology.OntClass in project answer-ontology-construction by YueHub.
the class MovieConstructionServiceImpl method queryIndividual.
public Individual queryIndividual(String individualName, String polysemantExplain, String url, boolean isAliases, OntologyClassEnum parentClass) {
// 以生省内存的方式读取Answer_Dict词典
LinkedList<String> dictIndividualList = IOUtil.readLineListWithLessMemory(Config.individualDictPath);
Individual movieIndividual = null;
Long rowNum = 0l;
// 遍历词典中的实体记录 判断当前实体是否已经存在
for (String row : dictIndividualList) {
++rowNum;
String[] fieldsDict = row.split("_");
// UUID
String dictIndividualUUID = fieldsDict[0];
// 实体名
String dictIndividualName = fieldsDict[1];
// 歧义说明
String dictPolysemantExplain = fieldsDict[2];
// 实体百科页面URL
String dictIndividualURL = fieldsDict[3];
// 是否是本名
String dictIsAliasesWrite = fieldsDict[4];
// 实体所属类型
int dictIndividualClass = Integer.parseInt(fieldsDict[5]);
// 第一种情况:如果找到实体名相同并且明确指出该实体没有歧义则 该实体就是当前迭代到的实体
if (individualName.equals(dictIndividualName) && dictPolysemantExplain.equals("无")) {
movieIndividual = constructionDAO.getIndividual(dictIndividualUUID);
// 找到完全相同的实体了 使用#去除所有框架定位网页
} else if (individualName.equals(dictIndividualName) && url.split("#")[0].equals(dictIndividualURL) && dictIndividualClass == parentClass.getIndex()) {
// 如果此时抓到的实体歧义不为空 则表示该实体有同名实体 则更新词典 TODO 应该把 != null 去掉
if (dictPolysemantExplain.equals("待更新")) {
if (polysemantExplain == null) {
polysemantExplain = "无";
}
// 更新词典 修改歧义说明字段
row = dictIndividualUUID + "_" + dictIndividualName + "_" + polysemantExplain + "_" + dictIndividualURL + "_" + dictIsAliasesWrite + "_" + parentClass.getIndex();
// 更新Answer_Dict
FileIOUtil.updateContent(Config.individualDictPath, rowNum, row);
}
// 获取该实体
movieIndividual = constructionDAO.getIndividual(dictIndividualUUID);
}
}
// 如果词典中不存在该实体,则插入词典并且创建一个实体
if (movieIndividual == null) {
String movieIndividualUUID = UUID.randomUUID().toString().replace("-", "");
String isAliasesWrite = null;
if (isAliases == true) {
isAliasesWrite = "1";
} else {
isAliasesWrite = "0";
}
if (polysemantExplain == null) {
polysemantExplain = "无";
}
String row_add_individual = movieIndividualUUID + "_" + individualName + "_" + polysemantExplain + "_" + url.split("#")[0] + "_" + isAliasesWrite + "_" + parentClass.getIndex();
FileIOUtil.appendContent(Config.individualDictPath, row_add_individual);
// 获取电影类型
OntClass movieClass = constructionDAO.getOntClass(parentClass.getName());
movieIndividual = constructionDAO.createIndividual(movieIndividualUUID, movieClass);
}
// 创建comment
constructionDAO.addComment(movieIndividual, individualName);
return movieIndividual;
}
use of org.apache.jena.ontology.OntClass in project answer-ontology-construction by YueHub.
the class AnimationConstructionServiceImpl method dealAreas.
public boolean dealAreas(Individual animationIndividual, BaikePage baikePage) {
List<String> areas = new ArrayList<String>();
int index = 0;
for (String parameterName : baikePage.getParameterNames()) {
if (parameterName.equals("地区")) {
areas = Arrays.asList(baikePage.getParameterValues().get(index).split("、"));
if (areas.size() == 1) {
areas = Arrays.asList(baikePage.getParameterValues().get(index).split(","));
}
for (String area : areas) {
String url = null;
int i = 0;
for (String parameterHasUrlValue : baikePage.getParameterHasUrlValues()) {
if (area.equals(parameterHasUrlValue)) {
url = baikePage.getParameterHasUrl().get(i);
}
++i;
}
if (url != null) {
String polysemantExplain = "待更新";
Individual areaIndividual = this.queryIndividual(area, polysemantExplain, url, true, OntologyClassEnum.AREA);
constructionDAO.addObjectProperty(animationIndividual, "属于", areaIndividual);
}
// 创建二级类
OntClass areaClass = constructionDAO.getOntClass(area);
// 如果本体中此时没有该二级类
if (areaClass == null) {
areaClass = constructionDAO.createOntClass(area);
}
// 获取一级类 动画类
OntClass animationClass = constructionDAO.getOntClass(OntologyClassEnum.ANIMATION.getName());
// 创建子类(二级类)
constructionDAO.addSubClass(animationClass, areaClass);
constructionDAO.addSubClass(animationIndividual, areaClass);
}
}
++index;
}
return false;
}
use of org.apache.jena.ontology.OntClass in project answer-ontology-construction by YueHub.
the class CompanyConstructionServiceImpl method queryIndividual.
public Individual queryIndividual(String individualName, String polysemantExplain, String url, boolean isAliases, OntologyClassEnum parentClass) {
// 以生省内存的方式读取Answer_Dict词典
LinkedList<String> dictIndividualList = IOUtil.readLineListWithLessMemory(Config.individualDictPath);
Individual companyIndividual = null;
Long rowNum = 0l;
// 遍历词典中的实体记录 判断当前实体是否已经存在
for (String row : dictIndividualList) {
++rowNum;
String[] fieldsDict = row.split("_");
// UUID
String dictIndividualUUID = fieldsDict[0];
// 实体名
String dictIndividualName = fieldsDict[1];
// 歧义说明
String dictPolysemantExplain = fieldsDict[2];
// 实体百科页面URL
String dictIndividualURL = fieldsDict[3];
// 是否是本名
String dictIsAliasesWrite = fieldsDict[4];
// 实体所属类型
int dictIndividualClass = Integer.parseInt(fieldsDict[5]);
// 第一种情况:如果找到实体名相同并且明确指出该实体没有歧义则 该实体就是当前迭代到的实体
if (individualName.equals(dictIndividualName) && dictPolysemantExplain.equals("无")) {
companyIndividual = constructionDAO.getIndividual(dictIndividualUUID);
// 找到完全相同的实体了 使用#去除所有框架定位网页
} else if (individualName.equals(dictIndividualName) && url.split("#")[0].equals(dictIndividualURL) && dictIndividualClass == parentClass.getIndex()) {
// 如果此时抓到的实体歧义不为空 则表示该实体有同名实体 则更新词典 TODO 应该把 != null 去掉
if (dictPolysemantExplain.equals("待更新")) {
if (polysemantExplain == null) {
polysemantExplain = "无";
}
// 更新词典 修改歧义说明字段
row = dictIndividualUUID + "_" + dictIndividualName + "_" + polysemantExplain + "_" + dictIndividualURL + "_" + dictIsAliasesWrite + "_" + parentClass.getIndex();
// 更新Answer_Dict
FileIOUtil.updateContent(Config.individualDictPath, rowNum, row);
}
// 获取该实体
companyIndividual = constructionDAO.getIndividual(dictIndividualUUID);
}
}
// 如果词典中不存在该实体,则插入词典并且创建一个实体
if (companyIndividual == null) {
String companyIndividualUUID = UUID.randomUUID().toString().replace("-", "");
String isAliasesWrite = null;
if (isAliases == true) {
isAliasesWrite = "1";
} else {
isAliasesWrite = "0";
}
if (polysemantExplain == null) {
polysemantExplain = "无";
}
String row_add_individual = companyIndividualUUID + "_" + individualName + "_" + polysemantExplain + "_" + url.split("#")[0] + "_" + isAliasesWrite + "_" + parentClass.getIndex();
FileIOUtil.appendContent(Config.individualDictPath, row_add_individual);
// 获取电影类型
OntClass companyClass = constructionDAO.getOntClass(parentClass.getName());
companyIndividual = constructionDAO.createIndividual(companyIndividualUUID, companyClass);
}
// 创建comment
constructionDAO.addComment(companyIndividual, individualName);
return companyIndividual;
}
use of org.apache.jena.ontology.OntClass in project answer-ontology-construction by YueHub.
the class AreaConstructionServiceImpl method dealAreas.
public boolean dealAreas(Individual areaIndividual, BaikePage baikePage) {
List<String> areas = new ArrayList<String>();
int index = 0;
for (String parameterName : baikePage.getParameterNames()) {
if (parameterName.equals("所属州")) {
areas = StringFilter.parameterValueSeparates(baikePage.getParameterValues().get(index));
for (String area : areas) {
// 创建二级类
OntClass stateAreaClass = constructionDAO.getOntClass(area);
// 如果本体中此时没有该二级类
if (stateAreaClass == null) {
stateAreaClass = constructionDAO.createOntClass(area);
}
// 获取一级类 地区类
OntClass areaClass = constructionDAO.getOntClass(OntologyClassEnum.AREA.getName());
// 创建子类(二级类)
constructionDAO.addSubClass(areaClass, stateAreaClass);
constructionDAO.addSubClass(areaIndividual, stateAreaClass);
}
}
if (parameterName.equals("首都")) {
areas = StringFilter.parameterValueSeparates(baikePage.getParameterValues().get(index));
for (String area : areas) {
String url = null;
int i = 0;
for (String parameterHasUrlValue : baikePage.getParameterHasUrlValues()) {
if (area.equals(parameterHasUrlValue)) {
url = baikePage.getParameterHasUrl().get(i);
}
++i;
}
if (url != null) {
String polysemantExplain = "待更新";
Individual capitalIndividual = this.queryIndividual(area, polysemantExplain, url, true, OntologyClassEnum.AREA);
constructionDAO.addObjectProperty(capitalIndividual, "属于", areaIndividual);
constructionDAO.addObjectProperty(areaIndividual, "首都", capitalIndividual);
}
}
}
++index;
}
return false;
}
Aggregations