tutorial de jena santiago atella nicolás genta diego gonzález inco - fing - udelar
TRANSCRIPT
![Page 1: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/1.jpg)
Tutorial de JENA
Santiago Atella
Nicolás Genta
Diego González
INCO - FING - UDELAR
![Page 2: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/2.jpg)
JENA
• Framework desarrollado por HP Labs para manipular metadata desde una aplicación Java.
• Dos versiones:– JENA 1
• Principalmente soporte para RDF.
• Capacidades de razonamiento limitadas.
– JENA 2• Incluye además una API para el manejo de Ontologias.
• Soporta el lenguaje OWL.
![Page 3: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/3.jpg)
JENA
• Incluye varios componentes:– ARP: Un Parser de RDF.– API RDF.– API de Ontologias con soporte para OWL,
DAML y RDF Schema.– Subsistema de Razonamiento.– Soporte para Persistencia.– RDQL: Lenguaje de consultas de RDF.
![Page 4: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/4.jpg)
RDF• Recurso:
– Todo aquello que se puede describir por una expresión RDF.
• Propiedad:– Una característica, atributo o relación usada para
describir un recurso.
• Literal:– Un tipo de datos simple (String, Integer, etc).
• Statements:– Un recurso junto con una propiedad y con un valor
asociado.
![Page 5: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/5.jpg)
Statements
• Cada statement tiene tres partes:– Sujeto, Predicado y Objeto.
• Un modelo RDF es un conjunto de statements.
![Page 6: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/6.jpg)
API RDF de Jena
• Permite crear y manipular modelos RDF desde una aplicación Java.
• Proporciona clases java para representar:– Modelos.– Recursos.– Propiedades.– Literales.– Statements.
![Page 7: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/7.jpg)
Ejemplo: vcards
![Page 8: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/8.jpg)
Crear un modelo RDFString personURI = "http://somewhere/JohnSmith";
String givenName = "John";
String familyName = "Smith";
String fullName = givenName + " " + familyName;
Model model = ModelFactory.createDefaultModel();
Resource johnSmith = model.createResource(personURI);
johnSmith.addProperty(VCARD.FN, fullName);
johnSmith.addProperty(VCARD.N, model.createResource()
.addProperty(VCARD.Given, givenName)
.addProperty(VCARD.Family, familyName));
![Page 9: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/9.jpg)
Escribir y Leer un modelo
• Para serializar el modelo a XML:
model.write(System.out);
• Para cargar un modelo en memoria:
Model model = ModelFactory.createDefaultModel();
InputStream in = Tutorial05.class.getClassLoader().
getResourceAsStream(ejemplo.owl);
model.read(new InputStreamReader(in), "");
![Page 10: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/10.jpg)
Navegar un modelo
• A partir de la URI de un recurso:
// recuperar el recurso John Smith
String johnSmithURI = "http://somewhere/JohnSmith";
Resource jSmith = model.getResource(johnSmithURI);
// recuperar el valor de la propiedad N
Resource name = (Resource) jSmith.getProperty(VCARD.N)
.getObject();
// recuperar el valor de la propiedad FN
String fullName = (String) jSmith.getProperty(VCARD.FN)
.getObject();
![Page 11: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/11.jpg)
Consultar un modelo
• Buscar información en un modelo:
// recuperar todos los recursos de tipo vcard (asumiendo que
// solo ellos tienen la propiedad FN y que todos la tienen)
ResIterator it = model.listSubjectsWithProperty(VCARD.FN);
while (it.hasNext()) {
Resource r = it.nextResource();
System.out.println(r);
}
• Consultas más avanzadas:– Utilizar la primitiva listStatements(Selector s).– Utilizar el lenguaje de consulta RDQL.
![Page 12: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/12.jpg)
Operaciones sobre modelos
• Los modelos son conjuntos de statements.
• Podemos realizar las siguientes operaciones:– Unión.– Intersección.– Diferencia.
// leer los modelos de los archivos RDF
model1.read(new InputStreamReader(in1), "");
model2.read(new InputStreamReader(in2), "");
// unir los modelos
Model model = model1.union(model2);
![Page 13: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/13.jpg)
API de Ontologías de Jena
• Soporta RDF Schema, DAML y OWL.• Es independiente del lenguaje.• Los recursos no están ligados estáticamente
a una clase java particular.– Ejemplo:
<owl:Class rdf:ID="DigitalCamera"></owl:Class>
<owl:Class rdf:ID="DigitalCamera"> <rdf:type owl:Restriction /></owl:Class>
![Page 14: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/14.jpg)
API de Ontologías de Jena
• Problema:– El recurso no cambió, pero la clase Java para
modelarlo es otra.– No podemos modificar la clase dinamicamente.
• Jena considera que la abstracción Java del recurso es solo una vista del mismo.– Ejemplo:
Resource r = model.getResource(myNS + "DigitalCamera");
OntClass c = (OntClass) r.as(OntClass.class);
Restriction rest = (Restriction) c.as(Restriction.class);
![Page 15: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/15.jpg)
Ejemplo: Ontología de Cámaras
![Page 16: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/16.jpg)
Crear un modelo ontológico
• Se usa el método createOntologyModel().
• Permite especificar:– Lenguaje a utilizar.– Razonador asociado.
String fileName = "c:/ejemplo.owl";
String baseURI = "file:///" + fileName;
OntModel model =
ModelFactory.createOntologyModel(ProfileRegistry.OWL_DL_LANG);
model.read(new FileReader(schemaFileName), baseURI);
![Page 17: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/17.jpg)
Clases
• Son los bloques constructivos básicos.
• Se representan con la interfase OntClass.– Ejemplo: Obtener las subclases de Camera.
<owl:Class rdf:ID="Camera">
<rdfs:subClassOf rdf:resource="#PurchaseableItem"/>
</owl:Class>
OntClass camera = model.getOntClass(camNS + "Camera");
for (Iterator i = camera.listSubClasses(); i.hasNext();) {
OntClass c = (OntClass) i.next();
System.out.println(c.getLocalName());
}
![Page 18: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/18.jpg)
Propiedades
• Se representan mediante la interfase OntProperty.– Ejemplo: Crear las propiedades part y body.
OntModel m = ModelFactory.createOntologyModel();
OntClass Camera = m.createClass(camNS + "Camera");
OntClass Body = m.createClass(camNS + "Body");
ObjectProperty part = m.createObjectProperty(camNS + "part");
ObjectProperty body = m.createObjectProperty(camNS + "body");
body.addSuperProperty(part);
body.addDomain(Camera);
body.addRange(Body);
![Page 19: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/19.jpg)
Propiedades
• DatatypeProperties• ObjectProperties
– FunctionalProperty– TransitiveProperty– SymmetricProperty– InverseFunctionalProperty
• Proporciona métodos isXXX para testear el tipo de una propiedad:– Ejemplo: isTransitiveProperty()
![Page 20: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/20.jpg)
Clases complejas
• Se pueden definir clases mediante operaciones de unión, intersección y complemento.
• Ejemplo:
<owl:Class rdf:ID="SLR"> <owl:intersectionOf rdf:parseType="Collection"> <owl:Class rdf:about="#Camera"/> <owl:Restriction> <owl:onProperty rdf:resource="#viewFinder"/> <owl:hasValue rdf:resource="#ThroughTheLens"/> </owl:Restriction> </owl:intersectionOf></owl:Class>
![Page 21: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/21.jpg)
// crear la instancia throughTheLens
OntClass Window = m.createClass(camNS + "Window");
Individual throughTheLens = m.createIndividual(camNS + "ThroughTheLens", Window);
// crear la propiedad viewfinder
ObjectProperty viewfinder = m.createObjectProperty(camNS + "viewfinder");
// crear la restriccion hasValue
HasValueRestriction viewThroughLens =
m.createHasValueRestriction(null, viewfinder, throughTheLens);
// crear la clase Camera
OntClass Camera = m.createClass(camNS + "Camera");
// crear la interseccion para definir la clase SLR
IntersectionClass SLR = m.createIntersectionClass(camNS + "SLR",
m.createList(new RDFNode[] {viewThroughLens, Camera}));
![Page 22: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/22.jpg)
Schema vs Instancia• Schema
– Se puede definir las:• Clases• Propiedades (DataTypeProperty, ObjectProperty)• Restricciones
– Tipos de Datos– Cardinalidad
• Instancia– Definimos la instancias (individuos) de los
elementos del Schema.
![Page 23: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/23.jpg)
<owl:Class rdf:ID="Camera">
<rdfs:subClassOf rdf:resource="#Item"/>
</owl:Class>
<owl:DatatypeProperty rdf:ID="name">
<rdfs:domain rdf:resource="#Camera"/>
<rdfs:range rdf:resource=“xsd:string"/>
</owl:DatatypeProperty>
<camera:Camera rdf:ID="camera1">
<camera:name>Kodak</camera:name>
</camera:Camera>
![Page 24: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/24.jpg)
<owl:Class rdf:ID="Camera"> <rdfs:subClassOf rdf:resource="#Item"/> </owl:Class><owl:Class rdf:ID="Lens"> <rdfs:subClassOf rdf:resource="#Item"/></owl:Class> <owl:ObjectProperty rdf:ID="compatibleWith"> <rdfs:domain rdf:resource="#Camera"/> <rdfs:range rdf:resource="#Lens"/> </owl:ObjectProperty><camera:Lens rdf:ID=“lens1“ /><camera:Camera rdf:ID="camera1"> <camera:compatibleWith rdf:resource="#lens1" /> </camera:Camera>
![Page 25: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/25.jpg)
Manejando Instancias
URI = http://test/camera/#Camera
OntClass c = model.getOntClass(URI +#Camera")
OntProperty p = model.getOntProperty(URI +#name")
Individual ind = model.getIndividual(URI +#camera1")
if (ind.hasProperty(p))
Statement st = ind.getProperty(p);
Object l = (Object)st.getObject();
![Page 26: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/26.jpg)
Manejando Instancias
• Otras operaciones:– model.listIndividuals()
• Todas las instancias del Modelo
– individual.hasProperty(Property p,Object o)• Retorna True si el objeto individual tiene la propiedad p con
valor o
– ontClass.listInstances();• Todas las instancias de la clase
![Page 27: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/27.jpg)
Validando OWL
• Validación Básica de OWL– http://owl.bbn.com/validator/– Valida sintaxis no infiere ni razona.
• Para validaciones mas complejas:– Jena 2 Inference Support– Detecta la violación de las restricciones
definidas en el schema por las instancias
![Page 28: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/28.jpg)
Jena Inference Support• Inferir Deducir información adicional
– El código que realiza la tarea de inferir se le llama razonador (Reasoner)
– Jena incluye un conjunto básico de razonadores• OWL Reasoner• DAML Reasoner• RDF Rule Reasoner• Generic Rule Reasoner
– Da un mecanismo para incluir nuevos razonadores
![Page 29: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/29.jpg)
Validación en JenaModel schema = ModelLoader.loadModel("file:c:/Schema.owl");Model data = ModelLoader.loadModel("file:c:/ejemplo.owl");Reasoner reasoner = ReasonerRegistry.getOWLReasoner();reasoner = reasoner.bindSchema(schema);InfModel modelInf = ModelFactory.createInfModel(reasoner, data);
ValidityReport vrp1 = modelInf.validate();if (vrp1.isValid()){
System.out.println(“OWL VALIDO");}else {
System.out.println(“OWL NO VALIDO");for (Iterator i = vrp1.getReports(); i.hasNext();){
System.out.println(" - " + i.next()); }
![Page 30: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/30.jpg)
<camera:Camera rdf:ID="camera1"><camera:name>KODAK</camera:name>
</camera:Camera><owl:DatatypeProperty rdf:ID="name"> <rdfs:domain rdf:resource="#Camera"/>
<rdfs:range rdf:resource=“xsd:integer"/></owl:DatatypeProperty>
Error (range check): Incorrectly typed literal due to range (prop, value)Culprit = http://www.xfront.com/owl/ontologies/camera/#camera1Implicated node: http://www.xfront.com/owl/ontologies/camera/#nameImplicated node: 'KODAK‘
![Page 31: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/31.jpg)
<owl:Class rdf:ID="Camera"><rdfs:subClassOf> <owl:Restriction> <owl:onProperty rdf:resource="#name" /> <owl:maxCardinality
rdf:datatype=“xsd:nonNegativeInteger">1</owl:maxCardinality> </owl:Restriction> </rdfs:subClassOf></owl:Class><camera:Camera rdf:ID="camera1">
<camera:name>KODAK</camera:name><camera:name>OLIMPUS</camera:name>
</camera:Camera>
Error (too many values): Too many values on max-N property (prop, class)Culprit = http://www.xfront.com/owl/ontologies/camera/#camera1Implicated node: http://www.xfront.com/owl/ontologies/camera/#nameImplicated node: http://www.xfront.com/owl/ontologies/camera/#Camera
![Page 32: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/32.jpg)
Ejemplo: Ontología de Cámaras
![Page 33: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/33.jpg)
<camera:Camera rdf:ID="camera1">
<camera:name>KODAK</camera:name>
</camera:Camera>
<camera:Lens rdf:ID=“lens1">
<camera:name>Lente_1</camera:name>
</camera:Lens>
<owl:DatatypeProperty rdf:ID="name">
<rdfs:domain rdf:resource="#Camera"/>
<rdfs:range rdf:resource=“xsd:integer"/>
</owl:DatatypeProperty>
![Page 34: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/34.jpg)
• Paradigma Orientado a Objetos rdfs:domain significaría “this is the class of objects to which this property can be applied.”
• rdfs:domain no describe que es legal. El statement <P rdfs:domain C> significa “all things to which P is applied can be inferred to have class C.”
![Page 35: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/35.jpg)
Jena Inference Support• Inferir Deducir información adicional
– El código que realiza la tarea de inferir se le llama razonador (Reasoner)
– Jena incluye un conjunto básico de razonadores• OWL Reasoner• DAML Reasoner• RDF Rule Reasoner• Generic Rule Reasoner
– Da un mecanismo para incluir nuevos razonadores
![Page 36: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/36.jpg)
Jena Inference Support• Para hacer inferencias debemos crear un Modelo Inferido a
partir de un razonador
• Ej:Reasoner reasoner = ReasonerRegistry.getOWLReasoner();reasoner = reasoner.bindSchema(schema);InfModel modelInf = ModelFactory.createInfModel(reasoner, data);
• Todas las consultas que le hacemos al modelo inferido, infiere información
![Page 37: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/37.jpg)
Jena Inference Support
OWL FB REASONEROWL FB
REASONER
GENERIC RULE REASONER
GENERIC RULE REASONER
![Page 38: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/38.jpg)
Jena Inference Support• Generic Rule Reasoner:
– Inferencia basada en Reglas.
• Cada regla es una lista de Premisas y Conclusiones
–T1,T2 ………., TN TO
»T = triple patterns como (?x rdf:type ?C)
–Si T1…….TN “matchea” en el conjunto de datos entonces T0 puede ser deducido
![Page 39: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/39.jpg)
Jena Inference Support• SubClass
(?A rdfs:subClassOf ?B)
(?B rdfs:subClassOf ?C)
(?A rdfs:subClassOf ?C)
![Page 40: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/40.jpg)
Jena Inference Support• Generic Rule Reasoner:
– Dos Motores de Inferencia• Forward Chaining engine
– Se chequea cada una de las reglas para ver si los datos satisfacen las premisas de alguna de las reglas.
– Si una regla es satisfecha, es ejecutada derivando nuevos hechos que pueden ser utilizados por otras reglas para derivar hechos adicionales.
• Backward Chaining engine– parte de los objetivos y trata de cumplir las condiciones
necesarias para llegar a ellos. – Cuando el Modelo Inferido es consultado, transforma la
consulta en un Objetivo– Ejecución similar a los motores PROLOG
![Page 41: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/41.jpg)
objetivo: A is C ?datos: A is B
B is Creglas :is2 = (?A p ?B) (?A is ?C)is1 = (?A is ?B) (?B is ?C) (?A is ?C)
• Forward Chaining engineAplica la regla is2 y no llega a cumplir el objetivoAplica la regla is1 y llega a cumplir el objetivo
• Backward Chaining engineel objetivo se “matchea” con el objetivo de is2 , luego a partir de los hechos no se cumple las premisasel objetivo se “matchea” con el objetivo de is1 , luego a partir de los hechos se cumple las premisas
![Page 42: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/42.jpg)
Jena Inference Support• Generic Rule Reasoner:
– Se puede usar los dos motores a la vez
– Ejemplo:(?p rdfs:subPropertyOf ?q),notEqual(?p,?q)
[ (?a ?q ?b) (?a ?p ?b) ]
![Page 43: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/43.jpg)
Jena Inference Support• Como invocar al Generic Rule Reasoner:
//Creo el String de las reglas
String ruleSrc = “[rule1: (?a eg:p ?b) (?b eg:p ?c)
(?a eg:p ?c)]”;//Parseo el String de las reglas
List rules = Rule.parseRules(ruleSrc);//Construyo el Razonador
Reasoner reasoner = new GenericRuleReasoner(rules);
![Page 44: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/44.jpg)
Jena Inference Support• Archivos de reglas para los razonadores predefinidos
DIR_INSTALACION\Jena-2.1\etc
• Ejemplo : owl-fb.rules
![Page 45: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/45.jpg)
RDQL
• RDF Data Query Language.• Es un lenguaje de consultas para RDF.• Enfoque totalmente declarativo.• Considera un modelo RDF como un
conjunto de tripletas:– (Sujeto Propiedad Valor)
• Permite especificar patrones que son matcheados contra las tripletas del modelo para retornar un resultado.
![Page 46: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/46.jpg)
Ejemplo
![Page 47: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/47.jpg)
Ejemplo
• q1 contiene la consulta:
SELECT ?x
WHERE (?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith")
• Para ejecutar q1 sobre el modelo m1.rdf:
java jena.rdfquery --data m1.rdf --query q1
• La salida es:
x
=============================
<http://somewhere/JohnSmith/>
![Page 48: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/48.jpg)
Ejemplo
• Retornar todos los recursos que tienen la propiedad FN y su valor:
SELECT ?x, ?fname
WHERE (?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname)
• La salida es:
x | fname
================================================
<http://somewhere/JohnSmith/> | "John Smith"
<http://somewhere/SarahJones/> | "Sarah Jones"
<http://somewhere/MattJones/> | "Matt Jones"
![Page 49: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/49.jpg)
Ejemplo
• Retornar los nombres de los Jones:
SELECT ?givenName
WHERE (?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Jones"),
(?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName)
• La salida es:
givenName
=========
"Matthew"
"Sarah"
![Page 50: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/50.jpg)
Uso desde Java
• Se pueden realizar consultas en RDQL desde una aplicación Java.
• Se usan las siguientes clases:– Query– QueryExecution– QueryEngine– QueryResults– ResultBinding
![Page 51: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/51.jpg)
EjemploSELECT ?x, ?fname
WHERE (?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?fname)
Query query = new Query("SELECT...") ;
query.setSource(model);
QueryExecution qe = new QueryEngine(query) ;
QueryResults results = qe.exec();
for (Iterator iter = results; iter.hasNext();)
{
ResultBinding res = (ResultBinding) iter.next();
Resource x = (Resource) res.get("x");
Literal fname = (Literal) res.get("fname");
System.out.println("x: " + x + " fname: " + fname);
}
![Page 52: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/52.jpg)
Modelos Persistentes
• Jena permite crear modelos persistentes:– Son persistidos de forma transparente en una base
de datos relacional.
• Jena 2 soporta:– MySQL– Oracle– PostgreSQL
• Para crear un modelo persistente se usa:– ModelFactory.createModelRDBMaker(conn).createModel()
![Page 53: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/53.jpg)
Ejemplo// Crear la conexión a la BD
DBConnection c = new DBConnection(DB_URL, DB_USER, DB_PASS, DB_TYPE);
// Crear un ModelMaker para modelos persistentes
ModelMaker maker = ModelFactory.createModelRDBMaker(c);
// Crear un nuevo modelo
Model model = maker.createModel("modelo_1");
// Comenzar una transacción
model.begin();
// Leer el modelo de un archivo XML
model.read(in, null);
// Hacer el commit de la transacción
model.commit();
![Page 54: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/54.jpg)
Referencias
Sitio Oficial de Jena 2
http://jena.sourceforge.net/
Jena 2 Ontology API
http://jena.sourceforge.net/ontology/
An Introduction to RDF and the Jena RDF API
http://jena.sourceforge.net/tutorial/RDF_API/
![Page 55: Tutorial de JENA Santiago Atella Nicolás Genta Diego González INCO - FING - UDELAR](https://reader033.vdocuments.site/reader033/viewer/2022061300/54cfe10b49795990548b4ef0/html5/thumbnails/55.jpg)
Referencias
A Programmer's Introduction to RDQL
http://jena.sourceforge.net/tutorial/RDQL/
Jena2 Database Interface
http://jena.sourceforge.net/DB/
Jena 2 Inference support
http://jena.sourceforge.net/inference/