dev-resources.site
for different kinds of informations.
Evaluate a Site for Solar Energy Potential with Corticon
Integrate data from external REST APIs to enrich your decision data
Table of Contents
- First Launch of Corticon Studio
- Rule Projects
-
Rule Vocabulary
- Entities
- Attributes
- Associations
- Complete the Vocabulary
-
Create your first rulesheet
- Logical Integrity Checks
- Testing Rules
-
Data Integration
- Incorporate the REST Datasource
- Update the rules
- Test the Updated Rules
First Launch of Corticon Studio
Workspaces, projects, and folders provide a way to organize rule-modeling components. A workspace is a 'root' directory that acts as a container for rule projects created in Corticon Studio. When you first launch Corticon Studio, you'll be prompted for a workspaceâthis is different than the folders that were assigned during the Corticon installation wizard. A workspace is where all of the things created by you, the end user, are stored. The work directoryâthe one from the installation wizardâis where all of the things that Corticon itself needs to work as an application will live. If you choose to later uninstall Corticon, make sure to do so using the uninstall wizard in your start menu, not just deleted these folders.
You can feel free to leave the default workspace folder (it will be created automatically), or point to a different folder if you prefer.
Rule Projects
A Corticon rule project is a âcontainerâ for rule-modeling files. A Corticon rule project can further contain folders to segregate rule-modeling components. A âtemplateâ rule project can be downloaded from hereâdownload the file, but donât unzip it.
To import that template:
- Click âFileâ on the main toolbar, then click âImportâŚâ
- Choose the âSelect archive file:â option, then browse to the downloaded zip file
- Click Finish
Rule Vocabulary
A Corticon Vocabulary is a rule-modeling component that enables you to define all the business terms that you require in your rules. For example, letâs take a look at how we might formulate a rule vocabulary from some common solar site evaluation rules.
An installation Site will have a size, annual radiation, and aspect. The Site will be where we install some number of Panels, each of which will have various values like length, width, and efficiency.
Letâs define this is as a rule vocabulary, with Site and Panel being entities, each having their respective attributes, and associated to one another as one to manyâone site to any number of panels.
Entities
Follow these steps to add an entity in the Vocabulary editor:
- Double click the vocabulary file in the project explorerâit is in the Vocabulary folder and has the extension â.ecoreâ:
- In the Vocabulary editor, right-click and select Add Entity, and type in
Site
. If you donât enter a name, it will default to âEntity_1â, but it can be changed by double clicking on the name of the entity.
- Add one more entity, and give it the name
Panel
:
Attributes
After you create an entity, you can add attributes to it. An attribute is like an adjective, whose values are populated at runtime.
- Right-click the entity name and then select Add Attribute.
- Youâre given a choice of the attributesâ data typeâBoolean (True or False), Decimal, DateTime, Date, Integer, String, Time. For this example, select decimal. Enter a name,
squareMeters
for the attribute, or edit the name in the same way you did with the entity. After you add an attribute, the property editor panel of the Vocabulary editor displays all the properties for the attribute with default values.
Associations
An association describes the relationship between two entities. In a Corticon Vocabulary, you can define the association under either entity. By default, the association that you define appears under both entities in the Vocabulary tree.
You add an association to an entity just as you add an attribute.
- Right-click Site and select Add Association.
- The default selections should be what we wantâone site to many associations. Click OK.
Your vocabulary will now look like this:
Complete the Vocabulary
Now, add the rest of the vocabulary elements weâll need.
Add these attributes to âSiteâ:
-
length
(decimal) -
width
(decimal) -
maxPanels
(integer) -
annualRadiation_kWh
(decimal) -
aspect
(string) -
isSuitable
(boolean) -
slopeDegrees
(decimal)
Add these attributes to Panel:
-
length
(decimal) -
width
(decimal) -
squareMeters
(decimal)
Your vocabulary should now look like:
As you likely deduced from the rule, âAspect should not be north,â the word aspect
is used here to mean compass direction (north, south, east, west). Each of the four compass directions are âstringâ (alphanumeric) datatypes, but there is a specific list of potential values for those stringsâwe want to make sure we donât deviate from those specific values when modeling rules. For this situation, weâre going to define a âCustom Data Typeâ:
- Click the name of the vocabulary at the top of the vocabulary tree, as shown:
- In the righthand side of the vocabulary editor, youâll see a pane come into focus called âCustom Data Typesâ. In the table, type âaspectâ on the first row under Data Type Name, then select âStringâ from Base Data Type. The default selection under âEnumerationâ will be âYesâ, donât change this. Lastly, on the right hand side, under âValueâ, type the four compass directions on the first four rows. Your custom data types table will look like this once complete:
- Lastly, click the âaspectâ attribute underneath the Site entity that you created previously. On the righthand side, a panel labeled âBasic Propertiesâ should appear. Next to the row called âData Typeâ, which will currently say âStringâ, select the dropdown button and select the newly created data type âaspect:
Youâll see why we defined this custom data type in the next step.
Create your first rulesheet
Letâs start with a rule that will solve for a siteâs area in square meters. First, right click on the âRulesheetsâ folder and select New > Rulesheet:
Give it the name âSite Areaâ, and then click Finish.
You define your rule logic in a Corticon Rulesheet. A rule is like an âif-thenâ statement. Each rule consists of one or more conditions (if) that are associated with one or more actions (then). Here is an example of a Rulesheet with five rules:
The Rulesheet editor has the following parts:
Conditions rowsâfor defining the specific value(s) of a given vocabulary attribute which should trigger this rule should fire. For example, car.price
> 45000. The condition value could be a single value (45000), a set of values (45000, 60000), or a range of values (20000 .. 45000).
Actionsâwhere you assign the value(s) to assign to vocabulary attribute(s) when the conditions are met. For example, car.potentialTheftRating
= 'High'.
But what about unconditional rules? For example, letâs say our Solar site is always a rectangle, so square meters can always be solved for by âarea=length * widthâ with no conditions involved. This is called an âaction-onlyâ ruleâit will always fire. We define these in column 0. Letâs try it out.
Open your newly created âSite Area.ersâ rulesheet. When defining rules, it is easiest to arrange the different panels of the screen in the way shown here:
- From vocabulary panel on the left, select the attribute
squareMeters
from underneath theSite
entity, and drag it onto the first action row: - In the 0 column, weâre going to define the expression which will be used to assign the value for the
squareMeters
attribute. Drag and drop (or just type in) the two other attributes that will be multiplied-- Site.length*Site.width:
- Now, lets define a rule statement for this rule. Rule statements are âexplainersâ of a sort for each rule that fires during the execution of the decision service, to help document the rules which contributed to the final output. In the Rule Statement section, copy the text below into the Text column in the first row: Site area is
Site.squareMeters
meters - Add the other values shown in the picture below to your rule statement:
Letâs now define the conditions and actions for our initial set of rules. We will set the boolean attribute isSuitable
based upon whether the Site meets these conditions:
- The slope must be 45° or less.
- Annual solar radiation should be at least 800 kWh.
- Aspect should not be north.
- Areas with a slope of 10° or less are determined as suitable areas regardless of the aspect.
- 20 m² and more area is needed.
- Copy and paste the five rows above into your Rule Statements underneath the one weâve just defined:
- Drag
Site.slopeDegrees
onto the first Condition row. In the first row of the â1â column, type '< 45' - Drag
Site.isSuitable
to the second Action row, then âTâ from the dropdown. - Drag
Site.annualRadiation_kWh
to the second Condition row. In the second row of the â2â column, type >=800. Set the corresponding action to also beSite.isSuitable = T
- Drag
Site.aspect
to the third condition row. In column 3, select the third condition row. A dropdown button will be available on the right side of the cellâselect North from it:
- Set the corresponding action to set
isSuitable
to F. - Implement the next two rules and then update the Rule Statements as shown below.
Logical Integrity Checks
Before we test these rules with test data, Corticon can analyze them for logical issues we may have introduced. Look for these three buttons
at the top of the rulesheet editor screen (if theyâre greyed out, then click anywhere in the rulesheet to bring it into focus).
- Click them from left to right, start with the Logical Loop Checker. As the name implies, if we were introducing any circular logic, Corticon would point us to it automatically with this check. We shouldnât have any loops.
- Next is the Completeness Checkerâare there more scenarios implicitly possible based upon the rules weâve authored so far? Corticon will condense all possibilities into a new column. This can be helpful, but some of the new conditions should be evaluated independently, and weâre not going to worry about null checks. You can select this column and click the delete button.
- Instead, letâs implement the missing rules in a few additional columns such that your rulesheet looks like this:
Since we have new rules, it is a good idea to add our additional rule statements for the rules.
You can copy and paste the italicized text below directly into your rule statements pane to save yourself some time.
| | | | |
|---- |----------- |------ |---------------------------------------------------------------------------------------------- |
| A0 | Info | Site | Site area is Site.squareMeters
} meters |
| 1 | Info | Site | The slope must be 45° or less. |
| 2 | Info | Site | Annual solar radiation should be at least 800 kWh. |
| 3 | Violation | Site | Aspect should not be north. |
| 4 | Info | Site | Areas with a slope of 10° or less are determined as suitable areas regardless of the aspect. |
| 5 | Info | Site | 20 m² and more area is needed. |
| 6 | Violation | Site | The slope is not 45° or less. |
| 7 | Violation | Site | Annual solar radiation is not at least 800 kWh. |
| 8 | Info | Site | Aspect is not north. |
| 9 | Violation | Site | Site is not at least 20 m² |
- Finally, letâs run the Conflict Checker. Here, we can really see the value of business users that are well-acquainted with the rules being the implementers of the logic versus developers, because they will know best how to address logical mistakes in the rules:
Corticon has identified 15 conflictsâsituations where the conditions weâve defined could presumably overlap. Currently, any of the cells with a â â â in them will evaluate that rule without considering the vocabulary attribute on the left hand side. For example, the first conflict which Corticon shows us is a conflict between 1 and 3, because Corticon has inferred there may be scenarios âNorthâ could be the value in cell c2, or a value < 45 could be in the cell a3âif both conditions are met, which matters more?
We can resolve this in a few waysâexplicitly define every combination of values in every rule, or simply override one with another in cases of conflicts. Weâre going to evaluate these rules such that if any aspect of the Site makes is unsuitable for solar, then that it will take precedent over any condition which resolves to isSuitable
=T. However, recall that the condition for rule #4âwhen the slope is less than 10 degreesâwill resolve to isSuitable
=T even when the Site.aspect= âNorthâ.
Weâll define overrides to tell Corticon which action to defer to in cases of conflict. Other than rule #4 overriding number #3 for the reason mentioned above, all other rules that lead to a determination of isSuitable
=F should override those which set isSuitable
=T. At the bottom of each rule column, youâll see an override row where you can specify which rule(s) that particular rule will override in cases of conflict. You can type them in or select them from the dropdown (hold down control to select multiple). Try this on your own, then check your work based on the screenshot below.
Testing Rules
Next, letâs set up a rule test to run test scenarios against this rulesheet. A Ruletest simulates a business scenario where the rules are applied to input data. If the data satisfies all the conditions in a rule, the rule fires and some output containing the results of the rule execution is produced. You can define different sets of input data to test how the rules behave in different scenarios. You can also use a Ruletest to compare the output of a rule execution with expected results. A Ruletest stores this information in a Ruletest file, enabling you to save use-cases that are of interest, change rules, and run the test again to see how the modified rules behave when applied to the same use-cases.
- Right click on the Ruletests folder > New > Ruletest.
- Give it a name like âSite Checkâ, and select the RuleTests folder if it isnât already.
- Hit next, and you should see the âSite Areaâ rulesheet already selected. This is the âtest subjectâ, i.e. the specific rule asset file weâre testing against. Click Finish.
The Ruletest editor has four parts:
Test Subjectâspecifies which Rulesheet or Ruleflow is being tested
Inputâwhere you define input data to be processed by the rules in the Rulesheet.
Outputâwhere Corticon Studio displays the result of a Ruletest execution.
Expectedâwhere you can optionally define the result that you expect.
In the real world, a Corticon Decision Service may receive input in different formats such as XML, JSON, Java, or .NET objects. However, in a Ruletest, you just specify input data using the same Rule Vocabulary weâve used for the rules.
- You can specify different entities as well as multiple instances of the same entity. Letâs try itâdrag in two instances of the
Site
entity, into the input pane. This adds all the attributes in the entity to the Input pane. However, you can delete attributes that you do not need. - Double click the attributes to specify values. The syntax for specifying values is similar to specifying values for attributes in Rulesheets, with one differenceâyou must not enter any values in quotes, even if the data type of the attribute is String, DateTime, Date or Time. If you enter quotes, the Ruletest treats it as part of the value. Copy the values as shown below for your input:
Above the ruletest, click the Run Test button:
The rules will be compiled into a Decision Service and executed against a test instance of the Corticon Server. In other words, the ruletest enables us to replicate the precise behavior we would expect from the rules if they were deployed as is. You should see the output populated in the Output pane, and Rule Messages sent back for the rules that fired.
Data Integration
So far, weâve built a rulesheet and a test case in which we define the test data. Corticon is not limited to only evaluating data provided on the initial request to the Decision Service (like the data we defined by double clicking the attributes in the ruletest). Letâs incorporate some external data retrieved from a REST API. Weâll use the API provided by the US National Renewable Energy Lab to retrieve data about the solar radiation for a specific address.
Weâll be using an API key that will expire after todayâs workshop, but they are free to generate at NREL.gov.
- Reopen your rule vocabulary
- Add two attributes to
Site
âaddress
(string), andavgDailyRadiation_kWh
(decimal) - At the top of the screen, click the Vocabulary menu > Add Datasource > REST Datasource
- Click the name of the rule vocabulary to bring the datasource configuration pane into focus:
- Rename the Datasource Name from the default, âREST Serviceâ, to âNRELâ.
- In the REST URL field, paste the URL: https://developer.nrel.gov/api/solar/solar_resource/v1.json?address=Tremont%20St,%20Boston,%20MA%2002111
- On the Authentication dropdown, select URL Parameter Token
- Under field name, enter âapi_keyâ
- Under token, paste your API key
- Click âTest Connectionâ. If everything is in the right place, you should get an alert that the connection was successful.
- Click âDiscoverâ. This will import the various fields at that datasource endpoint, which we will then âmapâ to our entities/attributes.
- Click your
Site
entity. On the right hand side, select AUTOREST.REST_DATA from the dropdown next to Table Name, then select âaddressâ from the entity identity dropdown.
- Next, click the address attribute. This time on the righty hand side, select ADDRESS from the column name dropdown.
- Finally, do the same for the new
avgDailyRadiation_kWh
attribute, selectingOUTPUTS_AVG_DNI_ANNUAL
from the column name dropdown.
Incorporate the REST Datasource
The one file we havenât worked with yet is the Ruleflow. A Ruleflow enables you to connect two or more Rulesheets in a sequence. When the Ruleflow is processed at runtime, the Rulesheets are executed one by one in that sequence. The output of one Rulesheet becomes the input of the next Rulesheet. Note that a Rulesheet can be used in multiple Ruleflows. This enables you to use Rulesheets as reusable modules of rule logic. Any change in the rule logic only has to be made once in the Rulesheet and it is propagated across all Ruleflows that refer to it.
Besides defining a sequence of rulesheet executions, we can specify behavior like branching to different rulesheets at specific points in the decision execution based upon attribute values. We also specify the REST callout within the ruleflow.
- Right click the Ruleflows folder > New > Ruleflow
- Name it Solar Flow and click finish
- When we were defining rules in a rulesheets, we dragged and dropped from the Rule Vocabulary. For the ruleflow, weâll be defining the order to rule execution by dragging and dropping our rulesheets onto the Ruleflow canvas, and then connecting them using the Connection tool on the righthand side of the canvas. Drag the Site Area rulesheet onto the ruleflow
- Click âService Calloutâ on the panel to the right, then click above the Site Area rulesheet to place the callout node. Connect the callout to the rulesheet by click Connection in the righthand pallet and dragging a connection from the callout to Site Area.
- Click on the service callout, and bring the âPropertiesâ pane into view. You should see a dropdown for Service Nameâselect âRetrieve Dataâ.
- Next, click âRuntime Propertiesâ on the tab to the left, underneath the âService Call-outâ tab. Here, youâll set options for 6 different cells by choosing from each cellâs dropdown. Mirror the content shown below:
Update the rules
The last step we need to perform is to annualize the solar radiation data because we need the sum total of kWh. Weâll solve for this by multiplying the daily kWh value which will be retrieved from the data source by 365. We can do this by just adding another action only rule to the Site Area rulesheet. Reopen that rulesheet, and define the new rule to annualize the data:
Test the Updated Rules
Letâs give this thing a whirl. Open your ruletest back up. Change the test subject to the ruleflow from the rulesheet by double clicking the folder path above the input pane, highlighted below. Change the inputs to match the input shown in the same photo:
When you execute this test, Corticon will retrieve the data value for avgDailyRadiation_kWh
from the REST endpoint, incorporate it into the data used for the rule processing, and evaluate the siteâs suitability based upon the existing rules and the annualized kWh value.
Featured ones: