Exemple #1
0
 /*
  * Arrays
  */
 private static void Log <T>(Redwood.RedwoodChannels channels, string description, T[] array)
 {
     Redwood.StartTrack(description);
     if (array == null)
     {
         channels.Log("(array is null)");
     }
     else
     {
         if (array.Length == 0)
         {
             channels.Log("(empty)");
         }
         else
         {
             int index = 0;
             foreach (T item in array)
             {
                 if (Dispatchable(item))
                 {
                     Log(channels, "Index " + index, item);
                 }
                 else
                 {
                     channels.Logf("Index %d: %s", index, item);
                 }
                 index++;
             }
         }
     }
     Redwood.EndTrack(description);
 }
Exemple #2
0
 /*
  * Iterables (includes Collection, List, Set, etc.)
  */
 private static void Log <T>(Redwood.RedwoodChannels channels, string description, IEnumerable <T> iterable)
 {
     Redwood.StartTrack(description);
     if (iterable == null)
     {
         channels.Log("(iterable is null)");
     }
     else
     {
         int index = 0;
         foreach (T item in iterable)
         {
             if (Dispatchable(item) && item != iterable)
             {
                 Log(channels, "Index " + index, item);
             }
             else
             {
                 channels.Logf("Index %d: %s", index, item == iterable ? "...<infinite loop>" : item);
             }
             index++;
         }
         if (index == 0)
         {
             channels.Log("(empty)");
         }
     }
     Redwood.EndTrack(description);
 }
 public KBPTokensregexExtractor(string tokensregexDir, bool verbose)
 {
     if (verbose)
     {
         logger.Log("Creating TokensRegexExtractor");
     }
     // Create extractors
     foreach (KBPRelationExtractor.RelationType rel in KBPRelationExtractor.RelationType.Values())
     {
         string relFileNameComponent = rel.canonicalName.ReplaceAll(":", "_");
         string path = tokensregexDir + File.separator + relFileNameComponent.ReplaceAll("/", "SLASH") + ".rules";
         if (IOUtils.ExistsInClasspathOrFileSystem(path))
         {
             IList <string> listFiles = new List <string>();
             listFiles.Add(tokensregexDir + File.separator + "defs.rules");
             listFiles.Add(path);
             if (verbose)
             {
                 logger.Log("Rule files for relation " + rel + " is " + path);
             }
             Env env = TokenSequencePattern.GetNewEnv();
             env.Bind("collapseExtractionRules", true);
             env.Bind("verbose", verbose);
             CoreMapExpressionExtractor extr = CoreMapExpressionExtractor.CreateExtractorFromFiles(env, listFiles).KeepTemporaryTags();
             rules[rel] = extr;
         }
     }
 }
Exemple #4
0
 /// <exception cref="System.IO.IOException"/>
 public KBPSemgrexExtractor(string semgrexdir, bool verbose)
 {
     if (verbose)
     {
         logger.Log("Creating SemgrexRegexExtractor");
     }
     // Create extractors
     foreach (KBPRelationExtractor.RelationType rel in KBPRelationExtractor.RelationType.Values())
     {
         string relFileNameComponent = rel.canonicalName.ReplaceAll(":", "_");
         string filename             = semgrexdir + File.separator + relFileNameComponent.Replace("/", "SLASH") + ".rules";
         if (IOUtils.ExistsInClasspathOrFileSystem(filename))
         {
             IList <SemgrexPattern> rulesforrel = SemgrexBatchParser.CompileStream(IOUtils.GetInputStreamFromURLOrClasspathOrFileSystem(filename));
             if (verbose)
             {
                 logger.Log("Read " + rulesforrel.Count + " rules from " + filename + " for relation " + rel);
             }
             rules[rel] = rulesforrel;
         }
     }
 }
