Commit f47a0b83 authored by Anze Vavpetic's avatar Anze Vavpetic
Browse files

Merge branch 'dev' of workflow.ijs.si:mothra into dev

parents 357a9a7a 0600b75b
......@@ -191,6 +191,7 @@ INSTALLED_APPS_WORKFLOWS_SUB = (
'workflows.subgroup_discovery',
'workflows.nlp',
'workflows.nl_toolkit',
'workflows.ilp',
#WORKFLOWS_SUBAPP_PLACEHOLDER
)
......
import aleph as __aleph_pkg
__aleph_pkg.DEBUG = False
# Create an aleph instance
runner = __aleph_pkg.Aleph()
\ No newline at end of file
This diff is collapsed.
#
# Python interface to Aleph.
#
# author: Anze Vavpetic <anze.vavpetic@ijs.si>
#
import os.path
import shutil
import logging
import re
import tempfile
from stat import *
from subprocess import Popen, PIPE
DEBUG = True
# Setup a logger
logger = logging.getLogger("Aleph [Python]")
logger.setLevel(logging.DEBUG if DEBUG else logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter("%(name)s %(levelname)s: %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
class Aleph(object):
# The aleph source file is presumed to be in the same dir as this file.
THIS_DIR = os.path.dirname(__file__) if os.path.dirname(__file__) else '.'
DIR = tempfile.mkdtemp()
ALEPH_FN = 'aleph.pl'
ALEPH = DIR + '/' + ALEPH_FN
YAP = '/usr/local/bin/yap'
RULES_SUFFIX = 'Rules'
SCRIPT = 'run_aleph.pl'
def __init__(self, verbosity=logging.NOTSET):
"""
Creates an Aleph object.
@param logging can be DEBUG, INFO or NOTSET (default). This controls the verbosity of the output.
"""
self.postGoal = None
self.postScript = None
# Dictionary of non-default settings
self.settings = dict()
logger.setLevel(verbosity)
shutil.copy("%s/%s" % (Aleph.THIS_DIR, Aleph.ALEPH_FN), Aleph.ALEPH)
def set(self, name, value):
"""
Sets the value of setting 'name' to 'value'.
"""
self.settings[name] = value
def settingsAsFacts(self, settings):
"""
Parses a string of settings in the form set(name1, val1), set(name2, val2)...
"""
pattern = re.compile('set\(([a-zA-Z0-9_]+),(\[a-zA-Z0-9_]+)\)')
pairs = pattern.findall(settings)
for name, val in pairs:
self.set(name, val)
def setPostScript(self, goal, script):
"""
After learning call the given script using 'goal'.
"""
self.postGoal = goal
self.postScript = script
def induce(self, mode, filestem, pos, neg, b):
"""
Induce a theory in 'mode'.
@param filestem The base name of this experiment.
@param mode In which mode to induce rules.
@param pos String of positive examples.
@param neg String of negative examples.
@param b String with background knowledge.
"""
# Write the inputs to appropriate files.
self.__prepare(filestem, pos, neg, b)
# Make a script to run aleph (with appropriate settings, stack/heap sizes, ...).
self.__script(mode, filestem)
logger.info("Running aleph...")
# Run the aleph script.
p = Popen(['./' + Aleph.SCRIPT], cwd=Aleph.DIR, stdout=PIPE)
stdout_str, stderr_str = p.communicate()
logger.debug(stdout_str)
logger.debug(stderr_str)
logger.info("Done.")
# Return the rules written in the output file.
rules = open('%s/%s' % (Aleph.DIR, filestem + Aleph.RULES_SUFFIX)).read()
#shutil.copy('%s/%s.py' % (Aleph.DIR, filestem), '/home/anzev/programiranje/sdm/results/')
# Cleanup.
self.__cleanup(filestem)
return rules
def __prepare(self, filestem, pos, neg, b):
"""
Prepares the needed files.
"""
posFile = open('%s/%s.f' % (Aleph.DIR, filestem), 'w')
negFile = open('%s/%s.n' % (Aleph.DIR, filestem), 'w')
bFile = open('%s/%s.b' % (Aleph.DIR, filestem), 'w')
posFile.write(pos)
negFile.write(neg)
bFile.write(b)
posFile.close()
negFile.close()
bFile.close()
def __cleanup(self, filestem):
"""
Cleans up all the temporary files.
"""
try:
os.remove('%s/%s.f' % (Aleph.DIR, filestem))
os.remove('%s/%s.n' % (Aleph.DIR, filestem))
os.remove('%s/%s.b' % (Aleph.DIR, filestem))
os.remove('%s/%s' % (Aleph.DIR, filestem + Aleph.RULES_SUFFIX))
os.remove('%s/%s' % (Aleph.DIR, Aleph.SCRIPT))
except:
logger.info('Problem removing temporary files. The files are probably in use.')
def __script(self, mode, filestem):
"""
Makes the script file to be run by yap.
"""
scriptPath = '%s/%s' % (Aleph.DIR, Aleph.SCRIPT)
script = open(scriptPath, 'w')
#print scriptPath
# Permit the owner to execute and read this script
os.chmod(scriptPath, S_IREAD | S_IEXEC)
cat = lambda x: script.write(x + '\n')
cat("#!%s -L -s50000 -h200000" % Aleph.YAP)
cat(":- initialization(run_aleph).")
cat("run_aleph :- ")
cat("consult(aleph),")
cat("read_all('%s')," % filestem)
# Cat all the non-default settings
for setting, value in self.settings.items():
cat("set(%s, %s)," % (setting, value))
cat("%s," % mode)
cat("write_rules('%s')%s" % (filestem + Aleph.RULES_SUFFIX, ',' if self.postScript else '.'))
if self.postScript:
cat(self.postGoal + ".")
cat(self.postScript)
script.close()
[
{
"pk": 20,
"model": "workflows.category",
"fields": {
"uid": "7aba1e97-0c95-46fb-92a6-47bdf7737393",
"parent": null,
"workflow": null,
"user": null,
"order": 1,
"name": "Ilp"
}
},
{
"pk": 98,
"model": "workflows.abstractwidget",
"fields": {
"category": 20,
"treeview_image": "",
"name": "Create Integer List",
"is_streaming": false,
"uid": "fb2d0355-5553-43d8-8da4-58345687d999",
"interaction_view": "",
"image": "",
"package": "ilp",
"static_image": "construction_work .png",
"post_interact_action": "",
"user": null,
"visualization_view": "",
"action": "ilp_create_integers",
"wsdl_method": "",
"wsdl": "",
"interactive": false,
"has_progress_bar": false,
"order": 1,
"description": ""
}
},
{
"pk": 248,
"model": "workflows.abstractinput",
"fields": {
"widget": 98,
"name": "Integer List String",
"short_name": "str",
"uid": "2b3923cf-5d05-4c60-8f54-989cc127e5a6",
"default": "3\r\n2\r\n1\r\n4",
"required": false,
"multi": false,
"parameter_type": "textarea",
"variable": "intStr",
"parameter": true,
"order": 1,
"description": "Comma or new-line separated list of integers"
}
},
{
"pk": 253,
"model": "workflows.abstractinput",
"fields": {
"widget": 98,
"name": "Sort list",
"short_name": "bol",
"uid": "a8860f91-114d-4b75-996b-d20fc3e604bf",
"default": "true",
"required": true,
"multi": false,
"parameter_type": "checkbox",
"variable": "sort",
"parameter": true,
"order": 2,
"description": "Should the list be sorted"
}
},
{
"pk": 104,
"model": "workflows.abstractoutput",
"fields": {
"widget": 98,
"name": "Integer List",
"short_name": "lst",
"variable": "intList",
"uid": "fb7dd14d-0da7-48de-971f-e0f3cc4a1ca3",
"order": 1,
"description": "List of integers"
}
},
{
"pk": 101,
"model": "workflows.abstractwidget",
"fields": {
"category": 20,
"treeview_image": "",
"name": "Filter Integers",
"is_streaming": false,
"uid": "efbe15e1-aa17-4ecd-8022-9213a2f23ce0",
"interaction_view": "ilp_filter_integers",
"image": "",
"package": "ilp",
"static_image": "construction_work .png",
"post_interact_action": "ilp_post_filter_integers",
"user": null,
"visualization_view": "",
"action": "ilp_pre_filter_integers",
"wsdl_method": "",
"wsdl": "",
"interactive": true,
"has_progress_bar": false,
"order": 2,
"description": ""
}
},
{
"pk": 250,
"model": "workflows.abstractinput",
"fields": {
"widget": 101,
"name": "Integer List",
"short_name": "lst",
"uid": "3eab7307-bb3c-442c-8b0c-e7f02843693c",
"default": "",
"required": true,
"multi": false,
"parameter_type": null,
"variable": "intList",
"parameter": false,
"order": 1,
"description": "List of integers"
}
},
{
"pk": 106,
"model": "workflows.abstractoutput",
"fields": {
"widget": 101,
"name": "Filtered Integer List",
"short_name": "lst",
"variable": "intList",
"uid": "4231bb99-97c6-4d49-938d-c36c4d139d50",
"order": 1,
"description": "Filtered list of integers"
}
},
{
"pk": 99,
"model": "workflows.abstractwidget",
"fields": {
"category": 20,
"treeview_image": "",
"name": "Sum Integers",
"is_streaming": false,
"uid": "17f89247-5e30-4999-983c-e97e8a1bd5a0",
"interaction_view": "",
"image": "",
"package": "ilp",
"static_image": "construction_work .png",
"post_interact_action": "",
"user": null,
"visualization_view": "",
"action": "ilp_sum_integers",
"wsdl_method": "",
"wsdl": "",
"interactive": false,
"has_progress_bar": false,
"order": 3,
"description": ""
}
},
{
"pk": 249,
"model": "workflows.abstractinput",
"fields": {
"widget": 99,
"name": "Integer List",
"short_name": "lst",
"uid": "e6413cd1-61ff-4a15-8953-109139d6e174",
"default": "",
"required": true,
"multi": false,
"parameter_type": null,
"variable": "intList",
"parameter": false,
"order": 1,
"description": "List of integers"
}
},
{
"pk": 105,
"model": "workflows.abstractoutput",
"fields": {
"widget": 99,
"name": "Sum",
"short_name": "int",
"variable": "sum",
"uid": "c3b89344-96c8-4e45-8dbb-82bc67ba9c4f",
"order": 1,
"description": "Sum of integer list"
}
},
{
"pk": 100,
"model": "workflows.abstractwidget",
"fields": {
"category": 20,
"treeview_image": "",
"name": "Display Summation",
"is_streaming": false,
"uid": "5497f9f3-6eec-4d10-992b-3b809bf2d1dd",
"interaction_view": "",
"image": "",
"package": "ilp",
"static_image": "construction_work .png",
"post_interact_action": "",
"user": null,
"visualization_view": "ilp_display_summation",
"action": "ilp_pre_display_summation",
"wsdl_method": "",
"wsdl": "",
"interactive": false,
"has_progress_bar": false,
"order": 4,
"description": ""
}
},
{
"pk": 251,
"model": "workflows.abstractinput",
"fields": {
"widget": 100,
"name": "Integer List",
"short_name": "lst",
"uid": "991d6d0e-c554-48cd-bd72-a1f3b15e139e",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "intList",
"parameter": false,
"order": 1,
"description": "List of integers"
}
},
{
"pk": 252,
"model": "workflows.abstractinput",
"fields": {
"widget": 100,
"name": "Sum",
"short_name": "int",
"uid": "1d772dea-3030-4e1b-967c-2a7b221ea893",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "sum",
"parameter": false,
"order": 2,
"description": "Sum (possibly correct) of integer list"
}
}
]
\ No newline at end of file
from django.shortcuts import render
def ilp_filter_integers(request,input_dict,output_dict,widget):
return render(request, 'interactions/ilp_filter_integers.html',{'widget':widget,'intList':input_dict['intList']})
\ No newline at end of file
import re
def ilp_create_integers(input_dict):
intStr = input_dict['intStr']
intList = []
for i in re.findall(r'\w+', intStr):
try:
intList.append(int(i))
except:
pass
if input_dict['sort'].lower() == "true":
intList.sort()
return {'intList':intList}
def ilp_sum_integers(input_dict):
intList = input_dict['intList']
return {'sum':sum(intList)}
def ilp_pre_filter_integers(input_dict):
return input_dict
def ilp_post_filter_integers(postdata,input_dict,output_dict):
intListOut = postdata['intListOut']
intList = []
for i in intListOut:
try:
intList.append(int(i))
except:
pass
return {'intList': intList}
def ilp_pre_display_summation(input_dict):
return {}
def ilp_pre_aleph(input_dict):
return input_dict
def ilp_post_aleph(postdata, input_dict, output_dict):
return {'result' : result}
\ No newline at end of file
import os
# === STANDARD PACKAGE SETTINGS ===
PACKAGE_ROOT = os.path.dirname(__file__)
# === AUTO IMPORT OPTIONS ===
#If auto_import_package_data is true then given data file is automatically imported when ClowdFlows project is newly deployed or refreshed from git
AUTO_IMPORT_DB = False
#For auto_import_package_data_replace_option description see the 'replace' option in workflows/import_package command
AUTO_IMPORT_DB_REPLACE_OPTION = True
#If file(s) other than ./db/package_data.json should be imported, auto_import_package_data_files should be corrected
AUTO_IMPORT_DB_FILES = [os.path.join(PACKAGE_ROOT,'db/package_data.json')]
<div id="widgetinteract-{{widget.pk}}" rel="{{widget.pk}}" class="widgetinteractdialog" title="{{widget.name}} wants your input!">
<form id="interactionform-{{widget.pk}}" name="interactionform-{{widget.pk}}">
{% for i in intList %}
<input type="checkbox" name="intListOut" value="{{i}}" style="display:inline;width:auto;">{{i}}<br>
{% endfor %}
<input type="hidden" name="widget_id" value="{{widget.pk}}">
</form>
</div>
\ No newline at end of file
<div id="widgetvisualization-{{widget.pk}}" rel="{{widget.pk}}" class="widgetvisualizationdialog" title="{{widget.name}} visualization">
<div style="width:400px;font-family:monospace;">
<table style="width:auto; border-collapse:collapse;">
{% for i in input_dict.intList %}
<tr style="width:auto">
<td style="text-align: right; width:auto; line-height: 0.5em; padding: 2px; padding-bottom: 4px; padding-top: 0; margin:2px;">
{% if forloop.first %} {% else %}+{% endif %}
</td>
<td style="text-align: right; width:auto; line-height: 0.5em; padding: 2px; padding-bottom: 4px; padding-top: 0; margin:2px;">
{{ i }}
</td>
</tr>
{% endfor %}
<tr style="width:auto">
<td style="text-align: right; width:auto; line-height: 0.5em; border-top: 1px solid black; padding: 2px; padding-top: 4px; margin:2px;">
=
</td>
<td style="text-align: right; width:auto; line-height: 0.5em; border-top: 1px solid black; padding: 2px;margin:2px;">
{{ input_dict.sum }}
</td>
</tr>
</table>
<br/>
{{ check }}
</div>
</div>
\ No newline at end of file
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/nx/Index.html$', 'workflows.latino.views.get_adc_index', name='get adc index'),
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/(?P<narrow_doc>n?)x/Index.html$', 'workflows.latino.views.get_adc_index', name='get adc index'),
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/(?P<narrow_doc>n?)x/Index(?P<document_id_from>[0-9]+)-(?P<document_id_to>[0-9]+).html$', 'workflows.latino.views.get_adc_index', name='get adc index'),
#url(r'^get-adc-index/widget(?P<widget_id>[0-9]+)/(?P<narrow_doc>n?)x/Document(?P<document_id>[0-9]+).html', 'workflows.latino.views.get_adc_page', name='get adc page'),
)
\ No newline at end of file
from django.shortcuts import render
def ilp_display_summation(request,input_dict,output_dict,widget):
if sum(input_dict['intList']) == input_dict['sum']:
check = 'The calculation appears correct.'
else:
check = 'The calculation appears incorrect!'
return render(request, 'visualizations/ilp_display_integers.html',{'widget':widget,'input_dict':input_dict, 'output_dict':output_dict, 'check':check})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment