packagemain
import(
   "fmt"
   "github.com/bilibili/gengine/engine"
   "github.com/bilibili/gengine/engine"
   "github.com/bilibili/gengine/engine"
)
typeTemperature struct{
   Tag string             //标签点名称
   Value float64           //数据值
   State int64                //状态
   Event string            //报警事件
}
typeWater struct{
   Tag string             //标签点名称
   Value int64             //数据值
   State int64                //状态
   Event string            //报警事件
}
typeSmoke struct{
   Tag string             //标签点名称
   Value int64             //数据值
   State int64                //状态
   Event string            //报警事件
}
const(
   eventRule = `
rule "TemperatureRule""温度事件计算规则"
begin
   println("/***************** 温度事件计算规则 ***************/")
   tempState = 0
   ifTemperature.Value < 0{
      tempState = 1
   }elseifTemperature.Value > 80{
      tempState = 2
   }
   ifTemperature.State != tempState{
      iftempState == 0{
         Temperature.Event = "温度正常"
      }elseiftempState == 1{
         Temperature.Event = "低温报警"
      }else{
         Temperature.Event = "高温报警"
      }
   }else{
      Temperature.Event = ""
   }
   Temperature.State = tempState
end
rule "WaterRule""水浸事件计算规则"
begin
   println("/***************** 水浸事件计算规则 ***************/")
   tempState = 0
   ifWater.Value != 0{
      tempState = 1
   }
   ifWater.State != tempState{
      iftempState == 0{
         Water.Event = "水浸正常"
      }else{
         Water.Event = "水浸异常"
      }
   }else{
      Water.Event = ""
   }
   Water.State = tempState
end
rule "SmokeRule""烟雾事件计算规则"
begin
   println("/***************** 烟雾事件计算规则 ***************/")
   tempState = 0
   ifSmoke.Value != 0{
      tempState = 1
   }
   ifSmoke.State != tempState{
      iftempState == 0{
         Smoke.Event = "烟雾正常"
      }else{
         Smoke.Event = "烟雾报警"
      }
   }else{
      Smoke.Event = ""
   }
   Smoke.State = tempState
end
`)
funcmain(){
   temperature := &Temperature{
      Tag: "temperature",
      Value:  90,
      State: 0,
      Event: "",
   }
   water := &Water{
      Tag: "water",
      Value:  0,
      State: 0,
      Event: "",
   }
   smoke := &Smoke{
      Tag: "smoke",
      Value:  1,
      State: 0,
      Event: "",
   }
   dataContext := context.NewDataContext()
   dataContext.Add("Temperature", temperature)
   dataContext.Add("Water", water)
   dataContext.Add("Smoke", smoke)
   dataContext.Add("println",fmt.Println)
   ruleBuilder := builder.NewRuleBuilder(dataContext)
   err1 := ruleBuilder.BuildRuleFromString(eventRule)
   iferr1 != nil {
      panic(err1)
   }
   eng := engine.NewGengine()
   eng.ExecuteConcurrent(ruleBuilder)
   fmt.Printf("temperature Event=%s\n", temperature.Event)
   fmt.Printf("water Event=%s\n", water.Event)
   fmt.Printf("smoke Event=%s\n", smoke.Event)
   fori := 0; i < 10; i++ {
      smoke.Value = int64(i % 3)
      eng.ExecuteConcurrent(ruleBuilder)
      fmt.Printf("smoke Event=%s\n", smoke.Event)
   }
}