Bug #7262

Prefer the symbols defined in the same module when resolving symbol references

Added by Tuukka Lehtonen 8 months ago. Updated 8 months ago.

Status:NewStart date:
Priority:4Due date:
Assignee:Hannu Niemistö% Done:

0%

Category:-Spent time:-
Target version:-
Release notes:
Story points-
Velocity based estimate-

Description

Adding the following code into a model as an SCL module and attempting to import it in the SCL console:

import "Simantics/Diagram" 
import "Simantics/Ontologies" 
import "Extras/HashMap" as Map

data SDiagramElement res =
    SElement res Position
  | SConnection [ConnectionNode res] [Edge] (Maybe String)
deriving instance (Show res) => Show (SDiagramElement res)

elementsOfR' :: Resource -> <ReadGraph> [(SDiagramElement Resource, Resource)]
elementsOfR' diagram = filterJust $ map readElement' ((singleObject diagram MOD.CompositeToDiagram) # L0.ConsistsOf)
  where
    readElement' element = match readElement element with
        Just el -> Just (el, element)
        Nothing -> Nothing
    readElement element =
        if element `isInstanceOf` DIA.RouteGraphConnection
        then readConnection element
        else if element `isInstanceOf` DIA.Element
        then readAnyElement element
        else Nothing
    readAnyElement element = Just $ SElement (singleTypeOf element DIA.Element) (transformOf element)
    readConnection element = do
        connectors = element # DIA.HasConnector
        routeLines = element # DIA.HasInteriorRouteNode
        nodes = map (readConnector element) connectors
              + map readRouteLine routeLines

        nodeResources = connectors + routeLines
        nodeResourceWithIds = zip nodeResources [0..length nodeResources-1]
        edges = runProc do
            rMap = Map.fromList nodeResourceWithIds
            edgesOf (node,i) = 
                [ Edge i j
                | r <- node # DIA.AreConnected
                , j = Map.unsafeGet rMap r
                , j > i ]
            concatMap edgesOf nodeResourceWithIds

        Just $ SConnection nodes edges (readConnectionName element)
    readConnectionName element = 
        match possibleObject element MOD.ElementToComponent with
            Just c -> possibleNameOf c
            Nothing -> Nothing
    readConnector connection r = Terminal
        (idOf $ objectOf stat)
        (inverseOf $ predicateOf stat)
      where
        stat = [ stat
            | stat <- statements r STR.Connects
            , objectOf stat != connection 
            ]!0
    readRouteLine r = RouteLine
        (relatedValue r DIA.IsHorizontal) 
        (relatedValue r DIA.HasPosition)
    transformOf element = do 
        da = fromDoubleArray $
            relatedValue element DIA.HasTransform
        Position (da!0) (da!1) (da!2) (da!3) (da!4) (da!5)
    nameOf r = relatedValue r L0.HasName
    idOf r = match possibleObject r MOD.ElementToComponent with
        Just c -> nameOf c
        Nothing -> if r `isInstanceOf` DIA.Flag
                   then "FLAG_" + nameOf r
                   else fail ("Element " + show r + " was not mapped to a component.")

round d = floor (d + 0.5)

scaleTranslation scale (Position sx shx shy sy tx ty) = Position sx shx shy sy (round (tx*scale)) (round (ty*scale))

scaleElementTranslations scale elements = for elements translateElement
  where
    translateElement ((SElement elementType position), element) =
        claimRelatedValue element DIA.HasTransform (positionToDoubleArray (scaleTranslation scale position))
    translateElement ((SConnection nodeSpecs edges _), element) = do
        for (element # DIA.HasInteriorRouteNode) (translateRouteNode scale element)
    translateElement (diagramElement, element) = do
        print ("unhandled diagram element " + (show diagramElement))
    translateRouteNode scale connection routeNode = do
        position = relatedValue routeNode DIA.HasPosition :: Double
        claimRelatedValue routeNode DIA.HasPosition (round (position*scale))

scaleDiagramElementTranslations scale diagram = scaleElementTranslations scale (elementsOfR' diagram)

currentModelDiagram path = diagram currentModel path

produces the following ICE:

Failed to import http://Projects/Development%20Project/Model/Library/SCLModule, because it contains compilation errors.
    java.lang.NullPointerException
    at org.simantics.scl.compiler.types.Skeletons.commonSkeleton(Skeletons.java:244)
    at org.simantics.scl.compiler.types.Skeletons.commonSkeleton(Skeletons.java:217)
    at org.simantics.scl.compiler.elaboration.expressions.EAmbiguous.getCommonSkeleton(EAmbiguous.java:70)
    at org.simantics.scl.compiler.elaboration.expressions.EAmbiguous.inferType(EAmbiguous.java:172)
    at org.simantics.scl.compiler.elaboration.expressions.EApply.inferType(EApply.java:256)
    at org.simantics.scl.compiler.elaboration.expressions.EApply.inferType(EApply.java:293)
    at org.simantics.scl.compiler.elaboration.expressions.Expression.checkBasicType(Expression.java:83)
    at org.simantics.scl.compiler.elaboration.expressions.Expression.checkType(Expression.java:155)
    at org.simantics.scl.compiler.elaboration.expressions.EApply.inferType(EApply.java:281)
    at org.simantics.scl.compiler.elaboration.expressions.EApply.inferType(EApply.java:293)
    at org.simantics.scl.compiler.elaboration.expressions.Expression.checkBasicType(Expression.java:83)
    at org.simantics.scl.compiler.elaboration.expressions.Expression.checkType(Expression.java:155)
    at org.simantics.scl.compiler.elaboration.expressions.Case.checkType(Case.java:100)
    at org.simantics.scl.compiler.elaboration.expressions.ELambda.checkBasicType(ELambda.java:157)
    at org.simantics.scl.compiler.elaboration.expressions.Expression.checkType(Expression.java:155)
    at org.simantics.scl.compiler.compilation.TypeChecking$1.checkType(TypeChecking.java:98)
    at org.simantics.scl.compiler.compilation.TypeCheckingScheduler.typeCheck(TypeCheckingScheduler.java:79)
    at org.simantics.scl.compiler.compilation.TypeCheckingScheduler.access$0(TypeCheckingScheduler.java:72)
    at org.simantics.scl.compiler.compilation.TypeCheckingScheduler$1.reportComponent(TypeCheckingScheduler.java:55)
    at org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents.visit(StronglyConnectedComponents.java:69)
    at org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents.visit(StronglyConnectedComponents.java:54)
    at org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents.visit(StronglyConnectedComponents.java:54)
    at org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents.findComponents(StronglyConnectedComponents.java:39)
    at org.simantics.scl.compiler.compilation.TypeCheckingScheduler.schedule(TypeCheckingScheduler.java:66)
    at org.simantics.scl.compiler.compilation.TypeChecking.typeCheck(TypeChecking.java:353)
    at org.simantics.scl.compiler.compilation.SCLCompiler.compile(SCLCompiler.java:117)
    at org.simantics.scl.compiler.source.TextualModuleSource.compileModule(TextualModuleSource.java:87)
    at org.simantics.scl.compiler.module.repository.ModuleRepository$ModuleEntry.initModuleEntryAndAddListener(ModuleRepository.java:136)
    at org.simantics.scl.compiler.module.repository.ModuleRepository$ModuleEntry.access$0(ModuleRepository.java:127)
    at org.simantics.scl.compiler.module.repository.ModuleRepository.getModuleEntry(ModuleRepository.java:246)
    at org.simantics.scl.compiler.module.repository.ModuleRepository.getModule(ModuleRepository.java:223)
    at org.simantics.scl.ui.editor2.SCLAnnotationModel.updateAnnotations(SCLAnnotationModel.java:42)
    at org.simantics.scl.ui.editor2.SCLAnnotationModel.access$1(SCLAnnotationModel.java:41)
    at org.simantics.scl.ui.editor2.SCLAnnotationModel$1.notifyAboutUpdate(SCLAnnotationModel.java:37)
    at org.simantics.scl.compiler.module.repository.ModuleRepository$ModuleEntry.notifyAboutUpdate(ModuleRepository.java:100)
    at org.simantics.modeling.scl.GraphModuleSourceRepository$ModuleListener.fireUpdate(GraphModuleSourceRepository.java:85)
    at org.simantics.modeling.scl.GraphModuleSourceRepository$ModuleListener.execute(GraphModuleSourceRepository.java:96)
    at org.simantics.modeling.scl.GraphModuleSourceRepository$ModuleListener.execute(GraphModuleSourceRepository.java:1)
    at org.simantics.db.common.procedure.wrapper.SyncToAsyncListener$1.run(SyncToAsyncListener.java:39)
    at org.simantics.db.common.request.ReadRequest.perform(ReadRequest.java:21)
    at org.simantics.db.impl.graph.ReadGraphImpl.asyncRequest(ReadGraphImpl.java:5496)
    at org.simantics.db.impl.graph.ReadGraphImpl.asyncRequest(ReadGraphImpl.java:5535)
    at org.simantics.db.common.procedure.wrapper.SyncToAsyncListener.execute(SyncToAsyncListener.java:34)
    at fi.vtt.simantics.procore.internal.SessionImplSocket$6$1.execute(SessionImplSocket.java:1524)
    at org.simantics.db.impl.query.ReadEntry.performFromCache(ReadEntry.java:137)
    at org.simantics.db.impl.query.QueryProcessor.performScheduledUpdates(QueryProcessor.java:2606)
    at fi.vtt.simantics.procore.internal.State.commitWriteTransaction(State.java:346)
    at fi.vtt.simantics.procore.internal.SessionRequestManager$4.run(SessionRequestManager.java:239)
    at org.simantics.db.impl.query.QueryThread.run(QueryThread.java:251)

because the round has no signature defined and it competes with the Prelude function round.

Hannu thought that it might be a good idea to make locally defined functions always override functions with the same name in other modules without type checking.

History

#1 Updated by Hannu Niemistö 8 months ago

  • Subject changed from NPE while importing SCL module containing function without signature that overrides a function in another module to Prefer the symbols defined in the same module when resolving symbol references

Also available in: Atom PDF