Skip to content

Commit

Permalink
Introduce metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
maedi committed Nov 24, 2020
1 parent d89d066 commit fcf83ad
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 201 deletions.
111 changes: 38 additions & 73 deletions lib/Aggregator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
################################################################################
# Aggregate control RuleSets. Validate reflection arguments against aggregates.
# Aggregate reflection metadata into rule sets.
# Validate reflection arguments against aggregates.
#
# @pattern Singleton
#
# @hierachy
# 1. Aggregator
# 2. RuleSet
Expand All @@ -14,7 +16,7 @@ class Aggregator

def initialize()

# Store by class and method.
# Key rule sets by class and method.
@rule_sets = {}

end
Expand Down Expand Up @@ -83,92 +85,55 @@ def set_output_rule_set(klass, method, rule_set)
end

##
# Create aggregated RuleSets.
# Create aggregated rule sets from reflection metadata.
#
# @param klass [Symbol]
# @param method [Symbol]
# @param controls [Array]
# @param reflections [Array] Controls with metadata.
##
def load(controls)
def train(reflections)

# Create aggregated RuleSets for each control's inputs/output.
controls.each do |control|
# On first use there are no previous reflections.
return if reflections.nil?

# Process inputs.
control[:inputs].each_with_index do |input, arg_num|
rule_set = get_input_rule_set(klass, method, arg_num)
if rule_set.nil?
rule_set = RuleSet.new()
set_input_rule_set(klass, method, arg_num, rule_set)
end
rule_set.train(input[:type], input[:value])
end
reflections.each do |reflection|

# Process output.
output_rule_set = get_output_rule_set(klass, method)
if output_rule_set.nil?
output_rule_set = RuleSet.new()
set_output_rule_set(klass, method, output_rule_set)
end
output_rule_set.train(control[:output][:type], control[:output][:value])
klass = reflection[:class]
method = reflection[:method]

end
##
# INPUT
##

end
unless reflection[:inputs].nil?
reflection[:inputs].each_with_index do |meta, arg_num|

##
# Train RuleSets from controls.
#
# @param klass [Symbol]
# @param method [Symbol]
##
def train(klass, method)
# Get rule set.
rule_set = get_input_rule_set(klass, method, arg_num)
if rule_set.nil?
rule_set = RuleSet.new()
set_input_rule_set(klass, method, arg_num, rule_set)
end

input_rule_sets = get_input_rule_sets(klass, method)
unless input_rule_sets.nil?
input_rule_sets.each do |input_rule_set|
input_rule_set.train()
end
end
# Train on metadata.
rule_set.train(meta)

output_rule_set = get_output_rule_set(klass, method)
unless output_rule_set.nil?
output_rule_set.train()
end

end

def train_from_rule(rule)

# Track data type.
@types << type
end
end

# Get rule for this data type.
rule = nil
##
# OUTPUT
##

case type
when "Integer"
unless @rules.key? IntegerRule
rule = IntegerRule.new()
@rules[IntegerRule] = rule
else
rule = @rules[IntegerRule]
end
when "String"
unless @rules.key? StringRule
rule = StringRule.new()
@rules[StringRule] = rule
else
rule = @rules[IntegerRule]
# Get rule set.
output_rule_set = get_output_rule_set(klass, method)
if output_rule_set.nil?
output_rule_set = RuleSet.new()
set_output_rule_set(klass, method, output_rule_set)
end
end

# Add value to rule.
unless rule.nil?
rule.load(value)
end
# Train on metadata.
output_rule_set.train(reflection[:output])

return self
end

end

Expand Down
36 changes: 4 additions & 32 deletions lib/Control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
################################################################################

require 'Reflection'
require 'MetaBuilder'

class Control < Reflection

Expand All @@ -22,6 +23,9 @@ class Control < Reflection
##
def reflect(*args)

# Create metadata for each argument.
@inputs = MetaBuilder.create_many(args)

# Action method with new arguments.
begin
output = @clone.send(@method, *args)
Expand All @@ -36,36 +40,4 @@ def reflect(*args)

