<commit_before>AGENT_ROOT = File.dirname(__FILE__)

require 'rubygems'
require 'active_support'
require 'systemu'
require 'sexpistol'
require "#{AGENT_ROOT}/../cm/logger.rb"
require "#{AGENT_ROOT}/../cm/agent.rb"
require "#{AGENT_ROOT}/../cm/saga.rb"
require "#{AGENT_ROOT}/../cm/kb.rb"
require "#{AGENT_ROOT}/../cm/cirrocumulus.rb"
require "#{AGENT_ROOT}/xen_node.rb"
require "#{AGENT_ROOT}/raid.rb"
require "#{AGENT_ROOT}/raid_assemble_saga.rb"
require "#{AGENT_ROOT}/raid_stop_saga.rb"
require "#{AGENT_ROOT}/raid_create_saga.rb"

class DomUKb < Kb
  def collect_knowledge()
    @knowledge = []
    domUs = XenNode::list_running_domUs()
    domUs.each do |domU|
      add_fact([:running, [:domu, domU]], 'yes')
    end

    add_fact([:free_memory], XenNode::free_memory)
    add_fact([:domus_running], XenNode::list_running_domUs().size)
  end
end

class XenAgent < Agent
  def initialize(cm)
    super(cm)
    @default_ontology = 'cirrocumulus-xen'
  end

  def restore_state()
    XenNode::set_cpu(0, 10000, 0)
    systemu 'aoe-discover'
  end

  def handle(message, kb)
    was_processed = false
    @sagas.each do |saga|
      next if saga.is_finished?

      if saga.id == message.in_reply_to
        was_processed = true
        saga.handle(message)
      end
    end

    return if was_processed

    s = Sexpistol.new

    case message.act
      when 'query-if' then
        fact = [s.to_sexp(message.content)]
        #p fact
        query = kb.query_fact(fact.first)
        reply = message.content
        if query.nil?
          reply = [:not, message.content]
        end
        
        msg = Cirrocumulus::Message.new('', 'inform', s.to_sexp(reply))
        msg.receiver = message.sender
        msg.ontology = @default_ontology
        msg.in_reply_to = message.reply_with

        @cm.send(msg)
      when 'query-ref' then
        msg = query(message.content)
        msg.receiver = message.sender
        msg.ontology = @default_ontology
        msg.in_reply_to = message.reply_with
        @cm.send(msg)
      when 'request' then
        handle_request(message)
    end

  end
  
  private

  # <fipa-message ontology="cirrocumulus-xen" act="query-ref"><content></content></fipa-message>
  def query(obj)
    Log4r::Logger['agent'].info(obj.inspect)

    if obj.first == :state
      return query_state(obj.second)
    else
      return query_kb(obj)
    end
  end

  def query_state(obj)
    msg = Cirrocumulus::Message.new(nil, 'inform', nil)

    if obj.first == :raid
      p obj
      disk_id = obj.second
      p disk_id
      raid_state = Raid::check_raid(disk_id)
      p raid_state
      msg.content = [:'=', message.content, [raid_state]]
      p msg
    elsif obj.first == :aoe
      disk_id = obj.second.to_i
      visible_exports = Raid::check_aoe(disk_id)
      msg.content = [:'=', message.content, [visible_exports].flatten]
    end

    Log4r::Logger['agent'].info(msg.inspect)
    msg
  rescue Error => ex
    Log4r::Logger['agent'].error(ex)
  end

  def query_kb(obj)
    msg = Cirrocumulus::Message.new(nil, 'inform', nil)<commit_msg>xen: debug<commit_after>AGENT_ROOT = File.dirname(__FILE__)

require 'rubygems'
require 'active_support'
require 'systemu'
require 'sexpistol'
require "#{AGENT_ROOT}/../cm/logger.rb"
require "#{AGENT_ROOT}/../cm/agent.rb"
require "#{AGENT_ROOT}/../cm/saga.rb"
require "#{AGENT_ROOT}/../cm/kb.rb"
require "#{AGENT_ROOT}/../cm/cirrocumulus.rb"
require "#{AGENT_ROOT}/xen_node.rb"
require "#{AGENT_ROOT}/raid.rb"
require "#{AGENT_ROOT}/raid_assemble_saga.rb"
require "#{AGENT_ROOT}/raid_stop_saga.rb"
require "#{AGENT_ROOT}/raid_create_saga.rb"

class DomUKb < Kb
  def collect_knowledge()
    @knowledge = []
    domUs = XenNode::list_running_domUs()
    domUs.each do |domU|
      add_fact([:running, [:domu, domU]], 'yes')
    end

    add_fact([:free_memory], XenNode::free_memory)
    add_fact([:domus_running], XenNode::list_running_domUs().size)
  end
end

class XenAgent < Agent
  def initialize(cm)
    super(cm)
    @default_ontology = 'cirrocumulus-xen'
  end

  def restore_state()
    XenNode::set_cpu(0, 10000, 0)
    systemu 'aoe-discover'
  end

  def handle(message, kb)
    was_processed = false
    @sagas.each do |saga|
      next if saga.is_finished?

      if saga.id == message.in_reply_to
        was_processed = true
        saga.handle(message)
      end
    end

    return if was_processed

    s = Sexpistol.new

    case message.act
      when 'query-if' then
        fact = [s.to_sexp(message.content)]
        #p fact
        query = kb.query_fact(fact.first)
        reply = message.content
        if query.nil?
          reply = [:not, message.content]
        end
        
        msg = Cirrocumulus::Message.new('', 'inform', s.to_sexp(reply))
        msg.receiver = message.sender
        msg.ontology = @default_ontology
        msg.in_reply_to = message.reply_with

        @cm.send(msg)
      when 'query-ref' then
        msg = query(message.content)
        msg.receiver = message.sender
        msg.ontology = @default_ontology
        msg.in_reply_to = message.reply_with
        @cm.send(msg)
      when 'request' then
        handle_request(message)
    end

  end
  
  private

  # <fipa-message ontology="cirrocumulus-xen" act="query-ref"><content></content></fipa-message>
  def query(obj)
    Log4r::Logger['agent'].info(obj.inspect)

    if obj.first == :state
      return query_state(obj.second)
    else
      return query_kb(obj)
    end
  end

  def query_state(obj)
    msg = Cirrocumulus::Message.new(nil, 'inform', nil)

    if obj.first == :raid
      disk_id = obj.second
      raid_state = Raid::check_raid(disk_id)
      msg.content = [:'=', obj, [raid_state]]
    elsif obj.first == :aoe
      disk_id = obj.second.to_i
      visible_exports = Raid::check_aoe(disk_id)
      msg.content = [:'=', message.content, [visible_exports].flatten]
    end

    Log4r::Logger['agent'].info(msg.inspect)
    msg
  end

  def query_kb(obj)
    msg = Cirrocumulus::Message.new(nil, 'inform', nil)
{'fraction_non_alphanumeric': 0.1339641434262948, 'fraction_numerical': 0.0031540504648074367, 'mean_word_length': 2.8795878943979396, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2303777', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2131, 'n_words': 400}