Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Implemented the stream access classes
  • Loading branch information
doudou committed Mar 29, 2016
0 parents commit fc051ee
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.sw?
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.1.6
before_install: gem install bundler -v 1.11.2
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in syskit-pocolog.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Sylvain Joyeux

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Syskit::Pocolog

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/syskit/pocolog`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'syskit-pocolog'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install syskit-pocolog

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/syskit-pocolog.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList['test/**/*_test.rb']
end

task :default => :spec
1 change: 1 addition & 0 deletions lib/syskit-pocolog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'syskit/pocolog'
9 changes: 9 additions & 0 deletions lib/syskit/pocolog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'pocolog'
require 'syskit'

require 'metaruby/dsls/find_through_method_missing'
require "syskit/pocolog/version"
require "syskit/pocolog/streams"
require "syskit/pocolog/task_streams"
require "syskit/pocolog/rock_stream_matcher"

15 changes: 15 additions & 0 deletions lib/syskit/pocolog/data_replay_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Syskit
module Pocolog
# A single task in the replay environment
class DataReplayTask < Syskit::RubyTaskContext
# Setup a task to replay the given streams
#
# @overload for(streams)
# Replay all tasks
# @param [
def self.for(stream_mappings)
end
end
end
end

60 changes: 60 additions & 0 deletions lib/syskit/pocolog/rock_stream_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Syskit
module Pocolog
# A stream matcher for Rock's standard metadata
#
# It can be used as parameter to {Streams#query}
#
# The final query is a logical AND of all the characteristics
class RockStreamMatcher
attr_reader :query

def initialize
@query = Hash.new
end

def add_regex(key, rx)
if existing = query[key]
query[key] = Regexp.union(existing, rx)
else
query[key] = rx
end
self
end

# Match only ports
def ports
add_regex('rock_stream_type', /^port$/)
end

# Match only properties
def properties
add_regex('rock_stream_type', /^property$/)
end

# Match the object (port/property) name
def object_name(name)
add_regex('rock_task_object_name', name)
end

# Match the task name
def task_name(name)
add_regex('rock_task_name', name)
end

# Match the task model
def task_model(model)
add_regex('rock_task_model', model.orogen_model.name)
end

# Tests whether a stream matches this query
def ===(stream)
query.all? do |key, matcher|
if metadata = stream.metadata[key]
matcher === metadata
end
end
end
end
end
end

78 changes: 78 additions & 0 deletions lib/syskit/pocolog/streams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module Syskit
module Pocolog
# A set of log streams
class Streams
# Load the set of streams available from a directory
#
# Note that in each directory, a stream's identity (task name,
# port/property name and type) must be unique. If you need to mix
# log streams, load files in separate {Streams} objects
def self.from_dir(path)
streams = new
streams.add_dir(Pathname(path))
streams
end

# Load the set of streams available from a file
def self.from_file(file)
streams = new
streams.add_file(Pathname(file))
streams
end

# The list of streams that are available
attr_reader :streams

def initialize(streams = Array.new)
@streams = streams
end

def each_stream(&block)
streams.each(&block)
end

# Load all log files from a directory
def add_dir(path)
path.children.each do |file_or_dir|
if file_or_dir.file? && file_or_dir.to_s =~ /\.\d+\.log$/
add_file(file_or_dir)
end
end
end

# Load the streams from a log file
def add_file(file)
::Pocolog::Logfiles.open(file).streams.each do |s|
add_stream(s)
end
end

# Add a new stream
#
# @param [Pocolog::DataStream] s
def add_stream(s)
streams << s
end

# Find all streams whose metadata match the given query
def find_all_streams(query)
streams.find_all { |s| query === s }
end

# Find all streams that belong to a task
def find_task_by_name(name)
streams = find_all_streams(RockStreamMatcher.new.task_name(name))
if !streams.empty?
TaskStreams.new(streams)
end
end

# Give access to the streams per-task by calling <task_name>_task
def method_missing(m, *args, &block)
MetaRuby::DSLs.find_through_method_missing(
self, m, args, 'task' => "find_task_by_name") || super
end
end
end
end

38 changes: 38 additions & 0 deletions lib/syskit/pocolog/task_streams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Syskit
module Pocolog
# Stream accessor for streams that have already be narrowed down to a
# single task
#
# It is returned from the main stream pool by
# {Streams#find_task_by_name)
class TaskStreams < Streams
# Find a port stream that matches the given name
def find_port_by_name(name)
objects = find_all_streams(RockStreamMatcher.new.ports.object_name(name))
if objects.size > 1
raise Ambiguous, "there are multiple ports with the name #{name}"
else objects.first
end
end

# Find a property stream that matches the given name
def find_property_by_name(name)
objects = find_all_streams(RockStreamMatcher.new.properties.object_name(name))
if objects.size > 1
raise Ambiguous, "there are multiple properties with the name #{name}"
else objects.first
end
end

# Syskit-looking accessors for ports (_port) and properties
# (_property)
def method_missing(m, *args)
MetaRuby::DSLs.find_through_method_missing(
self, m, args,
"port" => "find_port_by_name",
"property" => "find_property_by_name") || super
end
end
end
end

5 changes: 5 additions & 0 deletions lib/syskit/pocolog/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Syskit
module Pocolog
VERSION = "0.1.0"
end
end
27 changes: 27 additions & 0 deletions syskit-pocolog.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'syskit/pocolog/version'

Gem::Specification.new do |spec|
spec.name = "syskit-pocolog"
spec.version = Syskit::Pocolog::VERSION
spec.authors = ["Sylvain Joyeux"]
spec.email = ["[email protected]"]

spec.summary = "A Syskit plugin that allows to replay log files generated by pocolog"
spec.description = "Adds the APIs necessary to transform component networks to replay log files"
spec.homepage = "https://github.com/rock-core/syskit-pocolog"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency 'syskit'
spec.add_dependency 'metaruby'
spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
end
Loading

0 comments on commit fc051ee

Please sign in to comment.