Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions components/logic/__init__.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------
"""

__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 <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------
"""
__all__ = [ 'logic_init','logic2sc','logic_keynodes','logic_editor','logic_viewer','sc2logic']
167 changes: 167 additions & 0 deletions components/logic/logic_converter.py
Original file line number Diff line number Diff line change
@@ -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
90 changes: 90 additions & 0 deletions components/logic/sc2logic.py
Original file line number Diff line number Diff line change
@@ -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)