Give every WorkspaceStructure a self.ctx member variable.

...which is currently initialized "by magic"; but removing that magic
will be the next step.
This commit is contained in:
Arthur O'Dwyer
2017-04-17 19:29:04 -07:00
parent 25ba9bfe93
commit 482c374886
8 changed files with 42 additions and 75 deletions

View File

@ -5,9 +5,8 @@ class Bond(WorkspaceStructure):
# pylint: disable=too-many-arguments
def __init__(self, source, destination, bondCategory, bondFacet,
sourceDescriptor, destinationDescriptor):
from context import context as ctx
slipnet = ctx.slipnet
WorkspaceStructure.__init__(self)
slipnet = self.ctx.slipnet
self.source = source
self.string = self.source.string
self.destination = destination
@ -30,8 +29,7 @@ class Bond(WorkspaceStructure):
self.directionCategory = None
def flippedVersion(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
return Bond(
self.destination, self.source,
self.category.getRelatedNode(slipnet.opposite),
@ -45,8 +43,7 @@ class Bond(WorkspaceStructure):
self.category.name, self.leftObject, self.rightObject)
def buildBond(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
workspace.structures += [self]
self.string.bonds += [self]
self.category.buffer = 100.0
@ -61,8 +58,7 @@ class Bond(WorkspaceStructure):
self.breakBond()
def breakBond(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
if self in workspace.structures:
workspace.structures.remove(self)
if self in self.string.bonds:
@ -77,8 +73,7 @@ class Bond(WorkspaceStructure):
def getIncompatibleCorrespondences(self):
# returns a list of correspondences that are incompatible with
# self bond
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
incompatibles = []
if self.leftObject.leftmost and self.leftObject.correspondence:
correspondence = self.leftObject.correspondence
@ -105,8 +100,7 @@ class Bond(WorkspaceStructure):
return incompatibles
def updateInternalStrength(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
# bonds between objects of same type(ie. letter or group) are
# stronger than bonds between different types
sourceGap = self.source.leftIndex != self.source.rightIndex
@ -157,8 +151,7 @@ class Bond(WorkspaceStructure):
# returns a rough measure of the density in the string
# of the same bond-category and the direction-category of
# the given bond
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
slotSum = 0.0
supportSum = 0.0
for object1 in workspace.objects:
@ -189,10 +182,9 @@ class Bond(WorkspaceStructure):
def possibleGroupBonds(bondCategory, directionCategory, bondFacet, bonds):
from context import context as ctx
slipnet = ctx.slipnet
result = []
for bond in bonds:
slipnet = bond.ctx.slipnet
if (bond.category == bondCategory and
bond.directionCategory == directionCategory):
result += [bond]

View File

@ -45,8 +45,7 @@ class Correspondence(WorkspaceStructure):
return initialBond
def getIncompatibleBond(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
initialBond = self.extract_initial_bond()
if not initialBond:
return None
@ -68,8 +67,7 @@ class Correspondence(WorkspaceStructure):
return None
def getIncompatibleCorrespondences(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
return [o.correspondence for o in workspace.initial.objects
if o and self.incompatible(o.correspondence)]
@ -102,8 +100,7 @@ class Correspondence(WorkspaceStructure):
return False
def support(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
if isinstance(self.objectFromInitial, Letter):
if self.objectFromInitial.spansString():
return 100.0
@ -166,8 +163,7 @@ class Correspondence(WorkspaceStructure):
return False
def buildCorrespondence(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
workspace.structures += [self]
if self.objectFromInitial.correspondence:
self.objectFromInitial.correspondence.breakCorrespondence()
@ -202,8 +198,7 @@ class Correspondence(WorkspaceStructure):
self.breakCorrespondence()
def breakCorrespondence(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
workspace.structures.remove(self)
self.objectFromInitial.correspondence = None
self.objectFromTarget.correspondence = None

View File

@ -15,8 +15,7 @@ class Description(WorkspaceStructure):
def __str__(self):
s = 'description(%s) of %s' % (self.descriptor.get_name(), self.object)
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
if self.object.string == workspace.initial:
s += ' in initial string'
else:
@ -31,8 +30,7 @@ class Description(WorkspaceStructure):
self.descriptionType.activation) / 2
def localSupport(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
described_like_self = 0
for other in workspace.objects:
if self.object == other:
@ -55,8 +53,7 @@ class Description(WorkspaceStructure):
self.object.descriptions += [self]
def breakDescription(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
if self in workspace.structures:
workspace.structures.remove(self)
self.object.descriptions.remove(self)

View File

@ -9,10 +9,9 @@ class Group(WorkspaceObject):
# pylint: disable=too-many-instance-attributes
def __init__(self, string, groupCategory, directionCategory, facet,
objectList, bondList):
from context import context as ctx
slipnet = ctx.slipnet
# pylint: disable=too-many-arguments
WorkspaceObject.__init__(self, string)
slipnet = self.ctx.slipnet
self.groupCategory = groupCategory
self.directionCategory = directionCategory
self.facet = facet
@ -42,7 +41,7 @@ class Group(WorkspaceObject):
self.clampSalience = False
self.name = ''
from description import Description
from description import Description # gross
if self.bondList and len(self.bondList):
firstFacet = self.bondList[0].facet
self.addBondDescription(
@ -74,8 +73,7 @@ class Group(WorkspaceObject):
def add_length_description_category(self):
#check whether or not to add length description category
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
probability = self.lengthDescriptionProbability()
if random.random() < probability:
length = len(self.objectList)
@ -101,8 +99,7 @@ class Group(WorkspaceObject):
self.bondDescriptions += [description]
def singleLetterGroupProbability(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
numberOfSupporters = self.numberOfLocalSupportingGroups()
if not numberOfSupporters:
return 0.0
@ -115,11 +112,10 @@ class Group(WorkspaceObject):
support = self.localSupport() / 100.0
activation = slipnet.length.activation / 100.0
supportedActivation = (support * activation) ** exp
return formulas.temperatureAdjustedProbability(ctx, supportedActivation)
return formulas.temperatureAdjustedProbability(self.ctx, supportedActivation)
def flippedVersion(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
flippedBonds = [b.flippedversion() for b in self.bondList]
flippedGroup = self.groupCategory.getRelatedNode(slipnet.flipped)
flippedDirection = self.directionCategory.getRelatedNode(
@ -128,8 +124,7 @@ class Group(WorkspaceObject):
self.facet, self.objectList, flippedBonds)
def buildGroup(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
workspace.objects += [self]
workspace.structures += [self]
self.string.objects += [self]
@ -144,15 +139,14 @@ class Group(WorkspaceObject):
description.descriptor.buffer = 100.0
def lengthDescriptionProbability(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
length = len(self.objectList)
if length > 5:
return 0.0
cubedlength = length ** 3
fred = cubedlength * (100.0 - slipnet.length.activation) / 100.0
probability = 0.5 ** fred
value = formulas.temperatureAdjustedProbability(ctx, probability)
value = formulas.temperatureAdjustedProbability(self.ctx, probability)
if value < 0.06:
value = 0.0 # otherwise 1/20 chance always
return value
@ -161,8 +155,7 @@ class Group(WorkspaceObject):
self.breakGroup()
def breakGroup(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
while len(self.descriptions):
description = self.descriptions[-1]
description.breakDescription()
@ -184,8 +177,7 @@ class Group(WorkspaceObject):
self.rightBond.breakBond()
def updateInternalStrength(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
relatedBondAssociation = self.groupCategory.getRelatedNode(
slipnet.bondCategory).degreeOfAssociation()
bondWeight = relatedBondAssociation ** 0.98
@ -247,8 +239,7 @@ class Group(WorkspaceObject):
return True
def morePossibleDescriptions(self, node):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
result = []
for i, number in enumerate(slipnet.numbers, 1):
if node == number and len(self.objects) == i:

View File

@ -3,9 +3,8 @@ from workspaceObject import WorkspaceObject
class Letter(WorkspaceObject):
def __init__(self, string, position, length):
from context import context as ctx
workspace = ctx.workspace
WorkspaceObject.__init__(self, string)
workspace = self.ctx.workspace
workspace.objects += [self]
string.objects += [self]
self.leftIndex = position
@ -14,8 +13,7 @@ class Letter(WorkspaceObject):
self.rightmost = self.rightIndex == length
def describe(self, position, length):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
if length == 1:
self.addDescription(slipnet.stringPositionCategory,
slipnet.single)

View File

@ -2,7 +2,7 @@ import logging
from workspaceStructure import WorkspaceStructure
from formulas import weightedAverage
import formulas
class Rule(WorkspaceStructure):
@ -24,8 +24,7 @@ class Rule(WorkspaceStructure):
self.externalStrength = self.internalStrength
def updateInternalStrength(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
if not (self.descriptor and self.relation):
self.internalStrength = 0.0
return
@ -53,7 +52,7 @@ class Rule(WorkspaceStructure):
weights = ((depthDifference, 12),
(averageDepth, 18),
(sharedDescriptorTerm, sharedDescriptorWeight))
self.internalStrength = weightedAverage(weights)
self.internalStrength = formulas.weightedAverage(weights)
if self.internalStrength > 100.0:
self.internalStrength = 100.0
@ -81,8 +80,7 @@ class Rule(WorkspaceStructure):
self.descriptor.buffer = 100.0
def incompatibleRuleCorrespondence(self, correspondence):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
if not correspondence:
return False
# find changed object
@ -97,8 +95,7 @@ class Rule(WorkspaceStructure):
for m in correspondence.conceptMappings)
def __changeString(self, string):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
# applies the changes to self string ie. successor
if self.facet == slipnet.length:
if self.relation == slipnet.predecessor:
@ -119,8 +116,7 @@ class Rule(WorkspaceStructure):
return self.relation.name.lower()
def buildTranslatedRule(self):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
slippages = workspace.slippages()
self.category = self.category.applySlippages(slippages)
self.facet = self.facet.applySlippages(slippages)

View File

@ -49,8 +49,7 @@ class WorkspaceObject(WorkspaceStructure):
self.descriptions += [description]
def addDescriptions(self, descriptions):
from context import context as ctx
workspace = ctx.workspace
workspace = self.ctx.workspace
copy = descriptions[:] # in case we add to our own descriptions
for description in copy:
logging.info('might add: %s', description)
@ -124,8 +123,7 @@ class WorkspaceObject(WorkspaceStructure):
def getPossibleDescriptions(self, descriptionType):
from group import Group # gross, TODO FIXME
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
logging.info('getting possible descriptions for %s', self)
descriptions = []
for link in descriptionType.instanceLinks:
@ -170,13 +168,11 @@ class WorkspaceObject(WorkspaceStructure):
return objectOnMyRightIsRightmost and objectOnMyLeftIsLeftmost
def distinguishingDescriptor(self, descriptor):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
return slipnet.isDistinguishingDescriptor(descriptor)
def relevantDistinguishingDescriptors(self):
from context import context as ctx
slipnet = ctx.slipnet
slipnet = self.ctx.slipnet
return [d.descriptor
for d in self.relevantDescriptions()
if slipnet.isDistinguishingDescriptor(d.descriptor)]

View File

@ -8,6 +8,8 @@ def abstract_call(objekt, name):
class WorkspaceStructure(object):
def __init__(self):
from context import context as ctx
self.ctx = ctx
self.string = None
self.internalStrength = 0.0
self.externalStrength = 0.0