110
jCOLIBRI2 Tutorial Juan A. Recio-García Belén Díaz-Agudo Pedro González-Calero September 16, 2008 Document version 1.2 GROUP FOR ARTIFICIAL INTELLIGENCE APPLICATIONS UNIVERSIDAD COMPLUTENSE DE MADRID

Tutorial

Embed Size (px)

DESCRIPTION

Jcolibri

Citation preview

  • jCOLIBRI2 Tutorial

    Juan A. Recio-Garca

    Beln Daz-Agudo

    Pedro Gonzlez-Calero

    September 16, 2008Document version 1.2

    GROUP FOR ARTIFICIAL INTELLIGENCE APPLICATIONSUNIVERSIDAD COMPLUTENSE DE MADRID

  • This work is supported by the MID-CBR project of the Spanish Ministry of Education& Science TIN2006-15140-C03-02 and the G.D. of Universities and Research of theCommunity of Madrid (UCM-CAM-910494 research group grant).

    jCOLIBRI2 TutorialJuan A. Recio-GarcaBeln Daz-AgudoPedro Gonzlez Calero

    Technical Report IT/2007/02Department of Software Engineering and Artificial Intelligence.University Complutense of MadridISBN: 978-84-691-6204-0

  • Contents 3

    Contents

    1 Introduction 8

    2 What is Case-Based Reasoning? 92.1 The CBR cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    3 The jCOLIBRI CBR framework 113.1 jCOLIBRI2 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 11

    4 Getting started with jCOLIBRI2 14

    5 The Travel Recommender CBR application 17

    6 Importing jCOLIBRI2 into Eclipse 246.1 Preparing the Travel Recommender files . . . . . . . . . . . . . . . . . 25

    7 Creating the CBR application 28

    8 The Travel Recommender Case Base 33

    9 Representing the Cases 359.1 Case Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359.2 Cases and Queries in jCOLIBRI2 . . . . . . . . . . . . . . . . . . . . . 369.3 Case Components of the Travel Recommender . . . . . . . . . . . . . . 399.4 Defining new attribute types . . . . . . . . . . . . . . . . . . . . . . . 41

    10 The two layers persistence architecture of jCOLIBRI2 4210.1 The Connectors of jCOLIBRI2 . . . . . . . . . . . . . . . . . . . . . . 4210.2 In-memory organization of the cases . . . . . . . . . . . . . . . . . . . 44

    11 Loading the Case Base 4611.1 Configuring the connector . . . . . . . . . . . . . . . . . . . . . . . . 47

    11.1.1 The Hibernate configuration file . . . . . . . . . . . . . . . . . 4811.1.2 Creating the mapping files . . . . . . . . . . . . . . . . . . . . 50

    12 Using Ontologies in CBR applications 5412.1 Case Base persistence in ontologies . . . . . . . . . . . . . . . . . . . 5512.2 Computing similarities using ontologies . . . . . . . . . . . . . . . . . 5512.3 Case and Query vocabulary . . . . . . . . . . . . . . . . . . . . . . . . 5612.4 Using an ontology in the Travel Recommender . . . . . . . . . . . . . 58

    13 Retrieval 6113.1 Similarity functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6213.2 Cases selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6313.3 Using the k-NN retrieval . . . . . . . . . . . . . . . . . . . . . . . . . 6413.4 Retrieval in the Travel Recommender application . . . . . . . . . . . . 66

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • Contents 4

    14 Reuse 6714.1 Adapting the Travel Recommender retrieved trips . . . . . . . . . . . . 67

    15 Revise 6915.1 Travel Recommender revision . . . . . . . . . . . . . . . . . . . . . . 69

    16 Retain 7016.1 Saving the new trips of the Travel Recommender . . . . . . . . . . . . 70

    17 Shutting down a CBR application 71

    18 Textual CBR 7218.1 Semantic retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    18.1.1 Representation of the texts . . . . . . . . . . . . . . . . . . . . 7318.1.2 IE methods implementation . . . . . . . . . . . . . . . . . . . 7518.1.3 Computing similarity . . . . . . . . . . . . . . . . . . . . . . . 7618.1.4 The Restaurant Recommender example . . . . . . . . . . . . . 77

    18.2 Statistical retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8118.2.1 The Restaurant Recommender using statistical methods . . . . . 82

    19 Evaluation of CBR applications 83

    20 Recommenders 8620.1 Templates guided design of recommendation systems . . . . . . . . . . 86

    20.1.1 One-Off Preference Elicitation . . . . . . . . . . . . . . . . . . 8720.1.2 Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8920.1.3 Iterated Preference Elicitation . . . . . . . . . . . . . . . . . . 89

    20.2 Methods for recommender systems . . . . . . . . . . . . . . . . . . . . 9020.2.1 User interaction methods . . . . . . . . . . . . . . . . . . . . . 9020.2.2 New Nearest Neighbor similarity measures . . . . . . . . . . . 9120.2.3 Conditional methods . . . . . . . . . . . . . . . . . . . . . . . 9220.2.4 Navigation by Asking methods . . . . . . . . . . . . . . . . . . 9220.2.5 Navigation by Proposing . . . . . . . . . . . . . . . . . . . . . 9320.2.6 Profile management methods . . . . . . . . . . . . . . . . . . . 9420.2.7 Collaborative Recommendations . . . . . . . . . . . . . . . . . 9420.2.8 Retrieval methods . . . . . . . . . . . . . . . . . . . . . . . . 9520.2.9 Cases selection . . . . . . . . . . . . . . . . . . . . . . . . . . 97

    21 Other Features 9921.1 Visualization of a Case Base . . . . . . . . . . . . . . . . . . . . . . . 9921.2 Classification and Maintenance . . . . . . . . . . . . . . . . . . . . . . 99

    22 Getting support 101

    23 Contributing to jCOLIBRI 10223.1 Required elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10223.2 Example applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • Contents 5

    23.3 How to submit a contribution . . . . . . . . . . . . . . . . . . . . . . . 103

    24 Versions ChangeLog 10424.1 Version 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

    24.1.1 Whats new in Version 2.1 . . . . . . . . . . . . . . . . . . . . 10424.2 Version 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • List of Figures 6

    List of Figures1 CBR cycle by Aamodt and Plaza . . . . . . . . . . . . . . . . . . . . . 102 Two layers architecture of jCOLIBRI2 . . . . . . . . . . . . . . . . . . 123 jCOLIBRI2 short-cuts under windows . . . . . . . . . . . . . . . . . . 154 jCOLIBRI2 Tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 jCOLIBRI2 Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Define Query step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Configure Similarity step . . . . . . . . . . . . . . . . . . . . . . . . . 198 Retrieved Cases step . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Adaptation step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2110 Revise Cases step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2211 Retain Cases step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2312 Importing the jCOLIBRI2 project into Eclipse . . . . . . . . . . . . . . 2413 The jCOLIBRI2 project in Eclipse . . . . . . . . . . . . . . . . . . . . . 2514 Files to delete in the project . . . . . . . . . . . . . . . . . . . . . . . . 2615 Project state to begin the tutorial . . . . . . . . . . . . . . . . . . . . . 2716 Cases representation UML diagram . . . . . . . . . . . . . . . . . . . 3917 Case Base management in jCOLIBRI2 . . . . . . . . . . . . . . . . . . 4218 Travel Recommender components mapping . . . . . . . . . . . . . . . 5019 Case mapping in a ontology . . . . . . . . . . . . . . . . . . . . . . . 5520 OntologyConnector behavior example . . . . . . . . . . . . . . . . . . 5621 Concept based similarity functions in jCOLIBRI2 . . . . . . . . . . . 5722 Example of application of the similarity functions . . . . . . . . . . . . 5823 Defining the Travel Recommender query through an ontology . . . . . 5924 Mapping between data base and ontology . . . . . . . . . . . . . . . . 6025 Representation of texts for IE. . . . . . . . . . . . . . . . . . . . . . . 7426 Global view of the representation of texts for IE. . . . . . . . . . . . . . 7527 Common organization of textual cases . . . . . . . . . . . . . . . . . . 7628 Evaluation of a CBR application . . . . . . . . . . . . . . . . . . . . . 8429 Single Shot template . . . . . . . . . . . . . . . . . . . . . . . . . . . 8730 Conversational templates . . . . . . . . . . . . . . . . . . . . . . . . . 8831 Preference Elicitation decompositions . . . . . . . . . . . . . . . . . . 8832 Visualization of a Case Base . . . . . . . . . . . . . . . . . . . . . . . 100

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • Listings 7

    Listings1 StandardCBRApplication interface . . . . . . . . . . . . . . . . . . . . 282 TravelRecommender initial code . . . . . . . . . . . . . . . . . . . . . 293 TravelRecommender singleton . . . . . . . . . . . . . . . . . . . . . . 294 TravelRecommender GUI code . . . . . . . . . . . . . . . . . . . . . . 305 TravelRecommender main() method . . . . . . . . . . . . . . . . . . . 316 Travel Recommender data base schema . . . . . . . . . . . . . . . . . 337 TravelRecommender configure() method (version 1) . . . . . . . . . . . 338 CaseComponent interface . . . . . . . . . . . . . . . . . . . . . . . . . 359 Bean example code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3510 Using Attribute example code . . . . . . . . . . . . . . . . . . . . . . 3611 CBRQuery code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3612 CBRCase code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3713 TravelDescription initial code . . . . . . . . . . . . . . . . . . . . . . . 3914 TravelSolution initial code . . . . . . . . . . . . . . . . . . . . . . . . 4015 TypeAdaptor interface . . . . . . . . . . . . . . . . . . . . . . . . . . 4116 Connector interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4317 CBRCaseBase interface . . . . . . . . . . . . . . . . . . . . . . . . . . 4418 TravelRecommender configure() (version 2) and precycle() . . . . . . . 4619 databaseconfig.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4720 hibernate.cfg.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4821 TravelSolution.hbm.xml . . . . . . . . . . . . . . . . . . . . . . . . . 5022 TravelDescription.hbm.xml . . . . . . . . . . . . . . . . . . . . . . . . 5123 Mapping template for user-defined types . . . . . . . . . . . . . . . . . 5224 Mapping template for enumerates . . . . . . . . . . . . . . . . . . . . 5225 TravelRecommender configure() method (final version) . . . . . . . . . 5826 NNScoringMethod signature . . . . . . . . . . . . . . . . . . . . . . . 6127 Local and Global similarity interfaces . . . . . . . . . . . . . . . . . . 6228 Cases selection methods . . . . . . . . . . . . . . . . . . . . . . . . . 6329 Example code for the k-NN Retrieval . . . . . . . . . . . . . . . . . . 6430 TravelRecommender.cycle() code (step1) . . . . . . . . . . . . . . . . 6631 TravelRecommender.cycle() code (step2) . . . . . . . . . . . . . . . . 6832 TravelRecommender.cycle() code (step3) . . . . . . . . . . . . . . . . 6933 TravelRecommender.cycle() code (step4) . . . . . . . . . . . . . . . . 7034 TravelRecommender.postCycle() code . . . . . . . . . . . . . . . . . . 7135 The Restaurant Recommender precycle using semantic TCBR methods 7836 The Restaurant Recommender cycle using semantic TCBR methods . . 7937 The Restaurant Recommender using statistical TCBR methods . . . . . 8238 Evaluation code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8439 Example of an evaluable application . . . . . . . . . . . . . . . . . . . 8540 Visualization code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9941 Config file for the Examples application . . . . . . . . . . . . . . . . . 103

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 1. Introduction 8

    1 Introduction

    Case-based reasoning (CBR) has became a mature and established subfield of ArtificialIntelligence (AI), both as a mean for addressing AI problems and as a basis for fieldedAI technology.

    Now that CBR fundamental principles have been established and numerous applica-tions have demonstrated CBR is an useful technology, many researchers agree aboutthe increasing necessity to formalise this kind of reasoning, define application analy-sis methodologies, and provide a design and implementation assistance with softwareengineering tools [5, 4, 27, 16, 20]. While the underlying ideas of CBR can be ap-plied consistently across application domains, the specific implementation of the CBRmethods in particular retrieval and similarity functions is highly customised to theapplication at hand. Two factors have became critical: the availability of tools to buildCBR systems, and the accumulated practical experience of applying CBR techniques toreal-world problems.

    Our work goes along all these increasing necessities. We have designed a tool to helpapplication designers to develop and quickly prototyping CBR systems. Besides wewant to provide a software tool useful for students who have little experience with thedevelopment of different types of CBR systems. jCOLIBRI has been designed as a widespectrum framework able to support several types of CBR systems from the simplenearest-neighbor approaches based on flat or simple structures to more complex Knowl-edge Intensive ones. It also supports the development of textual and conversational CBRapplications [41, 21]. Other features of the framework like: ontology integration, visu-alization of case bases, evaluation of CBR applications, classification and maintenancemethods, ... will be explained in this tutorial.

    The framework implementation is evolving as new methods are included. Our (ambi-tious) goal is to provide a reference framework for CBR development that would growwith contributions from the community. We invite the jCOLIBRI developers to send ustheir methods to enrich the functionality of the framework and make them available tothe whole CBR community.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 2. What is Case-Based Reasoning? 9

    2 What is Case-Based Reasoning?

    Case-based reasoning is a paradigm for combining problem-solving and learning thathas became one of the most successful applied subfield of AI of recent years. CBR isbased on the intuition that problems tend to recur. It means that new problems are oftensimilar to previously encountered problems and, therefore, that past solutions may beof use in the current situation [12]. CBR is rooted in the works of Roger Schank ondynamic memory and the central role that a reminding of earlier episodes (cases) andscripts (situation patterns) has in problem solving and learning [48].

    CBR is particularly applicable to problems where earlier cases are available, even whenthe domain is not understood well enough for a deep domain model. Helpdesks, diag-nosis or classification systems have been the most successful areas of application, e.g.,to determine a fault or diagnostic an illness from observed attributes, or to determinewhether or not a certain treatment or repair is necessary given a set of past solved cases[54].

    Central tasks that all CBR methods have to deal with are [2]: "to identify the currentproblem situation, find a past case similar to the new one, use that case to suggest a so-lution to the current problem, evaluate the proposed solution, and update the system bylearning from this experience. How this is done, what part of the process that is focused,what type of problems that drives the methods, etc. varies considerably, however".

    For a detailed description on these and other CBR related aspects, we address the inter-ested reader to detailed surveys about the CBR field ([2],[13],[32], [12]) and to the lasteuropean and international conferences in the field (ECCBR and ICCBR).

    2.1 The CBR cycle

    At the highest level of generality, a general CBR application can be described by a cyclecomposed of the following four processes[2]:

    RETRIEVE the most similar case or cases. REUSE the information and knowledge in that case to solve the problem. REVISE the proposed solution. RETAIN the parts of this experience likely to be useful for future problem solving.

    A new problem is solved by retrieving one or more previously experienced cases,reusing the case in one way or another, revising the solution based on reusing a previouscase, and retaining the new experience by incorporating it into the existing knowledge-base (case-base). In Figure 1, this cycle is illustrated.

    An initial description of a problem (top of figure) defines the query. This new case isused to RETRIEVE a case from the collection of previous cases. The retrieved caseis combined with the new case - through REUSE - into a solved case, i.e. a proposedsolution to the initial problem. Through the REVISE process this solution is tested for

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 2.1 The CBR cycle 10

    Figure 1: CBR cycle by Aamodt and Plaza

    success, e.g. by being applied to the real world environment or evaluated by a teacher,and repaired if failed. During RETAIN (or REMEMBER), useful experience is retainedfor future reuse, and the case base is updated by a new learned case, or by modificationof some existing cases.

    As indicated in the figure, general knowledge usually plays a part in this cycle, by sup-porting the CBR processes. This support may range from very weak (or none) to verystrong, depending on the type of CBR method. By general knowledge we mean generaldomain-dependent knowledge, as opposed to specific knowledge embodied by cases.For example, in diagnosing a patient by retrieving and reusing the case of a previous pa-tient, a model of anatomy together with causal relationships between pathological statesmay constitute the general knowledge used by a CBR system. A set of rules may havethe same role.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 3. The jCOLIBRI CBR framework 11

    3 The jCOLIBRI CBR framework

    jCOLIBRI is an object-oriented framework in Java for building CBR systems that is anevolution of previous work on knowledge intensive CBR [16, 18]. The first versionof our software (COLIBRI1)was prototyped in LISP and was far from being usableoutside of our own research group. However we learnt good lessons from it, and wehave designed jCOLIBRI as a technological evolution of COLIBRI that incorporates theadvantages of the software technologies developed during the last few years.

    The jCOLIBRI framework has two major releases:

    jCOLIBRI version 1 : jCOLIBRI version 1 is the first release of the framework. Itincludes a complete Graphical User Interface that guides the user in the design ofa CBR system. This version is recommended for non-developer users that wantto create CBR systems without programming any code.

    jCOLIBRI version 2 : jCOLIBRI version 2 is a new implementation that follows anew and clear architecture divided into two layers: one oriented to developers(finished) and other oriented to designers (future work). This new design is acomplete white box framework (see below) open to java developers that want toinclude the features of jCOLIBRI in their CBR applications.

    This tutorial shows how to develop a CBR application using the jCOLIBRI version 2(jCOLIBRI2 ahead). For a reference guide of the first version read [45].

    3.1 jCOLIBRI2 Architecture

    jCOLIBRI2 is the result of the experience acquired during the development of the firstversion. It solves many drawbacks like case representation, management of metadata,development problems, etc. But the architecture of this new version is very different(although compatible) as is based on a complete revision of CBR systems and frame-works.

    There are many definitions of framework, but the one that is probably most referencedis found in [28]:

    A framework is a set of classes that embodies an abstract design for solu-tions to a family of related problems.

    In other words, a framework is a partial design and implementation for an applicationin a given problem domain.

    1Cases and Ontology Libraries Integration for Building Reasoning Infrastructures

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 3.1 jCOLIBRI2 Architecture 12

    For classifying a framework we can distinguish two types. A white-box framework isreused mostly by subclassing and a black-box framework is reused through parametriza-tion. The usual development of a framework begins with a design as a white-box archi-tecture that evolves into a black-box one. The resulting black-box framework has anassociated builder that will generate the applications code. The visual builder allowsthe software designer to connect the framework objects and activate them. The first ver-sion of jCOLIBRIis closer to a black-box framework with visual builder and lacks of aclear white- box structure.

    The new design of jCOLIBRI2 attempts to remodel the architecture into a clear white-box system oriented to programmers, and a black-box with builder layer that is orientedto designers.

    The key idea in the new design consists of separating core classes and user interface.That separation will give us the two layers architecture shown in the following figure:

    Figure 2: Two layers architecture of jCOLIBRI2

    The bottom layer contains the basic components of the framework with well defined andclear interfaces. This layer does not contain any kind of graphical tool for developingCBR applications; it is simply a white-box object-oriented framework that must be usedby programmers. The top layer contains semantic descriptions of the components andseveral tools that aid users in the development of CBR applications (black-box withvisual builder framework).

    The bottom layer has new features that solve most of the problems identified in thefirst version. It takes advantage of the new possibilities offered by the newest versionsof the Java language. The most important change is the representation of cases as Java

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 3.1 jCOLIBRI2 Architecture 13

    Beans. The persistence of cases is managed by the Hibernate package. Hibernate is aJava Data Objects (JDO) implementation, so it can automatically store Java Beans ina relational data base, using one or more tables. Java Beans and Hibernate are coretechnologies in the Java 2 Enterprise Edition platform that is oriented to business appli-cations. Using these technologies in jCOLIBRI2 we guarantee the future developmentof commercial CBR applications using this framework. All these features will be ex-plained in this tutorial.

    Regarding the top layer, it is oriented to designers and includes several tools withgraphical interfaces. This layer is the black-box version of the framework, helpingusers to develop complete CBR applications guiding the configuration process. The toplayer is still under development, so this tutorial explains how to use the bottom layer ofjCOLIBRI2.

    A detailed explanation of the features and motivations of the jCOLIBRI2 architecturecan be found in [44].

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 4. Getting started with jCOLIBRI2 14

    4 Getting started with jCOLIBRI2

    The framework can be downloaded from the web page:

    http://gaia.fdi.ucm.es/projects/jcolibri

    Or directly from the sourceforge.net project page:

    http://sourceforge.net/projects/jcolibri-cbr/

    There are three main versions of the framework: v1.1, v2.0 and v2.1. This tutorialcovers versions 2.0 and 2.1. These two versions are very similar. jCOLIBRI 2.1 extendsjCOLIBRI 2.0 including the recommenders package explained in Section 20. There arealso a few changes detailed in Section 24. In this tutorial we will refer to both versionsas jCOLIBRI2.

    Make sure that you download the jCOLIBRI 2.1 version (not the previous ones).

    If you download the windows version, a wizard will guide you during the installationprocess. Or if you use any other operative system just unzip the file in any folder.

    The jCOLIBRI2 release contains the following files:

    ./jCOLIBRI2-Tester.bat : The tester application for Windows. ./jCOLIBRI2-Tester.sh : The tester application for Unix. ./README.txt : This file. ./.project : Eclipse project file that can be imported into that IDE. ./.classpath : Eclipse classpath file with the libraries used in jCOLIBRI. ./build.xml : Apache Ant build file to compile the sources. ./jcolibri2.LICENSE : License agreement. ./src/jcolibri : The souce code of the jCOLIBRI package. ./doc/api/index.html : The index to the javadoc of the jCOLIBRI package. ./doc/uml/ : UML class and sequence diagrams. ./doc/configfilesSchemas/ : XML schemas for the configuration files of the con-

    nectors used to stores persistent data

    ./lib : jCOLIBRI as well as third-party libraries included in this release. ./lib/jcolibri2.jar : Compiled jCOLIBRI jar file. ./TravelRecommender : Folder for the Travel Recommender example. ./TravelRecommender.bat : The Travel Recommender for Windows. ./TravelRecommender.sh : The Travel Recommender for Unix.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 4. Getting started with jCOLIBRI2 15

    jCOLIBRI2 requires that Java (1.6 or later) is installed on your computer.

    Once the framework is installed, you can launch the framework examples through thestart menu (only windows O.S.).

    Figure 3: jCOLIBRI2 short-cuts under windows

    These short cuts link to the files located in the installation directory.

    There is an application tester that can be launched using the following scripts:

    In Windows: jCOLIBRI2-Tester.bat In UNIX: jCOLIBRI2-Tester.sh

    The tester let you run each one of the 30 test applications included in the release toexemplify the main features of jCOLIBRI. In the Tester you can access to the javadocsof the tests which are the main source of documentation as shown in Figure 4.

    Figure 4: jCOLIBRI2 Tester

    In the Code Examples section of the jCOLIBRI web page (http://gaia.fdi.ucm.es/projects/jcolibri/jcolibri2/examples.html), a table summarizes

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 4. Getting started with jCOLIBRI2 16

    which features are explained by each test (see Figure 5). This table is also shownthrough the Map button of the Tester application.

    The first 16 examples illustrate the behavior of general CBR application meanwhile thefollowing 14 show how to implement recommender systems. The reading of these testsis recommended while studying this tutorial as many sections cite them for details.

    Figure 5: jCOLIBRI2 Tests

    There is also an example of using the jCOLIBRI2 jar library in a stand-alone applicationnamed "Travel Recommender". It can be launched using the following scripts:

    In Windows: TravelRecommender.bat In UNIX: TravelRecommender.sh

    This tutorial will show you how to develop the Travel Recommender application fol-lowing some simple steps.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 17

    5 The Travel Recommender CBR application

    The Travel Recommender is a well known application example in the CBR community.Therefore, in this tutorial we have chosen this application to show how to implementCBR applications using jCOLIBRI2. Following sections guide the readers in the de-velopment process of this application in an incremental way. Before starting with theimplementation, this section describes the features and behavior of the Travel Recom-mender application.

    In this application, the case base is composed of several trips. The system receivesa desired trip as a query, compares the query with the trips in the case base using asimilarity function, and returns the most similar ones. After the retrieval, these mostsimilar cases can be adapted according with the restrictions of the query.

    Each case is represented by several attributes: Id, holiday type, number of persons, re-gion, transportation, duration, season, accommodation type, price and hotel. In ourapplication, the price and the hotel will be considered as the solution of the case mean-while the remaining attributes will be the case description.

    This way, our cases are divided into: a description used to retrieve similar cases given aquery, and a solution that is adapted depending on the values of the query.

    Lets look at the different steps of our CBR application. You can execute it throughthe short-cuts in the windows installation (see Figure 3 or the TravelRecommender.batand TravelRecommender.sh scripts located in the installation folder.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 18

    Define Query. In this step the user defines her query to the system. She has to definewhich are the values of the different attributes of a trip: duration, season, transportation,etc.

    Figure 6: Define Query step

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 19

    Configure Similarity. Here the user configures the similarity measure used to retrievethe cases most similar to the query. jCOLIBRI2 implements several similarity functionsthat can be used depending on the type of the attribute (integers, strings, etc.). Moreover,developers can define their own similarity measures.

    You can also assign a weight to each attribute of the query that will be taken into accountwhen computing the average of all attributes. Also, some similarity functions can haveparameters used to configure the similarity measure.

    Finally, the k value indicates how many cases must be retrieved. We are using an algo-rithm named K Nearest Neighbor (k-NN) that computes the similarity of the query withall the cases and then orders the result depending on that similarity value. Then the firstk most similar cases are returned.

    Figure 7: Configure Similarity step

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 20

    Retrieved Cases. This step shows the documents retrieved by the k-NN method. Itshows each case and its similarity to the query.

    Figure 8: Retrieved Cases step

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 21

    Adaptation. In this step the system adapts the retrieved cases to the requirements of theuser depending on the values defined in the query. This stage use to be very domaindependent and will be different in other CBR systems. In our Travel Recommenderapplication we will adapt the price of the trips depending on the number of persons andduration defined in the query.

    Our system will use simple direct proportions to perform the adaptation. For example,imagine that the retrieved case has a duration of 7 days and costs 1000. If the user islooking for a 14 days trip we have to adapt the price using a direct proportion: if a 7days trip costs 1000, then a 14 days trip costs 2000. This process is also repeated forthe number of persons in an analogous way.

    After adapting the solution of the cases, their description can be substituted by the de-scription of the query. At this point, the system will manage a list of working cases thatare different from the cases in the case base. These working cases represent possiblesolutions to the problem described in the query.

    Figure 9: Adaptation step

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 22

    Revise Cases. Once cases have been adapted, the user (or a domain expert, in thiscase the trip agent) would adjust the values of the working cases in a manual way.For example, imagine that the hotel has not available rooms and clients have to go toa similar one. Another situation is that retrieved cases are not similar enough to therequirements of the query and the trip agent has to define manually the solution of thecase.

    Figure 10: Revise Cases step

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 5. The Travel Recommender CBR application 23

    Retain Cases. Finally, the trip agent would save the new trip case into the case base forbeing used in future queries. If it is done, a new Id must be assigned to the trip.

    Figure 11: Retain Cases step

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 6. Importing jCOLIBRI2 into Eclipse 24

    6 Importing jCOLIBRI2 into Eclipse

    To develop a new CBR application with jCOLIBRI2 we recommend the Eclipse IDE(www.eclipse.org). jCOLIBRI2 includes the required files to import the frameworkproject into Eclipse: .classpath and .project. But if you import the whole project, youwill have the source files of the framework into your Eclipse project. The smart solutionconsists on loading only the jcolibri2.jar file (and related libraries) into your project.That way, you will have only your source files in the Eclipse project.

    For teaching purposes, in this tutorial we will load the whole jCOLIBRI project intoEclipse to allow an easier navigation through the source code of the framework. Toavoid modifications of the source files of your installation, we will make a copy of theproject into the Eclipse workspace when importing the project

    To import jCOLIBRI2 into Eclipse use the File - Import menu. Then choose the General- Existing projects into workspace option and finally select the installation folder of theframework into the Select root directory field. Make sure that the Copy projects intoworkspace option is checked and click on Finish.

    Figure 12: Importing the jCOLIBRI2 project into Eclipse

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 6.1 Preparing the Travel Recommender files 25

    Once the project is imported, you can navigate through its contents and source filesusing the "Package Explorer"

    Figure 13: The jCOLIBRI2 project in Eclipse

    6.1 Preparing the Travel Recommender files

    The complete source code of the travel recommender application is located into the ex-ample subfolder of the framework and is compressed as travelrecommender-source.zip.In the following sections we will create the most important source files from scratch.However, we will not create the code related with the GUI of the application because itis out of the scope of this tutorial.

    So, you must decompress the contents of the zip file into the src subfolder of yourjCOLIBRI2 Eclipse project (not the original installation folder). This project will belocated into your Eclipse workspace directory. Make sure that you preserve the foldernames when unziping the file.

    Once done, come back to Eclipse, select the jCOLIBRI2 project into the Package Ex-plorer, and refresh it using the contextual menu option or the F5 key. Then a newsubpackage named jcolibri.examples.TravelRecommender will appear thesource folder.

    As explained before we only keep the GUI files. During the tutorial, we are doingthe rest of the files step by step. So, select the following files and delete them: Trav-elDescription.java, TravelRecommender.java, TravelSolution.java, databaseconfig.xml,hibernate.cfg.xml, TravelDescription.hbm.xml, and TravelSolution.hbm.xml. (They areshown in Figure 14).

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 6.1 Preparing the Travel Recommender files 26

    Figure 14: Files to delete in the project

    The contents of your project before beginning with the tutorial is also shown in Figure15.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 6.1 Preparing the Travel Recommender files 27

    Figure 15: Project state to begin the tutorial

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 7. Creating the CBR application 28

    7 Creating the CBR application

    A CBR application in jCOLIBRI must implement or extend thejcolibri.cbrapplications.StandardCBRApplication interface:

    Listing 1: StandardCBRApplication interface pub l i c i n t e r f a c e S t a n d a r d C B R A p p l i c a t i o n{

    / C o n f i g u r e s t h e a p p l i c a t i o n : case base , c o n n e c t o r s , e t c . @throws E x e c u t i o n E x c e p t i o n /

    pub l i c vo id c o n f i g u r e ( ) throws E x e c u t i o n E x c e p t i o n ;

    / Runs t h e p r e c y l e where t y p i c a l l y c a s e s are read and o r g a n i z e d i n t o a case base . @return The c r e a t e d case base w i t h t h e c a s e s i n t h e s t o r a g e . @throws E x e c u t i o n E x c e p t i o n /

    pub l i c CBRCaseBase p r e C y c l e ( ) throws E x e c u t i o n E x c e p t i o n ;

    / E x e c u t e s a CBR c y c l e w i t h t h e g i v e n query . @throws E x e c u t i o n E x c e p t i o n /

    pub l i c vo id c y c l e ( CBRQuery que ry ) throws E x e c u t i o n E x c e p t i o n;

    / Runs t h e code t o shutdown t h e a p p l i c a t i o n . T y p i c a l l y i t c l o s e s t h e c o n n e c t o r . @throws E x e c u t i o n E x c e p t i o n /

    pub l i c vo id p o s t C y c l e ( ) throws E x e c u t i o n E x c e p t i o n ;} This interface divides the CBR application behavior into 3 steps:

    Precycle: Initializes the CBR application, usually loading the case base and pre-computing expensive algorithms (really useful when working with texts). It isexecuted only once.

    Cycle: Executes the CBR cycle. It is executed many times. Postcycle: Post-execution or maintenance code.

    Now, lets create the main class of our Travel Recommender. The nameof the class must be TravelRecommender.java of the package

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 7. Creating the CBR application 29

    jcolibri.examples.TravelRecommender. Include also the main()method in the class:

    Listing 2: TravelRecommender initial code pub l i c c l a s s TravelRecommender implements

    S t a n d a r d C B R A p p l i c a t i o n {

    pub l i c vo id c o n f i g u r e ( ) throws E x e c u t i o n E x c e p t i o n {}

    pub l i c vo id c y c l e ( CBRQuery que ry ) throws E x e c u t i o n E x c e p t i o n{

    }

    pub l i c vo id p o s t C y c l e ( ) throws E x e c u t i o n E x c e p t i o n {}

    pub l i c CBRCaseBase p r e C y c l e ( ) throws E x e c u t i o n E x c e p t i o n {}

    pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {}

    } To ease the access to this class from the different GUI frames and to ensure that there isonly one instance of this class we are going to implement a singleton pattern. Includethe following code into the class:

    Listing 3: TravelRecommender singleton pr i v a t e s t a t i c TravelRecommender _ i n s t a n c e = nu l l ;pub l i c s t a t i c TravelRecommender g e t I n s t a n c e ( ){

    i f ( _ i n s t a n c e == nu l l )_ i n s t a n c e = new TravelRecommender ( ) ;

    re turn _ i n s t a n c e ;}

    pr i v a t e TravelRecommender ( ){}

    This way, the unique instance of the TravelRecommender class must be accessedthrough the getInstance() method.

    As the application uses several frames we need a main frame that acts as the parentof all of them. We are going to create a simple main frame that only contains thejCOLIBRI2 logo. Then we will have a separated frame for each step of the application.Include the following code in the class:

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 7. Creating the CBR application 30

    Listing 4: TravelRecommender GUI code . . .

    S i m i l a r i t y D i a l o g s i m i l a r i t y D i a l o g ;R e s u l t D i a l o g r e s u l t D i a l o g ;A u t o A d a p t a t i o n D i a l o g a u t o A d a p t D i a l o g ;R e v i s i o n D i a l o g r e v i s i o n D i a l o g ;R e t a i n D i a l o g r e t a i n D i a l o g ;. . .pub l i c vo id c o n f i g u r e ( ) throws E x e c u t i o n E x c e p t i o n {

    t ry {/ / Cr ea t e t h e d i a l o g ss i m i l a r i t y D i a l o g = new S i m i l a r i t y D i a l o g ( main ) ;r e s u l t D i a l o g = new R e s u l t D i a l o g ( main ) ;a u t o A d a p t D i a l o g = new A u t o A d a p t a t i o n D i a l o g ( main ) ;r e v i s i o n D i a l o g = new R e v i s i o n D i a l o g ( main ) ;r e t a i n D i a l o g = new R e t a i n D i a l o g ( main ) ;

    } catch ( E x c e p t i o n e ) {throw new E x e c u t i o n E x c e p t i o n ( e ) ;

    }}. . .s t a t i c JFrame main ;

    void showMainFrame ( ){

    main = new JFrame ( " T r a v e l Recommender " ) ;main . s e t R e s i z a b l e ( f a l s e ) ;main . s e t U n d e c o r a t e d ( t rue ) ;J L a b e l l a b e l = new J L a b e l (new ImageIcon ( j c o l i b r i . u t i l .

    F i l e I O . f i n d F i l e ( " / j c o l i b r i / t e s t / main / j c o l i b r i 2 . j p g " )) ) ;

    main . g e t C o n t e n t P a n e ( ) . add ( l a b e l ) ;main . pack ( ) ;Dimension s c r e e n S i z e = j a v a . awt . T o o l k i t .

    g e t D e f a u l t T o o l k i t ( ) . g e t S c r e e n S i z e ( ) ;main . s e tBounds ( ( s c r e e n S i z e . wid th main . ge tWid th ( ) ) /

    2 ,( s c r e e n S i z e . h e i g h t main . g e t H e i g h t ( ) ) / 2 ,main . ge tWid th ( ) ,main . g e t H e i g h t ( ) ) ;

    main . s e t V i s i b l e ( t rue ) ;}

    This code shows an useful class of jCOLIBRI2 named jcolibri.util.FileIO.This class finds a file in the src, bin or main folders of the project even if it is packagedinto a jar file. This is very useful if your project must access to external files.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 7. Creating the CBR application 31

    Now lets create the main method of the Travel Recommender application. It config-ures the application, executes the precycle (only once), executes the cycle several timesand finally calls the postcycle code:

    Listing 5: TravelRecommender main() method pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

    / / Ob ta in TravelRecommender o b j e c tTravelRecommender recommender = g e t I n s t a n c e ( ) ;/ / Show t h e main framerecommender . showMainFrame ( ) ;

    t ry{

    / / C o n f i g u r e t h e a p p l i c a t i o nrecommender . c o n f i g u r e ( ) ;/ / E x e c u t e t h e P r e c y c l erecommender . p r e C y c l e ( ) ;

    / / Cr ea t e t h e frame t h a t o b t a i n s t h e queryQueryDia log qf = new QueryDia log ( main ) ;

    / / Main CBR c y c l eboolean c o n t = t rue ;whi le ( c o n t ){

    / / Show t h e query frameqf . s e t V i s i b l e ( t rue ) ;/ / Ob ta in t h e queryCBRQuery que ry = qf . ge tQuery ( ) ;

    / / C a l l t h e c y c l erecommender . c y c l e ( que ry ) ;

    / / Ask i f c o n t i n u ei n t ans = j a v a x . swing . JOp t ionPane . showConf i rmDia log ( nul l ,

    "CBR c y c l e f i n i s h e d , que ry a g a i n ? " , " Cycle f i n i s h e d " ,j a v a x . swing . JOp t ionPane . YES_NO_OPTION) ;

    c o n t = ( ans == j a v a x . swing . JOp t ionPane . YES_OPTION ) ;}

    / / E x e c u t e p o s t c y c l erecommender . p o s t C y c l e ( ) ;

    } ca tch ( E x c e p t i o n e ) {/ / E r r o r sorg . apache . commons . l o g g i n g . LogFac to ry . ge tLog (

    TravelRecommender . c l a s s ) . e r r o r ( e ) ;j a v a x . swing . JOp t ionPane . showMessageDialog ( nul l , e .

    ge tMessage ( ) ) ;}

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 7. Creating the CBR application 32

    System . e x i t ( 0 ) ;} This code shows how to access the unique instance of the TravelRecommenderclass through the getInstance() method. Then the main frame is shown. After thatthe CBR application is configured and the precycle is executed. That code is executedonly once.

    Then we create the frame that obtains the query: QueryDialog. As explained before,this class must receive the main frame in the constructor. This frame (shown in Figure6) returns the query defined by the user.

    jCOLIBRI2 defines a class to store the queries namedjcolibri.cbrcore.CBRQuery. It will be explained later in Section 9.2.

    Finally, the code shows that jCOLIBRI2 uses the Apache Log4j library to manage thelogging messages.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 8. The Travel Recommender Case Base 33

    8 The Travel Recommender Case Base

    The case base of the application is stored in a Data Base. jCOLIBRI2 canmanage any Data Base Manager (DBM) because it uses internally the Hibernate(www.hibernate.org) middleware. Hibernate is a powerful, high performance object/re-lational persistence and query service. This project and a critical component of broadlyused JBoss J2EE server supporting persistence in Data Bases or XML files among manyother features like its own object-oriented query language. Using Hibernate, jCOLIBRI2allows the use of any DBM like Oracle, MySQL, etc. This way, you can reuse any database to create your case base for a CBR application.

    The Travel Recommender application uses the data base defined in the travel.sql filelocated in the jcolibri.example.TravelRecommender package. You can usethat file to generate the data base in your favorite DBM.

    This file generates a data base named travel that contains a table also named travel. Thetable contains 1024 trips and is defined with the following schema:

    Listing 6: Travel Recommender data base schema c r e a t e t ab l e t r a v e l (

    c a s e I d VARCHAR( 1 5 ) ,Hol idayType VARCHAR( 2 0 ) ,P r i c e INTEGER ,NumberOfPersons INTEGER ,Region VARCHAR( 3 0 ) ,T r a n s p o r t a t i o n VARCHAR( 3 0 ) ,D u r a t i o n INTEGER ,Season VARCHAR( 3 0 ) ,Accommodation VARCHAR( 3 0 ) ,H o t e l VARCHAR( 5 0 ) ) ;

    To simplify the use of the examples, jCOLIBRI2 includes the HSQLDB Data Base Man-ager (www.hsqldb.org). This DBM is completely implemented in Java and can be easilyincluded in any other Java project. This way, HSQLDB is used by the examples of theframework and by the Travel Recommender application.

    To launch the DBM you can use the jcolibri.test.database.HSQLDBserverclass. Its init() method initializes the DBM loading the data bases used by theexamples. This initialization also includes the tables used by our Travel Recommenderdefined in travel.sql.

    Now you have to add the following code into the TravelRecommender class tolauch and stop the DBM:

    Listing 7: TravelRecommender configure() method (version 1) pub l i c vo id c o n f i g u r e ( ) throws E x e c u t i o n E x c e p t i o n {

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 8. The Travel Recommender Case Base 34

    t ry {

    / / Emulate da ta base s e r v e rj c o l i b r i . t e s t . d a t a b a s e . HSQLDBserver . i n i t ( ) ;

    / / Cr ea t e t h e d i a l o g s. . .

    } catch ( E x c e p t i o n e ) {throw new E x e c u t i o n E x c e p t i o n ( e ) ;

    }}

    pub l i c vo id p o s t C y c l e ( ) throws E x e c u t i o n E x c e p t i o n {_ c o n n e c t o r . c l o s e ( ) ;j c o l i b r i . t e s t . d a t a b a s e . HSQLDBserver . shutDown ( ) ;

    } Note that this code is not the final code for those methods. They will be extended infollowing sections.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9. Representing the Cases 35

    9 Representing the Cases

    Now that we have the data base that contains the cases, we have to represent them inour system. This section details how cases are represented inside a jCOLIBRI2 CBRapplication.

    jCOLIBRI2 represents the cases using Java Beans. A Java Bean is any class that has aget() and set() method for each public attribute. Its modification and managementcan be performed automatically using a Java technology called Introspection (that iscompletely transparent for the developer). Using Java Beans in jCOLIBRI2, developerscan design their cases as normal Java classes, choosing the most natural design. Thissimplifies programming and debugging the CBR applications, and the configurationfiles became simpler because most of the metadata of the cases can be extracted usingIntrospection. Java Beans also offer automatically generated user interfaces that allowthe modification of their attributes and automatic persistence into data bases and XMLfiles. It is important to note that every Java web application uses Java Beans as a basetechnology, so the development of web interfaces is very straightforward. Moreover,Hibernate uses Java Beans to store the information into a data base. Java Beans andHibernate are core technologies in the Java 2 Enterprise Edition platform that is orientedto business applications. Using these technologies in jCOLIBRI2 we guarantee the futuredevelopment of commercial CBR applications using this framework.

    9.1 Case Components

    The unique restriction of the Java Beans that compose a case is that they must definean Id that identifies them in the table of the DBM. This restriction is defined in thejcolibri.cbrcore.CaseComponent interface:

    Listing 8: CaseComponent interface pub l i c i n t e r f a c e CaseComponent {

    / R e t u r n s t h e a t t r i b u t e t h a t i d e n t i f i e s t h e component . An ida t t r i b u t e must be un iq ue f o r each component . /

    j c o l i b r i . c b r c o r e . A t t r i b u t e g e t I d A t t r i b u t e ( ) ;} The jcolibri.cbrcore.Attribute class represents an attribute of a Java Bean.For example, if you have a Java Bean like this:

    Listing 9: Bean example code pub l i c c l a s s MyBean{

    i n t d a t a ;i n t g e t D a t a ( ) {

    re turn d a t a ;}

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9.2 Cases and Queries in jCOLIBRI2 36

    void s e t D a t a ( i n t _ d a t a ) {t h i s . d a t a = _ d a t a ;

    }} You can create an Attribute object that represent the data attribute of MyBean. It isdone with the following code:

    Listing 10: Using Attribute example code A t t r i b u t e a t = new A t t r i b u t e ( " d a t a " , MyBean . c l a s s ) ;

    The Attribute class allows jCOLIBRI2 to manage the contents of the cases (Java Beans).

    9.2 Cases and Queries in jCOLIBRI2

    In jCOLIBRI2 cases and queries are composed of CaseComponents. A case will bedivided into four components:

    Description of the problem. Solution to the problem. Result of applying the solution. Justification of the Solution (why this solution was chosen).

    These components were chosen after reviewing the applications detailed in [54], butthey are not required in some applications, so the developer can leave them empty (onlythe description component is mandatory).

    Every query has always a description, so the jcolibri.cbrcore.CBRQuery ob-ject defines the queries as:

    Listing 11: CBRQuery code pub l i c c l a s s CBRQuery{

    CaseComponent d e s c r i p t i o n ;

    / R e t u r n s t h e d e s c r i p t i o n component . /

    pub l i c CaseComponent g e t D e s c r i p t i o n ( ) {re turn d e s c r i p t i o n ;

    }

    / S e t s t h e d e s c r i p t i o n component . /

    pub l i c vo id s e t D e s c r i p t i o n ( CaseComponent d e s c r i p t i o n ) {

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9.2 Cases and Queries in jCOLIBRI2 37

    t h i s . d e s c r i p t i o n = d e s c r i p t i o n ;}

    / R e t u r n s t h e ID a t t r i b u t e o f t h e Query / Case t h a t i s t h e ID a t t r i b u t e o f i t s d e s c r i p t i o n component . /

    pub l i c O b j e c t ge t ID ( ){

    i f ( t h i s . d e s c r i p t i o n == nu l l )re turn nu l l ;

    e l s et ry {

    re turn d e s c r i p t i o n . g e t I d A t t r i b u t e ( ) . g e t V a l u e (d e s c r i p t i o n ) ;

    } catch ( A t t r i b u t e A c c e s s E x c e p t i o n e ) {re turn nu l l ;

    }}

    pub l i c S t r i n g t o S t r i n g ( ){

    re turn " [ D e s c r i p t i o n : "+ d e s c r i p t i o n +" ] " ;}

    } If a query contains a description, we can say that a case is a query plus solution, re-sult and justification of the solution. This way, the jcolibri.cbrcore.CBRCaseextends jcolibri.cbrcore.CBRQuery adding those components:

    Listing 12: CBRCase code pub l i c c l a s s CBRCase ex tends CBRQuery {

    CaseComponent s o l u t i o n ;CaseComponent j u s t i f i c a t i o n O f S o l u t i o n ;CaseComponent r e s u l t ;

    / R e t u r n s t h e j u s t i f i c a t i o n O f S o l u t i o n . /

    pub l i c CaseComponent g e t J u s t i f i c a t i o n O f S o l u t i o n ( ) {re turn j u s t i f i c a t i o n O f S o l u t i o n ;

    }

    / S e t s t h e J u s t i f i c a t i o n o f S o l u t i o n component . @param j u s t i f i c a t i o n O f S o l u t i o n t o s e t . /

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9.2 Cases and Queries in jCOLIBRI2 38

    pub l i c vo id s e t J u s t i f i c a t i o n O f S o l u t i o n ( CaseComponentj u s t i f i c a t i o n O f S o l u t i o n ) {t h i s . j u s t i f i c a t i o n O f S o l u t i o n = j u s t i f i c a t i o n O f S o l u t i o n ;

    }

    / R e t u r n s t h e r e s u l t . /

    pub l i c CaseComponent g e t R e s u l t ( ) {re turn r e s u l t ;

    }

    / S e t s t h e R e s u l t component /

    pub l i c vo id s e t R e s u l t ( CaseComponent r e s u l t ) {t h i s . r e s u l t = r e s u l t ;

    }

    / R e t u r n s t h e s o l u t i o n . /

    pub l i c CaseComponent g e t S o l u t i o n ( ) {re turn s o l u t i o n ;

    }

    / S e t s t h e s o l u t i o n component /

    pub l i c vo id s e t S o l u t i o n ( CaseComponent s o l u t i o n ) {t h i s . s o l u t i o n = s o l u t i o n ;

    }

    pub l i c S t r i n g t o S t r i n g ( ){

    re turn super . t o S t r i n g ( ) +" [ S o l u t i o n : "+ s o l u t i o n +" ] [ So l .J u s t . : "+ j u s t i f i c a t i o n O f S o l u t i o n +" ] [ R e s u l t : "+ r e s u l t+" ] " ;

    }} Each CaseComponent bean can have attributes that also are CaseComponentsbeans. This way, developers can create case structures with nested attributes. Thisfeature is shown in the Test 3 of the examples.

    The UML diagram in Figure 16 shows the relationship between cases, queries and case-components.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9.3 Case Components of the Travel Recommender 39

    Figure 16: Cases representation UML diagram

    9.3 Case Components of the Travel Recommender

    The cases of the Travel Recommender application are composed by a description anda solution. The description contains the Id, holiday type, number of persons, region,transportation, duration, season and accommodation type attributes. And the solutioncontains the Id, hotel and price attributes.

    To create the description of the case you have to implement a JavaBean that contains those attributes and obeys the CaseComponent inter-face. This class is named TravelDescription and must be located underjcolibri.example.TravelRecommender. This is a portion of the code:

    Listing 13: TravelDescription initial code pub l i c c l a s s T r a v e l D e s c r i p t i o n implements j c o l i b r i . c b r c o r e .

    CaseComponent {

    pub l i c enum AccommodationTypes { OneStar , TwoStars ,T h r e e S t a r s , H o l i d a y F l a t , F o u r S t a r s , F i v e S t a r s } ;

    pub l i c enum Seasons { Janua ry , Februa ry , March , A p r i l , May , June, Ju ly , August , September , October , November , December } ;

    S t r i n g c a s e I d ;S t r i n g Hol idayType ;I n t e g e r NumberOfPersons ;I n s t a n c e Region ;S t r i n g T r a n s p o r t a t i o n ;I n t e g e r D u r a t i o n ;Seasons Season ;AccommodationTypes Accommodation ;

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9.3 Case Components of the Travel Recommender 40

    pub l i c A t t r i b u t e g e t I d A t t r i b u t e ( ) {re turn new A t t r i b u t e ( " c a s e I d " , t h i s . g e t C l a s s ( ) ) ;

    }

    pub l i c S t r i n g t o S t r i n g ( ){

    re turn " ( "+ c a s e I d +" ; "+ Hol idayType +" ; "+NumberOfPersons+"; "+Region+" ; "+ T r a n s p o r t a t i o n +" ; "+ D u r a t i o n +" ; "+ Season+" ; "+Accommodation+" ) " ;

    }. . .

    Now you have to add a get() and set() method for each attribute. But, dont worrybecause Eclipse does it automatically selecting the menu item: Source - Generate Get-ters and Setters....

    In this description we are using two enumerate types to define the accommodation andseason. Also, there is an strange type named Instance that defines the region. This typeis defined in jcolibri.datatypes.Instance and represents an instance of anontology. Now, we are not going into detail with this because it will be explained inSection 12.4.

    The solution bean must be created in a similar way. Its name is TravelSolutionand this is the code without the getters and setters (that you have to generate):

    Listing 14: TravelSolution initial code pub l i c c l a s s T r a v e l S o l u t i o n implements j c o l i b r i . c b r c o r e .

    CaseComponent {

    S t r i n g i d ;I n t e g e r p r i c e ;S t r i n g h o t e l ;

    pub l i c S t r i n g t o S t r i n g ( ){

    re turn " ( "+ i d +" ; "+ p r i c e +" ; "+ h o t e l +" ) " ;}

    pub l i c A t t r i b u t e g e t I d A t t r i b u t e ( ) {re turn new A t t r i b u t e ( " i d " , t h i s . g e t C l a s s ( ) ) ;

    }. . .

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 9.4 Defining new attribute types 41

    9.4 Defining new attribute types

    Sometimes, the Java built-in types are not enough and the developer has to define herown ones. This is the case of the Region attribute of the TravelDescription beanthat uses a custom data type named Instance.

    When defining a new type the developer has to specify how it is going tobe stored and in the persistence media. This is done by implementing thejcolibri.connectors.TypeAdaptor interface:

    Listing 15: TypeAdaptor interface pub l i c i n t e r f a c e TypeAdaptor {

    / R e t u r n s a s t r i n g r e p r e s e n t a t i o n o f t h e t y p e . /

    pub l i c ab s t r a c t S t r i n g t o S t r i n g ( ) ;

    / Reads t h e t y p e from a s t r i n g . /

    pub l i c ab s t r a c t vo id f r o m S t r i n g ( S t r i n g c o n t e n t ) throwsE x c e p t i o n ;

    / You must d e f i n e t h i s method t o a v o i d prob lems w i t h t h e

    da ta base c o n n e c t o r ( H i b e r n a t e ) /

    pub l i c ab s t r a c t boolean e q u a l s ( O b j e c t o ) ;} jCOLIBRI2 includes two examples of user-defined data types in thejcolibri.datatypes package. Moreover, the Test 2 of the examples explains thismechanism in detail.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 10. The two layers persistence architecture of jCOLIBRI2 42

    10 The two layers persistence architecture ofjCOLIBRI2

    The CaseComponents defined in the previous section are used to fill the attributesof the CBRCase objects when loading the cases. Before explaining this mechanism wehave to depict the two layers persistence architecture of jCOLIBRI2.

    CBR systems must access the stored cases in an efficient way, a problem that becomesmore relevant as the size of the Case Base grows. jCOLIBRI (both versions 1 and 2)splits the problem of Case Base management in two separate although related concerns:persistence mechanism and in-memory organization. This architecture is illustrated inFigure 17.

    Figure 17: Case Base management in jCOLIBRI2

    10.1 The Connectors of jCOLIBRI2

    Persistence is built around connectors. A connector represents the first layer ofjCOLIBRI on top of the physical storage. Connectors are objects that know how toaccess and retrieve cases from the medium and return those cases to the CBR systemin a uniform way. The use of connectors give jCOLIBRI flexibility against the physicalstorage so the system designer can choose the most appropriate one for the system athand.

    jCOLIBRI2 includes the following connectors:

    jcolibri.connectors.DataBaseConnector. Manages the persistence of cases intodata bases. Internally, it uses the Hibernate library.

    jcolibri.connectors.PlainTextConnector. Manages the persistence of the cases intotextual files.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 10.1 The Connectors of jCOLIBRI2 43

    jcolibri.connectors.OntologyConnector. It uses OntoBridge2 to manage casebases stored into ontologies.

    The obvious interface for a connector must include methods to read the Case Base intomemory and update it back into persistent media. More specifically, jCOLIBRI2 includesan interface named Connector that belongs to package jcolibri.cbrcore. Ev-ery connector is supposed to implement the methods defined by this interface:

    Listing 16: Connector interface pub l i c i n t e r f a c e Connec to r {

    / I n i t i a l i c e s t h e c o n n e c t o r w i t h t h e g i v e n XML f i l e /

    pub l i c vo id i n i tF romXMLf i l e ( j a v a . n e t .URL f i l e ) throwsI n i t i a l i z i n g E x c e p t i o n ;

    / Cleanup any r e s o u r c e t h a t t h e c o n n e c t o r migh t be us ing , and s u s p e n d s t h e s e r v i c e /

    pub l i c vo id c l o s e ( ) ;

    / S t o r e s g i v e n c l a s s e s on t h e s t o r a g e media /

    pub l i c vo id s t o r e C a s e s ( C o l l e c t i o n c a s e s ) ;

    / D e l e t e s g i v e n c a s e s f o r t h e s t o r a g e media /

    pub l i c vo id d e l e t e C a s e s ( C o l l e c t i o n c a s e s ) ;

    / R e t u r n s a l l t h e c a s e s i n t h e s t o r a g e media /

    pub l i c C o l l e c t i o n r e t r i e v e A l l C a s e s ( ) ;

    / R e t r i e v e s some c a s e s depend ing on t h e f i l t e r . TODO. /

    pub l i c C o l l e c t i o n r e t r i e v e S o m e C a s e s ( C a s e B a s e F i l t e rf i l t e r ) ;

    } 2OntoBridge (http://gaia.fdi.ucm.es/projects/ontobridge/) is a library developed by the GAIA team thateases the management of ontologies.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 10.2 In-memory organization of the cases 44

    Connectors are configured through XML configuration files. Each jCOLIBRI2 connectordefines the XML schema of its configuration file. These schemes can be found in thedocumentation.

    An interface such that assumes that the whole Case Base can be read into memory forthe CBR processes to work with it. However, in a real sized CBR application thisapproach may not be feasible. For that reason, we are working to extend connec-tor interface to retrieve those cases that satisfy a query expressed in a subset of SQL(retrieveSomeCases(CaseBaseFilter)). This way the designer can decidewhat part of the Case Base is loaded into memory.

    If a developer requires a specific connector, she can create her own one extending theConnector interface. This is shown in the Test 13 of the code examples of the frame-work.

    10.2 In-memory organization of the cases

    The second layer of Case Base management is the data structure used to organize thecases once loaded into memory. The organization of the case base (linear, k-d trees, caseretrieval nets, etc.) may have a big influence on the CBR processes, so the frameworkleaves open the election of the data structure that is going to be used.

    In the same way connectors offer a common interface to the Case Base, the Case Basealso implements a common interface for the CBR methods to access the cases. Thisway the organization and indexation chosen for the Case Base will not affect the imple-mentation of the methods. The interface that defines the in-memory organization of thecases is jcolibri.cbrcore.CBRCaseBase:

    Listing 17: CBRCaseBase interface pub l i c i n t e r f a c e CBRCaseBase {

    / I n i t i a l i z e s t h e case base . T h i s methods r e c i e v e s t h e c o n n e c t o r t h a t manages t h e p e r s i s t e n c e media . /

    pub l i c vo id i n i t ( Connec to r c o n n e c t o r ) throws j c o l i b r i .e x c e p t i o n . I n i t i a l i z i n g E x c e p t i o n ;

    / De I n i t i a l i z e s t h e case base . /

    pub l i c vo id c l o s e ( ) ;

    / R e t u r n s a l l t h e c a s e s a v a i l a b l e on t h i s case base /

    pub l i c C o l l e c t i o n g e t C a s e s ( ) ;

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 10.2 In-memory organization of the cases 45

    / R e t u r n s some c a s e s depend ing on t h e f i l t e r /

    pub l i c C o l l e c t i o n g e t C a s e s ( C a s e B a s e F i l t e r f i l t e r ) ;

    / Adds a c o l l e c t i o n o f new CBRCase o b j e c t s t o t h e c u r r e n t case base /

    pub l i c vo id l e a r n C a s e s ( C o l l e c t i o n c a s e s ) ;

    / Removes a c o l l e c t i o n o f new CBRCase o b j e c t s t o t h e

    c u r r e n t case base /

    pub l i c vo id f o r g e t C a s e s ( C o l l e c t i o n c a s e s ) ; Analogous to the Connector interface, developers can create their in-memory orga-nizations of cases implementing the CBRCaseBase interface. jCOLIBRI2 includes thefollowing Case Bases:

    jcolibri.casebase.LinealCaseBase: Basic Lineal Case Base thatstores cases into a List.

    jcolibri.casebase.CachedLinealCaseBase: Cached case base thatonly persists cases when closing the application.

    jcolibri.casebase.IDIndexedLinealCaseBase: Extension ofLinealCaseBase that also keeps an index of cases using their IDs.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11. Loading the Case Base 46

    11 Loading the Case Base

    Now, lets add the code for loading the cases of our Travel Recommender application.We are using the Data Base connector (implemented with Hibernate) and the LinealCase Base. The first step consists on modifying our TravelRecommender classwith the following code:

    Listing 18: TravelRecommender configure() (version 2) and precycle() . . ./ Connec tor o b j e c t /

    Connec to r _ c o n n e c t o r ;/ CaseBase o b j e c t /CBRCaseBase _caseBase ;. . .

    pub l i c vo id c o n f i g u r e ( ) throws E x e c u t i o n E x c e p t i o n {t ry {

    / / Emulate da ta base s e r v e rj c o l i b r i . t e s t . d a t a b a s e . HSQLDBserver . i n i t ( ) ;

    / / Cr ea t e a da ta base c o n n e c t o r_ c o n n e c t o r = new DataBaseConnec to r ( ) ;/ / I n i t t h e ddbb c o n n e c t o r w i t h t h e c o n f i g f i l e_ c o n n e c t o r . i n i tF romXMLf i l e ( j c o l i b r i . u t i l . F i l e I O

    . f i n d F i l e ( " j c o l i b r i / examples /TravelRecommender / d a t a b a s e c o n f i g . xml " ) ) ;

    / / Cr ea t e a L i n e a l case base f o r inmemoryo r g a n i z a t i o n

    _caseBase = new L i n e a l C a s e B a s e ( ) ;

    / / Cr ea t e t h e d i a l o g s. . .

    } catch ( E x c e p t i o n e ) {throw new E x e c u t i o n E x c e p t i o n ( e ) ;

    }

    pub l i c CBRCaseBase p r e C y c l e ( ) throws E x e c u t i o n E x c e p t i o n {/ / Load c a s e s from c o n n e c t o r i n t o t h e case base_caseBase . i n i t ( _ c o n n e c t o r ) ;/ / P r i n t t h e c a s e sj a v a . u t i l . C o l l e c t i o n c a s e s = _caseBase .

    g e t C a s e s ( ) ;f o r ( CBRCase c : c a s e s )

    System . o u t . p r i n t l n ( c ) ;re turn _caseBase ;

    }

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 47

    Firstly, we are creating the two variables that will contain the connector and case base:_connector and _caseBase. They are defined with the type of the interfacesConnector and CBRCaseBase, but in the configure() method we will assignan instance of DataBaseConnector and LinealCaseBase that implement theseinterfaces. As explained before, each connector can use a xml file that defines its con-figuration. In this case, we are using the file jcolibri/examples/TravelRecommender/-databaseconfig.xml to configure the Data Base connector. This file is explained in thefollowing subsection.

    In the preCycle() method we initializes the case base object with the connectorthrough the init() method. This action will load the cases from the persistence intothe memory. Then we can access the cases in the case base object (here to print them toconsole).

    11.1 Configuring the connector

    The data base connector is configured with the databaseconfig.xml file. So, next stepconsists on creating this file in the jcolibri.examples.TravelRecommenderpackage.

    The schema of this file is explained in the documentation of the connector. Here youshould write this:

    Listing 19: databaseconfig.xml < D a t a B a s e C o n f i g u r a t i o n >

    < H i b e r n a t e C o n f i g F i l e >j c o l i b r i / examples / TravelRecommender / h i b e r n a t e . c f g . xml

    < / H i b e r n a t e C o n f i g F i l e >< D e s c r i p t i o n M a p p i n g F i l e >

    j c o l i b r i / examples / TravelRecommender / T r a v e l D e s c r i p t i o n . hbm .xml

    < / D e s c r i p t i o n M a p p i n g F i l e >< D e s c r i p t i o n C l a s s N a m e >

    j c o l i b r i . examples . TravelRecommender . T r a v e l D e s c r i p t i o n< / D e s c r i p t i o n C l a s s N a m e >< S o l u t i o n M a p p i n g F i l e >

    j c o l i b r i / examples / TravelRecommender / T r a v e l S o l u t i o n . hbm . xml< / S o l u t i o n M a p p i n g F i l e >< S o lu t i on Cla s sNam e >

    j c o l i b r i . examples . TravelRecommender . T r a v e l S o l u t i o n< / S o lu t i on Cla s sNam e >

    < / D a t a B a s e C o n f i g u r a t i o n > This file defines the following information:

    HibernateConfigFile: locates the configuration file of Hibernate.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 48

    DescriptionMappingFile: locates the mapping file of the description. DescriptionClassName: class that represents the solution. SolutionMappingFile: locates the mapping file of the solution. SolutionClassName: class that represents the description.

    The solution and description classes are the ones developed in Section 9.3. Us-ing these fields, the connector knows which are the CaseComponents that fillthe description, solution, justificationOfSolution and result at-tributes of the CBRCase.

    The mapping files define how to map those classes into the tables of the database. Andfinally, the Hibernate configuration file configures the connection with the DBMS.

    11.1.1 The Hibernate configuration file

    This file tells Hibernate how to access the DBMS. If you need special requirements readthe Hibernate documentation located here: http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html

    To configure Hibernate to work with our HSQLDB Data Base Man-ager you must create the file hibernate.cfg.xml into thejcolibri.examples.TravelRecommender package. Following listingshows the content:

    Listing 20: hibernate.cfg.xml

    < !DOCTYPE h i b e r n a t ec o n f i g u r a t i o n PUBLIC

    " / / H i b e r n a t e / H i b e r n a t e C o n f i g u r a t i o n DTD 3 . 0 / / EN"" h t t p : / / h i b e r n a t e . s o u r c e f o r g e . n e t / h i b e r n a t ec o n f i g u r a t i o n 3 .0 .

    d t d ">< h i b e r n a t ec o n f i g u r a t i o n >

    < s e s s i o nf a c t o r y >< ! Database c o n n e c t i o n s e t t i n g s >< p r o p e r t y name=" c o n n e c t i o n . d r i v e r _ c l a s s ">

    org . h s q l d b . j d b c D r i v e r< / p r o p e r t y >

    < p r o p e r t y name=" c o n n e c t i o n . u r l ">j d b c : h s q l d b : h s q l : / / l o c a l h o s t / t r a v e l

    < / p r o p e r t y >

    < p r o p e r t y name=" c o n n e c t i o n . username ">sa

    < / p r o p e r t y >

    < p r o p e r t y name=" c o n n e c t i o n . password ">< / p r o p e r t y >

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 49

    < ! JDBC c o n n e c t i o n poo l ( use t h e b u i l t i n ) >< p r o p e r t y name=" c o n n e c t i o n . p o o l _ s i z e ">

    1< / p r o p e r t y >

    < ! SQL d i a l e c t >< p r o p e r t y name=" d i a l e c t ">

    org . h i b e r n a t e . d i a l e c t . HSQLDialect< / p r o p e r t y >

    < ! Enable H i b e r n a t e a u t o m a t i c s e s s i o n c o n t e x t management>

    < p r o p e r t y name=" c u r r e n t _ s e s s i o n _ c o n t e x t _ c l a s s ">t h r e a d

    < / p r o p e r t y >

    < ! D i s a b l e t h e second l e v e l cache >< p r o p e r t y name=" cache . p r o v i d e r _ c l a s s ">

    org . h i b e r n a t e . cache . NoCacheProv ider< / p r o p e r t y >

    < ! Echo a l l e x e c u t e d SQL t o s t d o u t >< p r o p e r t y name=" show_sql ">

    t r u e< / p r o p e r t y >

    < / s e s s i o nf a c t o r y >< / h i b e r n a t ec o n f i g u r a t i o n > To use Hibernate with other DBMs, developers should modify the following properties(described in the Hibernate documentation at http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html#configuration-hibernatejdbc):

    connection.driver_class: jdbc driver class of your DBMs (must be included in theclasspath).

    connection.url: jdbc connection url. username and password. dialect: choose one from the table at: http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html#configuration-optional-dialects

    These are the small changes required to use the Hibernate connector with other DBM.Now, we must define the mapping files.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 50

    11.1.2 Creating the mapping files

    The mapping files define how to map a Java Bean with a table of the data base. Firstly,you define which table is used to store the bean. Then you configure which column ofthe table contains each attribute of the bean.

    Our cases were composed by a description represented in the TravelDescriptionclass, and a solution represented in the TravelSolution class. We are going tomap both components to the same table. Some columns will store the attributes of thedescription and other will store the attributes of the solution. Figure 18 illustrates thismapping (the caseId column is in blue because it is used by both components).

    Figure 18: Travel Recommender components mapping

    Lets begin with the mapping file of the solution. You must create the file TravelSolu-tion.hbm.xml into jcolibri.example.travelrecommender with the follow-ing content:

    Listing 21: TravelSolution.hbm.xml

    < !DOCTYPE h i b e r n a t emapping PUBLIC

    " / / H i b e r n a t e / H i b e r n a t e Mapping DTD / / EN"" h t t p : / / h i b e r n a t e . s o u r c e f o r g e . n e t / h i b e r n a t emapping 3 .0 . d t d ">

    < h i b e r n a t emapping de fau l tl a z y =" f a l s e ">< c l a s s

    name=" j c o l i b r i . examples . TravelRecommender . T r a v e l S o l u t i o n "t a b l e =" t r a v e l ">

    < i d name=" i d " column=" c a s e I d ">< / i d >< p r o p e r t y name=" p r i c e " column=" P r i c e " / >< p r o p e r t y name=" h o t e l " column=" H o t e l " / >

    < / c l a s s >< / h i b e r n a t emapping>

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 51

    The class tag sets the jcolibri.examples.TravelRecommender.TravelSolution class to be stored into the travel table.

    The id attribute of TravelSolution is mapped to the caseId column. It is donethrough the id tag that defines the attribute as the primary key of the table.

    Finally, the remaining attributes are mapped to the other columns of the table using theproperty tag.

    Now, we have to create the mapping file of the description. Thefile name is TravelDescription.hbm.xml and must be located intojcolibri.example.travelrecommender:

    Listing 22: TravelDescription.hbm.xml

    < !DOCTYPE h i b e r n a t emapping PUBLIC

    " / / H i b e r n a t e / H i b e r n a t e Mapping DTD / / EN"" h t t p : / / h i b e r n a t e . s o u r c e f o r g e . n e t / h i b e r n a t emapping 3 .0 . d t d ">

    < h i b e r n a t emapping de fau l tl a z y =" f a l s e ">< c l a s s

    name=" j c o l i b r i . examples . TravelRecommender . T r a v e l D e s c r i p t i o n"

    t a b l e =" T r a v e l ">< i d name=" c a s e I d " column=" c a s e I d ">

    < g e n e r a t o r c l a s s =" n a t i v e " / >< / i d >< p r o p e r t y name=" Hol idayType " column=" Hol idayType " / >< p r o p e r t y name=" NumberOfPersons " column=" NumberOfPersons " / >< p r o p e r t y name=" Region " column=" Region ">

    < t y p e name=" j c o l i b r i . c o n n e c t o r . d a t a b a s e u t i l s .Gener i cUse rType ">

    j c o l i b r i . d a t a t y p e s . I n s t a n c e < /param>

    < / t y p e >< / p r o p e r t y >< p r o p e r t y name=" T r a n s p o r t a t i o n " column=" T r a n s p o r t a t i o n " / >< p r o p e r t y name=" D u r a t i o n " column=" D u r a t i o n " / >< p r o p e r t y name=" Season " column=" Season ">

    < t y p e name=" j c o l i b r i . c o n n e c t o r . d a t a b a s e u t i l s . EnumUserType">

    j c o l i b r i . examples . TravelRecommender . T r a v e l D e s c r i p t i o n

    $ Seasons< / param>

    < / t y p e >< / p r o p e r t y >< p r o p e r t y name=" Accommodation " column=" Accommodation ">

    < t y p e name=" j c o l i b r i . c o n n e c t o r . d a t a b a s e u t i l s . EnumUserType">

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 52

    j c o l i b r i . examples . TravelRecommender . T r a v e l D e s c r i p t i o n

    $ AccommodationTypes< / param>

    < / t y p e >< / p r o p e r t y >

    < / c l a s s >< / h i b e r n a t emapping> Here, we are mapping again the jcolibri.examples.TravelRecommender.TravelDescription class into the travel table using the class tag.

    Then we map the caseId attribute to the column with the same name caseId. Thisattribute is the primary key of the table because we are mapping it using the id tag.Inside this tag we found the generator tag that defines how to define unique ids fornew Beans that will be inserted in the table. Defining this property as native, Hibernatedelegates this task to the underlaying DBM.

    After this tag we continue with the property tags that map normal attributes to thecolumns of the database. However there are some of them that have the type childtag. Those attributes have a special mapping because are user-defined types like theregion attribute or because they are enumerates like season or accommodation.

    To map a user-defined type with Hibernate we have to use the type tag with thejcolibri.connector.databaseutils.GenericUserType value. Thisclass allows to map any user-defined type indicated by the param tag. As ex-plained in Section 9.4, the user-defined types in jCOLIBRI2 must implement thejcolibri.connector.TypeAdaptor interface. This way, the general templateto map user-defined types is:

    Listing 23: Mapping template for user-defined types < p r o p e r t y name=" b e a n A t t r i b u t e " column=" tab leCo lumn ">

    < t y p e name=" j c o l i b r i . c o n n e c t o r . d a t a b a s e u t i l s . Gener i cUserType ">

    i m p l e m e n t a t i o n _ o f _ T y p e A d a p t o r _ t h a t _ d e f i n e s _ a t t r i b u t e _ t y p e

    < / param>< / t y p e >

    < / p r o p e r t y > The other special configuration is the mapping of enumerates. To do this, we have touse the jcolibri.connector.databaseutils.EnumUserType class in thetype tag and then indicate the class of the enumerate into the param tag. The generaltemplate is:

    Listing 24: Mapping template for enumerates < p r o p e r t y name=" b e a n A t t r i b u t e " column=" tab leCo lumn ">

    < t y p e name=" j c o l i b r i . c o n n e c t o r . d a t a b a s e u t i l s . EnumUserType ">

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 11.1 Configuring the connector 53

    e n u m e r a t e _ c l a s s _ t h a t _ d e f i n e s _ a t t r i b u t e _ t y p e< / param>

    < / t y p e >< / p r o p e r t y > The complete Hibernate mapping documentation can be found at: http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 12. Using Ontologies in CBR applications 54

    12 Using Ontologies in CBR applications

    For the last few years our research group has been working in Knowledge IntensiveCBR using ontologies [23, 16, 17, 19]. We claim that ontologies are useful for design-ing KI-CBR applications because they allow the knowledge engineer to use knowledgealready acquired, conceptualized and implemented in a formal language, like DLs basedlanguages, reducing considerably the knowledge acquisition bottleneck. Our approachproposes the use of ontologies to build models of general domain knowledge. Althoughin a CBR system the main source of knowledge is the set of previous experiences, ourapproach to CBR is towards integrated applications that combine case specific knowl-edge with models of general domain knowledge. The more knowledge is embeddedinto the system, the more effective is expected to be. Semantic CBR processes can takeadvantage of this domain knowledge and obtain more accurate results.

    We state that the formalization of ontologies is useful for the CBR community regardingdifferent purposes, namely:

    1. Persistence of cases and/or indexes using individuals or concepts that are embed-ded in the ontology itself.

    2. As the vocabulary to define the case structure, either if the cases are embedded asindividuals in the ontology itself, or if the cases are stored in a different persis-tence media as a data base.

    3. As the terminology to define the query vocabulary. The user can express betterhis requirements if he can use a richer vocabulary to define the query. During thesimilarity computation the ontology allows to bridge the gap between the queryterminology and the case base terminology.

    4. Retrieval and similarity [22, 46, 38], adaptation [23] and learning [1].

    5. Knowledge reuse between different CBR systems.

    jCOLIBRI2 allows to implement CBR systems with all these features or, at least, offersan ontology management architecture that is the basis of this kind of CBR applications.

    The ontology support of jCOLIBRI2 is built arround the OntoBridge library (http://gaia.fdi.ucm.es/grupo/projects/ontobridge/). This library wasdeveloped by the GAIA research group to easily manage ontologies and DLs reason-ers. It is base on the JENA3 library and allows to reason with several DL reasoners likePellet 4.

    3http://jena.sourceforge.net/4http://pellet.owldl.com

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 12.1 Case Base persistence in ontologies 55

    12.1 Case Base persistence in ontologies

    jCOLIBRI2 allows to use ontologies as a persistence media for the cases. The con-nectors architecture of the framework eases the incorporation of this feature as thesingle requirement is the implementation of a new connector. This connector isjcolibri.connector.OntologyConnector. To learn how to use it, read thedocumentation of the Test 10 in the examples. This example shows how to map a case(similar to the Travel Recommender case of this tutorial) into an ontology. Figure 19illustrates this process.

    Figure 19: Case mapping in a ontology

    To explain in detail how works the OntologyConnector lets use the example shownin Figure 20. A concept of the ontology will define the cases (VACATION_CASE)and the other related concepts define the attributes (CATEGORY, PRICE and HOLI-DAY_TYPE). This way, the OntologyConnector obtains the instances of the VA-CATION_CASE concept (case1, and case2) and follows their relationships to obtain thevalues of the attributes (That will be instances of the concepts that define the attributes:CATEGORY, PRICE and HOLIDAY_TYPE).

    12.2 Computing similarities using ontologies

    There are different approaches for computing similarity measures based on ontologiesthat are provided in jCOLIBRI2. It initially offers four functions to compute the concept

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 12.3 Case and Query vocabulary 56

    Figure 20: OntologyConnector behavior example

    based similarity that depends on the location of the cases in the ontology. Of course,other similarity functions can easily be included. These similarity functions are shownin Figure 21 whereas Figure 22 explains how they work with a small ontology taken asexample.

    These similarity measures are implemented in the package: jcolibri.method.retrieve.NNretrieval.similarity.local.ontology. Theuse of these measures will be explained in Section 13.

    12.3 Case and Query vocabulary

    Regarding the case vocabulary, there is a direct approach consisting on the use of adomain ontology in an object-oriented way: concepts are types, or classes, individualsare allowed values, or objects, and relations are the attributes describing the objects.There are also simple types like string or numbers that are considered in the traditionalway. The case structure is defined using types from the ontology even if the cases are notstored as individuals in the ontology. For example, the Region concept of an ontologycan be used as a type where every one of its instances are the type values: Madrid,Barcelona, Sevilla,...

    Regarding the query vocabulary we have two options to define the queries:

    Using exactly the same vocabulary used in the cases, i.e, the same types used inthe case structure definition.

    Using the ontology as the query vocabulary, what allows richer queries. The usercan express better his requirements if he can use a richer vocabulary to define thequery. During the similarity computation the ontology allows to bridge the gapbetween the query terminology and the case base terminology.

    Group for Artificial Intelligence Applications jCOLIBRI2 Tutorial

  • 12.3 Case and Query vocabulary 57

    fdeep_basic(i1, i2) =max(prof(LCS(i1, i2)))

    maxCiCN

    (prof(Ci))fdeep(i1, i2) =

    max(prof(LCS(i1, i2)))max(prof(i1), prof(i2))

    cosine(i1, i2) =

    sim(t(i1), t(i2)) =

    dit(i1)(super(di, CN))

    dit(i2)

    (super(di, CN))

    dit(i1)(super(di, CN))

    dit(i2)(super(di, CN))

    detail(i1, i2) =detail(t(i1), t(i2)) = 1 1

    2

    dit(i1)(super(di, CN))

    dit(i2)

    (super(di, CN))

    Where:

    CN is the set of all the concepts in the current knowledge basesuper(c, C) is the subset of concepts in C which are superconcepts of cLCS(i1, i2) is the set of the least common subsumer concepts of the twogiven individualsprof(c) is the depth of concept ct(i) is the set of concepts the individual i is instance of

    Figure 21: Concept based similarity functions in jCOLIBRI2

    Example: In the travel domain, lets suppose we have an existing case base whereit is defined an enumerated type for the Region attribute where the allowed values arecountries: Spain, Franc