Oracle plsql de ref cursor örneği

Ref cursor kullanarak ihtiyacımı olan veri kümesi üzerinde çalışmaya bir örnek:
declare
type rec_musteri is record(
musteriNo number(20),
musteriAd varchar2(50));

rcMusteri omwb_emulation.globalpkg.RCT1;
erMusteri rec_musteri;

v_MusteriNo number(20);

begin

open rcMusteri for
select musterino, musteriAd
from tbl_musteri m
where m.tcknNo = TO_CHAR('01234567890');
exception
when others then
v_Hata := sqlerrm;

if rcMusteri%isopen then
loop
v_MusteriNo := '';
fetch rcMusteri
into erMusteri;
exit when rcMusteri%notfound;
v_MusteriNo := erMusteri.musteriNo;
v_MusteriAd := erMusteri.musteriAd;
end loop;
end if;
end;

SPARQL ve ARQ ile sorgu çalıştırma

Çalışmamızda bir Java uygulaması ile hazır bir ontoloji üzerinde sorgu çalıştırma yoluyla sonuca ulaşmayı hedefliyorduk. Çalıştıracağımız sorgu bir SPARQL sorgusu olacak ve Jena yardımı ile ontolojimiz içinden istediğimiz veriyi getirmeye çalışacaktı.

Bu amaçla öncelikle Jena SPARQL dökümanına bakmak yararlı olacaktır.

Jena SPARQL sorgularını işlemek üzere ARQ isimli bir kütüphane kullanıyor: ARQ dökümanı.


ARQ yardımı ile SPARQL sorgularına örnek olarak şu adreslere bakılabilir:
http://jena.sourceforge.net/ARQ/Tutorial/query1.html
http://www.ibm.com/developerworks/xml/library/j-sparql/

SPARQL ile sorgulama yapmak için örnek olarak aşağıdaki sorgu gibi bir sorgu hazırlayıp, yine aşağıda metodu çağırarak bu sorgunun sonucunu alabiliriz:

Q: which persons has same surname with "Ayder"

