Author: ssmiweve
Date: 2008-04-02 12:15:17 +0200 (Wed, 02 Apr 2008)
New Revision: 6328
Modified:
branches/2.16/generic.sesam/war/src/main/conf/AnalysisRules.xml
branches/2.16/query-api/src/main/java/no/sesat/search/query/token/TokenPredicate.java
Log:
SEARCH-4421 - Anonymous TokenPredicates
Modified: branches/2.16/generic.sesam/war/src/main/conf/AnalysisRules.xml
===================================================================
--- branches/2.16/generic.sesam/war/src/main/conf/AnalysisRules.xml
2008-04-02 07:55:17 UTC (rev 6327)
+++ branches/2.16/generic.sesam/war/src/main/conf/AnalysisRules.xml
2008-04-02 10:15:17 UTC (rev 6328)
@@ -342,7 +342,6 @@
<score predicate="priocompanyNameWithoutFirstNameAndGeo">400</score>
<score predicate="exactWiki-notCompanyUnlessFullname">300</score>
<score predicate="NEWS_TRIGGER">250</score>
- <score predicate="ALWAYSTRUE">100</score>
<score predicate="companyNameWithoutPrioCompanyName">-20</score>
<score predicate="tnsOrCompanyNotPerson">-20</score>
<score predicate="anyMagic">-500</score>
Modified:
branches/2.16/query-api/src/main/java/no/sesat/search/query/token/TokenPredicate.java
===================================================================
---
branches/2.16/query-api/src/main/java/no/sesat/search/query/token/TokenPredicate.java
2008-04-02 07:55:17 UTC (rev 6327)
+++
branches/2.16/query-api/src/main/java/no/sesat/search/query/token/TokenPredicate.java
2008-04-02 10:15:17 UTC (rev 6328)
@@ -56,106 +56,42 @@
this.name = name;
this.cls = cls;
- Static.TOKENS_BY_TYPE.put(this, new
CopyOnWriteArraySet<TokenPredicate>());
+ TokenPredicateImpl.TOKENS_BY_TYPE.put(this, new
CopyOnWriteArraySet<TokenPredicate>());
}
}
- public static final class Static{
+ /**
+ *
+ * @return
+ */
+ String name();
- private static final String ERR_ARG_NOT_TOKEN_EVALUATOR_FACTORY
- = "Argument to evaluate must be an instance of a
TokenEvaluationEngine";
- private static final String ERR_TOKEN_NOT_FOUND = "Token argument not
found ";
- private static final String ERR_METHOD_CLOSED_TO_OTHER_THREADS
- = "TokenPredicate.evaluate(..) can only be used by same thread
that created TokenEvaluationEngine!";
- private static final String ERR_ENGINE_MISSING_STATE =
"TokenEvaluationEngine must have state assigned";
-
-
- private static final Set<TokenPredicate> TOKENS = new
CopyOnWriteArraySet<TokenPredicate>();
- private static final Map<String,TokenPredicate> ANONYMOUS_TOKENS = new
ConcurrentHashMap<String,TokenPredicate>();
- private static final Set<TokenPredicate> MAGIC_TOKENS = new
CopyOnWriteArraySet<TokenPredicate>(); // TODO take out of sesat
- private static final Set<TokenPredicate> TRIGGER_TOKENS = new
CopyOnWriteArraySet<TokenPredicate>(); // TODO take out of sesat
- private static final Map<Type,Set<TokenPredicate>> TOKENS_BY_TYPE =
new ConcurrentHashMap<Type,Set<TokenPredicate>>();
-
- /**
- *
- * @param name
- * @return null if no such a TokenPredicate exists.
- */
- public static TokenPredicate getTokenPredicate(final String name){
-
- return null != Categories.valueOf(name) ? Categories.valueOf(name)
: getAnonymousTokenPredicate(name);
- }
-
- /**
- *
- * @param name
- * @return
- */
- public static TokenPredicate getAnonymousTokenPredicate(final String
name){
-
- return ANONYMOUS_TOKENS.get(name);
- }
-
- /** Creates an anonymous TokenPredicate.
- * Ensure the name doesn't clash with anonymous TokenPredicates from
other skins.
- *
- * @param name
- * @param type
- * @return
- */
- public static TokenPredicate createAnonymousTokenPredicate(final
String name, final Type type){
-
- ANONYMOUS_TOKENS.put(name, new TokenPredicateImpl(name, type));
- return getAnonymousTokenPredicate(name);
- }
-
- /** Utility method to use all TokenPredicates in existence.
- * @return
- */
- public static Set<TokenPredicate> getTokenPredicates() {
-
- return Collections.unmodifiableSet(TOKENS);
- }
-
- /** Utility method to use all TokenPredicates belonging to a given
type.
- * @param type
- * @return
- */
- public static Set<TokenPredicate> getTokenPredicates(final Type type) {
- return Collections.unmodifiableSet(TOKENS_BY_TYPE.get(type));
- }
-
- /** Utility method to use all MagicTokenPredicates in existence.
- * @return
- * @deprecated
- */
- public static Set<TokenPredicate> getMagicTokenPredicates() {
- return Collections.unmodifiableSet(MAGIC_TOKENS);
- }
-
- /** Utility method to use all TriggerTokenPredicates in existence.
- * @return
- * @deprecated
- */
- public static Set<TokenPredicate> getTriggerTokenPredicates() {
- return Collections.unmodifiableSet(TRIGGER_TOKENS);
- }
- }
-
- public abstract String name();
-
/**
*
* @return
*/
- public abstract Type getType();
-
+ Type getType();
+
/**
+ * Evaluates to true if fastListName occurs in the query. This method uses
a
+ * TokenEvaluationEngine to get a TokenEvaluator.
*
+ * <b>This method can only be called from the RunningQuery thread, not
spawned search commands.</b>
+ *
+ * @param evalFactory
+ * The TTokenEvaluationEngineused to get a TokenEvaluator for
+ * this fastListName, AND to get the current term in the query
being tokenised.
+ * @return true if, according to the TokenEvaluator provided by the
+ * TokTokenEvaluationEngineastListName evaluates to true.
+ */
+ boolean evaluate(Object evalFactory);
+
+ /** A formalised breakdown of metadata categories that search terms can
match.
+ *
* The break down of these categories should roughly follow what is found
at
* http://en.wikipedia.org/wiki/Portal:Contents/Categorical_index
*/
- public enum Categories implements TokenPredicate {
+ enum Categories implements TokenPredicate {
// Common predicates.
@@ -497,37 +433,39 @@
Categories(final Type type) {
this.impl = new TokenPredicateImpl(name(), type);
+ // replace impl's entry with myself
+ TokenPredicateImpl.TOKENS.remove(impl);
+ TokenPredicateImpl.TOKENS_BY_TYPE.get(type).remove(impl);
+ TokenPredicateImpl.TOKENS.add(this);
+ TokenPredicateImpl.TOKENS_BY_TYPE.get(type).add(this);
}
- /**
- *
- * @return
- */
public Type getType(){
return impl.getType();
}
- /**
- * Evaluates to true if fastListName occurs in the query. This method
uses a
- * TokenEvaluationEngine to get a TokenEvaluator.
- *
- * <b>This method can only be called from the RunningQuery thread, not
spawned search commands.</b>
- *
- * @param evalFactory
- * The TTokenEvaluationEngineused to get a TokenEvaluator
for
- * this fastListName, AND to get the current term in the
query being tokenised.
- * @return true if, according to the TokenEvaluator provided by the
- * TokTokenEvaluationEngineastListName evaluates to true.
- */
public boolean evaluate(final Object evalFactory) {
- return impl.evaluate(evalFactory);
+ return TokenPredicateImpl.evaluate(this, evalFactory);
}
+
+
}
static class TokenPredicateImpl implements TokenPredicate{
+ private static final String ERR_ARG_NOT_TOKEN_EVALUATOR_FACTORY
+ = "Argument to evaluate must be an instance of a
TokenEvaluationEngine";
+ private static final String ERR_METHOD_CLOSED_TO_OTHER_THREADS
+ = "TokenPredicate.evaluate(..) can only be used by same thread
that created TokenEvaluationEngine!";
+ private static final String ERR_ENGINE_MISSING_STATE =
"TokenEvaluationEngine must have state assigned";
+
+ private static final Set<TokenPredicate> TOKENS = new
CopyOnWriteArraySet<TokenPredicate>();
+ private static final Set<TokenPredicate> MAGIC_TOKENS = new
CopyOnWriteArraySet<TokenPredicate>(); // TODO take out of sesat
+ private static final Set<TokenPredicate> TRIGGER_TOKENS = new
CopyOnWriteArraySet<TokenPredicate>(); // TODO take out of sesat
+ private static final Map<Type,Set<TokenPredicate>> TOKENS_BY_TYPE =
new ConcurrentHashMap<Type,Set<TokenPredicate>>();
+
private final String name;
private final Type type;
@@ -539,15 +477,15 @@
// TODO take out of sesat
if(Type.REGEX == type){
if(name.endsWith("_MAGIC")){
- Static.MAGIC_TOKENS.add(this);
+ MAGIC_TOKENS.add(this);
}else if(name.endsWith("_TRIGGER")){
- Static.TRIGGER_TOKENS.add(this);
+ TRIGGER_TOKENS.add(this);
}
}//end-take out of sesat
- Static.TOKENS.add(this);
- Static.TOKENS_BY_TYPE.get(type).add(this);
+ TOKENS.add(this);
+ TOKENS_BY_TYPE.get(type).add(this);
}
public String name(){
@@ -557,17 +495,22 @@
public Type getType(){
return type;
}
-
+
public boolean evaluate(final Object evalFactory) {
+ return TokenPredicateImpl.evaluate(this, evalFactory);
+ }
+
+ public static boolean evaluate(final TokenPredicate token, final
Object evalFactory) {
+
// pre-condition checks
if (! (evalFactory instanceof TokenEvaluationEngine)) {
- throw new
IllegalArgumentException(Static.ERR_ARG_NOT_TOKEN_EVALUATOR_FACTORY);
+ throw new
IllegalArgumentException(ERR_ARG_NOT_TOKEN_EVALUATOR_FACTORY);
}
// process
final TokenEvaluationEngine engine = (TokenEvaluationEngine)
evalFactory;
if(Thread.currentThread() != engine.getOwningThread()){
- throw new
IllegalStateException(Static.ERR_METHOD_CLOSED_TO_OTHER_THREADS);
+ throw new
IllegalStateException(ERR_METHOD_CLOSED_TO_OTHER_THREADS);
}
try{
@@ -580,22 +523,22 @@
// possible predicate is really
// applicable now (in the context of the whole query).
final Set<TokenPredicate> knownPredicates =
engine.getState().getKnownPredicates();
- if(null != knownPredicates && knownPredicates.contains(this)){
+ if(null != knownPredicates && knownPredicates.contains(token)){
return true;
}
- final TokenEvaluator evaluator = engine.getEvaluator(this);
+ final TokenEvaluator evaluator = engine.getEvaluator(token);
if(null != engine.getState().getTerm()){
// Single term or clause evaluation
- return evaluator.evaluateToken(this,
engine.getState().getTerm(), engine.getQueryString());
+ return evaluator.evaluateToken(token,
engine.getState().getTerm(), engine.getQueryString());
}else if(null != engine.getState().getQuery()){
// Whole query evaluation
- return
engine.getState().getPossiblePredicates().contains(this)
- && evaluator.evaluateToken(this, null,
engine.getQueryString());
+ return
engine.getState().getPossiblePredicates().contains(token)
+ && evaluator.evaluateToken(token, null,
engine.getQueryString());
}
@@ -605,15 +548,93 @@
throw new EvaluationException(ie);
}
- throw new IllegalStateException(Static.ERR_ENGINE_MISSING_STATE);
+ throw new IllegalStateException(ERR_ENGINE_MISSING_STATE);
}
}
- public static final class EvaluationException extends RuntimeException{
+ static final class EvaluationException extends RuntimeException{
public EvaluationException(final VeryFastListQueryException vflqe){
super(vflqe);
}
}
+
+ /** @todo move out to TokenPredicateUtility. **/
+ static final class Static{
+
+ private static final Map<String,TokenPredicate> ANONYMOUS_TOKENS = new
ConcurrentHashMap<String,TokenPredicate>();
+
+ static{
+ // ensures all the enums have been loaded before any of the
following static methods are called.
+ // offspin to this is that there can be no references back to
Static from Categories or TokenPredicateImpl.
+ Categories.values();
+ }
+
+ /**
+ *
+ * @param name
+ * @return null if no such a TokenPredicate exists.
+ */
+ public static TokenPredicate getTokenPredicate(final String name){
+
+ return null != Categories.valueOf(name) ? Categories.valueOf(name)
: getAnonymousTokenPredicate(name);
+ }
+
+ /**
+ *
+ * @param name
+ * @return
+ */
+ public static TokenPredicate getAnonymousTokenPredicate(final String
name){
+
+ return ANONYMOUS_TOKENS.get(name);
+ }
+
+ /** Creates an anonymous TokenPredicate.
+ * Ensure the name doesn't clash with anonymous TokenPredicates from
other skins.
+ *
+ * @param name
+ * @param type
+ * @return
+ */
+ public static TokenPredicate createAnonymousTokenPredicate(final
String name, final Type type){
+
+ ANONYMOUS_TOKENS.put(name, new TokenPredicateImpl(name, type));
+ return getAnonymousTokenPredicate(name);
+ }
+
+ /** Utility method to use all TokenPredicates in existence.
+ * @return
+ */
+ public static Set<TokenPredicate> getTokenPredicates() {
+
+ return Collections.unmodifiableSet(TokenPredicateImpl.TOKENS);
+ }
+
+ /** Utility method to use all TokenPredicates belonging to a given
type.
+ * @param type
+ * @return
+ */
+ public static Set<TokenPredicate> getTokenPredicates(final Type type) {
+ return
Collections.unmodifiableSet(TokenPredicateImpl.TOKENS_BY_TYPE.get(type));
+ }
+
+ /** Utility method to use all MagicTokenPredicates in existence.
+ * @return
+ * @deprecated
+ */
+ public static Set<TokenPredicate> getMagicTokenPredicates() {
+ return
Collections.unmodifiableSet(TokenPredicateImpl.MAGIC_TOKENS);
+ }
+
+ /** Utility method to use all TriggerTokenPredicates in existence.
+ * @return
+ * @deprecated
+ */
+ public static Set<TokenPredicate> getTriggerTokenPredicates() {
+ return
Collections.unmodifiableSet(TokenPredicateImpl.TRIGGER_TOKENS);
+ }
+ }
+
}
\ No newline at end of file
_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits