您的位置:首页 > 编程语言 > Java开发

JAVA细节使用20131128

2013-11-28 22:13 302 查看
1.工厂模式太不利于别的程序员阅读了,除非跑一遍程序,要不然很难搞清具体的代码实现是哪一块搞的,只有开发的一小部分人估计会乐在其中,要么说程序员都是猥琐的天使,这里可见一斑。

2.ARQ中sparql实现的关键一句是将DatasetGraph和Context绑定到一起,DatasetGraph是供查询的一个实际内容,Context的官方解释是“A class for setting and keeping named values. Used to pass implementation-specific parameters across general interfaces.”也就是一种数据的传递结构,至此还是一头雾水,不妨按照例子来看看它们的实际内容:

原始rdf文件:

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:admin="http://webns.net/mvcb/">
<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker rdf:resource="#me"/>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource="http://www.ldodds.com/foaf/foaf-a-matic"/>
<admin:errorReportsTo rdf:resource="mailto:leigh@ldodds.com"/>
</foaf:PersonalProfileDocument>
<foaf:Person rdf:ID="me">
<foaf:name>Semantic Web</foaf:name>
<foaf:title>Dr</foaf:title>
<foaf:givenname>Semantic</foaf:givenname>
<foaf:family_name>Web</foaf:family_name>
<foaf:nick>Webby</foaf:nick>
<foaf:homepage rdf:resource="http://semwebprogramming.org"/>
<foaf:depiction rdf:resource="http://semwebprogramming.org/semweb.jpg"/>
<foaf:phone rdf:resource="tel:410-679-8999"/>
<foaf:workplaceHomepage rdf:resource="http://semwebprogramming.com"/>
<foaf:workInfoHomepage rdf:resource="http://semwebprogramming.com/dataweb.html"/>
<foaf:schoolHomepage rdf:resource="http://www.web.edu"/>
<foaf:knows>
<foaf:Person rdf:ID="Ontology">
<foaf:name>I. M. Ontology</foaf:name>
<foaf:mbox rdf:resource="mailto:ont@gmail.com"/>
<rdfs:seeAlso rdf:resource="http://ont.com"/></foaf:Person></foaf:knows>
<foaf:knows>
<foaf:Person rdf:ID="Reasoner">
<foaf:name>Ican Reason</foaf:name>
<foaf:mbox rdf:resource="mailto:reason@firefox.com"/>
<rdfs:seeAlso rdf:resource="http://reasoner.com"/></foaf:Person></foaf:knows>
<foaf:knows>
<foaf:Person rdf:ID="Statement">
<foaf:name>Makea Statement</foaf:name>
<foaf:mbox rdf:resource="mailto:mstatement@gmail.com"/>
<rdfs:seeAlso rdf:resource="http://statement.com"/></foaf:Person></foaf:knows></foaf:Person>
</rdf:RDF>


DatasetGraph的内容是:

