Sample Rules

From KynetxDocs

Jump to: navigation, search

Here are some sample KRL rules. Note that these would need to be assembled into a Ruleset to be used by the interpreter.

This rule replaces an element on the selected page if the referer was a search engine.

// rule for displaying google ads
rule google_ads is active {
   select using "/archives/\d+/\d+/" setting ()

   pre {
   }

   if search_engine_referer() then {
      replace("kynetx_10", "/kynetx/google_ad.inc");
   }
}


This rule pops up a weather widget on the selected pages if it's nighttime. Note that this rule is inactive. Also note that it sets variables in the pre block and then uses them in the float action.

// This is a test rule for archives for nighttime
rule nighttime is inactive {
   select using "/archives/(\d+)/(\d+)/" setting (year,month)

   pre {
      tc = weather:tomorrow_cond_code();
      city = geoip:city();
   }

   if nighttime()
   then {
      float("absolute", "top: 10px", "right: 10px",
            "/cgi-bin/weather.cgi?city=" + city + "&tc=" + tc)
        with delay = 0 and
             draggable = true and
             scrollable = true and
             effect = "appear";

   }
}


This simple rule uses an alert box when a user goes to the selected page and they're not from Utah.

// test alert boxes
rule alert_test is active {
   select using "/cto_forum" setting ()

   pre {
     city = geoip:city();
   }

   if outside_state("UT") 
   then {
      alert("Sorry, the CTO Breakfast isn't held near " + city)
   }
}


This rule shows how the market functions and predicates can be used.

// test market up predicate
rule market_up_test is active {
   select using "/essays/2004/how_to_start_a_blog" setting ()

   pre {
     current_price = stocks:last("^DJI");
     change = stocks:change("^DJI");
   }

   if djia_up_more_than(10) 
   then {
      alert("The market is up by " + 
            change + 
            "!!! The DOW is at " + 
            current_price + 
            "! Buy Something!")
   }
}


This rule shows how counters can be used to track how many times someone has visited a set of pages within a fixed amount of time. The name of the counter, in this case archive_pages is arbitrary and counted on a per-user basis. Note the use of the fired post block to take action based on what happened with a rule.

// rule for counting visits to a page
rule frequent_archive_visitor is active {
   select using "/archives/\d+/\d+/" setting ()

   pre {
      c = counter.archive_pages;
   }

   if counter.archive_pages > 2 within 3 days then {
      alert("You win the prize!  You've seen " + c + " pages from the archives!")
   }

   fired {
      clear counter.archive_pages; 
   } else {
      counter.archive_pages += 1 from 1;  
   }
}


This rule shows how "click" callbacks can be set on a page. Callbacks an be classified as indicating "success" or "failure."


// rule for testing callbacks
rule accept_offer is inactive {
    select using "/identity-policy/" setting ()

    pre {
    }

    if djia_down_more_than(10) then {
      replace("kobj_test", "/kynetx/newsletter_invite.inc")
    } 

    callbacks {
      success {
        click id="rssfeed";
        click class="newsletter"
      } 

      failure {
        click id="close_rss"
      }
    }
}

This rule demonstrates the move_to_top action

 // move categories to the top for search engine visitors
 rule move_categories_to_top is active {
   select using "/archives/" setting ()

   pre {

   }

   if search_engine_referer() then 
       move_to_top("categories")

 }
Personal tools