Thermostat case

0 views
Skip to first unread message

Lukasz

unread,
Apr 12, 2009, 5:32:35 AM4/12/09
to take rule compiler
Hi all,

I've just started working with Take and that's why my question is on
beginner level. I wanted to create thermostat case logic. It is the
example from Negnevitzky's book. More information about that can be
found here: https://ai.ia.agh.edu.pl/wiki/hekate:hekate_case_thermostat.

I want to ask, if my Take implementation is correct. I'm using simple
types: String, Integer. I need it for my master thesis, so I'd
thankfull for any feedback ;-)

My rules:

// thermostat rules
@@dc:creator=Lukasz Lysik
@@dc:date=22/02/2009

var String operation,season,today,day
var Integer month, hour

@take.compilerhint.slots=month,day,hour,setting
@take.compilerhint.method=thermostat_setting
query thermostat_setting[in,in,in,out]

os01: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "nbizhrs" and
season == "summer" then thermostat_setting[month,day,hour,27]
os02: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "bizhrs" and
season == "summer" then thermostat_setting[month,day,hour,24]
os03: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "nbizhrs" and
season == "spring" then thermostat_setting[month,day,hour,15]
os04: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "bizhrs" and
season == "spring" then thermostat_setting[month,day,hour,20]
os05: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "bizhrs" and
season == "winter" then thermostat_setting[month,day,hour,18]
os06: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "nbizhrs" and
season == "winter" then thermostat_setting[month,day,hour,14]
os07: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "nbizhrs" and
season == "fall" then thermostat_setting[month,day,hour,16]
os08: if season_name[month,season] and today_is[day,today] and
operation_name[today,hour,operation] and operation == "bizhrs" and
season == "fall" then thermostat_setting[month,day,hour,20]

ms01: if month == 1 then season_name[month,"winter"]
ms02: if month == 2 then season_name[month,"winter"]
ms03: if month == 3 then season_name[month,"spring"]
ms04: if month == 4 then season_name[month,"spring"]
ms05: if month == 5 then season_name[month,"spring"]
ms06: if month == 6 then season_name[month,"summer"]
ms07: if month == 7 then season_name[month,"summer"]
ms08: if month == 8 then season_name[month,"summer"]
ms09: if month == 9 then season_name[month,"fall"]
ms10: if month == 10 then season_name[month,"fall"]
ms11: if month == 11 then season_name[month,"fall"]
ms12: if month == 12 then season_name[month,"winter"]

dt01: today_is["mon","workday"]
dt02: today_is["tue","workday"]
dt03: today_is["wed","workday"]
dt04: today_is["thu","workday"]
dt05: today_is["fri","workday"]
dt06: today_is["sat","weekend"]
dt07: today_is["sun","weekend"]

th01: if today == "workday" and hour > 17 then operation_name
[today,hour,"nbizhrs"]
th02: if today == "weekend" then operation_name[today,hour,"nbizhrs"]
th03: if today == "workday" and hour < 9 then operation_name
[today,hour,"nbizhrs"]
th04: if today == "workday" and hour >= 9 and hour <= 17 then
operation_name[today,hour,"bizhrs"]

Thank you,

Lukasz

jens

unread,
Apr 15, 2009, 5:54:29 PM4/15/09
to take rule compiler
Lukasz,

Its almost correct. The only problem is that the parser does not
accept line breaks in the rules. We will address this in the next
version that has a completely new ANTRL based implementation of the
grammar. You can use this script to compile the rules once the new
line characters in the rules are removed (I have tried it with take
1.5.1, it works):

BasicConfigurator.configure();
nz.org.take.compiler.Compiler compiler = new DefaultCompiler();
compiler.add(new JalopyCodeFormatter());
compiler.setNameGenerator(new DefaultNameGenerator());
compiler.setLocation(new DefaultLocation());
// generate kb
InputStream script = GenerateClasses.class.getResourceAsStream
("thermostat.take");
ScriptKnowledgeSource ksource = new ScriptKnowledgeSource(script);

compiler.setPackageName
("example.nz.org.take.compiler.thermostat.generated");
compiler.setClassName("ThermostatRules");

long before = System.currentTimeMillis();
compiler.compile(ksource.getKnowledgeBase());
System.out.println("Compilation took "+(System.currentTimeMillis()-
before)+"ms");

This will give you a class ThermostatRules with a method
ResultSet<thermostat_setting> thermostat_setting(final long
month,final java.lang.String day, final long hour)


Cheers, Jens

jens

unread,
Apr 15, 2009, 7:36:52 PM4/15/09
to take rule compiler
One more thought about the example. It suffers from "primitive
obsession". Take encourages a more OO approach. For instance, you
could simplify the rules by using Date objects (using your own Date
class subclassing java.util.Date). This class could have methods like
isWorkday. You could then get rid of the dt* facts and simplify rules
like

th01: if day.isWorkday and day.hour > 17 then operation_name
[day,"nbizhrs"]

Hope this helps, Jens

On Apr 12, 9:32 pm, Lukasz <lly...@gmail.com> wrote:

Lukasz

unread,
Apr 17, 2009, 9:55:40 AM4/17/09
to take rule compiler
Hi,

Thanks a lot ;-)

Btw, is there any detailed roadmap for the projects? Is there any way
I could contribute?

Lukasz

jens

unread,
Apr 19, 2009, 7:12:46 AM4/19/09
to take rule compiler
Hi Lukasz,

The main two milestones for this year are:

- an updated grammar (hopefully still in April)
- the .NET port.

Carlos Crasborn is in charge of both. We would need help in the
following areas:

- tooling (preferably Eclipse based - visual editors, refactoring)
- examples (integration of dynamic compilation with web server or OSGi-
based app) and documentation
- alternative XML syntax for rules

Any help is welcome !!

Cheers, Jens
Reply all
Reply to author
Forward
0 new messages