ConceptNet.java |
/* Copyright (C) 2009 CSE,IIT Bombay http://www.cse.iitb.ac.in This file is part of the ConStore open source storage facility for concept-nets. ConStore is free software and distributed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License; you can copy, distribute and transmit the work with the work attribution in the manner specified by the author or licensor. You may not use this work for commercial purposes and may not alter, transform, or build upon this work. Please refer the legal code of the license, available at http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode ConStore is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ package iitb.con.net; import iitb.con.core.ConStoreConstants; import iitb.con.core.Entity; import iitb.con.core.Instance; import iitb.con.core.Relation; import iitb.con.core.Type; import iitb.con.io.BufferedFileAdapter; import iitb.con.io.IOAdapter; import iitb.con.query.Query; import iitb.con.util.FileUtil; import iitb.con.util.Result; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.logging.Level; import java.util.logging.Logger; /** * ConceptNet abstracts and manages the {@link Type} and {@link Instance} * in the concept-nets.<br> * It provides methods to add, update, and remove the types and instances in the concept-nets.<br> * It also provides a high level retrieval support through <tt>Query</tt> object.<br> * * @author Prathab K * @see Type * @see Instance * @see Entity * @see Relation */ public abstract class ConceptNet { static Logger log = Logger.getLogger(ConceptNet.class.getName()); /** Name of the Concept-Net */ protected static String ConceptNetName; /** Directory of the Concept-Net */ protected static String ConceptNetDir; /** Mode(r, rw) in which concept-net was opened */ protected static String conceptNetMode; /** * Creates new concept-net and opens in the specified mode.<br> * The opening mode can read (r) or read-write (rw). * @param name concept-net name * @param mode opening mode * @throws IOException if the file operations fails. */ protected static ConceptNet create (String name, String mode) throws IOException { if(!FileUtil.isFileExists(name)) { try{ createConceptNetFiles(name); return open(name,mode); }catch(FileNotFoundException fe) { log.log(Level.SEVERE, "ConceptNet FileNotFound", fe); }catch(IOException ie) { log.log(Level.SEVERE, "ConceptNet file exception", ie); } return null; }else { log.info("ConceptNet '" + name + "' already exists"); return null; } } /** * Creates the concept-net folder and files. Initializes them with deafult values. * @param name concept-net name * @throws IOException if the file operations fails. */ private static void createConceptNetFiles(String name) throws IOException { if(!FileUtil.createDir(name)) { throw new IOException("Error in creating the ConceptNet directory"); } ConceptNetDir = name + FileUtil.FILE_SEPARATOR; ConceptNetName = ConceptNetDir+ FileUtil.getName(name); FileUtil.createFile(ConceptNetDir + ConStoreConstants.META_FILE); FileUtil.createFile(ConceptNetDir + ConStoreConstants.NET_FILE); FileUtil.createFile(ConceptNetDir + ConStoreConstants.INSTANCE_TABLE_FILE); FileUtil.createFile(ConceptNetDir + ConStoreConstants.RELATION_TABLE_FILE); FileUtil.createFile(ConceptNetDir + ConStoreConstants.RELATION_IDX_FILE); String fslFileName = ConceptNetDir + ConStoreConstants.NET_FSL_FILE; FileUtil.createFile(fslFileName); initializeFslFile(fslFileName); String propFileName = ConceptNetDir + ConStoreConstants.PROPERTIES_FILE; FileUtil.createFile(propFileName); initializePropFile(propFileName); } /** * Initializes the concept-net <tt>.fsl</tt> files.<br> * <tt>.fsl</tt> file is used to maintain the file's free spaces blocks information. * @param name <tt>.fsl</tt> file name */ protected static void initializeFslFile(String name){ try { IOAdapter fslFile = new BufferedFileAdapter(name, "rw"); ByteBuffer buf = ByteBuffer.allocate(16); buf.putLong(0); //buf.putLong(Integer.MAX_VALUE - 1); //This restricts maximum file size (2GB) buf.putLong(Long.MAX_VALUE - 1); fslFile.write(buf, 0); fslFile.close(); }catch(FileNotFoundException fe) { log.log(Level.SEVERE, name + " FileNotFound", fe); }catch(IOException ie) { log.log(Level.SEVERE, name + " file exception", ie); } } /** * Initializes the concept-net <tt>.prop</tt> files.<br> * <tt>.prop</tt> file is the concept-net properties file. * @param name <tt>.prop</tt> file name */ private static void initializePropFile(String name){ try { FileOutputStream outputFile = new FileOutputStream (name); String s = "TYPE_ID=0\n"; outputFile.write(s.getBytes()); s = "INSTANCE_ID=0\n"; outputFile.write(s.getBytes()); s = "IO_BUFFER_SIZE=4096\n"; outputFile.write(s.getBytes()); s = "QUERY_CACHE_SIZE=2097152\n"; outputFile.write(s.getBytes()); s = "INSTANCE_CACHE_SIZE=1048576"; outputFile.write(s.getBytes()); outputFile.close(); }catch(FileNotFoundException fe) { log.log(Level.SEVERE, name + " FileNotFound", fe); }catch(IOException ie) { log.log(Level.SEVERE, name + " file exception", ie); } } /** * Opens the existing concept-net in the specified mode.<br> * The opening mode can read (r) or read-write (rw).<br> * If Concept-Net does not exists then it throws exception. * @param name concept-net name * @param mode opening mode * @throws FileNotFoundException if the specified concept-net file is not found * @throws IOException if file operation fails * */ protected static ConceptNet open (String name, String mode) throws FileNotFoundException, IOException { ConceptNetDir = name + FileUtil.FILE_SEPARATOR; ConceptNetName = ConceptNetDir+ FileUtil.getName(name); conceptNetMode = mode; FileConceptNet fileConceptNet = new FileConceptNet(name, mode); return fileConceptNet; } /** * Closes the concept-net. * @return <code>true</code> on success */ public abstract boolean close(); /* * Deletes the existing concept-net. * @param conceptNet <code>ConceptNet</code> instance * @return <code>true</code> on success */ /*protected static boolean delete(ConceptNet conceptNet) { return FileUtil.deleteDir(ConceptNetDir); }*/ /** * Deletes the existing concept-net. * @param conceptNetDir Concept-Net Directory * @return <code>true</code> on success */ protected static boolean delete(String conceptNetDir) { return FileUtil.deleteDir(conceptNetDir); } /** * Restarts the concept-net */ protected void restart() { try { close(); open(ConceptNetDir, conceptNetMode); }catch(FileNotFoundException fe) { log.log(Level.SEVERE, ConceptNetName + " FileNotFound", fe); }catch(IOException ie) { log.log(Level.SEVERE, ConceptNetName + " file exception", ie); } } /** * Inducts the <code>Type</code> to the concept-nets. * @param type {@link Type} item. * @return {@link Result} */ public abstract Result induct(Type type); /** * Returns <code>true</code> if the type with the same name exists * in the concept-net * @param name type name * @return boolean - <code>true</code> if type exists */ public abstract boolean hasType(String name); /** * Returns the existing <code>Type</code> from * the concept-nets.<br> * Returns <code>NULL</code>, if <code>Type</code> does not exists. * @param name type name * @return {@link Type} */ public abstract Type getType(String name); /** * Adds the <code>Instance</code> to the concept-nets. * @param instance {@link Instance} item. * @return instance id */ public abstract int add(Instance instance); /** * Updates the <code>Instance</code> to the concept-nets. * Returns status as <code>false</code> if <code>Instance</code> * does not exists. * @param instance {@link Instance} item. * @return {@link Result} */ public abstract Result update(Instance instance); /** * Removes the existing <code>Instance</code> from * the concept-nets.<br> * @param instance {@link Instance} * @return {@link Result} */ public abstract Result remove(Instance instance); /** * Removes the existing <code>Instance</code> from * the concept-nets.<br> * @param instanceId instance's id * @return {@link Result} */ public abstract Result remove(int instanceId); /** * Returns the <tt>Query</tt> object. User can query the concept-net * using the methods of the <tt>Query</tt> object. * @return {@link Query} */ public abstract Query query(); /** * Creates the index for the specified type attribute.<br> * @param <K> data type object * @param typeName type name * @param attributeName attribute name * @param dataType data type * @return {@link Result} */ public abstract <K extends Comparable<K>> Result createIndex(String typeName, String attributeName, K dataType); /* * Index the relations in the concept-net. * @return {@link Result} */ //public abstract Result indexRelations(); /** * Drops the index for the specified type attribute. * @param type <tt>Type</tt> object * @param attributeName attribute name * @return {@link Result} */ public abstract Result dropIndex(Type type, String attributeName); /** * Commits the changes to the concept-net.<p> * The operations like induct, add, update, remove, etc., does not affect the * concept-net unless it is committed. * @return {@link Result} */ public abstract Result commit(); /** * Display the statistical information about the concept-net. */ public abstract void showStaistics(); /** * Returns the concept-net name along with path * @return concept-net name */ public static String getConceptNetName() { return ConceptNetName; } /** * Returns the concept-net directory * @return concept-net directory as string */ public static String getConceptNetDir() { return ConceptNetDir; } }