Ejemplo n.º 1
0
    def check(self):
        """\
		check() -> Interpretor, Design

		Checks the requirements of a design.
		"""
        if hasattr(self, "_check"):
            return self._check

            # Step 1, calculate the properties
        i, design = self.calculate()

        total_okay = True
        total_feedback = []

        # Step 2, calculate the requirements for the properties
        ranks = self.rank()
        for rank in ranks.keys():
            for property_id in ranks[rank]:

                property = Property(property_id)
                if property.requirements == "":
                    print "Property with id (%i) doesn't have any requirements" % property_id
                    continue

                print "Now checking the following requirement"
                print property.requirements
                result = i.eval(scheme.parse("""(%s design)""" % property.requirements))
                print "Result was:", result
                okay, feedback = scheme.pair.car(result), scheme.pair.cdr(result)

                if okay != scheme.symbol.Symbol("#t"):
                    total_okay = False

                if feedback != "":
                    total_feedback.append(feedback)

                    # Step 3, calculate the requirements for the components
        for component_id, amount in self.components:
            component = Component(component_id)
            if component.requirements == "":
                print "Component with id (%i) doesn't have any requirements" % component_id
                continue

            print "Now checking the following requirement"
            print component.requirements
            result = i.eval(scheme.parse("""(%s design)""" % component.requirements))
            print "Result was:", result
            okay, feedback = scheme.pair.car(result), scheme.pair.cdr(result)

            if okay != scheme.symbol.Symbol("#t"):
                total_okay = False

            if feedback != "":
                total_feedback.append(feedback)

        self._check = (total_okay, "\n".join(total_feedback))
        return total_okay, "\n".join(total_feedback)
Ejemplo n.º 2
0
def TpclSyntaxIsValid(tpcl_code):
    """\
    Ensures that the given piece of TPCL code is syntactically
    valid. Returns True if correct syntax, false otherwise
    
    This will be improved in the future.
    """
    try:
        scheme.parse(tpcl_code)
        return True
    except scheme.parser.ParserError, e:
        return False
Ejemplo n.º 3
0
	def check(self, i, properties):
		"""\
		check(Interperator, Properties) -> Valid, Feedback

		Checks the requirements of a design.

		Returns if the properties are valid and a string which has human readable feedback.
		"""
		total_okay = True
		total_feedback = []

		# Step 2, calculate the requirements for the properties
		ranks = self.rank()
		for rank in ranks.keys():
			for property_id in ranks[rank]:

				property = self.cache.properties[property_id]
				if property.requirements == '':
					print "Property with id (%i) doesn't have any requirements" % property_id
					continue
			
				print "Now checking the following requirement"
				print property.requirements
				result = i.eval(scheme.parse("""(%s design)""" % property.requirements))
				print "Result was:", result
				okay, feedback = scheme.pair.car(result), scheme.pair.cdr(result)

				if okay != scheme.symbol.Symbol('#t'):
					total_okay = False
		
				if feedback != "":
					total_feedback.append(feedback)
				
		# Step 3, calculate the requirements for the components
		for component_id, amount in self.design.components:
			component = self.cache.components[component_id]
			if component.requirements == '':
				print "Component with id (%i) doesn't have any requirements" % property_id
				continue
			
			print "Now checking the following requirement"
			print component.requirements
			result = i.eval(scheme.parse("""(%s design)""" % component.requirements))
			print "Result was:", result
			okay, feedback = scheme.pair.car(result), scheme.pair.cdr(result)

			if okay != scheme.symbol.Symbol('#t'):
				total_okay = False
		
			if feedback != "":
				total_feedback.append(feedback)

		return total_okay, "\n".join(total_feedback)
Ejemplo n.º 4
0
def TpclSyntaxIsValid(tpcl_code):
    """\
    Ensures that the given piece of TPCL code is syntactically
    valid. Returns True if correct syntax, false otherwise
    
    This will be improved in the future.
    """
    try:
        scheme.parse(tpcl_code)
        return True
    except scheme.parser.ParserError, e:
        return False
def TpclCodeIsValid(prop):
    """\
    Checks the TPCL code for the given component.
    Returns boolean indicating whether an error is present or not.
    """
    interp = tpcl.GetInterpreter()
    old_env = interp.get_environment()
    new_env = GetPropertyEnv()
    interp._env = new_env
    #actually check the code
    #check the tpcl requirements function
    valid = True
    if not tpcl.TpclSyntaxIsValid(prop.tpcl_display):
        prop.errors['tpcl_display'] = "Syntax error"
        valid = False
    else:
        expression = "(let ((bits (list 0))) (%s design bits))" % prop.tpcl_display
        try:
            interp.eval(scheme.parse(expression))
        except SchemeError, e:
            print "In Property TPCL Validate:"
            print "\tTPCL Code invalid - %s" % e.message
            print "\t\tcode:<%s>" % expression
            prop.errors['tpcl_display'] = e.message
            valid = False
def TpclCodeIsValid(prop):
    """\
    Checks the TPCL code for the given component.
    Returns boolean indicating whether an error is present or not.
    """
    interp = tpcl.GetInterpreter()
    old_env = interp.get_environment()
    new_env = GetPropertyEnv()
    interp._env = new_env
    #actually check the code
    #check the tpcl requirements function
    valid = True
    if not tpcl.TpclSyntaxIsValid(prop.tpcl_display):
        prop.errors['tpcl_display'] = "Syntax error"
        valid = False
    else:
        expression = "(let ((bits (list 0))) (%s design bits))" % prop.tpcl_display
        try:
            interp.eval(scheme.parse(expression))
        except SchemeError, e:
            print "In Property TPCL Validate:"
            print "\tTPCL Code invalid - %s" % e.message
            print "\t\tcode:<%s>" % expression
            prop.errors['tpcl_display'] = e.message
            valid = False
Ejemplo n.º 7
0
def TpclCodeIsValid(comp):
    """\
    Checks the TPCL code for the given component.
    Returns boolean indicating whether an error is present or not.
    """
    interp = tpcl.GetInterpreter()
    old_env = interp.get_environment()
    new_env = GetComponentEnv(comp.node.object_database)
    interp._env = new_env
    #actually check the code
    #check the tpcl requirements function
    valid = True
    if not tpcl.TpclSyntaxIsValid(comp.tpcl_requirements):
        comp.errors['tpcl_requirements'] = "Syntax error"
        valid = False
    else:
        try:
            interp.eval(scheme.parse("(%s design)" % comp.tpcl_requirements))
        except SchemeError, e:
            comp.errors['tpcl_requirements'] = e.message
            valid = False
Ejemplo n.º 8
0
    def calculate(self):
        """\
		calculate() -> Interpretor, Design

		Calculates all the properties on a design. 
		Returns the Interpretor (used to create the design and the actual design).
		"""
        if hasattr(self, "_calculate"):
            return self._calculate
        i = scheme.make_interpreter()

        # Step 1 -------------------------------------

        ranks = self.rank()
        print "The order I need to calculate stuff in is,", ranks

        # Step 2 -------------------------------------

        # The design object
        class Design(dict):
            pass

        design = Design()
        scheme.environment.defineVariable(scheme.symbol.Symbol("design"), design, i.get_environment())

        # Step 3 -------------------------------------
        for rank in ranks.keys():
            for property_id in ranks[rank]:
                property = Property(property_id)

                # Where we will store the values as calculated
                bits = []

                # Get all the components we contain
                for component_id, amount in self.components:
                    # Create the component object
                    component = Component(component_id)

                    # Calculate the actual value for this design
                    value = component.property(property_id)
                    if value:
                        print "Now evaluating", value
                        value = i.eval(scheme.parse("""( %s design)""" % value))

                        print "The value calculated for component %i was %r" % (component_id, value)

                        for x in range(0, amount):
                            bits.append(value)

                print "All the values calculated where", bits
                bits_scheme = "(list"
                for bit in bits:
                    bits_scheme += " " + str(bit).replace("L", "")
                bits_scheme += ")"
                print "In scheme that is", bits_scheme

                print """(let ((bits %s)) (%s design bits))""" % (bits_scheme, property.calculate)
                total = i.eval(
                    scheme.parse("""(let ((bits %s)) (%s design bits))""" % (bits_scheme, property.calculate))
                )
                value, display = scheme.pair.car(total), scheme.pair.cdr(total)

                print "In total I got '%i' which will be displayed as '%s'" % (value, display)
                design[property.name] = (property_id, value, display)

                def t(design, name=property.name):
                    return design[name][1]

                i.install_function("designtype." + property.name, t)

        print "The final properties we have are", design.items()
        self._calculate = (i, design)
        return i, design
