Class: Sfp::Parser

Inherits:
Object
  • Object
show all
Includes:
SasTranslator
Defined in:
lib/sfp/parser.rb

Overview

main class which processes configuration description in SFP language either in file or as a string

Constant Summary collapse

AbstractEliminator =
Sfp::Visitor::AbstractEliminator.new

Constants included from SasTranslator

SasTranslator::ActivateSimpleGlobalConstraint, SasTranslator::GlobalConstraintMethod, SasTranslator::GlobalOperator, SasTranslator::GlobalVariable, SasTranslator::GoalOperator, SasTranslator::GoalVariable, SasTranslator::ImplicationToDisjunction, SasTranslator::SometimeOperatorPrefix, SasTranslator::SometimeVariablePrefix

Instance Attribute Summary collapse

Attributes included from SasTranslator

#axioms, #benchmarks, #goals, #operators, #types, #variables

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SasTranslator

#add_unknown_undefined_value_to_variables, #add_unknown_value_to_nonstatic_variables, #and_equals_constraint_to_map, #apply_global_constraint_method_1, #apply_global_constraint_method_2, #array_to_or_constraint, #break_nested_reference, #calculate_depth, #combinator, #compile_step_1, #compile_step_2, #conjunction_only, #consistent_with_and, #consistent_with_equals, #create_and_constraint, #create_equals_constraint, #create_not_constraint, #create_not_equals_constraint, #create_or_constraint, #create_output, #cross_product_and, #dump, #dump_axioms, #dump_operators, #dump_types, #dump_vars, #enforce_equals_constraint, #evaluate_set_variables_and_types, #flatten_and_or_graph, #get_list_not_value_of, #ground_procedure_parameters, #identify_immutable_variables, #is_this, next_axiom_id, next_constraint_id, next_constraint_key, next_operator_id, next_variable_id, #normalize_formula, #normalize_nested_left_only, #normalize_nested_left_right, #normalize_nested_right_only, #normalize_nested_right_only_multiple_values, #normalize_nested_right_values, #not_equals_statement_to_or_constraint, null_of, #post_support_premise, #post_threat_conclusion, #postprocess_simple_global_constraint, #postprocess_simple_global_constraint_to_operator, #pre_support_premise, #pre_threat_conclusion, #preprocess_simple_global_constraint, #process_conditions, #process_effects, #process_global_constraint, #process_goal, #process_grounded_operator, #process_operator, #process_procedure, #process_sometime, #process_sometime_after, #ref_combinator, #remove_not_and_iterator_constraint, reset_operator_id, #reset_operators_name, #sas, #search_and_merge_mutually_inclusive_operators, #set_variable_values, #simple_formulae, #substitute_template, #to_and_or_graph, #to_sas, #to_set, #total_element, #variable_name_and_value, #visit_and_or_graph

Constructor Details

#initialize(params = {}) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sfp/parser.rb', line 12

def initialize(params={})
	@root_dir = (params[:root_dir].is_a?(String) ?
	             params[:root_dir].strip :
	             nil)
	@home_dir = (params[:home_dir].is_a?(String) ?
	             params[:home_dir].strip :
	             nil)
	@root = params[:root]
	@conformant = !!params[:conformant]
	@constraint_next_id = (params[:constraint_next_id] ? params[:constraint_next_id] : 0)
end

Instance Attribute Details

#conformantObject

Returns the value of attribute conformant.



9
10
11
# File 'lib/sfp/parser.rb', line 9

def conformant
  @conformant
end

#constraint_next_idObject (readonly)

Returns the value of attribute constraint_next_id.



10
11
12
# File 'lib/sfp/parser.rb', line 10

def constraint_next_id
  @constraint_next_id
end

#home_dirObject

Returns the value of attribute home_dir.



9
10
11
# File 'lib/sfp/parser.rb', line 9

def home_dir
  @home_dir
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/sfp/parser.rb', line 10

def root
  @root
end

#root_dirObject

Returns the value of attribute root_dir.



9
10
11
# File 'lib/sfp/parser.rb', line 9

def root_dir
  @root_dir
end

Class Method Details

.parse_file(filepath, options = {}) ⇒ Object



54
55
56
57
58
# File 'lib/sfp/parser.rb', line 54

def self.parse_file(filepath, options={})
	options[:home_dir] = File.expand_path(File.dirname(filepath)) if !options[:home_dir]
	parser = Sfp::Parser.new(options)
	parser.parse(File.read(filepath))
end

Instance Method Details

#parse(string, options = {}) ⇒ Hash

Returns : the result Hash.

Parameters:

  • : (String)

    a string of specification in SFP language

Returns:

  • (Hash)

    : the result Hash



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sfp/parser.rb', line 32

def parse(string, options={})
	lexer = SfpLang::Lexer.new(string)
	tokens = ANTLR3::CommonTokenStream.new(lexer)
	parser = SfpLang::Parser.new(tokens)
	parser.root_dir = @root_dir
	parser.home_dir = @home_dir
	parser.constraint_next_id = @constraint_next_id
	parser.sfp
	@constraint_next_id = parser.constraint_next_id
	@root = parser.root
	parser.root.accept(AbstractEliminator) if not options[:keep_abstract]
	@conformant = parser.conformant
	@parser_arrays = parser.arrays
	parser.root
end

#to_json(params = {}) ⇒ Object



48
49
50
51
52
# File 'lib/sfp/parser.rb', line 48

def to_json(params={})
	return 'null' if @root.nil?
	return Sfp::Helper.to_pretty_json(@root) if params[:pretty]
	return Sfp::Helper.to_json(@root)
end