Exemple #5
0
 /// <summary>Pretty log an object.</summary>
 /// <param name="channels">the channels to pretty log to</param>
 /// <param name="description">denote the object in the logs (via a track name, etc.).</param>
 /// <param name="obj">the object to be pretty logged</param>
 public static void Log <T>(Redwood.RedwoodChannels channels, string description, object obj)
 {
     // TODO perhaps some reflection magic can simplify this process?
     if (obj is IDictionary)
     {
         Log(channels, description, (IDictionary)obj);
     }
     else
     {
         if (obj is IPrettyLoggable)
         {
             ((IPrettyLoggable)obj).PrettyLog(channels, description);
         }
         else
         {
             if (obj is Dictionary)
             {
                 Log(channels, description, (Dictionary)obj);
             }
             else
             {
                 if (obj is IEnumerable)
                 {
                     Log(channels, description, (IEnumerable)obj);
                 }
                 else
                 {
                     if (obj.GetType().IsArray)
                     {
                         object[] arrayCopy;
                         // the array to log
                         if (obj.GetType().GetElementType().IsPrimitive)
                         {
                             //(case: a primitive array)
                             Type componentClass = obj.GetType().GetElementType();
                             if (typeof(bool).IsAssignableFrom(componentClass))
                             {
                                 arrayCopy = new object[((bool[])obj).Length];
                                 for (int i = 0; i < arrayCopy.Length; i++)
                                 {
                                     arrayCopy[i] = ((bool[])obj)[i];
                                 }
                             }
                             else
                             {
                                 if (typeof(byte).IsAssignableFrom(componentClass))
                                 {
                                     arrayCopy = new object[((byte[])obj).Length];
                                     for (int i = 0; i < arrayCopy.Length; i++)
                                     {
                                         arrayCopy[i] = ((byte[])obj)[i];
                                     }
                                 }
                                 else
                                 {
                                     if (typeof(char).IsAssignableFrom(componentClass))
                                     {
                                         arrayCopy = new object[((char[])obj).Length];
                                         for (int i = 0; i < arrayCopy.Length; i++)
                                         {
                                             arrayCopy[i] = ((char[])obj)[i];
                                         }
                                     }
                                     else
                                     {
                                         if (typeof(short).IsAssignableFrom(componentClass))
                                         {
                                             arrayCopy = new object[((short[])obj).Length];
                                             for (int i = 0; i < arrayCopy.Length; i++)
                                             {
                                                 arrayCopy[i] = ((short[])obj)[i];
                                             }
                                         }
                                         else
                                         {
                                             if (typeof(int).IsAssignableFrom(componentClass))
                                             {
                                                 arrayCopy = new object[((int[])obj).Length];
                                                 for (int i = 0; i < arrayCopy.Length; i++)
                                                 {
                                                     arrayCopy[i] = ((int[])obj)[i];
                                                 }
                                             }
                                             else
                                             {
                                                 if (typeof(long).IsAssignableFrom(componentClass))
                                                 {
                                                     arrayCopy = new object[((long[])obj).Length];
                                                     for (int i = 0; i < arrayCopy.Length; i++)
                                                     {
                                                         arrayCopy[i] = ((long[])obj)[i];
                                                     }
                                                 }
                                                 else
                                                 {
                                                     if (typeof(float).IsAssignableFrom(componentClass))
                                                     {
                                                         arrayCopy = new object[((float[])obj).Length];
                                                         for (int i = 0; i < arrayCopy.Length; i++)
                                                         {
                                                             arrayCopy[i] = ((float[])obj)[i];
                                                         }
                                                     }
                                                     else
                                                     {
                                                         if (typeof(double).IsAssignableFrom(componentClass))
                                                         {
                                                             arrayCopy = new object[((double[])obj).Length];
                                                             for (int i = 0; i < arrayCopy.Length; i++)
                                                             {
                                                                 arrayCopy[i] = ((double[])obj)[i];
                                                             }
                                                         }
                                                         else
                                                         {
                                                             throw new InvalidOperationException("I forgot about the primitive class: " + componentClass);
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         else
                         {
                             //(case: a regular array)
                             arrayCopy = (T[])obj;
                         }
                         Log(channels, description, arrayCopy);
                     }
                     else
                     {
                         if (!description.Equals(string.Empty))
                         {
                             description += ": ";
                         }
                         channels.Log(description + obj);
                     }
                 }
             }
         }
     }
 }
Exemple #6
0
 /*
  * Mappings
  */
 private static void Log <K, V>(Redwood.RedwoodChannels channels, string description, IDictionary <K, V> mapping)
 {
     Redwood.StartTrack(description);
     if (mapping == null)
     {
         channels.Log("(mapping is null)");
     }
     else
     {
         if (mapping.IsEmpty())
         {
             channels.Log("(empty)");
         }
         else
         {
             // convert keys to sorted list, if possible
             IList <K> keys = new LinkedList <K>();
             foreach (K key in mapping.Keys)
             {
                 keys.Add(key);
             }
             keys.Sort(null);
             // log key/value pairs
             int entryCounter = 0;
             foreach (K key_1 in keys)
             {
                 V value = mapping[key_1];
                 if (!Dispatchable(key_1) && Dispatchable(value))
                 {
                     Log(channels, key_1.ToString(), value);
                 }
                 else
                 {
                     if (Dispatchable(key_1) || Dispatchable(value))
                     {
                         Redwood.StartTrack("Entry " + entryCounter);
                         if (Dispatchable(key_1))
                         {
                             Log(channels, "Key", key_1);
                         }
                         else
                         {
                             channels.Logf("Key %s", key_1);
                         }
                         if (Dispatchable(value))
                         {
                             Log(channels, "Value", value);
                         }
                         else
                         {
                             channels.Logf("Value %s", value);
                         }
                         Redwood.EndTrack("Entry " + entryCounter);
                     }
                     else
                     {
                         channels.Logf("%s = %s", key_1, value);
                     }
                 }
                 entryCounter++;
             }
         }
     }
     Redwood.EndTrack(description);
 }