Ejemplo n.º 9
0
        try:
            interp.eval(scheme.parse("(%s design)" % comp.tpcl_requirements))
        except SchemeError, e:
            comp.errors['tpcl_requirements'] = e.message
            valid = False

    #check the tpcl cost functions
    cost_msg = "Cost Function Errors:"
    err_in_cost = False
    for prop_name, cost_func in comp.properties.iteritems():
        if not tpcl.TpclSyntaxIsValid(cost_func):
            cost_msg = cost_msg + "\n   %s - Syntax error" % prop_name
            err_in_cost = True
        else:
            try:
                interp.eval(scheme.parse("(%s design)" % cost_func))
            except SchemeError, e:
                cost_msg = cost_msg + "\n   %s - %s" % (prop_name, e.message)
                err_in_cost = True
    if err_in_cost:
        comp.errors['properties'] = cost_msg
        valid = False

    interp._env = old_env
    return valid


def GetComponentEnv(object_database):
    global COMP_ENV
    if not COMP_ENV:
        #we have to define symbols so that we don't get eval errors
        expression = "(let ((bits (list 0))) (%s design bits))" % prop.tpcl_display
        try:
            interp.eval(scheme.parse(expression))
        except SchemeError, e:
            print "In Property TPCL Validate:"
            print "\tTPCL Code invalid - %s" % e.message
            print "\t\tcode:<%s>" % expression
            prop.errors['tpcl_display'] = e.message
            valid = False

    if not tpcl.TpclSyntaxIsValid(prop.tpcl_requires):
        prop.errors['tpcl_requires'] = "Syntax error"
        valid = False
    else:
        try:
            interp.eval(scheme.parse("(%s design)" % prop.tpcl_requires))
        except SchemeError, e:
            prop.errors['tpcl_requires'] = e.message
            valid = False

    interp._env = old_env
    return valid


def GetPropertyEnv():
    global PROP_ENV
    if not PROP_ENV:
        #we have to define symbols so that we don't get eval errors
        PROP_ENV = tpcl.GetBaseEnvironment()
        #define the design variable
        scheme.environment.defineVariable(Symbol('design'), {}, PROP_ENV)
Ejemplo n.º 11
0
	def calculate(self):
		"""\
		calculate() -> Interpretor, Properties

		Calculates all the properties on a design. 
		Returns the Interpretor and the object with the Properties.
		"""
		i = scheme.make_interpreter()

		# Step 1 -------------------------------------
		ranks = self.rank()
		print "The order I need to calculate stuff in is,", ranks

		# Step 2 -------------------------------------
		# The object which will store the properties calculated
		class Properties(dict):
			pass

		properties = Properties()
		scheme.environment.defineVariable(scheme.symbol.Symbol('design'), properties, i.get_environment())

		# Step 3 -------------------------------------
		for rank in ranks.keys():
			for property_id in ranks[rank]:
				property = self.cache.properties[property_id]

				# Where we will store the values as calculated
				bits = []
		
				# Get all the components we contain
				for component_id, amount in self.design.components:
					# Create the component object
					component = self.cache.components[component_id] 

					# Calculate the actual value for this design
					value = get(component.properties, property_id)
					if value:
						print "Now evaluating", value
						value = i.eval(scheme.parse("""(%s design)""" % value))

						print "The value calculated for component %i was %r" % (component_id, value)
					
						for x in range(0, amount):
							bits.append(value)

				print "All the values calculated where", bits
				bits_scheme = "(list"
				for bit in bits:
					bits_scheme += " " + str(bit).replace('L', '')
				bits_scheme += ")"
				print "In scheme that is", bits_scheme
				
				total = i.eval(scheme.parse("""(let ((bits %s)) (%s design bits))""" % (bits_scheme, property.calculate)))
				value, display = scheme.pair.car(total), scheme.pair.cdr(total)

				print "In total I got '%i' which will be displayed as '%s'" % (value, display)
				properties[property.name] = (property_id, value, display)

				def t(properties, name=property.name):
					return properties[name][1]
				
				i.install_function('designtype.'+property.name, t)
				
		print "The final properties we have are", properties.items()
		return i, properties