end

##
# Provide the results of the control.
#
# @return [Hash] Control metadata.
##
def result()

# The ID of the first execution in the ShadowStack.
base_id = nil
unless @execution.base == nil
base_id = @execution.base.unique_id
end

# Build control.
control = {
:base_id => base_id,
:exe_id => @execution.unique_id,
:ref_id => @unique_id,
:ref_num => @number,
:time => @time,
:class => @klass,
:method => @method,
:status => @status,
:inputs => @inputs,
:output => @output,
:message => @message
}

return control

end

end
27 changes: 27 additions & 0 deletions lib/Meta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
################################################################################
# Meta for input and output. All meta behave the same.
#
# @pattern Abstract class.
# @see lib/meta for each meta.
################################################################################

class Meta

##
# Each meta loads values.
#
# @param value [Dynamic]
##
def load(value)
end

##
# Each meta provides metadata.
#
# @return [Hash]
##
def result()
{}
end

end
56 changes: 56 additions & 0 deletions lib/MetaBuilder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
################################################################################
# Create Meta.
#
# @pattern Builder.
# @see lib/meta for each meta.
################################################################################

require 'Meta'
require_relative './meta/IntegerMeta'
require_relative './meta/StringMeta'

class MetaBuilder

##
# Create meta.
#
# @param value
##
def self.create(value)

meta = nil

# Creates values for matching data type.
case value.class.to_s
when "Integer"
meta = IntegerMeta.new()
when "String"
meta = StringMeta.new()
end

unless meta.nil?
meta.load(value)
end

return meta

end

##
# Create meta for multiple values.
#
# @param values
##
def self.create_many(values)

meta = []

values.each do |value|
meta << self.create(value)
end

return meta

end

end
46 changes: 9 additions & 37 deletions lib/Reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#
# @nomenclature
# args/inputs/values are the same thing but at a different stage of lifecycle.
#
# @hierachy
# 1. Execution
# 2. Reflection
# 3. RuleSet
################################################################################

require 'MetaBuilder'

class Reflection

attr_accessor :clone
Expand All @@ -33,7 +36,7 @@ def initialize(execution, number, aggregator)
@klass = execution.klass
@method = execution.method

# Rule sets.
# Metadata.
@inputs = []
@output = nil

Expand Down Expand Up @@ -62,8 +65,8 @@ def reflect(*args)
# Create random arguments.
new_args = randomize(args)

# Create RuleSet for each argument.
@inputs = create_rule_sets(new_args)
# Create metadata for each argument.
@inputs = MetaBuilder.create_many(new_args)

# Action method with new arguments.
begin
Expand All @@ -77,7 +80,7 @@ def reflect(*args)

# Run reflection.
output = @clone.send(@method, *new_args)
@output = create_rule_set(output)
@output = MetaBuilder.create(output)

# Validate output with aggregated control RuleSets.
unless agg_output_rule_set.nil?
Expand Down Expand Up @@ -117,37 +120,6 @@ def randomize(args)

end

def create_rule_sets(args)

rule_sets = []

args.each do |arg|
rule_sets << create_rule_set(arg)
end

rule_sets
end

def create_rule_set(arg)

rule_set = RuleSet.new()
type = arg.class.to_s

# Creates values for matching data type.
case type
when "Integer"
rule = IntegerRule.new()
rule.train(arg)
rule_set.rules[IntegerRule] = rule
when "String"
rule = StringRule.new()
rule.train(arg)
rule_set.rules[StringRule] = rule
end

rule_set
end

##
# Get the results of the reflection.
#
Expand Down Expand Up @@ -175,8 +147,8 @@ def result()
:inputs => [],
:output => @output,
}
@inputs.each do |input_rule_set|
reflection[:inputs] << input_rule_set.result()
@inputs.each do |meta|
reflection[:inputs] << meta.result()
end

return reflection
Expand Down
Loading

0 comments on commit fcf83ad

Please sign in to comment.