Saturday, July 10, 2010

An Introduction to Semantic Web (I)

Introduction to Semantic Web (Part I)

(Based on the book Programming the Semantic Web. from O'Reilly)

What is the Semantic Web?

Semantic refers basically to the meaning of anything tha is under evaluation. In few words something has semantic content if it has a meaning within the context it is been evaluated.

For example: “34” Doesn’t have any real meaning at all. Just a number
On the other hand “34 celcius degrees”. Has a meaning tha anybody can understand (it’s hot)

Semantic Web tries to integrate semantic expressions to the Web. Giving meaning to the data on it, and not just showing meaningless raw data. In this way, knowledge instead of data can be exposed and shared between machines with humans or other machines.

Knowledge representation.

To represent knowledge instead of data, this data must be delevered with it’s meaning. The meaning of the data is actually data about the data, which is known as metadata.
So to represent the knowledge on the Web we must provide the descriptive metadata along with the actual data. This is important to understand: Unlike a database (for example) where the meaning of the data comes somehow in the table and column names, in our representations the metadata goes hand in hand with the actual data.

The basic constructor for the representation of semantic data is the Triple. A Triple is nothing more than a languaje construction of the form (subject, predicate, object). Where “subject” is normally an entity, “predicate” is an attribute or characteristic applied to the subject over the object, which can be an entity with further relations, or literal final value, like a String.

For example: Aragorn son_of Arathorn

With this simple representation we can describe infinite relations and meaningful semantic attributes on infinite entitites originating a complex directed graph of semantic relationships between nodes.

The idea is to represent everything we can as Triples, and save them to a Triple Store. There are many of this triplestores on line, but we’ll see this in the next article.


Example of a mini triple store for our Lord of The Rings Triples:                                                                                                                                                                                                     
SubjectPredicateObject
Aragornson_ofArathorn
Frodobearer_ofAnillo
Samfriend_ofFrodo
Frodois_aHobbit
Aragorntakes_care_ofFrodo
Pippinfriend_ofFrodo


As you can see, the idea is to define a standard language for a particular domain and make it easy to extend and share. The structure itself of the Triple Store makes it easy to extend (By adding new Triples) and the sharing comes from standarizing the language used for the particular domain (which we’ll see in the next article). This sharing and extending is the fundamental to the Semantic Web ideas.

As we mentioned before, to represent semantic data and relationships we use directed graphs. For the Triple Store from before, the graph would be something like:

This kind of structure allows us to give answers to questions like the following, in natural language and in Triple mode.

Natural LanguageTripleMeaning



¿who takes care of Frodo?? takes_care_of FrodoReturns all Triples that make the sentence true. In our case
Aragorn takes_care_of Frodo
Who is a hobbit and a ring bearer??a is_a Hobbit
?a bearer_of Anillo
Like the previous case, but this time with a binding variable which must make both sentences true.
In our case this is true just for Frodo.
Frodo es_un Hobbit
Frodo portador_de Anillo
Who is Sam friend of and also a hobbit?
Sam friend_of ?a
a? is_a hobbit
Like in the previous case but with the binding variable in different positions.
Sam friend_of Frodo
Frodo is_a hobbit
Who is friend of Frodo?? friend_of FrodoTriple with multiple results.
Sam friend_of Frodo
Pippin friend_of Frodo



Infering new Triples

From the data and asserts existent in our triplestore, as from our knowledge of the specific domain, we can build rules that help us create new knowledge in our store.

For example. We can define a Rule saying “If X is friend of Frodo the X is friend of Sam”. By implementing this inference rule, we will generate new Triples of knowledge in our store.


Graph merging and linked data

Other option all this alloes, is graph merging. For example, let’s suppose we find somewhere a Triplestore refering to “The Hobbit” novel. It could be merged with our datastore and create more knowledge in our store:

For example. Our Triplestore

Frodo nephew_of Bilbo

The Hobbit

Bilbo saves dwarfes

By relating both Bilbos, we can add this new knowledge to our Triplestore.

This introduction was focused on the description of how the Semantic Web essentials works. In the next article we’ll see the standards languages and tools that make use of this concepts to deliver content and share it  on the Web.

Sharing will be machine-human and most importantly machine-machine.

We’ll see public Triplestores and how to merge them, query them, make inferences out of them and more.

Carlo


Monday, July 5, 2010

Introduction to semantic Web from a p...

Introduccion a la Red Semantica desde el punto de vista de un programador. (Parte I)

(Basado en el libro Programming the Semantic Web. de O'Reilly)

¿Que es la Red Semantica?


Semantica se refiere basicamente al significado de un elemento cualquiera. En pocas palabras algo tiene contenido semantico, si tiene significado dentro del contexto especifico en el que se está evaluando.

Por ejemplo "34". No tiene ningun significado mas que un numero.

Sin embargo "34 grados celcius de temperatura". Tiene un significado que cualquier persona puede entender e interpretar facilmente. (Hace calor).

La Red Semantica, intenta integrar las expresiones semanticas a la Web. Dando significado a los datos que se encuentran en ella, y no unicamente presentando dichos datos sin significado. Permitiendo de esta forma representar conocimiento en vez de datos crudos y compartirlos entre maquinas.

Representacion del conocimiento

Para representar conocimiento y no datos, es necesario dar significado a estos datos. Información acerca de los datos es lo que conocemos como Metadatos. Por lo tanto para representar el conocimiento en la Red, debemos proveer los datos que forman el conocimiento, y los metadatos que describen estos datos. Es importante entender que los metadatos van explicitamente acompañando a los datos. es Decir, la semantica acompaña al dato.

El constructor básico para la representación del conocimiento es el "tripleta". Un tripleta no es mas que una construcción de la forma: sujeto, predicado, objeto. Donde sujeto es normalmente una entidad, predicado es un atributo o caracteristica sobre esa entidad, y el objeto, puede ser o una entidad, que puede relacionarse con otras, o un valor literal final, como una cadena o un numero.

Por ejemplo Aragorn hijo_de Arathorn

Con esta simple representación podemos describir infinitas relaciones y atributos semanticos sobre distintas entidades, originando un complejo Grafo dirigido de relaciones semanticas. Todos las "tripletas" que se generan se guardan en una estructura conocida como el Almacenamiento de tripletas.

Ejemplo:

                                                                                                                                                                                                                                
Sujeto
Predicado
Objeto
Aragorn
hijo_de
Arathorn
Frodo
portador_de
Anillo
Sam
amigo_de
Frodo
Frodo
es_un
Hobbit
Aragorn
cuida_de
Frodo
Pippin
amigo_de
Frodo


Como se puede observar en el ejemplo anterior, la idea es definir un lenguaje estandar para un dominio particular, que como queda demostrado por el propio ejemplo, es muy facil de extender y compartir, lo cual es la escencia de la Red Semantica.
Facil de Extender porque para añadir nuevas relaciones solo es necesario añadir un tripleta mas al almacen.
Facil de compartir porque es un lenguaje sencillo que se estandariza alrededor de un dominio y que puede ser utilizado por todos para referirse a ese dominio.

Como mencionamos antes, la representación de los datos semanticos, se da mediante el uso de grafos dirigidos. El grafo del ejemplo anterior seria asi:


Esta estructura nos permitiria responder a los siguientes problemas planteados en lenguaje natural y a modo de tripletas:

Lenguaje natural
tripletas
Significado_de_las_tripletas______________________________________________



¿Quien cuida de Frodo?
? cuida_de Frodo
Nos devuelve todas las tripletas que coincidan con el predicado. En este caso Aragorn cuida_de Frodo
¿Quien es un Hobbit y porta el anillo?
?a es_un Hobbit
?a portador_de Anillo
Como el caso anterior. Pero esta vez con una variable de enlace que debe cumplir los 2 predicados. En este caso solo Frodo lo cumple:
Frodo es_un Hobbit
Frodo portador_de Anillo

¿De quien es Sam amigo, que ademas es un Hobbit?
Sam amigo_de ?a
a? portador_de anillo
Igual que el caso anterior pero con las variables de enlace cambiadas de posición
Sam amigo_de Frodo
Frodo portador_de Anillo

¿Quien es Amigo de Frodo?
? amigo_de Frodo
tripleta con multiples resultados posibles que satisfacen la "ecuacion"
Sam amigo_de Frodo
Pippin amigo_de Frodo


Inferencia de nuevas tripletas

A partir de los datos, y las afirmaciones planteados en nuestra base asi como de nuestro conocimiento del mundo y del dominio en particular, podemos crear reglas que nos ayuden a crear nuevo conocimiento en nuestro almacen.

Por ejemplo. Podemos definir una Regla diciendo que "Si X es amigo de Frodo, entonces X es amigo de SAM". Al implementar esta regla de inferencia, se generarán nuevas tripletas por cada tripleta de los amigos de Frodo.

Combinacion de Grafos

Otra opción que nos permite este enfoque, es la combinación de Grafos. Por ejemplo, supongamos que nos llega un conjunto de tripletas referentes a la novela "El Hobbit". Se podría facilmente combinar con nuestro almacen de tripletas y hacer nuestra base de conocimiento mas grande y completa.

Ejemplo En nuestro Grafo podriamos tener:

Frodo sobrino_de Bilbo

En el Hobbit:

Bilbo salva_a Enanos

Al tener relación los identificadores "Bilbo", podemos hacer este ultimo tripleta parte de nuestra base de conocimientos. Combinado así los grafos.


Desarrollaremos nuestro propio Almacen de tripletas en Groovy para ver como almacenamos y accedemos a este conocimiento. Guardaremos las tripletas en memoria en esta implementación. y utilizaremos Groovy para el desarrollo. Crearemos El sistema para hacer Queries a los datos, El sistema para crear los enlaces entre variables y la creación de Reglas de Inferencia.

API del triplestore

//Añadir Triple al Almacen de Triples

void add(subject,predicate,object)

// Hace una query pasando una serie de clausulas al metodo de la forma "?alguien hijo_de Arathorn y ?alguien amigo_de Frodo",  y retorna las tripletas que hagan verdad todos los predicados en conjunto.

def query(clauses)

// Añadir una clase que implemente una regla de inferencia.

void add(inferenceRule)

//Ejecutar las reglas de inferencia y cargar las nuevos tripletas:

void infereTriples()


El codigo Fuente en Groovy estará disponible en http://code.google.com/p/groovysemantictriplestore/

Hemos visto una implementación Rudimentaria hecha a mano de un Almacen de tripletas en Groovy, que nos muestra la esencia del funcionamiento de la RED semantica. Sin embargo, es una solución completamente personal y no distribuible. En la siguiente entrega veremos como el uso de Estandars nos permite utilizar el conocimiento adquirido para crear almacenes de datos semanticos que nos permitan compartirlos con otras personas y mas importante aun, con maquinas.

La siguiente entrega por tanto estará orientada a la estandarización, y como hacer compartible nuestro conocimiento de un dominio por Internet. Veremos como combinar grafos existentes y muchas otras cosas relacionadas con las herramientas y tecnologías que hay a nuestra disposición para el manejo de datos semanticos.




Tuesday, April 27, 2010

Presentación TDD

Hola,

El viernes pasado hice una presentación de Introducción a TDD de la mano de mi empresa Paradigma Tecnologico.

La grabación de la presentación esta en la siguiente URL

Parte 1: http://www.parleys.com/#st=5&id=1932

La presentación con el codigo fuente utilizado la encontrarán en esta URL:

http://code.google.com/p/presentacion-tdd/

Un Saludo

Wednesday, February 10, 2010

Usefull commands

In this post i'll put all Super usefull commands that i use in my daily work. This is an incremental post. Whenever i find i'm using a usefull command i'll put it here.


Delete CLRF (Windows Carriage Return and new Line) from a file to be usable in Linux

tr -d '\r' < in_file > out_file

Command to delete all .svn files recursively

find . -name .svn | gawk '{print "rm -rf " $0}' | bash

SQL command to disable all FK constraints

select 'alter table ' || table_name ||' disable constraint ' || constraint_name || ';' from (select constraint_name,table_name from user_constraints where constraint_name like 'FK_%')

Capturing Network Traffic

tcpdump -i eth0 -w /tmp/xxx.dmp -s 1500

The file xxx.dmp can then be open with wireshark.



The following is not mine. I found it on the WEB

Adding svn files recursively

svn status | grep "^\?" | awk '{print $2}' | xargs svn add


disable constraints that references a specific table or tables

select 'alter table '||a.owner||'.'||a.table_name||
' disable constraint '||a.constraint_name||';'
from user_constraints a, user_constraints b
where a.constraint_type = 'R'
and a.r_constraint_name = b.constraint_name
and a.r_owner = b.owner
and b.table_name like 'DEYDE%';


get all open ports and processes associated to them:

sudo lsof -i -P


Get the Certificate from a SSL HTTPS site

openssl s_client -showcerts -connect HOST:443 >/tmp/ukash.cert

Importing the certificate to java

keytool -import -alias joe -file server.crt -keystore /home/user/jdk1.5.0_06/jre/lib/security/cacerts

Remember the keystore password is by default "changeit"


To look at what process is using a particular port, you just run the following

lsof -i tcp:8443

To setup a git daemon on your local repo. Execute this from the root of the your project repo (or a folder above it)

git daemon --reuseaddr --base-path=. --export-all --verbose --enable=receive-pack

Then from another machine or the same you can clone it:

git clone git://localhost:9418/ something

Obtain a access token for Github API Oauth (using client_id and client_secret of a registered application): curl -i -X POST https://api.github.com/authorizations -d '{"client_id":"xxxxxx", "client_secret":"xxxxxx"}' -u calo81 Git grep and replace words in your git repo: git grep -l 'original_text' | xargs sed -i '' 's/original_text/new_text/g' do a find excluding some file extensions find /logs/xxx*/home/some/app/shared/log/log/yyy*log* ! -iname "*.gz" Remove a file from the whole git history of your repo: git filter-branch --index-filter 'git update-index --remove postcodes.csv'

Tuesday, October 13, 2009

Struts 2 cookie interceptor

A few weeks ago i developed a Struts2 cookie interceptor plugin.

I just upload it to http://code.google.com/p/struts-cookie-interceptor/

The interceptor is a Bidirectional interceptor (In Out interceptor) that works the following way.

in your action you annotate a property declaration like this:

@Cookie("cookie")
private String valor;
.. getter...setter

Then, the interceptor will inject the value of the "cookie" cookie to the annotated property. If the value of the property "valor" is changed in the action, this value, will be written to the cookie on the return of the action automatically. this is way i say it is bidirectional.

The annotation takes as values, the name of the cookie, the path, and the time to live attributes.

For full detail, feel free to download the source code.

Bye

Tuesday, September 1, 2009

Spring Security 2 Explained. Part 1. The filters.

The following article will try to explain Spring security with detail.

The vast majority of Spring Security articles and books, explain the framework making emphasis in the easy of configuration and use. Which is great for almost everything. However to really understand what is going on, i need to go a little deeper and study the elements tha actually participate on the framework.

I will make a very simple application and try to explain what happens in Spring Security through the different paths of executions i do.

The first thing to do is to add the security filter and context configuration to your web.xml file.

This is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>articleSpringSecurity</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/security.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Here, the Delegating Filter Proxy is the entry point into Spring Security. It's actually not a Spring Security Filter, but a Spring Web Filter, is just that in this case we are using it to delegate to the security filters we will define in the Spring Application Context “security.xml”

Defining the simplest Spring Security Configuration File:

security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>

<authentication-provider>
<user-service>
<user name="carlo" password="carlo" authorities="ROLE_USER" />
</user-service>
</authentication-provider>

</beans:beans>

This file will include Spring security in our Application.

Let's explain what is on this file, by accessing our application. Pointing to the base path of our web application automatically shows us a login form. How does this happen?.


The XML element http with auto-config= “true” defines the following:

First of all Spring will automatically register the following servlet filters:

HttpSessionContextIntegrationFilter
LogoutFilter
AuthenticationProcessingFilter
DefaultLoginPageGeneratingFilter
BasicProcessingFilter
SecurityContextHolderAwareRequestFilter
RememberMeProcessingFilter
AnonymousProcessingFilter
ExceptionTranslationFilter
SessionFixationProtectionFilter
FilterSecurityInterceptor


These filters execute from top to bottom and carry the following resposibilities:


HttpSessionContextIntegrationFilter job is to create a default SecurityContext. It tries to read the context from the Session, and if there isn't any, it creates a new Instance of SecurityContextImpl and put it in the ThreadLocal SecurityContextHolder. The it passes Control to the next filter in the Chain.
After returning from the chain, this filter consults the SecurityContextHolder for the SecurityContext in it to see if it has changed during request processing.
I SecurityContext has changed and authentication is not anonymous, then the new SecurityContext is stored in session.


LogoutFilter. This filter consults the URL invoked to see if it is a URL that requires logout. The default Logout Requring URL is j_spring_security_logout. So if you invoke this URL in your application you are explicitly asking to logout from the application. If the URL is for logut, the This filter delegates in handlers the actual logout process. By default it delegates to SecurityContextLogoutHandler and TokenBasedRememberMeServices. The first Handler invalidates the Session, and the second handler, deletes the remember me cookie (set age to 0)


AuthenticationProcessingFilter
. This is one of the most important filters. Its job is the following. First it it checks if the Invocation URL is for a login petition, it does this by consulting if the URL matches the default /j_spring_security_check URL.
If the previous step requirement is met, then this filter looks in the request parameters for a Username and a password (in the request parameters j_username and j_password respectively). Then it creates an UsernamePasswordAuthenticationToken which is an Implementation of an Authentication Class, so it set the authentication Principal to the username and the Authentication credentials to the passoword provided.
Then it puts the username in the session under the name SPRING_SECURITY_LAST_USERNAME. The next important Step is to call the AuthenticationManager's authenticate method with our newly created Authentication Object.
From the preceding paragraph we can see that the first time we access the application home, the filter will stop it's processing in the point where it checks if the invocation URL is an authentication petition. The home page is not the URL for authentication (which we now know is j_spring_security_check) so this filter won't do anything (right now) but to continue to the next filter.

DefaultLoginPageGeneratingFilter
. This Filter is almost explained by its name. It's function is to generate a default login page when required. With "when required" I mean when the URL invoked corresponds to the default URL for login page which is /spring_security_login. When this filter detects that this URL is being invoked it renders a default login page. When we first invoke the application home we are not invoking this URL. So why does it show the login Page. We'll see why soon enough.


BasicProcessingFilter. This filter's job is to check if the request has standard http authentication headers. If it does, it creates a new Authentication (UsernamePasswordAuthenticationToken) object from it, and like the AuthenticationProcessingFilter, it delegates to the AuthenticationManager's authenticate method the authentication job. If authentication fails, the filters sets the SecurityContext to null.

SecurityContextHolderAwareRequestFilter. This filter wraps the Servlet request in a SavedRequestAwareWrapper. This wrapper somehow maintains a saved request, but I don't know what this wrapper exactly brings to the table.


RememberMeProcessingFilter. This filter's job is to query the security context to see if there is an Authentication Object on it, if not, it will create (or try to) a new one, by calling the autoLogin method of AbstractRememberMeServices which will retrieve a Remember Me cookie and try to authenticate using it.
The cookie's default name is SPRING_SECURITY_REMEMBER_ME_COOKIE. If the filter finds this cookie, it decodes it, make some validations and call the userDetailsService with the cookie information. If the userDetails returned is valid, The RememberMeServices creates a new RememberMeAuthenticationToken that return to the filter and the filter saves it into the SecurityContext.

AnonymousProcessingFilter
. This simple filter, detects if there already is an authentication object in the security context. if there is not any, it creates a new one (AnonymousAuthenticationToken) with the username "guest" and the rol "ROLE_ANONYMOUS" (this names can be overriden).


ExceptionTranslationFilter. This filter is in charge of Capturing and initiating the handling of any security exceptions that may come from the application (AccessDeniedException,AuthenticationException). The filter serves as a translator between the Java Exceptions and the Http Errors. If the filter detects an authentication Exception (Basically No Authentication Object) it initiates the authentication mechanism by invoking the authentication entry point to show the login page. If the exception is a AccessDeniedException, the filter first checks if the authenticated user is Anonymous. If it is, it follows the same path as if there wasn't any authentication at all. If it isn't anonymous, it basically means that a USer with some privileges is trying to access resources that he can't. So an access denied exception is returned to the browser

SessionFixationProtectionFilter. This filter checks if there is a user authenticated but there is not a Session attribute associated with the securityContext. This can happen if the user is not authenticated from the start of the request but is authenticated during the duration of the request thread. If this is the case, the filter creates a new session with the information required.

FilterSecurityInterceptor. This is the Filter version of the AbstractSecurityInterceptor. it delegates to it's super class (the mentioned AbstractSecurityInterceptor) the authorization to access the required resource. If it can not access the resource the AbstractSecurityInterceptor will throw an accessDeniedException. We'll cover the AbstractSecurityInterceptor in detail in the next par of this Article.

Sunday, August 23, 2009

Running Java App in Debugging Mode

Without an IDE, to Run An Application In Debug Mode you do the following

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=11550,suspend=n "your-program"

The Application "your-program" Starts and listens for connections on the 11550 port for debugging.