From da68443d448dad6c0cd42c63d0642a8af4a4519e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=B2=D1=8B=D0=B4=D0=B8=D0=BA=20=D0=95=D0=B2?= =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Thu, 13 Dec 2012 00:05:41 +0300 Subject: [PATCH] added sc2logic translator --- components/logic/__init__.py | 46 ++++---- components/logic/logic_converter.py | 167 ++++++++++++++++++++++++++++ components/logic/sc2logic.py | 90 +++++++++++++++ 3 files changed, 280 insertions(+), 23 deletions(-) create mode 100644 components/logic/logic_converter.py create mode 100644 components/logic/sc2logic.py diff --git a/components/logic/__init__.py b/components/logic/__init__.py index e492d7c..d9bbae5 100644 --- a/components/logic/__init__.py +++ b/components/logic/__init__.py @@ -1,23 +1,23 @@ -""" ------------------------------------------------------------------------------ -This source file is part of OSTIS (Open Semantic Technology for Intelligent Systems) -For the latest info, see http://www.ostis.net - -Copyright (c) 2010 OSTIS - -OSTIS is free software: you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -OSTIS is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with OSTIS. If not, see . ------------------------------------------------------------------------------ -""" - -__all__ = [ 'logic_init','logic2sc','logic_keynodes','logic_editor','logic_viewer'] +""" +----------------------------------------------------------------------------- +This source file is part of OSTIS (Open Semantic Technology for Intelligent Systems) +For the latest info, see http://www.ostis.net + +Copyright (c) 2010 OSTIS + +OSTIS is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +OSTIS is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with OSTIS. If not, see . +----------------------------------------------------------------------------- +""" + +__all__ = [ 'logic_init','logic2sc','logic_keynodes','logic_editor','logic_viewer','sc2logic'] diff --git a/components/logic/logic_converter.py b/components/logic/logic_converter.py new file mode 100644 index 0000000..d23b595 --- /dev/null +++ b/components/logic/logic_converter.py @@ -0,0 +1,167 @@ +# coding=utf-8 +import sc_core.pm as sc +import suit.core.keynodes as keynodes +import suit.core.sc_utils as sc_utils +import sc_core.constants as sc_constants +import suit.core.kernel as core +import logic_keynodes + +session = core.Kernel.session() + +disjunctionNode = logic_keynodes.Relation.nrel_disjunction +conjunctionNode = logic_keynodes.Relation.nrel_conjunction +equalNode = logic_keynodes.Relation.nrel_equivalence +implicationNode = logic_keynodes.Relation.nrel_implication +negationNode = logic_keynodes.Relation.nrel_negation + +def getFormula(session, node, prevOp): + """""" + it_attr = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_a_a_f, sc.SC_CONST | sc.SC_NODE, sc.SC_A_CONST | sc.SC_POS, node), True) + + attr_node = None + + while not it_attr.is_over(): + cur_node = it_attr.value(0) + if cur_node==disjunctionNode or cur_node==conjunctionNode or cur_node==equalNode or cur_node==implicationNode or cur_node==negationNode: + attr_node = cur_node + break + it_attr.next() + + if attr_node==None: + return getNode(session, node) + else: + if attr_node==disjunctionNode: + return getDisjunction(session, node, prevOp) + elif attr_node==conjunctionNode: + return getConjunction(session, node, prevOp) + elif attr_node==equalNode: + return getEqual(session, node, prevOp) + elif attr_node==implicationNode: + return getImplication(session, node, prevOp) + elif attr_node==negationNode: + return getNegation(session, node) + +def getDisjunction(session, node, prevOp): + """""" + first = True + brackets = (prevOp==negationNode or prevOp==conjunctionNode) + res = u"" + if (brackets==True): + res+=u"(" + it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a, node, sc.SC_A_CONST | sc.SC_POS, sc.SC_CONST | sc.SC_NODE), True) + + while not it.is_over(): + val = getFormula(session, it.value(2), disjunctionNode) + if first==True: + res += val + first = False + else: + res += u" ^ "+val + it.next() + + if (brackets==True): + res+=u")" + + return res + +def getConjunction(session, node, prevOp): + """""" + first = True; + brackets = (prevOp==negationNode) + res = u""; + if (brackets==True): + res+=u"(" + it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a, node, sc.SC_A_CONST | sc.SC_POS, sc.SC_CONST | sc.SC_NODE), True) + while not it.is_over(): + val = getFormula(session, it.value(2), conjunctionNode) + if first==True: + res+=val + first = False + else: + res+=u" & "+val + it.next() + + if (brackets==True): + res+=u" )" + + return res + +def getImplication(session, node, prevOp): + """""" + ifNode = session.find_keynode_full_uri(u"/seb/logic/если_") + thenNode = session.find_keynode_full_uri(u"/seb/logic/то_") + ifVal = u""; + thenVal = u""; + + if_it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_5_f_a_a_a_f, node, sc.SC_A_CONST | sc.SC_POS, sc.SC_CONST | sc.SC_NODE, sc.SC_A_CONST | sc.SC_POS, ifNode), True) + ifVal = getFormula(session, if_it.value(2), implicationNode) + + then_it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_5_f_a_a_a_f, node, sc.SC_A_CONST | sc.SC_POS, sc.SC_CONST | sc.SC_NODE, sc.SC_A_CONST | sc.SC_POS, thenNode), True) + thenVal = getFormula(session, then_it.value(2), implicationNode) + + res = ifVal + u" -> " + thenVal; + if (prevOp==negationNode or prevOp==conjunctionNode or prevOp==disjunctionNode): + res = u" (" + res + u" )" + + return res + +def getEqual(session, node, prevOp): + """""" + res = u"" + first = True + brackets = (prevOp==negationNode or prevOp==conjunctionNode or prevOp==disjunctionNode or prevOp==implicationNode) + + if (brackets==True): + res+=u"(" + + it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a, node, sc.SC_A_CONST | sc.SC_POS, sc.SC_CONST | sc.SC_NODE), True) + + while not it.is_over(): + val = getFormula(session, it.value(2), equalNode) + if first==True: + res+=val + first = False + else: + res+=u" <-> "+val + it.next() + + if (brackets==True): + res+=u" )" + + return res + +def getNegation(session, node): + """""" + res = u" ! " + + it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a, node, sc.SC_A_CONST | sc.SC_POS, sc.SC_CONST | sc.SC_NODE), True) + + if not it.is_over(): + res += getFormula(session, it.value(2), negationNode) + + return res + +def getNode(session, node): + """""" + return session.get_idtf(node) + + +def getOrientedPair(session, begin, sheaf, end, attr): + """""" + result = [] + result.append(begin) + result.append(sheaf) + result.append(end) + result.append(attr) + result.append(keynodes.n_1) + result.append(keynodes.n_2) + it1 = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_5_f_a_f_a_a, sheaf, sc.SC_POS, begin, sc.SC_POS, sc.SC_CONST | sc.SC_NODE), True) + it2 = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_5_f_a_f_a_a, sheaf, sc.SC_POS, end, sc.SC_POS, sc.SC_CONST | sc.SC_NODE), True) + it3 = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_f, attr, sc.SC_A_CONST | sc.SC_POS, sheaf), True) + result.append(it1.value(1)) + result.append(it1.value(3)) + result.append(it2.value(1)) + result.append(it1.value(3)) + result.append(it3.value(1)) + print result + return result \ No newline at end of file diff --git a/components/logic/sc2logic.py b/components/logic/sc2logic.py new file mode 100644 index 0000000..a0dcad2 --- /dev/null +++ b/components/logic/sc2logic.py @@ -0,0 +1,90 @@ + +from suit.core.objects import Translator +import sc_core.pm as sc +import suit.core.kernel as core +import suit.core.objects as objects +import sc_core.constants as sc_constants + + +class TranslatorSc2Logic(Translator): + + def __init__(self): + Translator.__init__(self) + + def __del__(self): + Translator.__del__(self) + + def translate_impl(self, _input, _output): + + errors = [] + + #getting sheet object + objs = objects.ScObject._sc2Objects(_output) + assert len(objs) > 0 + sheet = objs[0] + assert type(sheet) is objects.ObjectSheet + + #getting session + kernel = core.Kernel.getSingleton() + session = kernel.session() + segment = kernel.segment() + trans_objs = [] + + it = session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a,_input,sc.SC_A_CONST | sc.SC_POS,0), True) + addrsList = [] + while not it.is_over(): + trans_objs.append(it.value(2)) + addrsList.append(str(it.value(2).this)) + it.next() + + for addrs in addrsList: + texNode = session.create_el(segment, sc.SC_N_CONST) + + + texStr = Translate(addrs) + session.set_content_str(texNode, texStr) + session.gen3_f_a_f(segment, _output, texNode, sc.SC_A_CONST | sc.SC_POS) + + return errors + +def Translate(root): + + kernel = core.Kernel.getSingleton() + session = kernel.session() + it=session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_a, + root, + sc.SC_ARC, + sc.SC_CONST|sc.SC_NODE), True) + sheaf=session.find_keynode_full_uri(u"/info/stype_sheaf") + while not it.is_over(): + it_2=session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_f, + sheaf, + sc.SC_ARC, + it.value(2)), True) + if not it_2.is_over(): + node=it_2.value(2) + break + else: + it.next() + it=session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_a_a_f, + sc.SC_CONST|sc.SC_NODE, + sc.SC_ARC, + node), True) + while not it.is_over(): + it_2=session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_f_a_f, + sheaf, + sc.SC_ARC, + it.value(0)), True) + if not it_2.is_over(): + node=it_2.value(2) + it=session.create_iterator(session.sc_constraint_new(sc_constants.CONSTR_3_a_a_f, + sc.SC_CONST|sc.SC_NODE, + sc.SC_ARC, + node), True) + break + else: + it.next() + import logic_converter as convertor + session = core.Kernel.session() + + return convertor.getFormula(session, node, None) \ No newline at end of file