SPARQL:
PREFIX rdf: http://localhost/TestDocumentOntology.owl#
SELECT ?Result
WHERE { ?Person rdf:Surname ?Result .
FILTER regex(?Result, \"ayder\", \"i\" )}


import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.shared.ConfigException;
import com.hp.hpl.jena.shared.JenaException;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Filter;


static OntModel ontModel = null;
static final String inputFileName = "MyOntology.owl";

public static OntModel loadOntology(){
try{

ontModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );

InputStream in = FileManager.get().open( inputFileName );
if (in == null) {
throw new IllegalArgumentException( "File: " + inputFileName + " not found");
}

// read the RDF/XML file
ontModel.read(in, "");

}
catch (JenaException jx) {
if (jx.getCause() instanceof NoRouteToHostException
|| jx.getCause() instanceof UnknownHostException
|| jx.getCause() instanceof ConnectException
|| jx.getCause() instanceof IOException) {
System.out.println("Cannot access public internet - content negotiation test not executed");
} else
throw jx;
}

catch(Exception e){
System.out.println(e.getMessage());
}

return ontModel;
}

public static ArrayList executeQuery( ArrayList queryList ){
ArrayList resultList = new ArrayList();
String key = null;

System.out.println("------------------------------------");

// Execute the query and obtain results
QueryExecution qe = null;//QueryExecutionFactory.create(query[0], model);
ResultSet results = null;//qe.execSelect();

//use alternative queries
for(int i= 0; i < queryList.size() ; i++)
{
qe = QueryExecutionFactory.create(queryList.get(i).toString(), ontModel);
results = qe.execSelect();

System.out.println("**********************");
System.out.println("query : " + queryList.get(i) );
System.out.println("**********************");

if(results.hasNext())
{
resultList.add(ResultSetFormatter.asText(results));
// Output query results
//ResultSetFormatter.out(System.out, results);
for (int k=0;k
System.out.println(resultList.get(k));
break;
}else
{
System.out.println("NO RESULT for the query " + i);
}
}


// Important - free up resources used running the query
qe.close();

return resultList;
}

Ontology Mapping

Yaptığımız çalışmada bir Java uygulaması ile hazır bir ontoloji üzerinde sorgu çalıştırma yoluyla sonuca ulaşmayı hedefleliyorduk. Üzerinde çalışabileceğimiz hazır bir ontolojimiz yoksa, bu projede yaptığımız gibi, Protege uygulamasını kullanarak kendi ontolojimizi oluşturabiliriz.

Ontolojimizi oluşturduktan sonra aşağıda örneği görülen kod parçası yardımı ile, java içinden bu ontolojiye ulaşabilir, içerdiği class, property ve individual değerlerini alabiliriz.

import com.hp.hpl.jena.ontology.*;

import com.hp.hpl.jena.vocabulary.RDF;

import com.hp.hpl.jena.rdf.model.*;

import com.hp.hpl.jena.util.FileManager;

import com.hp.hpl.jena.shared.ConfigException;

import com.hp.hpl.jena.shared.JenaException;

import com.hp.hpl.jena.query.*;

import com.hp.hpl.jena.shared.PrefixMapping;

import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import com.hp.hpl.jena.util.iterator.Filter;

public class OntoMapper {

static OntModel ontModel = null;

static final String inputFileName = "MyOntology.owl";

static ArrayList classList = null;

static ArrayList propertyList = null;

static ArrayList rootClassesList = null;

static ArrayList individualList = null;

public static OntModel loadOntology(){

try{

ontModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );

InputStream in = FileManager.get().open( inputFileName );

if (in == null) {

throw new IllegalArgumentException( "File: " + inputFileName + " not found");

}

// read the RDF/XML file

ontModel.read(in, "");

}

catch (JenaException jx) {

if (jx.getCause() instanceof NoRouteToHostException

|| jx.getCause() instanceof UnknownHostException

|| jx.getCause() instanceof ConnectException

|| jx.getCause() instanceof IOException) {

System.out.println("Cannot access public internet - content negotiation test not executed");

} else

throw jx;

}

catch(Exception e){

System.out.println(e.getMessage());

}

if(classList == null)

classList = new ArrayList();

if(propertyList == null)

propertyList = new ArrayList();

if(rootClassesList == null)

rootClassesList = new ArrayList();

if(individualList == null)

individualList = new ArrayList();

OntClass superCls = null;

OntClass cls = null;

Property prty = null;

for (Iterator i = ontModel.listClasses(); i.hasNext(); ) {

cls = (OntClass) i.next();

System.out.println( "Class " + cls.getURI() );

classList.add(cls.getURI());

for (Iterator k = cls.listSubClasses( true ); k.hasNext(); )

{

rootClassesList.add(cls.getURI());

break;

}

Iterator iter = ontModel.listIndividuals();

while(iter.hasNext()){

Individual c = (Individual)iter.next();

System.out.println( "Individual " + c.getLocalName() );

individualList.add(c.getLocalName());

cls = (OntClass) c.getOntClass();

superCls = cls.getSuperClass();

Iterator iterTables = c.listProperties();

while (iterTables.hasNext()){

Statement stmt = (Statement)iterTables.next();

//Resource r = (Resource)stmt.getObject();

prty = (Property)stmt.getPredicate();

if (superCls != null)

{

System.out.println( "Property getLocalName::: " + prty.getLocalName());

propertyList.add(prty.getLocalName());

}

else

{

System.out.println( "Property getLocalName::: " + prty.getLocalName());

propertyList.add(prty.getLocalName());

}

}

}

}



İlgili Kaynaklar:
http://protege.stanford.edu/

Generic Question Templates(GQT)

SPARQL dili ontoloji üzerinde arama yaparken “SPARQL triple” olarak bilinen üç anahtara dayalı olarak çalışır. Bu üç anahtarı {class}, {property} ve {value} olarak adlandırabiliriz. Value yerine {object} ve ya {criteria} da gelebilir.

Kullanıcı tarafından girilen soruları işlerken, öncelikle soru yapısını anlamaya çalışmalı; soru içinde bulunan kelimeleri, “SPARQL triple” oluşturacak şekilde ayıklamalıyız. Bir başka deyişle, sorunun içindeki class, property ve value değerlerini bulmalıyız.

Geliştirdiğimiz doğal dil işleme algoritması sonucu elde ettiğimiz veriyi, elimizdeki ontolojiyi dikkate alarak, önceden belirleyeceğimiz soru şablonlarına (Generic Question Template) uydurmaya çalışırız.

Soru şablonlarının öncelikle anahtar kelimelerimize göre belirleneceğini kabul ederek aşağıdaki gibi yapılar belirleriz:


/*** Question Templates
-Which {class} has the {property} with {value}
-Which {class} has the {property} with {criteria}
-Which {class} has the {property}
-Who is the {property} of {value}
-How many {class} are/has {property} by {value}
-When did {value} {property}
-What is the {property} of {value} {class}
-What is the {property} of {class} which has the {value}
*/

Buna göre herbir soru tipini karşılayabilecek ortak bir algortima ile girilen soruyu önceden belirlediğimiz bu soru şablonlarına uygun şekilde işlemek; şablonda varsa class, property ve value değerlerini bulmamız gerekir. Bu değerleri elde etmek için ontolojimiz içinde arama yapmak gerekecektir ki, bir sonraki başlıkta bunu göreceğiz.

Python öğrenme aracı: Acire

Python öğrenmek isteyenler için karşılaştığım bir yazıdaki aracı duyurmak isterim. İngilizce bir araç, ama belki de gönüllü biri Türkçe sürümünü de yapabilir. Acire için karşıma çıkaran yazıyı ve Jono Bacon'un ilgili duyurusunu okuyabilirsiniz.