Ejemplo n.º 12
0
    def check(self):
        """\
		check() -> Interpretor, Design

		Checks the requirements of a design.
		"""
        if hasattr(self, '_check'):
            return self._check

        # Step 1, calculate the properties
        i, design = self.calculate()

        total_okay = True
        total_feedback = []

        # Step 2, calculate the requirements for the properties
        ranks = self.rank()
        for rank in ranks.keys():
            for property_id in ranks[rank]:

                property = Property(property_id)
                if property.requirements == '':
                    print "Property with id (%i) doesn't have any requirements" % property_id
                    continue

                print "Now checking the following requirement"
                print property.requirements
                result = i.eval(
                    scheme.parse("""(%s design)""" % property.requirements))
                print "Result was:", result
                okay, feedback = scheme.pair.car(result), scheme.pair.cdr(
                    result)

                if okay != scheme.symbol.Symbol('#t'):
                    total_okay = False

                if feedback != "":
                    total_feedback.append(feedback)

        # Step 3, calculate the requirements for the components
        for component_id, amount in self.components:
            component = Component(component_id)
            if component.requirements == '':
                print "Component with id (%i) doesn't have any requirements" % component_id
                continue

            print "Now checking the following requirement"
            print component.requirements
            result = i.eval(
                scheme.parse("""(%s design)""" % component.requirements))
            print "Result was:", result
            okay, feedback = scheme.pair.car(result), scheme.pair.cdr(result)

            if okay != scheme.symbol.Symbol('#t'):
                total_okay = False

            if feedback != "":
                total_feedback.append(feedback)

        self._check = (total_okay, "\n".join(total_feedback))
        return total_okay, "\n".join(total_feedback)
Ejemplo n.º 13
0
    def calculate(self):
        """\
		calculate() -> Interpretor, Design

		Calculates all the properties on a design. 
		Returns the Interpretor (used to create the design and the actual design).
		"""
        if hasattr(self, '_calculate'):
            return self._calculate
        i = scheme.make_interpreter()

        # Step 1 -------------------------------------

        ranks = self.rank()
        print "The order I need to calculate stuff in is,", ranks

        # Step 2 -------------------------------------

        # The design object
        class Design(dict):
            pass

        design = Design()
        scheme.environment.defineVariable(scheme.symbol.Symbol('design'),
                                          design, i.get_environment())

        # Step 3 -------------------------------------
        for rank in ranks.keys():
            for property_id in ranks[rank]:
                property = Property(property_id)

                # Where we will store the values as calculated
                bits = []

                # Get all the components we contain
                for component_id, amount in self.components:
                    # Create the component object
                    component = Component(component_id)

                    # Calculate the actual value for this design
                    value = component.property(property_id)
                    if value:
                        print "Now evaluating", value
                        value = i.eval(scheme.parse("""( %s design)""" %
                                                    value))

                        print "The value calculated for component %i was %r" % (
                            component_id, value)

                        for x in range(0, amount):
                            bits.append(value)

                print "All the values calculated where", bits
                bits_scheme = "(list"
                for bit in bits:
                    bits_scheme += " " + str(bit).replace('L', '')
                bits_scheme += ")"
                print "In scheme that is", bits_scheme

                print """(let ((bits %s)) (%s design bits))""" % (
                    bits_scheme, property.calculate)
                total = i.eval(
                    scheme.parse("""(let ((bits %s)) (%s design bits))""" %
                                 (bits_scheme, property.calculate)))
                value, display = scheme.pair.car(total), scheme.pair.cdr(total)

                print "In total I got '%i' which will be displayed as '%s'" % (
                    value, display)
                design[property.name] = (property_id, value, display)

                def t(design, name=property.name):
                    return design[name][1]

                i.install_function('designtype.' + property.name, t)

        print "The final properties we have are", design.items()
        self._calculate = (i, design)
        return i, design
        expression = "(let ((bits (list 0))) (%s design bits))" % prop.tpcl_display
        try:
            interp.eval(scheme.parse(expression))
        except SchemeError, e:
            print "In Property TPCL Validate:"
            print "\tTPCL Code invalid - %s" % e.message
            print "\t\tcode:<%s>" % expression
            prop.errors['tpcl_display'] = e.message
            valid = False
            
    if not tpcl.TpclSyntaxIsValid(prop.tpcl_requires):
        prop.errors['tpcl_requires'] = "Syntax error"
        valid = False
    else:
        try:
            interp.eval(scheme.parse("(%s design)" % prop.tpcl_requires))
        except SchemeError, e:
            prop.errors['tpcl_requires'] = e.message
            valid = False
            
    interp._env = old_env
    return valid
    
def GetPropertyEnv():
    global PROP_ENV
    if not PROP_ENV:
        #we have to define symbols so that we don't get eval errors
        PROP_ENV = tpcl.GetBaseEnvironment()
        #define the design variable
        scheme.environment.defineVariable(Symbol('design'), {}, PROP_ENV)
    return PROP_ENV