$search
00001 package instruction.opencyc; 00002 00003 import java.io.File; 00004 import java.io.FileReader; 00005 import java.io.FileWriter; 00006 import java.io.IOException; 00007 import java.util.ArrayList; 00008 import java.util.HashMap; 00009 import java.util.Iterator; 00010 import java.util.List; 00011 import java.util.Map; 00012 import java.util.Set; 00013 00014 import org.jdom.Document; 00015 import org.jdom.Element; 00016 import org.jdom.JDOMException; 00017 import org.jdom.input.SAXBuilder; 00018 import org.jdom.output.XMLOutputter; 00019 00020 public class CycCache { 00021 00022 public static final String TAG_CACHE = "cyccache"; 00023 00024 public static final String TAG_SYNSET = "synsetid"; 00025 00026 public static final String TAG_CONCEPT = "cycconcept"; 00027 00028 public static final String ATTR_VALUE = "value"; 00029 00030 Map<String, List<String>> map = new HashMap<String, List<String>>(); 00031 00039 public void tell( String synset, List<String> concepts ) { 00040 00041 map.put( synset, concepts ); 00042 } 00043 00051 public List<String> ask( String synset ) { 00052 00053 return map.get( synset ); 00054 } 00055 00063 @SuppressWarnings("unchecked") 00064 public void load() throws IOException { 00065 00066 File file = new File( "etc/cyccache.xml" ); 00067 if ( ! file.exists() ) 00068 return; 00069 00070 FileReader reader = new FileReader( file ); 00071 00072 SAXBuilder sax = new SAXBuilder(); 00073 try { 00074 Document doc; 00075 00076 doc = sax.build( reader ); 00077 00078 Element cache = doc.getRootElement(); 00079 00080 List<Element> synsetids = cache.getChildren(); 00081 00082 // ================================================================== 00083 // Iterate over Synset-IDs 00084 // ================================================================== 00085 for ( Iterator<Element> i = synsetids.iterator(); i.hasNext(); ) { 00086 00087 Element synsetid = i.next(); 00088 String id = synsetid.getAttributeValue( ATTR_VALUE ); 00089 if ( id == null ) 00090 continue; 00091 00092 List<String> conceptNames = new ArrayList<String>(); 00093 List<Element> concepts = synsetid.getChildren(); 00094 00095 // ================================================================== 00096 // Iterate over associated Cyc concept names 00097 // ================================================================== 00098 for ( Iterator<Element> j = concepts.iterator(); j.hasNext(); ) { 00099 Element concept = j.next(); 00100 String conceptName = concept.getAttributeValue( ATTR_VALUE ); 00101 if ( conceptName == null ) 00102 continue; 00103 conceptNames.add( conceptName ); 00104 00105 } 00106 00107 map.put( id, conceptNames ); 00108 00109 } 00110 } 00111 catch ( JDOMException e ) { 00112 e.printStackTrace(); 00113 } 00114 00115 } 00116 00122 public void save() throws Exception { 00123 00124 File file = new File( "etc/cyccache.xml" ); 00125 if ( ! file.exists() ) 00126 file.createNewFile(); 00127 00128 Element cache = new Element( TAG_CACHE ); 00129 00130 Set<String> synsets = map.keySet(); 00131 00132 // ================================================================== 00133 // Iterate over all keys (Synset-IDs) contained in the cache 00134 // ================================================================== 00135 for ( Iterator<String> i = synsets.iterator(); i.hasNext(); ) { 00136 Element synset = new Element( TAG_SYNSET ); 00137 String synsetid = i.next(); 00138 synset.setAttribute( ATTR_VALUE, synsetid ); 00139 00140 // ================================================================== 00141 // Iterate over all Cyc concept names associated with the current 00142 // Synset-ID 00143 // ================================================================== 00144 List<String> concepts = map.get( synsetid ); 00145 00146 for ( Iterator<String> j = concepts.iterator(); j.hasNext(); ) { 00147 String conceptName = j.next(); 00148 Element concept = new Element( TAG_CONCEPT ); 00149 concept.setAttribute( ATTR_VALUE, conceptName ); 00150 synset.addContent( concept ); 00151 } 00152 cache.addContent( synset ); 00153 } 00154 00155 // ================================================================== 00156 // Create XML document and write it to file 00157 // ================================================================== 00158 Document doc = new Document( cache ); 00159 00160 XMLOutputter outPutter = new XMLOutputter( " ", true ); 00161 FileWriter outStream = new FileWriter( file ); 00162 00163 outPutter.output( doc, outStream ); 00164 } 00165 00166 }