Module: PubliSci::Prov::DSL

Includes:
Vocabulary
Included in:
Instance
Defined in:
lib/publisci/dsl/prov_dsl.rb

Defined Under Namespace

Classes: Instance

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Vocabulary

#vocabulary

Class Method Details

.included(mod) ⇒ Object



15
16
17
18
# File 'lib/publisci/dsl/prov_dsl.rb', line 15

def self.included(mod)
  Prov.registry.clear
  Prov.reset_settings
end

Instance Method Details

#activity(name, args = {}, &block) ⇒ Object



60
61
62
# File 'lib/publisci/dsl/prov_dsl.rb', line 60

def activity(name,args={}, &block)
  named_element(name,Prov::Activity,args,&block)
end

#agent(name, args = {}, &block) ⇒ Object



42
43
44
# File 'lib/publisci/dsl/prov_dsl.rb', line 42

def agent(name, args={}, &block)
  named_element(name,Prov::Agent,args,&block)
end

#base_url(url) ⇒ Object



64
65
66
# File 'lib/publisci/dsl/prov_dsl.rb', line 64

def base_url(url)
  Prov.base_url=url
end

#configure {|Prov.configuration| ... } ⇒ Object

def configure(&block)

Prov.configuration.instance_eval(&block)

end

Yields:



24
25
26
# File 'lib/publisci/dsl/prov_dsl.rb', line 24

def configure
  yield Prov.configuration
end

#entity(name, args = {}, &block) ⇒ Object Also known as: data



51
52
53
# File 'lib/publisci/dsl/prov_dsl.rb', line 51

def entity(name, args={}, &block)
  named_element(name,Prov::Entity,args,&block)
end

#generate_n3(abbreviate = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/publisci/dsl/prov_dsl.rb', line 68

def generate_n3(abbreviate = false)
  entities = Prov.entities.values.map(&:to_n3).join
  agents = Prov.agents.values.map(&:to_n3).join
  activities = Prov.activities.values.map(&:to_n3).join
  plans = Prov.plans.values.map(&:to_n3).join
  associations = Prov.registry[:associations].values.map(&:to_n3).join if Prov.registry[:associations]
  derivations = Prov.registry[:derivation].values.map(&:to_n3).join if Prov.registry[:derivation]
  usages = Prov.registry[:usage].values.map(&:to_n3).join if Prov.registry[:usage]
  roles = Prov.registry[:role].values.map(&:to_n3).join if Prov.registry[:role]

  str = "#{entities}#{agents}#{activities}#{plans}#{associations}#{derivations}#{usages}#{roles}"

  if abbreviate
    abbreviate_known(str)
  else
    str
  end
end

#named_element(name, element_class, args = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/publisci/dsl/prov_dsl.rb', line 28

def named_element(name,element_class,args={},&block)
  el = element_class.new
  el.__label=name
  if block_given?
    el.instance_eval(&block)
    Prov.register(name,el)
  else
    args.keys.map{|k|
      raise "Unkown #{element_class} setting #{k}" unless try_auto_set(el,k,args[k])
    }
    Prov.register(name,el)
  end
end

#organization(name, args = {}, &block) ⇒ Object



46
47
48
49
# File 'lib/publisci/dsl/prov_dsl.rb', line 46

def organization(name,args={},&block)
  args[:type] = :organization
  agent(name,args,&block)
end

#outputObject



111
112
113
114
115
116
117
118
119
# File 'lib/publisci/dsl/prov_dsl.rb', line 111

def output
  cfg = Prov.configuration
  case cfg.output
  when :generate_n3
    generate_n3(cfg.abbreviate)
  when :to_repository
    raise "not implemented yet"
  end
end

#plan(name, args = {}, &block) ⇒ Object



56
57
58
# File 'lib/publisci/dsl/prov_dsl.rb', line 56

def plan(name, args={}, &block)
  named_element(name,Prov::Plan,args,&block)
end

#return_objectsObject



91
92
93
# File 'lib/publisci/dsl/prov_dsl.rb', line 91

def return_objects
  Prov.registry
end

#settingsObject



87
88
89
# File 'lib/publisci/dsl/prov_dsl.rb', line 87

def settings
  Prov.configuration
end

#to_repository(turtle_string = (Prov.prefixes+generate_n3)) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/publisci/dsl/prov_dsl.rb', line 95

def to_repository(turtle_string=(Prov.prefixes+generate_n3))
  repo = settings.repository
  case repo
  when :in_memory
    repo = RDF::Repository.new
  when :fourstore
    repo = RDF::FourStore::Repository.new('http://localhost:8080')
  end
  f = Tempfile.new(['repo','.ttl'])
  f.write(turtle_string)
  f.close
  repo.load(f.path, :format => :ttl)
  f.unlink
  repo
end