(dataset
(graph
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#List>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/1999/02/22-rdf-syntax-ns#List>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#List> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#List> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#Resource>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#Resource>)
(triple <http://www.w3.org/2000/01/rdf-schema#label> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#Literal>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#Resource>)
(triple <http://www.w3.org/2000/01/rdf-schema#Class> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/2000/01/rdf-schema#Class> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#Resource>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/2000/01/rdf-schema#Resource> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Datatype>)
(triple <http://www.w3.org/2000/01/rdf-schema#comment> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#Literal>)
(triple <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#object>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#List>)
(triple <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rd
4000
f-schema#Class>)
(triple <http://www.w3.org/2000/01/rdf-schema#Literal> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/2000/01/rdf-schema#Literal> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#Resource>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/1999/02/22-rdf-syntax-ns#List>)
(triple <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#first>)
(triple <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#Class>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/title> "Dr")
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/knows> <http://org.semwebprogramming/chapter2/people#Ontology>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/family_name> "Web")
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/knows> <http://org.semwebprogramming/chapter2/people#Reasoner>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/knows> <http://org.semwebprogramming/chapter2/people#Statement>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/schoolHomepage> <http://www.web.edu>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/name> "Semantic Web")
(triple <http://org.semwebprogramming/chapter2/people#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/homepage> <http://semwebprogramming.org>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/depiction> <http://semwebprogramming.org/semweb.jpg>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/workInfoHomepage> <http://semwebprogramming.com/dataweb.html>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/nick> "Webby")
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/phone> <tel:410-679-8999>)
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/givenname> "Semantic")
(triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/workplaceHomepage> <http://semwebprogramming.com>)
(triple <http://org.semwebprogramming/chapter2/people> <http://webns.net/mvcb/errorReportsTo> <mailto:leigh@ldodds.com>)
(triple <http://org.semwebprogramming/chapter2/people> <http://webns.net/mvcb/generatorAgent> <http://www.ldodds.com/foaf/foaf-a-matic>)
(triple <http://org.semwebprogramming/chapter2/people> <http://xmlns.com/foaf/0.1/primaryTopic> <http://org.semwebprogramming/chapter2/people#me>)
(triple <http://org.semwebprogramming/chapter2/people> <http://xmlns.com/foaf/0.1/maker> <http://org.semwebprogramming/chapter2/people#me>)
(triple <http://org.semwebprogramming/chapter2/people> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/PersonalProfileDocument>)
(triple <http://org.semwebprogramming/chapter2/people#Ontology> <http://www.w3.org/2000/01/rdf-schema#seeAlso> <http://ont.com>)
(triple <http://org.semwebprogramming/chapter2/people#Ontology> <http://xmlns.com/foaf/0.1/mbox> <mailto:ont@gmail.com>)
(triple <http://org.semwebprogramming/chapter2/people#Ontology> <http://xmlns.com/foaf/0.1/name> "I. M. Ontology")
(triple <http://org.semwebprogramming/chapter2/people#Ontology> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>)
(triple <http://org.semwebprogramming/chapter2/people#Reasoner> <http://www.w3.org/2000/01/rdf-schema#seeAlso> <http://reasoner.com>)
(triple <http://org.semwebprogramming/chapter2/people#Reasoner> <http://xmlns.com/foaf/0.1/mbox> <mailto:reason@firefox.com>)
(triple <http://org.semwebprogrammi...


最初查询Sparql语句是:

PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  people: <http://org.semwebprogramming/chapter2/people#>

SELECT DISTINCT  ?name
WHERE
{ people:me foaf:name ?name }


而Context的内容是:

symbol:http://jena.hpl.hp.com/ARQ/system#now = "2013-11-28T20:34:40.062+08:00"^^http://www.w3.org/2001/XMLSchema#dateTime
symbol:http://jena.hpl.hp.com/ARQ#regexImpl = symbol:http://jena.hpl.hp.com/ARQ#javaRegex
symbol:http://jena.hpl.hp.com/ARQ/system#query = PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX people: <http://org.semwebprogramming/chapter2/people#> SELECT DISTINCT ?name WHERE { people:me foaf:name ?name }

symbol:http://jena.hpl.hp.com/ARQ#constantBNodeLabels = true
symbol:http://jena.hpl.hp.com/ARQ#strictGraph = false
symbol:http://jena.hpl.hp.com/ARQ#strictSPARQL = false
symbol:http://jena.hpl.hp.com/ARQ#enablePropertyFunctions = true
symbol:http://jena.hpl.hp.com/ARQ#stageGenerator = com.hp.hpl.jena.sparql.engine.main.StageGeneratorGeneric@a14c53
symbol:http://jena.hpl.hp.com/ARQ#romanNumerals = false


可以看出,DatasetGraph和Context分别对应着处理后的本体数据和查询语句。因此将两个东西绑定到一起的原因也可以猜测出来了。但是问题在于Sparql查询语句的处理和封装应该是放进query中了,但是这里Context为啥也有了查询语句?而且Context中的查询语句还是没有处理过的raw数据,这一点很可疑。Context里也不一定就是raw数据,因为myeclipse在debug下Variables的数据显示似乎很强大,Query查看的时候也直接查看到完整的Query了,虽然已经分析过Query是将原始sparql查询语句进行拆分封装了,但是以让显示完整的raw数据,这就开始挑战我对myeclipse变量显示机制的理解程度了。

3.在编译Query内的Element的时候,Jena使用了其定义的一种类型Pair使用时的情况如下:

protected Op compileElementGroup(ElementGroup groupElt)
{
Pair<List<Expr>, List<Element>> pair = prepareGroup(groupElt) ;
List<Expr> filters = pair.getLeft() ;
List<Element> groupElts = pair.getRight() ;

// Compile the consolidated group elements.
// "current" is the completed part only - there may be thing pushed into the accumulator.
Op current = OpTable.unit() ;
Deque<Op> acc = new ArrayDeque<Op>() ;

for (Iterator<Element> iter = groupElts.listIterator() ; iter.hasNext() ; )
{
Element elt = iter.next() ;
if ( elt != null )
current = compileOneInGroup(elt, current, acc) ;
}

// Deal with any remaining ops.
//current = joinOpAcc(current, acc) ;

if ( filters != null )
{
// Put filters round the group.
for ( Expr expr : filters )
current = OpFilter.filter(expr, current) ;
}
return current ;
}


这个Pair就是一个组合,仅仅是一个配对,研究之后才发现,它没有什么高级方法,害我心中一紧张以为又遇到什么高端东西了。

程序中,这个Pair语句处理后,因为就传入了一个pattern,所以实际上就只有一对pair进去了,这个独立的Pair的List<Expr>是null,而List<Element>则是传入的唯一一个pattern。

4.在将element编译为op之后,jena做了一个简化的环节,暂且看看这一简化前后op的变化:

简化前:(join
(table unit)
(bgp (triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/name> ?name)))
简化后:
(bgp (triple <http://org.semwebprogramming/chapter2/people#me> <http://xmlns.com/foaf/0.1/name> ?name)
可以看出简化只是将join的table这一块去掉了,实际上table当时加入也是加入了一个默认的东西,join的op其实是一个基于OpBase的Op2类,本身就是为了描述更复杂的情况的,本例中本来sparql语句没有那些复杂因素,因此直接就把默认加上去的东西给删去了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: